[tizen] Fix issue with regular arrays in Bundle. 00/209900/4
authorMichal Michalski <m.michalski2@partner.samsung.com>
Mon, 15 Jul 2019 10:01:08 +0000 (12:01 +0200)
committerMichal Michalski <m.michalski2@partner.samsung.com>
Mon, 15 Jul 2019 10:01:08 +0000 (12:01 +0200)
Bundle now stored byte streams as Uint8Arrays and converts regular arrays with
octet values to Uint8Arrays in Bundle::set() method.

toString() method now prints Uint8Arrays in the same way as regular arrays
using "[1,2,3]" format, instead of the "{0: 0, 1: 1, 2: 2}" format which is the
default behavior of toString() method on typed arrays.

[Verification] Modified tests passed.

Signed-off-by: Michal Michalski <m.michalski2@partner.samsung.com>
Change-Id: I9eeca40d9760413c81888f4980146c8025d227d2

src/tizen/tizen_api.js

index 625b385..2509791 100644 (file)
@@ -497,10 +497,12 @@ function getValueType(value) {
     if (xwalk.utils.type.isStringArray(value)) {
         return BundleValueType.STRING_ARRAY;
     }
-    if (xwalk.utils.type.isByteStream(value)) {
+    if (xwalk.utils.type.isByteStream(value) ||
+        xwalk.utils.type.isLegacyByteStream(value)) {
         return BundleValueType.BYTES;
     }
-    if (xwalk.utils.type.isByteStreamArray(value)) {
+    if (xwalk.utils.type.isByteStreamArray(value) ||
+        xwalk.utils.type.isLegacyByteStreamArray(value)) {
         return BundleValueType.BYTES_ARRAY;
     }
 }
@@ -521,12 +523,28 @@ exports.Bundle = function(json) {
 exports.Bundle.prototype.constructor = exports.Bundle;
 
 exports.Bundle.prototype.set = function(key, value) {
-    if (undefined === getValueType(value)) {
-        value = xwalk.utils.converter.toString(value);
-    }
-    this.data[key] = value;
+  this.data[key] = convertToBundleValue(value);
 };
 
+function convertToBundleValue(value) {
+    var converted = value;
+    switch (getValueType(value)) {
+      case undefined:
+        converted = xwalk.utils.converter.toString(value);
+        break;
+      case BundleValueType.BYTES:
+        converted = new Uint8Array(value);
+        break;
+      case BundleValueType.BYTES_ARRAY:
+        converted = [];
+        value.forEach((function (stream) {
+          converted.push(new Uint8Array(stream));
+        }).bind(this));
+        break;
+    }
+    return converted;
+}
+
 exports.Bundle.prototype.get = function(key) {
     if (!this.data.hasOwnProperty(key)) {
         throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
@@ -553,12 +571,29 @@ exports.Bundle.prototype.forEach = function(callback) {
 exports.Bundle.prototype.toJSON = function() {
     var json = {};
     this.forEach(function(key, value) {
-        json[key] = value;
+      json[key] = value;
     });
     return json;
 };
 
+// Conversion from Uint8Array to Array happens in toString instead of toJSON
+// because toJSON method should preserve bundle values types.
 exports.Bundle.prototype.toString = function() {
-    return JSON.stringify(this.data);
+    var json = {};
+    this.forEach(function(key, value, type) {
+        if (type == BundleValueType.BYTES) {
+          json[key] = [].slice.call(value);
+        }
+        else if (type == BundleValueType.BYTES_ARRAY) {
+          json[key] = [];
+          value.forEach(function(stream) {
+            json[key].push([].slice.call(stream));
+          });
+        }
+        else {
+          json[key] = value;
+        }
+    });
+    return JSON.stringify(json);
 };