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
parent 3b85fd90
......@@ -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() {
......
......@@ -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 {
......
......@@ -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;
});
......@@ -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 \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment