diff options
author | sammc@chromium.org <sammc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-16 20:32:41 +0000 |
---|---|---|
committer | sammc@chromium.org <sammc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-16 20:32:41 +0000 |
commit | 582f88ac5208c658811b8b053d9199513f072eeb (patch) | |
tree | 632563953cd4e023a9b9a1be0659cf80f429cd73 | |
parent | abd4b686e423705d305e68b29dc4b9822fb8417e (diff) | |
download | chromium_src-582f88ac5208c658811b8b053d9199513f072eeb.zip chromium_src-582f88ac5208c658811b8b053d9199513f072eeb.tar.gz chromium_src-582f88ac5208c658811b8b053d9199513f072eeb.tar.bz2 |
Mojo: Correctly handle large unsigned integer literals.
This adds U suffixes to literals for unsigned int types in C++. For
Java, because integer literals >= 2 ** 63 are not allowed, such values
are transformed to the equivalent 64 bit signed value.
BUG=375522
Review URL: https://codereview.chromium.org/397453004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283487 0039d316-1c4b-4281-b951-d872f2087c98
8 files changed, 38 insertions, 22 deletions
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java index 6424cbe..0005b07 100644 --- a/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java +++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java @@ -179,7 +179,10 @@ public class BindingsTest extends TestCase { checkField(DefaultsTest.class.getField("a2"), short.class, test, (short) 1234); checkField(DefaultsTest.class.getField("a3"), short.class, test, (short) 34567); checkField(DefaultsTest.class.getField("a4"), int.class, test, 123456); + checkField(DefaultsTest.class.getField("a5"), int.class, test, (int) 3456789012L); checkField(DefaultsTest.class.getField("a6"), long.class, test, 111111111111L); + // -8446744073709551617 == 9999999999999999999 - 2 ^ 64. + checkField(DefaultsTest.class.getField("a7"), long.class, test, -8446744073709551617L); checkField(DefaultsTest.class.getField("a8"), int.class, test, 0x12345); checkField(DefaultsTest.class.getField("a9"), int.class, test, -0x12345); checkField(DefaultsTest.class.getField("a10"), int.class, test, 1234); diff --git a/mojo/apps/js/bindings/sample_service_unittests.js b/mojo/apps/js/bindings/sample_service_unittests.js index eead798..2261384 100644 --- a/mojo/apps/js/bindings/sample_service_unittests.js +++ b/mojo/apps/js/bindings/sample_service_unittests.js @@ -104,12 +104,11 @@ define([ expect(defaults.a2).toBe(1234); expect(defaults.a3).toBe(34567); expect(defaults.a4).toBe(123456); - // TODO(vtl): crbug.com/375522 - // expect(defaults.a5).toBe(3456789012); + expect(defaults.a5).toBe(3456789012); expect(defaults.a6).toBe(111111111111); - // TODO(vtl): crbug.com/375522 (Also, can we get exact values for large - // int64/uint64's in JS?) - // expect(defaults.a7).toBe(9999999999999999999); + // JS doesn't have a 64 bit integer type so this is just checking that the + // expected and actual values have the same closest double value. + expect(defaults.a7).toBe(9999999999999999999); expect(defaults.a8).toBe(0x12345); expect(defaults.a9).toBe(-0x12345); expect(defaults.a10).toBe(1234); diff --git a/mojo/public/cpp/bindings/tests/sample_service_unittest.cc b/mojo/public/cpp/bindings/tests/sample_service_unittest.cc index 0e4f0ce..156f7a5 100644 --- a/mojo/public/cpp/bindings/tests/sample_service_unittest.cc +++ b/mojo/public/cpp/bindings/tests/sample_service_unittest.cc @@ -353,11 +353,9 @@ TEST_F(BindingsSampleTest, DefaultValues) { EXPECT_EQ(1234, defaults->a2); EXPECT_EQ(34567U, defaults->a3); EXPECT_EQ(123456, defaults->a4); - // TODO(vtl): crbug.com/375522 - // EXPECT_EQ(3456789012U, defaults->a5); + EXPECT_EQ(3456789012U, defaults->a5); EXPECT_EQ(111111111111LL, defaults->a6); - // TODO(vtl): crbug.com/375522 - // EXPECT_EQ(9999999999999999999ULL, defaults->a7); + EXPECT_EQ(9999999999999999999ULL, defaults->a7); EXPECT_EQ(0x12345, defaults->a8); EXPECT_EQ(-0x12345, defaults->a9); EXPECT_EQ(1234, defaults->a10); diff --git a/mojo/public/interfaces/bindings/tests/sample_service.mojom b/mojo/public/interfaces/bindings/tests/sample_service.mojom index 2863540..b4ba370 100644 --- a/mojo/public/interfaces/bindings/tests/sample_service.mojom +++ b/mojo/public/interfaces/bindings/tests/sample_service.mojom @@ -9,6 +9,7 @@ import "sample_import2.mojom" module sample { const uint8 kTwelve = 12; +const uint64 kTooBigForSignedInt64 = 9999999999999999999; struct Bar { enum Type { @@ -49,11 +50,9 @@ struct DefaultsTest { int16 a2@2 = 1234; uint16 a3@3 = 34567; int32 a4@4 = 123456; - // TODO(vtl): crbug.com/375522 - uint32 a5@5 /* = 3456789012 */; + uint32 a5@5 = 3456789012; int64 a6@6 = 111111111111; - // TODO(vtl): crbug.com/375522 - uint64 a7@7 /* = 9999999999999999999 */; + uint64 a7@7 = 9999999999999999999; int32 a8@8 = 0x12345; int32 a9@9 = -0x12345; int32 a10@10 = +1234; diff --git a/mojo/public/tools/bindings/generators/cpp_templates/interface_definition.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/interface_definition.tmpl index 9b6a324..0164c2d 100644 --- a/mojo/public/tools/bindings/generators/cpp_templates/interface_definition.tmpl +++ b/mojo/public/tools/bindings/generators/cpp_templates/interface_definition.tmpl @@ -76,7 +76,7 @@ params->{{param.name}} const char* {{class_name}}::Name_ = "{{namespace_as_string}}::{{class_name}}"; {#--- Constants #} {% for constant in interface.constants %} -const {{constant.kind|cpp_pod_type}} {{interface.name}}::{{constant.name}} = {{constant.value|expression_to_text}}; +const {{constant.kind|cpp_pod_type}} {{interface.name}}::{{constant.name}} = {{constant|constant_value}}; {%- endfor %} {#--- ForwardToCallback definition #} diff --git a/mojo/public/tools/bindings/generators/cpp_templates/module.cc.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/module.cc.tmpl index 8cf8e99..66b717e 100644 --- a/mojo/public/tools/bindings/generators/cpp_templates/module.cc.tmpl +++ b/mojo/public/tools/bindings/generators/cpp_templates/module.cc.tmpl @@ -22,7 +22,7 @@ namespace {{namespace}} { {#--- Constants #} {% for constant in module.constants %} -const {{constant.kind|cpp_pod_type}} {{constant.name}} = {{constant.value|expression_to_text}}; +const {{constant.kind|cpp_pod_type}} {{constant.name}} = {{constant|constant_value}}; {%- endfor %} namespace internal { @@ -58,7 +58,7 @@ const uint32_t {{method_name}} = {{method.ordinal}}; {#--- Struct Constants #} {%- for struct in structs %} {% for constant in struct.constants %} -const {{constant.kind|cpp_pod_type}} {{struct.name}}::{{constant.name}} = {{constant.value|expression_to_text}}; +const {{constant.kind|cpp_pod_type}} {{struct.name}}::{{constant.name}} = {{constant|constant_value}}; {%- endfor %} {%- endfor %} diff --git a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py index fa1df07..98f56b6 100644 --- a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py +++ b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py @@ -29,12 +29,23 @@ _kind_to_cpp_type = { mojom.DOUBLE: "double", } +_kind_to_cpp_literal_suffix = { + mojom.UINT8: "U", + mojom.UINT16: "U", + mojom.UINT32: "U", + mojom.FLOAT: "f", + mojom.UINT64: "ULL", +} + +def ConstantValue(constant): + return ExpressionToText(constant.value, kind=constant.kind) + def DefaultValue(field): if field.default: if isinstance(field.kind, mojom.Struct): assert field.default == "default" return "%s::New()" % GetNameForKind(field.kind) - return ExpressionToText(field.default) + return ExpressionToText(field.default, kind=field.kind) return "" def NamespaceToArray(namespace): @@ -193,7 +204,7 @@ def IsStructWithHandles(struct): return True return False -def TranslateConstants(token): +def TranslateConstants(token, kind): if isinstance(token, (mojom.NamedValue, mojom.EnumValue)): # Both variable and enum constants are constructed like: # Namespace::Struct::CONSTANT_NAME @@ -209,10 +220,10 @@ def TranslateConstants(token): else: name.append(token.name) return "::".join(name) - return token + return '%s%s' % (token, _kind_to_cpp_literal_suffix.get(kind, '')) -def ExpressionToText(value): - return TranslateConstants(value) +def ExpressionToText(value, kind=None): + return TranslateConstants(value, kind) def HasCallbacks(interface): for method in interface.methods: @@ -234,6 +245,7 @@ _HEADER_SIZE = 8 class Generator(generator.Generator): cpp_filters = { + "constant_value": ConstantValue, "cpp_const_wrapper_type": GetCppConstWrapperType, "cpp_field_type": GetCppFieldType, "cpp_pod_type": GetCppPodType, diff --git a/mojo/public/tools/bindings/generators/mojom_java_generator.py b/mojo/public/tools/bindings/generators/mojom_java_generator.py index 7364afc..ec897f7 100644 --- a/mojo/public/tools/bindings/generators/mojom_java_generator.py +++ b/mojo/public/tools/bindings/generators/mojom_java_generator.py @@ -235,7 +235,12 @@ def ExpressionToText(context, token): return _TranslateNamedValue(token) # Add Long suffix to all number literals. if re.match('^[0-9]+$', token): - return token + 'L' + number = int(token) + # If the literal is too large to fit a signed long, convert it to the + # equivalent signed long. + if number >= 2 ** 63: + number -= 2 ** 64 + return '%dL' % number return token def IsPointerArrayKind(kind): |