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 {
struct ContainsInterfaceRequest {
SomeInterface& request;
};
// Used to verify that boolean fields are correctly serialized/deserialized.
struct SingleBoolStruct {
bool value;
};
......@@ -12,6 +12,7 @@ define([
testBar();
testFoo();
testNamedRegion();
testSingleBooleanStruct();
testTypes();
testAlign();
testUtf8();
......@@ -176,6 +177,23 @@ define([
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 encodeDecode(cls, input, expectedResult, encodedSize) {
var messageName = 42;
......
......@@ -65,6 +65,16 @@ define([
var decoded = structEncodeDecode(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({
object_union: new unions.ObjectUnion({
f_dummy: new unions.DummyStruct({
......
......@@ -59,7 +59,8 @@
var numberOfBytes = decoder.readUint32();
var version = decoder.readUint32();
{%- 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();
{%- for packed_field in byte.packed_fields %}
val.{{packed_field.field.name}} = (packed >> {{packed_field.bit}}) & 1 ? true : false;
......@@ -82,7 +83,8 @@
encoder.writeUint32({{struct.versions[-1].version}});
{%- 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;
{%- for packed_field in byte.packed_fields %}
packed |= (val.{{packed_field.field.name}} & 1) << {{packed_field.bit}}
......
......@@ -89,7 +89,11 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
switch (val.$tag) {
{%- for field in union.fields %}
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}});
{%- endif %}
break;
{%- endfor %}
}
......@@ -111,7 +115,11 @@ Object.defineProperty({{union.name}}.prototype, "{{field.name}}", {
switch (tag) {
{%- for field in union.fields %}
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}};
{%- endif %}
break;
{%- endfor %}
}
......
......@@ -337,6 +337,9 @@ def IsInterfaceRequestField(field):
def IsUnionField(field):
return mojom.IsUnionKind(field.kind)
def IsBoolField(field):
return mojom.IsBoolKind(field.kind)
class Generator(generator.Generator):
......@@ -351,6 +354,7 @@ class Generator(generator.Generator):
"field_offset": JavaScriptFieldOffset,
"has_callbacks": mojom.HasCallbacks,
"is_array_pointer_field": IsArrayPointerField,
"is_bool_field": IsBoolField,
"is_map_pointer_field": IsMapPointerField,
"is_struct_pointer_field": IsStructPointerField,
"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