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

Side by Side Diff: mojo/public/tools/bindings/generators/mojom_js_generator.py

Issue 2380303003: [Mojo] Make javascript enums extensible. (Closed)
Patch Set: Make Enum a codec type Created 4 years, 2 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 unified diff | Download patch
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Generates JavaScript source files from a mojom.Module.""" 5 """Generates JavaScript source files from a mojom.Module."""
6 6
7 import mojom.generate.generator as generator 7 import mojom.generate.generator as generator
8 import mojom.generate.module as mojom 8 import mojom.generate.module as mojom
9 import mojom.generate.pack as pack 9 import mojom.generate.pack as pack
10 from mojom.generate.template_expander import UseJinja 10 from mojom.generate.template_expander import UseJinja
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 if mojom.IsInterfaceKind(kind): 122 if mojom.IsInterfaceKind(kind):
123 return "codec.%s" % ("NullableInterface" if mojom.IsNullableKind(kind) 123 return "codec.%s" % ("NullableInterface" if mojom.IsNullableKind(kind)
124 else "Interface") 124 else "Interface")
125 if mojom.IsInterfaceRequestKind(kind): 125 if mojom.IsInterfaceRequestKind(kind):
126 return CodecType(mojom.MSGPIPE) 126 return CodecType(mojom.MSGPIPE)
127 if mojom.IsAssociatedInterfaceKind(kind): 127 if mojom.IsAssociatedInterfaceKind(kind):
128 return "codec.AssociatedInterfaceNotSupported" 128 return "codec.AssociatedInterfaceNotSupported"
129 if mojom.IsAssociatedInterfaceRequestKind(kind): 129 if mojom.IsAssociatedInterfaceRequestKind(kind):
130 return "codec.AssociatedInterfaceRequestNotSupported" 130 return "codec.AssociatedInterfaceRequestNotSupported"
131 if mojom.IsEnumKind(kind): 131 if mojom.IsEnumKind(kind):
132 return _kind_to_codec_type[mojom.INT32] 132 enum_type = "Enum"
133 element_type = kind.name
134 return "new codec.%s(%s)" % (enum_type, element_type)
Ken Rockot(use gerrit already) 2016/09/30 21:51:54 nit: No need for enum_type, you can just use "new
mbjorge 2016/09/30 22:14:51 Done.
133 if mojom.IsMapKind(kind): 135 if mojom.IsMapKind(kind):
134 map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf" 136 map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf"
135 key_type = ElementCodecType(kind.key_kind) 137 key_type = ElementCodecType(kind.key_kind)
136 value_type = ElementCodecType(kind.value_kind) 138 value_type = ElementCodecType(kind.value_kind)
137 return "new codec.%s(%s, %s)" % (map_type, key_type, value_type) 139 return "new codec.%s(%s, %s)" % (map_type, key_type, value_type)
138 raise Exception("No codec type for %s" % kind) 140 raise Exception("No codec type for %s" % kind)
139 141
140 142
141 def ElementCodecType(kind): 143 def ElementCodecType(kind):
142 return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) 144 return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind)
143 145
146
144 def JavaScriptDecodeSnippet(kind): 147 def JavaScriptDecodeSnippet(kind):
145 if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or 148 if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or
146 mojom.IsInterfaceKind(kind) or mojom.IsAssociatedKind(kind)): 149 mojom.IsInterfaceKind(kind) or mojom.IsAssociatedKind(kind)):
147 return "decodeStruct(%s)" % CodecType(kind) 150 return "decodeStruct(%s)" % CodecType(kind)
148 if mojom.IsStructKind(kind): 151 if mojom.IsStructKind(kind):
149 return "decodeStructPointer(%s)" % JavaScriptType(kind) 152 return "decodeStructPointer(%s)" % JavaScriptType(kind)
150 if mojom.IsMapKind(kind): 153 if mojom.IsMapKind(kind):
151 return "decodeMapPointer(%s, %s)" % \ 154 return "decodeMapPointer(%s, %s)" % \
152 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) 155 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind))
153 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): 156 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 element_kind = field.kind.kind 224 element_kind = field.kind.kind
222 element_size = pack.PackedField.GetSizeForKind(element_kind) 225 element_size = pack.PackedField.GetSizeForKind(element_kind)
223 expected_dimension_sizes = GetArrayExpectedDimensionSizes( 226 expected_dimension_sizes = GetArrayExpectedDimensionSizes(
224 field.kind) 227 field.kind)
225 element_type = ElementCodecType(element_kind) 228 element_type = ElementCodecType(element_kind)
226 return "%s, %s, %s, %s, 0" % \ 229 return "%s, %s, %s, %s, 0" % \
227 (element_size, element_type, nullable, 230 (element_size, element_type, nullable,
228 expected_dimension_sizes) 231 expected_dimension_sizes)
229 232
230 233
234 def JavaScriptValidateEnumParams(field):
235 nullable = JavaScriptNullableParam(field)
236 enum_type = JavaScriptType(field.kind)
237 return "%s, %s" % (enum_type, nullable)
238
231 def JavaScriptValidateStructParams(field): 239 def JavaScriptValidateStructParams(field):
232 nullable = JavaScriptNullableParam(field) 240 nullable = JavaScriptNullableParam(field)
233 struct_type = JavaScriptType(field.kind) 241 struct_type = JavaScriptType(field.kind)
234 return "%s, %s" % (struct_type, nullable) 242 return "%s, %s" % (struct_type, nullable)
235 243
236 def JavaScriptValidateUnionParams(field): 244 def JavaScriptValidateUnionParams(field):
237 nullable = JavaScriptNullableParam(field) 245 nullable = JavaScriptNullableParam(field)
238 union_type = JavaScriptType(field.kind) 246 union_type = JavaScriptType(field.kind)
239 return "%s, %s" % (union_type, nullable) 247 return "%s, %s" % (union_type, nullable)
240 248
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 317
310 return token 318 return token
311 319
312 320
313 def ExpressionToText(value): 321 def ExpressionToText(value):
314 return TranslateConstants(value) 322 return TranslateConstants(value)
315 323
316 def IsArrayPointerField(field): 324 def IsArrayPointerField(field):
317 return mojom.IsArrayKind(field.kind) 325 return mojom.IsArrayKind(field.kind)
318 326
327 def IsEnumField(field):
328 return mojom.IsEnumKind(field.kind)
329
319 def IsStringPointerField(field): 330 def IsStringPointerField(field):
320 return mojom.IsStringKind(field.kind) 331 return mojom.IsStringKind(field.kind)
321 332
322 def IsStructPointerField(field): 333 def IsStructPointerField(field):
323 return mojom.IsStructKind(field.kind) 334 return mojom.IsStructKind(field.kind)
324 335
325 def IsMapPointerField(field): 336 def IsMapPointerField(field):
326 return mojom.IsMapKind(field.kind) 337 return mojom.IsMapKind(field.kind)
327 338
328 def IsHandleField(field): 339 def IsHandleField(field):
(...skipping 19 matching lines...) Expand all
348 "payload_size": JavaScriptPayloadSize, 359 "payload_size": JavaScriptPayloadSize,
349 "decode_snippet": JavaScriptDecodeSnippet, 360 "decode_snippet": JavaScriptDecodeSnippet,
350 "encode_snippet": JavaScriptEncodeSnippet, 361 "encode_snippet": JavaScriptEncodeSnippet,
351 "union_decode_snippet": JavaScriptUnionDecodeSnippet, 362 "union_decode_snippet": JavaScriptUnionDecodeSnippet,
352 "union_encode_snippet": JavaScriptUnionEncodeSnippet, 363 "union_encode_snippet": JavaScriptUnionEncodeSnippet,
353 "expression_to_text": ExpressionToText, 364 "expression_to_text": ExpressionToText,
354 "field_offset": JavaScriptFieldOffset, 365 "field_offset": JavaScriptFieldOffset,
355 "has_callbacks": mojom.HasCallbacks, 366 "has_callbacks": mojom.HasCallbacks,
356 "is_array_pointer_field": IsArrayPointerField, 367 "is_array_pointer_field": IsArrayPointerField,
357 "is_bool_field": IsBoolField, 368 "is_bool_field": IsBoolField,
369 "is_enum_field": IsEnumField,
358 "is_map_pointer_field": IsMapPointerField, 370 "is_map_pointer_field": IsMapPointerField,
359 "is_struct_pointer_field": IsStructPointerField, 371 "is_struct_pointer_field": IsStructPointerField,
360 "is_string_pointer_field": IsStringPointerField, 372 "is_string_pointer_field": IsStringPointerField,
361 "is_union_field": IsUnionField, 373 "is_union_field": IsUnionField,
362 "is_handle_field": IsHandleField, 374 "is_handle_field": IsHandleField,
363 "is_interface_field": IsInterfaceField, 375 "is_interface_field": IsInterfaceField,
364 "is_interface_request_field": IsInterfaceRequestField, 376 "is_interface_request_field": IsInterfaceRequestField,
365 "js_type": JavaScriptType, 377 "js_type": JavaScriptType,
366 "js_proxy_method_parameter_value": JavaScriptProxyMethodParameterValue, 378 "js_proxy_method_parameter_value": JavaScriptProxyMethodParameterValue,
367 "js_stub_method_parameter_value": JavaScriptStubMethodParameterValue, 379 "js_stub_method_parameter_value": JavaScriptStubMethodParameterValue,
368 "stylize_method": generator.StudlyCapsToCamel, 380 "stylize_method": generator.StudlyCapsToCamel,
369 "validate_array_params": JavaScriptValidateArrayParams, 381 "validate_array_params": JavaScriptValidateArrayParams,
382 "validate_enum_params": JavaScriptValidateEnumParams,
370 "validate_handle_params": JavaScriptValidateHandleParams, 383 "validate_handle_params": JavaScriptValidateHandleParams,
371 "validate_interface_params": JavaScriptValidateInterfaceParams, 384 "validate_interface_params": JavaScriptValidateInterfaceParams,
372 "validate_map_params": JavaScriptValidateMapParams, 385 "validate_map_params": JavaScriptValidateMapParams,
373 "validate_string_params": JavaScriptValidateStringParams, 386 "validate_string_params": JavaScriptValidateStringParams,
374 "validate_struct_params": JavaScriptValidateStructParams, 387 "validate_struct_params": JavaScriptValidateStructParams,
375 "validate_union_params": JavaScriptValidateUnionParams, 388 "validate_union_params": JavaScriptValidateUnionParams,
376 } 389 }
377 390
378 def GetParameters(self): 391 def GetParameters(self):
379 return { 392 return {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 return self.module.imports 439 return self.module.imports
427 440
428 def GetImportedInterfaces(self): 441 def GetImportedInterfaces(self):
429 interface_to_import = {}; 442 interface_to_import = {};
430 for each_import in self.module.imports: 443 for each_import in self.module.imports:
431 for each_interface in each_import["module"].interfaces: 444 for each_interface in each_import["module"].interfaces:
432 name = each_interface.name 445 name = each_interface.name
433 interface_to_import[name] = each_import["unique_name"] + "." + name 446 interface_to_import[name] = each_import["unique_name"] + "." + name
434 return interface_to_import; 447 return interface_to_import;
435 448
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698