Commit ae685fa4 authored by hansmuller's avatar hansmuller Committed by Commit bot

The default value for NullableString should be null (was ""). Mojo struct...

The default value for NullableString should be null (was ""). Mojo struct fields whose type is string? should be initialized to null, not "".

BUG=417039

Review URL: https://codereview.chromium.org/605443002

Cr-Commit-Position: refs/heads/master@{#296612}
parent 068213a3
...@@ -20,4 +20,66 @@ struct RectPair { ...@@ -20,4 +20,66 @@ struct RectPair {
struct EmptyStruct { struct EmptyStruct {
}; };
// Used to verify that struct fields which don't specify a deafult are
// initialized to: false for bool, 0 for numbers, and null for strings,
// handles, and structs. The "?" nullable suffix shouldn't have any
// impact on initial field values.
struct NoDefaultFieldValues {
bool f0;
int8 f1;
uint8 f2;
int16 f3;
uint16 f4;
int32 f5;
uint32 f6;
int64 f7;
uint64 f8;
float f9;
double f10;
string f11;
string? f12;
handle<message_pipe> f13;
handle<data_pipe_consumer> f14;
handle<data_pipe_producer> f15;
handle<message_pipe>? f16;
handle<data_pipe_consumer>? f17;
handle<data_pipe_producer>? f18;
handle f19;
handle? f20;
handle<shared_buffer> f21;
handle<shared_buffer>? f22;
string[] f23;
string?[] f24;
string[]? f25;
string?[]? f26;
EmptyStruct f27;
EmptyStruct? f28;
};
// Used to verify that struct fields with an explicit default value
// are initialized correctly. The "?" nullable suffix shouldn't have any
// impact on initial field values.
struct DefaultFieldValues {
const string kFoo = "foo";
bool f0 = true;
int8 f1 = 100;
uint8 f2 = 100;
int16 f3 = 100;
uint16 f4 = 100;
int32 f5 = 100;
uint32 f6 = 100;
int64 f7 = 100;
uint64 f8 = 100;
float f9 = 100;
float f10 = 100.0;
double f11 = 100;
double f12 = 100.0;
string f13 = kFoo;
string? f14 = kFoo;
Rect f15 = default;
Rect? f16 = default;
};
} }
...@@ -188,6 +188,7 @@ define([ ...@@ -188,6 +188,7 @@ define([
expect(result).toEqual(expectedResult); expect(result).toEqual(expectedResult);
} }
encodeDecode(codec.String, "banana", "banana", 24); encodeDecode(codec.String, "banana", "banana", 24);
encodeDecode(codec.NullableString, null, null, 8);
encodeDecode(codec.Int8, -1, -1); encodeDecode(codec.Int8, -1, -1);
encodeDecode(codec.Int8, 0xff, -1); encodeDecode(codec.Int8, 0xff, -1);
encodeDecode(codec.Int16, -1, -1); encodeDecode(codec.Int16, -1, -1);
......
...@@ -26,8 +26,7 @@ define([ ...@@ -26,8 +26,7 @@ define([
expect(new testStructs.RectPair({second: r}).first).toBeNull(); expect(new testStructs.RectPair({second: r}).first).toBeNull();
var nr = new testStructs.NamedRegion(); var nr = new testStructs.NamedRegion();
// TODO(hansmuller): nr.name should be null, see crbug.com/417039. expect(nr.name).toBeNull();
expect(nr.name).toBe("");
expect(nr.rects).toBeNull(); expect(nr.rects).toBeNull();
expect(nr).toEqual(new testStructs.NamedRegion({})); expect(nr).toEqual(new testStructs.NamedRegion({}));
...@@ -42,6 +41,39 @@ define([ ...@@ -42,6 +41,39 @@ define([
expect(e).toEqual(new testStructs.EmptyStruct({foo:123})); expect(e).toEqual(new testStructs.EmptyStruct({foo:123}));
} }
function testNoDefaultFieldValues() {
var s = new testStructs.NoDefaultFieldValues();
expect(s.f0).toEqual(false);
// f1 - f10, number type fields
for (var i = 1; i <= 10; i++)
expect(s["f" + i]).toEqual(0);
// f11,12 strings, f13-22 handles, f23-f26 arrays, f27,28 structs
for (var i = 11; i <= 28; i++)
expect(s["f" + i]).toBeNull();
}
function testDefaultFieldValues() {
var s = new testStructs.DefaultFieldValues();
expect(s.f0).toEqual(true);
// f1 - f12, number type fields
for (var i = 1; i <= 12; i++)
expect(s["f" + i]).toEqual(100);
// f13,14 "foo"
for (var i = 13; i <= 14; i++)
expect(s["f" + i]).toEqual("foo");
// f15,16 a default instance of Rect
var r = new rect.Rect();
expect(s.f15).toEqual(r);
expect(s.f16).toEqual(r);
}
testConstructors(); testConstructors();
testNoDefaultFieldValues();
testDefaultFieldValues();
this.result = "PASS"; this.result = "PASS";
}); });
\ No newline at end of file
...@@ -233,6 +233,9 @@ def TranslateConstants(token, kind): ...@@ -233,6 +233,9 @@ def TranslateConstants(token, kind):
if token.value == "double.NAN" or token.value == "float.NAN": if token.value == "double.NAN" or token.value == "float.NAN":
return "NAN"; return "NAN";
if (kind is not None and mojom.IsFloatKind(kind)):
return token if token.isdigit() else token + "f";
return '%s%s' % (token, _kind_to_cpp_literal_suffix.get(kind, '')) return '%s%s' % (token, _kind_to_cpp_literal_suffix.get(kind, ''))
def ExpressionToText(value, kind=None): def ExpressionToText(value, kind=None):
......
...@@ -31,8 +31,8 @@ _kind_to_javascript_default_value = { ...@@ -31,8 +31,8 @@ _kind_to_javascript_default_value = {
mojom.INT64: "0", mojom.INT64: "0",
mojom.UINT64: "0", mojom.UINT64: "0",
mojom.DOUBLE: "0", mojom.DOUBLE: "0",
mojom.STRING: '""', mojom.STRING: "null",
mojom.NULLABLE_STRING: '""' mojom.NULLABLE_STRING: "null"
} }
......
...@@ -334,6 +334,10 @@ def IsBoolKind(kind): ...@@ -334,6 +334,10 @@ def IsBoolKind(kind):
return kind.spec == BOOL.spec return kind.spec == BOOL.spec
def IsFloatKind(kind):
return kind.spec == FLOAT.spec
def IsStringKind(kind): def IsStringKind(kind):
return kind.spec == STRING.spec or kind.spec == NULLABLE_STRING.spec return kind.spec == STRING.spec or kind.spec == NULLABLE_STRING.spec
......
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