summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhansmuller@chromium.org <hansmuller@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-10 00:06:10 +0000
committerhansmuller@chromium.org <hansmuller@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-10 00:06:10 +0000
commit84763789bba88cf7542200856d9dc6691b07f886 (patch)
tree29b5fbb4ab40bd3ca8e1cf881761ee393fb0b9f3
parent3b85fd90f30a8abef2a9c2a7a93cee208c2801a9 (diff)
downloadchromium_src-84763789bba88cf7542200856d9dc6691b07f886.zip
chromium_src-84763789bba88cf7542200856d9dc6691b07f886.tar.gz
chromium_src-84763789bba88cf7542200856d9dc6691b07f886.tar.bz2
Support packed arrays of booleans in Mojo messages
BUG=390949 Review URL: https://codereview.chromium.org/377713004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282182 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--mojo/bindings/js/codec_unittests.js12
-rw-r--r--mojo/public/interfaces/bindings/tests/sample_service.mojom1
-rw-r--r--mojo/public/js/bindings/codec.js62
-rw-r--r--mojo/public/tools/bindings/generators/mojom_js_generator.py6
4 files changed, 74 insertions, 7 deletions
diff --git a/mojo/bindings/js/codec_unittests.js b/mojo/bindings/js/codec_unittests.js
index fd49c73..e30acb5 100644
--- a/mojo/bindings/js/codec_unittests.js
+++ b/mojo/bindings/js/codec_unittests.js
@@ -87,9 +87,13 @@ define([
foo.array_of_array_of_bools = [
[true], [false, true]
];
+ foo.array_of_bools = [
+ true, false, true, false, true, false, true, true
+ ];
+
var messageName = 31;
- var payloadSize = 280;
+ var payloadSize = 304;
var builder = new codec.MessageBuilder(messageName, payloadSize);
builder.encodeStruct(sample.Foo, foo);
@@ -99,10 +103,10 @@ define([
var expectedMemory = new Uint8Array([
/* 0: */ 16, 0, 0, 0, 2, 0, 0, 0,
/* 8: */ 31, 0, 0, 0, 0, 0, 0, 0,
- /* 16: */ 88, 0, 0, 0, 14, 0, 0, 0,
+ /* 16: */ 96, 0, 0, 0, 15, 0, 0, 0,
/* 24: */ 0xD5, 0xB4, 0x12, 0x02, 0x93, 0x6E, 0x01, 0,
/* 32: */ 5, 0, 0, 0, 0, 0, 0, 0,
- /* 40: */ 64, 0, 0, 0, 0, 0, 0, 0,
+ /* 40: */ 72, 0, 0, 0, 0, 0, 0, 0,
]);
// TODO(abarth): Test more of the message's raw memory.
var actualMemory = new Uint8Array(message.buffer.arrayBuffer,
@@ -135,6 +139,8 @@ define([
expect(foo2.extra_bars).toEqual(foo.extra_bars);
expect(foo2.name).toBe(foo.name);
expect(foo2.source).toEqual(foo.source);
+
+ expect(foo2.array_of_bools).toEqual(foo.array_of_bools);
}
function testTypes() {
diff --git a/mojo/public/interfaces/bindings/tests/sample_service.mojom b/mojo/public/interfaces/bindings/tests/sample_service.mojom
index 1dcc1c7..4ebc558 100644
--- a/mojo/public/interfaces/bindings/tests/sample_service.mojom
+++ b/mojo/public/interfaces/bindings/tests/sample_service.mojom
@@ -40,6 +40,7 @@ struct Foo {
handle<data_pipe_producer>[] output_streams@11;
bool[][] array_of_array_of_bools@12;
string[][][] multi_array_of_strings@13;
+ bool[] array_of_bools@14;
};
struct DefaultsTest {
diff --git a/mojo/public/js/bindings/codec.js b/mojo/public/js/bindings/codec.js
index 84c86c1..9756f4a 100644
--- a/mojo/public/js/bindings/codec.js
+++ b/mojo/public/js/bindings/codec.js
@@ -229,6 +229,20 @@ define("mojo/public/js/bindings/codec", [
return val;
};
+ Decoder.prototype.decodeBoolArray = function() {
+ var numberOfBytes = this.readUint32();
+ var numberOfElements = this.readUint32();
+
+ var val = new Array(numberOfElements);
+ var byte;
+ for (var i = 0; i < numberOfElements; ++i) {
+ if (i % 8 == 0)
+ byte = this.readUint8();
+ val[i] = (byte & (1 << i % 8)) ? true : false;
+ }
+ return val;
+ };
+
Decoder.prototype.decodeStruct = function(cls) {
return cls.decode(this);
};
@@ -249,6 +263,14 @@ define("mojo/public/js/bindings/codec", [
return this.decodeAndCreateDecoder(pointer).decodeArray(cls);
};
+ Decoder.prototype.decodeBoolArrayPointer = function() {
+ var pointer = this.decodePointer();
+ if (!pointer) {
+ return null;
+ }
+ return this.decodeAndCreateDecoder(pointer).decodeBoolArray();
+ };
+
Decoder.prototype.decodeStringPointer = function() {
var pointer = this.decodePointer();
if (!pointer) {
@@ -364,12 +386,13 @@ define("mojo/public/js/bindings/codec", [
this.next += numberOfElements;
};
- Encoder.prototype.encodeArray = function(cls, val) {
- var numberOfElements = val.length;
- var numberOfBytes = kArrayHeaderSize + cls.encodedSize * numberOfElements;
+ Encoder.prototype.encodeArray = function(cls, val, numberOfElements) {
+ if (numberOfElements === undefined)
+ numberOfElements = val.length;
+ var numberOfBytes = kArrayHeaderSize + cls.encodedSize * val.length;
this.writeUint32(numberOfBytes);
this.writeUint32(numberOfElements);
- for (var i = 0; i < numberOfElements; ++i) {
+ for (var i = 0; i < val.length; ++i) {
cls.encode(this, val[i]);
}
};
@@ -397,6 +420,23 @@ define("mojo/public/js/bindings/codec", [
encoder.encodeArray(cls, val);
};
+ Encoder.prototype.encodeBoolArrayPointer = function(val) {
+ if (!val) {
+ this.encodePointer(val);
+ return;
+ }
+ var numberOfElements = val.length;
+ var encodedSize = kArrayHeaderSize + Math.ceil(numberOfElements / 8);
+ var encoder = this.createAndEncodeEncoder(encodedSize);
+
+ var bits = new Uint8Array(Math.ceil(numberOfElements / 8));
+ for (var i = 0; i < numberOfElements; i++) {
+ if (val[i])
+ bits[Math.floor(i / 8)] |= (1 << i % 8);
+ }
+ encoder.encodeArray(Uint8, bits, numberOfElements);
+ };
+
Encoder.prototype.encodeStringPointer = function(val) {
if (!val) {
this.encodePointer(val);
@@ -696,6 +736,19 @@ define("mojo/public/js/bindings/codec", [
encoder.encodeArrayPointer(this.cls, val);
};
+ function ArrayOfBoolArrayPointers() {
+ }
+
+ ArrayOfBoolArrayPointers.prototype.encodedSize = 8;
+
+ ArrayOfBoolArrayPointers.prototype.decode = function(decoder) {
+ return decoder.decodeBoolArrayPointer();
+ };
+
+ ArrayOfBoolArrayPointers.prototype.encode = function(encoder, val) {
+ encoder.encodeBoolArrayPointer(val);
+ };
+
function Handle() {
}
@@ -734,6 +787,7 @@ define("mojo/public/js/bindings/codec", [
exports.String = String;
exports.PointerTo = PointerTo;
exports.ArrayOf = ArrayOf;
+ exports.ArrayOfBoolArrayPointers = ArrayOfBoolArrayPointers;
exports.Handle = Handle;
return exports;
});
diff --git a/mojo/public/tools/bindings/generators/mojom_js_generator.py b/mojo/public/tools/bindings/generators/mojom_js_generator.py
index 68f7908..5920832 100644
--- a/mojo/public/tools/bindings/generators/mojom_js_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_js_generator.py
@@ -85,6 +85,8 @@ def CodecType(kind):
return _kind_to_codec_type[kind]
if isinstance(kind, mojom.Struct):
return "new codec.PointerTo(%s)" % CodecType(kind.name)
+ if isinstance(kind, mojom.Array) and kind.kind == mojom.BOOL:
+ return "new codec.ArrayOfBoolArrayPointers()"
if isinstance(kind, mojom.Array):
return "new codec.ArrayOf(%s)" % CodecType(kind.kind)
if isinstance(kind, mojom.Interface) or \
@@ -100,6 +102,8 @@ def JavaScriptDecodeSnippet(kind):
return "decodeStruct(%s)" % CodecType(kind)
if isinstance(kind, mojom.Struct):
return "decodeStructPointer(%s)" % CodecType(kind.name)
+ if isinstance(kind, mojom.Array) and kind.kind == mojom.BOOL:
+ return "decodeBoolArrayPointer()"
if isinstance(kind, mojom.Array):
return "decodeArrayPointer(%s)" % CodecType(kind.kind)
if isinstance(kind, mojom.Interface) or \
@@ -114,6 +118,8 @@ def JavaScriptEncodeSnippet(kind):
return "encodeStruct(%s, " % CodecType(kind)
if isinstance(kind, mojom.Struct):
return "encodeStructPointer(%s, " % CodecType(kind.name)
+ if isinstance(kind, mojom.Array) and kind.kind == mojom.BOOL:
+ return "encodeBoolArrayPointer(";
if isinstance(kind, mojom.Array):
return "encodeArrayPointer(%s, " % CodecType(kind.kind)
if isinstance(kind, mojom.Interface) or \