Commit 401e1678 authored by yzshen's avatar yzshen Committed by Commit bot

Mojo JS bindings: fix deserialization of boolean fields.

Previously, if there was only a single boolean field in a byte, it would be
deserialized as an integer instead of a boolean.

BUG=None

Review-Url: https://codereview.chromium.org/2310443003
Cr-Commit-Position: refs/heads/master@{#416352}
parent 69f5de1c
...@@ -380,3 +380,9 @@ struct ContainsOther { ...@@ -380,3 +380,9 @@ struct ContainsOther {
struct ContainsInterfaceRequest { struct ContainsInterfaceRequest {
SomeInterface& request; SomeInterface& request;
}; };
// Used to verify that boolean fields are correctly serialized/deserialized.
struct SingleBoolStruct {
bool value;
};
...@@ -12,6 +12,7 @@ define([ ...@@ -12,6 +12,7 @@ define([
testBar(); testBar();
testFoo(); testFoo();
testNamedRegion(); testNamedRegion();
testSingleBooleanStruct();
testTypes(); testTypes();
testAlign(); testAlign();
testUtf8(); testUtf8();
...@@ -176,6 +177,23 @@ define([ ...@@ -176,6 +177,23 @@ define([
expect(result.rects[1]).toEqual(createRect(10, 20, 30, 40)); expect(result.rects[1]).toEqual(createRect(10, 20, 30, 40));
} }
// Verify that a single boolean field in a struct is correctly decoded to
// boolean type.
function testSingleBooleanStruct() {
var single_bool = new structs.SingleBoolStruct();
single_bool.value = true;
var builder = new codec.MessageBuilder(
1, structs.SingleBoolStruct.encodedSize);
builder.encodeStruct(structs.SingleBoolStruct, single_bool);
var reader = new codec.MessageReader(builder.finish());
var result = reader.decodeStruct(structs.SingleBoolStruct);
// Use toEqual() instead of toBeTruthy() to make sure the field type is
// actually boolean.
expect(result.value).toEqual(true);
}
function testTypes() { function testTypes() {
function encodeDecode(cls, input, expectedResult, encodedSize) { function encodeDecode(cls, input, expectedResult, encodedSize) {
var messageName = 42; var messageName = 42;
......
...@@ -65,6 +65,16 @@ define([ ...@@ -65,6 +65,16 @@ define([
var decoded = structEncodeDecode(s); var decoded = structEncodeDecode(s);
expect(decoded).toEqual(s); expect(decoded).toEqual(s);
var s = new unions.WrapperStruct({
pod_union: new unions.PodUnion({
f_bool : true})});
var decoded = structEncodeDecode(s);
expect(decoded.pod_union.$tag).toEqual(unions.PodUnion.Tags.f_bool);
// Use toEqual() instead of toBeTruthy() to make sure the field type is
// actually boolean.
expect(decoded.pod_union.f_bool).toEqual(true);
var s = new unions.WrapperStruct({ var s = new unions.WrapperStruct({
object_union: new unions.ObjectUnion({ object_union: new unions.ObjectUnion({
f_dummy: new unions.DummyStruct({ f_dummy: new unions.DummyStruct({
......
...@@ -59,7 +59,8 @@ ...@@ -59,7 +59,8 @@
var numberOfBytes = decoder.readUint32(); var numberOfBytes = decoder.readUint32();
var version = decoder.readUint32(); var version = decoder.readUint32();
{%- for byte in struct.bytes %} {%- for byte in struct.bytes %}
{%- if byte.packed_fields|length > 1 %} {%- if byte.packed_fields|length >= 1 and
byte.packed_fields[0].field|is_bool_field %}
packed = decoder.readUint8(); packed = decoder.readUint8();
{%- for packed_field in byte.packed_fields %} {%- for packed_field in byte.packed_fields %}
val.{{packed_field.field.name}} = (packed >> {{packed_field.bit}}) & 1 ? true : false; val.{{packed_field.field.name}} = (packed >> {{packed_field.bit}}) & 1 ? true : false;
...@@ -82,7 +83,8 @@ ...@@ -82,7 +83,8 @@
encoder.writeUint32({{struct.versions[-1].version}}); encoder.writeUint32({{struct.versions[-1].version}});
{%- for byte in struct.bytes %} {%- for byte in struct.bytes %}
{%- if byte.packed_fields|length > 1 %} {%- if byte.packed_fields|length >= 1 and
byte.packed_fields[0].field|is_bool_field %}
packed = 0; packed = 0;
{%- for packed_field in byte.packed_fields %} {%- for packed_field in byte.packed_fields %}
packed |= (val.{{packed_field.field.name}} & 1) << {{packed_field.bit}} packed |= (val.{{packed_field.field.name}} & 1) << {{packed_field.bit}}
......
...@@ -89,7 +89,11 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", { ...@@ -89,7 +89,11 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
switch (val.$tag) { switch (val.$tag) {
{%- for field in union.fields %} {%- for field in union.fields %}
case {{union.name}}.Tags.{{field.name}}: case {{union.name}}.Tags.{{field.name}}:
{%- if field|is_bool_field %}
encoder.writeUint8(val.{{field.name}} ? 1 : 0);
{%- else %}
encoder.{{field.kind|union_encode_snippet}}val.{{field.name}}); encoder.{{field.kind|union_encode_snippet}}val.{{field.name}});
{%- endif %}
break; break;
{%- endfor %} {%- endfor %}
} }
...@@ -111,7 +115,11 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", { ...@@ -111,7 +115,11 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
switch (tag) { switch (tag) {
{%- for field in union.fields %} {%- for field in union.fields %}
case {{union.name}}.Tags.{{field.name}}: case {{union.name}}.Tags.{{field.name}}:
{%- if field|is_bool_field %}
result.{{field.name}} = decoder.readUint8() ? true : false;
{%- else %}
result.{{field.name}} = decoder.{{field.kind|union_decode_snippet}}; result.{{field.name}} = decoder.{{field.kind|union_decode_snippet}};
{%- endif %}
break; break;
{%- endfor %} {%- endfor %}
} }
......
...@@ -337,6 +337,9 @@ def IsInterfaceRequestField(field): ...@@ -337,6 +337,9 @@ def IsInterfaceRequestField(field):
def IsUnionField(field): def IsUnionField(field):
return mojom.IsUnionKind(field.kind) return mojom.IsUnionKind(field.kind)
def IsBoolField(field):
return mojom.IsBoolKind(field.kind)
class Generator(generator.Generator): class Generator(generator.Generator):
...@@ -351,6 +354,7 @@ class Generator(generator.Generator): ...@@ -351,6 +354,7 @@ class Generator(generator.Generator):
"field_offset": JavaScriptFieldOffset, "field_offset": JavaScriptFieldOffset,
"has_callbacks": mojom.HasCallbacks, "has_callbacks": mojom.HasCallbacks,
"is_array_pointer_field": IsArrayPointerField, "is_array_pointer_field": IsArrayPointerField,
"is_bool_field": IsBoolField,
"is_map_pointer_field": IsMapPointerField, "is_map_pointer_field": IsMapPointerField,
"is_struct_pointer_field": IsStructPointerField, "is_struct_pointer_field": IsStructPointerField,
"is_string_pointer_field": IsStringPointerField, "is_string_pointer_field": IsStringPointerField,
......
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