From: Michal Michalski Date: Mon, 15 Jul 2019 10:01:08 +0000 (+0200) Subject: [tizen] Fix issue with regular arrays in Bundle. X-Git-Tag: submit/tizen/20190715.124605~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f5f097402dd0300d82d1455083f4d4686c07b0e1;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [tizen] Fix issue with regular arrays in Bundle. 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 Change-Id: I9eeca40d9760413c81888f4980146c8025d227d2 --- diff --git a/src/tizen/tizen_api.js b/src/tizen/tizen_api.js index 625b3852..25097911 100644 --- a/src/tizen/tizen_api.js +++ b/src/tizen/tizen_api.js @@ -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); };