Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1067)

Unified Diff: mojo/public/tools/bindings/generators/mojom_js_generator.py

Issue 2864753002: Mojo code generator: simplify how imported types and values are handled. (Closed)
Patch Set: . Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/generators/mojom_js_generator.py
diff --git a/mojo/public/tools/bindings/generators/mojom_js_generator.py b/mojo/public/tools/bindings/generators/mojom_js_generator.py
index 49620ad0345800408cb7b94e84e67e744dd6308b..20a2b23fd431a9bad41f536820daf9086b47848a 100644
--- a/mojo/public/tools/bindings/generators/mojom_js_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_js_generator.py
@@ -36,56 +36,6 @@ _kind_to_javascript_default_value = {
mojom.NULLABLE_STRING: "null"
}
-
-def JavaScriptType(kind):
- name = []
- if kind.imported_from:
- name.append(kind.imported_from["unique_name"])
- if kind.parent_kind:
- name.append(kind.parent_kind.name)
- name.append(kind.name)
- return ".".join(name)
-
-
-def JavaScriptDefaultValue(field):
- if field.default:
- if mojom.IsStructKind(field.kind):
- assert field.default == "default"
- return "new %s()" % JavaScriptType(field.kind)
- return ExpressionToText(field.default)
- if field.kind in mojom.PRIMITIVES:
- return _kind_to_javascript_default_value[field.kind]
- if mojom.IsStructKind(field.kind):
- return "null"
- if mojom.IsUnionKind(field.kind):
- return "null"
- if mojom.IsArrayKind(field.kind):
- return "null"
- if mojom.IsMapKind(field.kind):
- return "null"
- if mojom.IsInterfaceKind(field.kind):
- return "new %sPtr()" % JavaScriptType(field.kind)
- if mojom.IsInterfaceRequestKind(field.kind):
- return "new bindings.InterfaceRequest()"
- if mojom.IsAssociatedInterfaceKind(field.kind):
- return "new associatedBindings.AssociatedInterfacePtrInfo()"
- if mojom.IsAssociatedInterfaceRequestKind(field.kind):
- return "new associatedBindings.AssociatedInterfaceRequest()"
- if mojom.IsEnumKind(field.kind):
- return "0"
- raise Exception("No valid default: %s" % field)
-
-
-def JavaScriptPayloadSize(packed):
- packed_fields = packed.packed_fields
- if not packed_fields:
- return 0
- last_field = packed_fields[-1]
- offset = last_field.offset + last_field.size
- pad = pack.GetPad(offset, 8)
- return offset + pad
-
-
_kind_to_codec_type = {
mojom.BOOL: "codec.Uint8",
mojom.INT8: "codec.Int8",
@@ -113,110 +63,20 @@ _kind_to_codec_type = {
}
-def CodecType(kind):
- if kind in mojom.PRIMITIVES:
- return _kind_to_codec_type[kind]
- if mojom.IsStructKind(kind):
- pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \
- else "PointerTo"
- return "new codec.%s(%s)" % (pointer_type, JavaScriptType(kind))
- if mojom.IsUnionKind(kind):
- return JavaScriptType(kind)
- if mojom.IsArrayKind(kind):
- array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf"
- array_length = "" if kind.length is None else ", %d" % kind.length
- element_type = ElementCodecType(kind.kind)
- return "new codec.%s(%s%s)" % (array_type, element_type, array_length)
- if mojom.IsInterfaceKind(kind):
- return "new codec.%s(%sPtr)" % (
- "NullableInterface" if mojom.IsNullableKind(kind) else "Interface",
- JavaScriptType(kind))
- if mojom.IsInterfaceRequestKind(kind):
- return "codec.%s" % (
- "NullableInterfaceRequest" if mojom.IsNullableKind(kind)
- else "InterfaceRequest")
- if mojom.IsAssociatedInterfaceKind(kind):
- return "codec.%s" % (
- "NullableAssociatedInterfacePtrInfo" if mojom.IsNullableKind(kind)
- else "AssociatedInterfacePtrInfo")
- if mojom.IsAssociatedInterfaceRequestKind(kind):
- return "codec.%s" % (
- "NullableAssociatedInterfaceRequest" if mojom.IsNullableKind(kind)
- else "AssociatedInterfaceRequest")
- if mojom.IsEnumKind(kind):
- return "new codec.Enum(%s)" % JavaScriptType(kind)
- if mojom.IsMapKind(kind):
- map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf"
- key_type = ElementCodecType(kind.key_kind)
- value_type = ElementCodecType(kind.value_kind)
- return "new codec.%s(%s, %s)" % (map_type, key_type, value_type)
- raise Exception("No codec type for %s" % kind)
-
-
-def ElementCodecType(kind):
- return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind)
-
-
-def JavaScriptDecodeSnippet(kind):
- if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or
- mojom.IsAnyInterfaceKind(kind)):
- return "decodeStruct(%s)" % CodecType(kind)
- if mojom.IsStructKind(kind):
- return "decodeStructPointer(%s)" % JavaScriptType(kind)
- if mojom.IsMapKind(kind):
- return "decodeMapPointer(%s, %s)" % \
- (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind))
- if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
- return "decodeArrayPointer(codec.PackedBool)"
- if mojom.IsArrayKind(kind):
- return "decodeArrayPointer(%s)" % CodecType(kind.kind)
- if mojom.IsUnionKind(kind):
- return "decodeUnion(%s)" % CodecType(kind)
- if mojom.IsEnumKind(kind):
- return JavaScriptDecodeSnippet(mojom.INT32)
- raise Exception("No decode snippet for %s" % kind)
-
-
-def JavaScriptEncodeSnippet(kind):
- if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or
- mojom.IsAnyInterfaceKind(kind)):
- return "encodeStruct(%s, " % CodecType(kind)
- if mojom.IsUnionKind(kind):
- return "encodeStruct(%s, " % JavaScriptType(kind)
- if mojom.IsStructKind(kind):
- return "encodeStructPointer(%s, " % JavaScriptType(kind)
- if mojom.IsMapKind(kind):
- return "encodeMapPointer(%s, %s, " % \
- (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind))
- if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
- return "encodeArrayPointer(codec.PackedBool, ";
- if mojom.IsArrayKind(kind):
- return "encodeArrayPointer(%s, " % CodecType(kind.kind)
- if mojom.IsEnumKind(kind):
- return JavaScriptEncodeSnippet(mojom.INT32)
- raise Exception("No encode snippet for %s" % kind)
-
-
-def JavaScriptUnionDecodeSnippet(kind):
- if mojom.IsUnionKind(kind):
- return "decodeStructPointer(%s)" % JavaScriptType(kind)
- return JavaScriptDecodeSnippet(kind)
-
-
-def JavaScriptUnionEncodeSnippet(kind):
- if mojom.IsUnionKind(kind):
- return "encodeStructPointer(%s, " % JavaScriptType(kind)
- return JavaScriptEncodeSnippet(kind)
+def JavaScriptPayloadSize(packed):
+ packed_fields = packed.packed_fields
+ if not packed_fields:
+ return 0
+ last_field = packed_fields[-1]
+ offset = last_field.offset + last_field.size
+ pad = pack.GetPad(offset, 8)
+ return offset + pad
def JavaScriptFieldOffset(packed_field):
return "offset + codec.kStructHeaderSize + %s" % packed_field.offset
-def JavaScriptNullableParam(field):
- return "true" if mojom.IsNullableKind(field.kind) else "false"
-
-
def GetArrayExpectedDimensionSizes(kind):
expected_dimension_sizes = []
while mojom.IsArrayKind(kind):
@@ -228,115 +88,6 @@ def GetArrayExpectedDimensionSizes(kind):
return expected_dimension_sizes
-def JavaScriptValidateArrayParams(field):
- nullable = JavaScriptNullableParam(field)
- element_kind = field.kind.kind
- element_size = pack.PackedField.GetSizeForKind(element_kind)
- expected_dimension_sizes = GetArrayExpectedDimensionSizes(
- field.kind)
- element_type = ElementCodecType(element_kind)
- return "%s, %s, %s, %s, 0" % \
- (element_size, element_type, nullable,
- expected_dimension_sizes)
-
-
-def JavaScriptValidateEnumParams(field):
- return JavaScriptType(field.kind)
-
-def JavaScriptValidateStructParams(field):
- nullable = JavaScriptNullableParam(field)
- struct_type = JavaScriptType(field.kind)
- return "%s, %s" % (struct_type, nullable)
-
-def JavaScriptValidateUnionParams(field):
- nullable = JavaScriptNullableParam(field)
- union_type = JavaScriptType(field.kind)
- return "%s, %s" % (union_type, nullable)
-
-def JavaScriptValidateMapParams(field):
- nullable = JavaScriptNullableParam(field)
- keys_type = ElementCodecType(field.kind.key_kind)
- values_kind = field.kind.value_kind;
- values_type = ElementCodecType(values_kind)
- values_nullable = "true" if mojom.IsNullableKind(values_kind) else "false"
- return "%s, %s, %s, %s" % \
- (nullable, keys_type, values_type, values_nullable)
-
-
-def TranslateConstants(token):
- if isinstance(token, (mojom.EnumValue, mojom.NamedValue)):
- # Both variable and enum constants are constructed like:
- # NamespaceUid.Struct[.Enum].CONSTANT_NAME
- name = []
- if token.imported_from:
- name.append(token.imported_from["unique_name"])
- if token.parent_kind:
- name.append(token.parent_kind.name)
- if isinstance(token, mojom.EnumValue):
- name.append(token.enum.name)
- name.append(token.name)
- return ".".join(name)
-
- if isinstance(token, mojom.BuiltinValue):
- if token.value == "double.INFINITY" or token.value == "float.INFINITY":
- return "Infinity";
- if token.value == "double.NEGATIVE_INFINITY" or \
- token.value == "float.NEGATIVE_INFINITY":
- return "-Infinity";
- if token.value == "double.NAN" or token.value == "float.NAN":
- return "NaN";
-
- return token
-
-
-def ExpressionToText(value):
- return TranslateConstants(value)
-
-def IsArrayPointerField(field):
- return mojom.IsArrayKind(field.kind)
-
-def IsEnumField(field):
- return mojom.IsEnumKind(field.kind)
-
-def IsStringPointerField(field):
- return mojom.IsStringKind(field.kind)
-
-def IsStructPointerField(field):
- return mojom.IsStructKind(field.kind)
-
-def IsMapPointerField(field):
- return mojom.IsMapKind(field.kind)
-
-def IsHandleField(field):
- return mojom.IsAnyHandleKind(field.kind)
-
-def IsInterfaceField(field):
- return mojom.IsInterfaceKind(field.kind)
-
-def IsInterfaceRequestField(field):
- return mojom.IsInterfaceRequestKind(field.kind)
-
-def IsAssociatedInterfaceField(field):
- return mojom.IsAssociatedInterfaceKind(field.kind)
-
-def IsAssociatedInterfaceRequestField(field):
- return mojom.IsAssociatedInterfaceRequestKind(field.kind)
-
-def IsUnionField(field):
- return mojom.IsUnionKind(field.kind)
-
-def IsBoolField(field):
- return mojom.IsBoolKind(field.kind)
-
-def IsObjectField(field):
- return mojom.IsObjectKind(field.kind)
-
-def IsAnyHandleOrInterfaceField(field):
- return mojom.IsAnyHandleOrInterfaceKind(field.kind)
-
-def IsEnumField(field):
- return mojom.IsEnumKind(field.kind)
-
def GetRelativePath(module, base_module):
return os.path.relpath(module.path, os.path.dirname(base_module.path))
@@ -362,40 +113,40 @@ class Generator(generator.Generator):
def GetFilters(self):
js_filters = {
- "decode_snippet": JavaScriptDecodeSnippet,
- "default_value": JavaScriptDefaultValue,
- "encode_snippet": JavaScriptEncodeSnippet,
- "expression_to_text": ExpressionToText,
+ "decode_snippet": self._JavaScriptDecodeSnippet,
+ "default_value": self._JavaScriptDefaultValue,
+ "encode_snippet": self._JavaScriptEncodeSnippet,
+ "expression_to_text": self._ExpressionToText,
"field_offset": JavaScriptFieldOffset,
"has_callbacks": mojom.HasCallbacks,
- "is_any_handle_or_interface_field": IsAnyHandleOrInterfaceField,
- "is_array_pointer_field": IsArrayPointerField,
- "is_associated_interface_field": IsAssociatedInterfaceField,
- "is_associated_interface_request_field":
- IsAssociatedInterfaceRequestField,
- "is_bool_field": IsBoolField,
- "is_enum_field": IsEnumField,
- "is_handle_field": IsHandleField,
- "is_interface_field": IsInterfaceField,
- "is_interface_request_field": IsInterfaceRequestField,
- "is_map_pointer_field": IsMapPointerField,
- "is_object_field": IsObjectField,
- "is_string_pointer_field": IsStringPointerField,
- "is_struct_pointer_field": IsStructPointerField,
- "is_union_field": IsUnionField,
- "js_type": JavaScriptType,
+ "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind,
+ "is_array_kind": mojom.IsArrayKind,
+ "is_associated_interface_kind": mojom.IsAssociatedInterfaceKind,
+ "is_associated_interface_request_kind":
+ mojom.IsAssociatedInterfaceRequestKind,
+ "is_bool_kind": mojom.IsBoolKind,
+ "is_enum_kind": mojom.IsEnumKind,
+ "is_any_handle_kind": mojom.IsAnyHandleKind,
+ "is_interface_kind": mojom.IsInterfaceKind,
+ "is_interface_request_kind": mojom.IsInterfaceRequestKind,
+ "is_map_kind": mojom.IsMapKind,
+ "is_object_kind": mojom.IsObjectKind,
+ "is_string_kind": mojom.IsStringKind,
+ "is_struct_kind": mojom.IsStructKind,
+ "is_union_kind": mojom.IsUnionKind,
+ "js_type": self._JavaScriptType,
"method_passes_associated_kinds": mojom.MethodPassesAssociatedKinds,
"payload_size": JavaScriptPayloadSize,
"get_relative_path": GetRelativePath,
"stylize_method": generator.StudlyCapsToCamel,
- "union_decode_snippet": JavaScriptUnionDecodeSnippet,
- "union_encode_snippet": JavaScriptUnionEncodeSnippet,
- "validate_array_params": JavaScriptValidateArrayParams,
- "validate_enum_params": JavaScriptValidateEnumParams,
- "validate_map_params": JavaScriptValidateMapParams,
- "validate_nullable_params": JavaScriptNullableParam,
- "validate_struct_params": JavaScriptValidateStructParams,
- "validate_union_params": JavaScriptValidateUnionParams,
+ "union_decode_snippet": self._JavaScriptUnionDecodeSnippet,
+ "union_encode_snippet": self._JavaScriptUnionEncodeSnippet,
+ "validate_array_params": self._JavaScriptValidateArrayParams,
+ "validate_enum_params": self._JavaScriptValidateEnumParams,
+ "validate_map_params": self._JavaScriptValidateMapParams,
+ "validate_nullable_params": self._JavaScriptNullableParam,
+ "validate_struct_params": self._JavaScriptValidateStructParams,
+ "validate_union_params": self._JavaScriptValidateUnionParams,
}
return js_filters
@@ -411,9 +162,10 @@ class Generator(generator.Generator):
self.MatchMojomFilePath("%s.js" % self.module.name))
def _GetImports(self):
+ # TODO(yzshen): Remove this method once the old JS bindings go away.
used_names = set()
for each_import in self.module.imports:
- simple_name = each_import["module_name"].split(".")[0]
+ simple_name = each_import.name.split(".")[0]
# Since each import is assigned a variable in JS, they need to have unique
# names.
@@ -424,15 +176,208 @@ class Generator(generator.Generator):
unique_name = simple_name + str(counter)
used_names.add(unique_name)
- each_import["unique_name"] = unique_name + "$"
+ each_import.unique_name = unique_name + "$"
counter += 1
return self.module.imports
def _GetImportedInterfaces(self):
interface_to_import = {};
for each_import in self.module.imports:
- for each_interface in each_import["module"].interfaces:
+ for each_interface in each_import.interfaces:
name = each_interface.name
- interface_to_import[name] = each_import["unique_name"] + "." + name
+ interface_to_import[name] = each_import.unique_name + "." + name
return interface_to_import;
+ def _JavaScriptType(self, kind):
+ name = []
+ if kind.module and kind.module.path != self.module.path:
+ name.append(kind.module.unique_name)
+ if kind.parent_kind:
+ name.append(kind.parent_kind.name)
+ name.append(kind.name)
+ return ".".join(name)
+
+ def _JavaScriptDefaultValue(self, field):
+ if field.default:
+ if mojom.IsStructKind(field.kind):
+ assert field.default == "default"
+ return "new %s()" % self._JavaScriptType(field.kind)
+ return self._ExpressionToText(field.default)
+ if field.kind in mojom.PRIMITIVES:
+ return _kind_to_javascript_default_value[field.kind]
+ if mojom.IsStructKind(field.kind):
+ return "null"
+ if mojom.IsUnionKind(field.kind):
+ return "null"
+ if mojom.IsArrayKind(field.kind):
+ return "null"
+ if mojom.IsMapKind(field.kind):
+ return "null"
+ if mojom.IsInterfaceKind(field.kind):
+ return "new %sPtr()" % self._JavaScriptType(field.kind)
+ if mojom.IsInterfaceRequestKind(field.kind):
+ return "new bindings.InterfaceRequest()"
+ if mojom.IsAssociatedInterfaceKind(field.kind):
+ return "new associatedBindings.AssociatedInterfacePtrInfo()"
+ if mojom.IsAssociatedInterfaceRequestKind(field.kind):
+ return "new associatedBindings.AssociatedInterfaceRequest()"
+ if mojom.IsEnumKind(field.kind):
+ return "0"
+ raise Exception("No valid default: %s" % field)
+
+ def _CodecType(self, kind):
+ if kind in mojom.PRIMITIVES:
+ return _kind_to_codec_type[kind]
+ if mojom.IsStructKind(kind):
+ pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \
+ else "PointerTo"
+ return "new codec.%s(%s)" % (pointer_type, self._JavaScriptType(kind))
+ if mojom.IsUnionKind(kind):
+ return self._JavaScriptType(kind)
+ if mojom.IsArrayKind(kind):
+ array_type = ("NullableArrayOf" if mojom.IsNullableKind(kind)
+ else "ArrayOf")
+ array_length = "" if kind.length is None else ", %d" % kind.length
+ element_type = self._ElementCodecType(kind.kind)
+ return "new codec.%s(%s%s)" % (array_type, element_type, array_length)
+ if mojom.IsInterfaceKind(kind):
+ return "new codec.%s(%sPtr)" % (
+ "NullableInterface" if mojom.IsNullableKind(kind) else "Interface",
+ self._JavaScriptType(kind))
+ if mojom.IsInterfaceRequestKind(kind):
+ return "codec.%s" % (
+ "NullableInterfaceRequest" if mojom.IsNullableKind(kind)
+ else "InterfaceRequest")
+ if mojom.IsAssociatedInterfaceKind(kind):
+ return "codec.%s" % ("NullableAssociatedInterfacePtrInfo"
+ if mojom.IsNullableKind(kind) else "AssociatedInterfacePtrInfo")
+ if mojom.IsAssociatedInterfaceRequestKind(kind):
+ return "codec.%s" % ("NullableAssociatedInterfaceRequest"
+ if mojom.IsNullableKind(kind) else "AssociatedInterfaceRequest")
+ if mojom.IsEnumKind(kind):
+ return "new codec.Enum(%s)" % self._JavaScriptType(kind)
+ if mojom.IsMapKind(kind):
+ map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf"
+ key_type = self._ElementCodecType(kind.key_kind)
+ value_type = self._ElementCodecType(kind.value_kind)
+ return "new codec.%s(%s, %s)" % (map_type, key_type, value_type)
+ raise Exception("No codec type for %s" % kind)
+
+ def _ElementCodecType(self, kind):
+ return ("codec.PackedBool" if mojom.IsBoolKind(kind)
+ else self._CodecType(kind))
+
+ def _JavaScriptDecodeSnippet(self, kind):
+ if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or
+ mojom.IsAnyInterfaceKind(kind)):
+ return "decodeStruct(%s)" % self._CodecType(kind)
+ if mojom.IsStructKind(kind):
+ return "decodeStructPointer(%s)" % self._JavaScriptType(kind)
+ if mojom.IsMapKind(kind):
+ return "decodeMapPointer(%s, %s)" % (
+ self._ElementCodecType(kind.key_kind),
+ self._ElementCodecType(kind.value_kind))
+ if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
+ return "decodeArrayPointer(codec.PackedBool)"
+ if mojom.IsArrayKind(kind):
+ return "decodeArrayPointer(%s)" % self._CodecType(kind.kind)
+ if mojom.IsUnionKind(kind):
+ return "decodeUnion(%s)" % self._CodecType(kind)
+ if mojom.IsEnumKind(kind):
+ return self._JavaScriptDecodeSnippet(mojom.INT32)
+ raise Exception("No decode snippet for %s" % kind)
+
+ def _JavaScriptEncodeSnippet(self, kind):
+ if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or
+ mojom.IsAnyInterfaceKind(kind)):
+ return "encodeStruct(%s, " % self._CodecType(kind)
+ if mojom.IsUnionKind(kind):
+ return "encodeStruct(%s, " % self._JavaScriptType(kind)
+ if mojom.IsStructKind(kind):
+ return "encodeStructPointer(%s, " % self._JavaScriptType(kind)
+ if mojom.IsMapKind(kind):
+ return "encodeMapPointer(%s, %s, " % (
+ self._ElementCodecType(kind.key_kind),
+ self._ElementCodecType(kind.value_kind))
+ if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
+ return "encodeArrayPointer(codec.PackedBool, ";
+ if mojom.IsArrayKind(kind):
+ return "encodeArrayPointer(%s, " % self._CodecType(kind.kind)
+ if mojom.IsEnumKind(kind):
+ return self._JavaScriptEncodeSnippet(mojom.INT32)
+ raise Exception("No encode snippet for %s" % kind)
+
+ def _JavaScriptUnionDecodeSnippet(self, kind):
+ if mojom.IsUnionKind(kind):
+ return "decodeStructPointer(%s)" % self._JavaScriptType(kind)
+ return self._JavaScriptDecodeSnippet(kind)
+
+ def _JavaScriptUnionEncodeSnippet(self, kind):
+ if mojom.IsUnionKind(kind):
+ return "encodeStructPointer(%s, " % self._JavaScriptType(kind)
+ return self._JavaScriptEncodeSnippet(kind)
+
+ def _JavaScriptNullableParam(self, field):
+ return "true" if mojom.IsNullableKind(field.kind) else "false"
+
+ def _JavaScriptValidateArrayParams(self, field):
+ nullable = self._JavaScriptNullableParam(field)
+ element_kind = field.kind.kind
+ element_size = pack.PackedField.GetSizeForKind(element_kind)
+ expected_dimension_sizes = GetArrayExpectedDimensionSizes(
+ field.kind)
+ element_type = self._ElementCodecType(element_kind)
+ return "%s, %s, %s, %s, 0" % \
+ (element_size, element_type, nullable,
+ expected_dimension_sizes)
+
+ def _JavaScriptValidateEnumParams(self, field):
+ return self._JavaScriptType(field.kind)
+
+ def _JavaScriptValidateStructParams(self, field):
+ nullable = self._JavaScriptNullableParam(field)
+ struct_type = self._JavaScriptType(field.kind)
+ return "%s, %s" % (struct_type, nullable)
+
+ def _JavaScriptValidateUnionParams(self, field):
+ nullable = self._JavaScriptNullableParam(field)
+ union_type = self._JavaScriptType(field.kind)
+ return "%s, %s" % (union_type, nullable)
+
+ def _JavaScriptValidateMapParams(self, field):
+ nullable = self._JavaScriptNullableParam(field)
+ keys_type = self._ElementCodecType(field.kind.key_kind)
+ values_kind = field.kind.value_kind;
+ values_type = self._ElementCodecType(values_kind)
+ values_nullable = "true" if mojom.IsNullableKind(values_kind) else "false"
+ return "%s, %s, %s, %s" % \
+ (nullable, keys_type, values_type, values_nullable)
+
+ def _TranslateConstants(self, token):
+ if isinstance(token, (mojom.EnumValue, mojom.NamedValue)):
+ # Both variable and enum constants are constructed like:
+ # NamespaceUid.Struct[.Enum].CONSTANT_NAME
+ name = []
+ if token.module and token.module.path != self.module.path:
+ name.append(token.module.unique_name)
+ if token.parent_kind:
+ name.append(token.parent_kind.name)
+ if isinstance(token, mojom.EnumValue):
+ name.append(token.enum.name)
+ name.append(token.name)
+ return ".".join(name)
+
+ if isinstance(token, mojom.BuiltinValue):
+ if token.value == "double.INFINITY" or token.value == "float.INFINITY":
+ return "Infinity";
+ if token.value == "double.NEGATIVE_INFINITY" or \
+ token.value == "float.NEGATIVE_INFINITY":
+ return "-Infinity";
+ if token.value == "double.NAN" or token.value == "float.NAN":
+ return "NaN";
+
+ return token
+
+ def _ExpressionToText(self, value):
+ return self._TranslateConstants(value)
+

Powered by Google App Engine
This is Rietveld 408576698