| OLD | NEW |
| 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 19 matching lines...) Expand all Loading... |
| 30 mojom.NULLABLE_SHAREDBUFFER: "null", | 30 mojom.NULLABLE_SHAREDBUFFER: "null", |
| 31 mojom.INT64: "0", | 31 mojom.INT64: "0", |
| 32 mojom.UINT64: "0", | 32 mojom.UINT64: "0", |
| 33 mojom.DOUBLE: "0", | 33 mojom.DOUBLE: "0", |
| 34 mojom.STRING: "null", | 34 mojom.STRING: "null", |
| 35 mojom.NULLABLE_STRING: "null" | 35 mojom.NULLABLE_STRING: "null" |
| 36 } | 36 } |
| 37 | 37 |
| 38 | 38 |
| 39 def JavaScriptType(kind): | 39 def JavaScriptType(kind): |
| 40 name = [] |
| 40 if kind.imported_from: | 41 if kind.imported_from: |
| 41 return kind.imported_from["unique_name"] + "." + kind.name | 42 name.append(kind.imported_from["unique_name"]) |
| 42 return kind.name | 43 if kind.parent_kind: |
| 44 name.append(kind.parent_kind.name) |
| 45 name.append(kind.name) |
| 46 return ".".join(name) |
| 43 | 47 |
| 44 | 48 |
| 45 def JavaScriptDefaultValue(field): | 49 def JavaScriptDefaultValue(field): |
| 46 if field.default: | 50 if field.default: |
| 47 if mojom.IsStructKind(field.kind): | 51 if mojom.IsStructKind(field.kind): |
| 48 assert field.default == "default" | 52 assert field.default == "default" |
| 49 return "new %s()" % JavaScriptType(field.kind) | 53 return "new %s()" % JavaScriptType(field.kind) |
| 50 return ExpressionToText(field.default) | 54 return ExpressionToText(field.default) |
| 51 if field.kind in mojom.PRIMITIVES: | 55 if field.kind in mojom.PRIMITIVES: |
| 52 return _kind_to_javascript_default_value[field.kind] | 56 return _kind_to_javascript_default_value[field.kind] |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 if mojom.IsInterfaceKind(kind): | 126 if mojom.IsInterfaceKind(kind): |
| 123 return "codec.%s" % ("NullableInterface" if mojom.IsNullableKind(kind) | 127 return "codec.%s" % ("NullableInterface" if mojom.IsNullableKind(kind) |
| 124 else "Interface") | 128 else "Interface") |
| 125 if mojom.IsInterfaceRequestKind(kind): | 129 if mojom.IsInterfaceRequestKind(kind): |
| 126 return CodecType(mojom.MSGPIPE) | 130 return CodecType(mojom.MSGPIPE) |
| 127 if mojom.IsAssociatedInterfaceKind(kind): | 131 if mojom.IsAssociatedInterfaceKind(kind): |
| 128 return "codec.AssociatedInterfaceNotSupported" | 132 return "codec.AssociatedInterfaceNotSupported" |
| 129 if mojom.IsAssociatedInterfaceRequestKind(kind): | 133 if mojom.IsAssociatedInterfaceRequestKind(kind): |
| 130 return "codec.AssociatedInterfaceRequestNotSupported" | 134 return "codec.AssociatedInterfaceRequestNotSupported" |
| 131 if mojom.IsEnumKind(kind): | 135 if mojom.IsEnumKind(kind): |
| 132 return _kind_to_codec_type[mojom.INT32] | 136 element_type = kind.name |
| 137 return "new codec.Enum(%s)" % (element_type) |
| 133 if mojom.IsMapKind(kind): | 138 if mojom.IsMapKind(kind): |
| 134 map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf" | 139 map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf" |
| 135 key_type = ElementCodecType(kind.key_kind) | 140 key_type = ElementCodecType(kind.key_kind) |
| 136 value_type = ElementCodecType(kind.value_kind) | 141 value_type = ElementCodecType(kind.value_kind) |
| 137 return "new codec.%s(%s, %s)" % (map_type, key_type, value_type) | 142 return "new codec.%s(%s, %s)" % (map_type, key_type, value_type) |
| 138 raise Exception("No codec type for %s" % kind) | 143 raise Exception("No codec type for %s" % kind) |
| 139 | 144 |
| 140 | 145 |
| 141 def ElementCodecType(kind): | 146 def ElementCodecType(kind): |
| 142 return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) | 147 return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) |
| 143 | 148 |
| 149 |
| 144 def JavaScriptDecodeSnippet(kind): | 150 def JavaScriptDecodeSnippet(kind): |
| 145 if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or | 151 if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or |
| 146 mojom.IsInterfaceKind(kind) or mojom.IsAssociatedKind(kind)): | 152 mojom.IsInterfaceKind(kind) or mojom.IsAssociatedKind(kind)): |
| 147 return "decodeStruct(%s)" % CodecType(kind) | 153 return "decodeStruct(%s)" % CodecType(kind) |
| 148 if mojom.IsStructKind(kind): | 154 if mojom.IsStructKind(kind): |
| 149 return "decodeStructPointer(%s)" % JavaScriptType(kind) | 155 return "decodeStructPointer(%s)" % JavaScriptType(kind) |
| 150 if mojom.IsMapKind(kind): | 156 if mojom.IsMapKind(kind): |
| 151 return "decodeMapPointer(%s, %s)" % \ | 157 return "decodeMapPointer(%s, %s)" % \ |
| 152 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) | 158 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) |
| 153 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 159 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 element_kind = field.kind.kind | 227 element_kind = field.kind.kind |
| 222 element_size = pack.PackedField.GetSizeForKind(element_kind) | 228 element_size = pack.PackedField.GetSizeForKind(element_kind) |
| 223 expected_dimension_sizes = GetArrayExpectedDimensionSizes( | 229 expected_dimension_sizes = GetArrayExpectedDimensionSizes( |
| 224 field.kind) | 230 field.kind) |
| 225 element_type = ElementCodecType(element_kind) | 231 element_type = ElementCodecType(element_kind) |
| 226 return "%s, %s, %s, %s, 0" % \ | 232 return "%s, %s, %s, %s, 0" % \ |
| 227 (element_size, element_type, nullable, | 233 (element_size, element_type, nullable, |
| 228 expected_dimension_sizes) | 234 expected_dimension_sizes) |
| 229 | 235 |
| 230 | 236 |
| 237 def JavaScriptValidateEnumParams(field): |
| 238 nullable = JavaScriptNullableParam(field) |
| 239 enum_type = JavaScriptType(field.kind) |
| 240 return "%s, %s" % (enum_type, nullable) |
| 241 |
| 231 def JavaScriptValidateStructParams(field): | 242 def JavaScriptValidateStructParams(field): |
| 232 nullable = JavaScriptNullableParam(field) | 243 nullable = JavaScriptNullableParam(field) |
| 233 struct_type = JavaScriptType(field.kind) | 244 struct_type = JavaScriptType(field.kind) |
| 234 return "%s, %s" % (struct_type, nullable) | 245 return "%s, %s" % (struct_type, nullable) |
| 235 | 246 |
| 236 def JavaScriptValidateUnionParams(field): | 247 def JavaScriptValidateUnionParams(field): |
| 237 nullable = JavaScriptNullableParam(field) | 248 nullable = JavaScriptNullableParam(field) |
| 238 union_type = JavaScriptType(field.kind) | 249 union_type = JavaScriptType(field.kind) |
| 239 return "%s, %s" % (union_type, nullable) | 250 return "%s, %s" % (union_type, nullable) |
| 240 | 251 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 | 320 |
| 310 return token | 321 return token |
| 311 | 322 |
| 312 | 323 |
| 313 def ExpressionToText(value): | 324 def ExpressionToText(value): |
| 314 return TranslateConstants(value) | 325 return TranslateConstants(value) |
| 315 | 326 |
| 316 def IsArrayPointerField(field): | 327 def IsArrayPointerField(field): |
| 317 return mojom.IsArrayKind(field.kind) | 328 return mojom.IsArrayKind(field.kind) |
| 318 | 329 |
| 330 def IsEnumField(field): |
| 331 return mojom.IsEnumKind(field.kind) |
| 332 |
| 319 def IsStringPointerField(field): | 333 def IsStringPointerField(field): |
| 320 return mojom.IsStringKind(field.kind) | 334 return mojom.IsStringKind(field.kind) |
| 321 | 335 |
| 322 def IsStructPointerField(field): | 336 def IsStructPointerField(field): |
| 323 return mojom.IsStructKind(field.kind) | 337 return mojom.IsStructKind(field.kind) |
| 324 | 338 |
| 325 def IsMapPointerField(field): | 339 def IsMapPointerField(field): |
| 326 return mojom.IsMapKind(field.kind) | 340 return mojom.IsMapKind(field.kind) |
| 327 | 341 |
| 328 def IsHandleField(field): | 342 def IsHandleField(field): |
| (...skipping 19 matching lines...) Expand all Loading... |
| 348 "payload_size": JavaScriptPayloadSize, | 362 "payload_size": JavaScriptPayloadSize, |
| 349 "decode_snippet": JavaScriptDecodeSnippet, | 363 "decode_snippet": JavaScriptDecodeSnippet, |
| 350 "encode_snippet": JavaScriptEncodeSnippet, | 364 "encode_snippet": JavaScriptEncodeSnippet, |
| 351 "union_decode_snippet": JavaScriptUnionDecodeSnippet, | 365 "union_decode_snippet": JavaScriptUnionDecodeSnippet, |
| 352 "union_encode_snippet": JavaScriptUnionEncodeSnippet, | 366 "union_encode_snippet": JavaScriptUnionEncodeSnippet, |
| 353 "expression_to_text": ExpressionToText, | 367 "expression_to_text": ExpressionToText, |
| 354 "field_offset": JavaScriptFieldOffset, | 368 "field_offset": JavaScriptFieldOffset, |
| 355 "has_callbacks": mojom.HasCallbacks, | 369 "has_callbacks": mojom.HasCallbacks, |
| 356 "is_array_pointer_field": IsArrayPointerField, | 370 "is_array_pointer_field": IsArrayPointerField, |
| 357 "is_bool_field": IsBoolField, | 371 "is_bool_field": IsBoolField, |
| 372 "is_enum_field": IsEnumField, |
| 358 "is_map_pointer_field": IsMapPointerField, | 373 "is_map_pointer_field": IsMapPointerField, |
| 359 "is_struct_pointer_field": IsStructPointerField, | 374 "is_struct_pointer_field": IsStructPointerField, |
| 360 "is_string_pointer_field": IsStringPointerField, | 375 "is_string_pointer_field": IsStringPointerField, |
| 361 "is_union_field": IsUnionField, | 376 "is_union_field": IsUnionField, |
| 362 "is_handle_field": IsHandleField, | 377 "is_handle_field": IsHandleField, |
| 363 "is_interface_field": IsInterfaceField, | 378 "is_interface_field": IsInterfaceField, |
| 364 "is_interface_request_field": IsInterfaceRequestField, | 379 "is_interface_request_field": IsInterfaceRequestField, |
| 365 "js_type": JavaScriptType, | 380 "js_type": JavaScriptType, |
| 366 "js_proxy_method_parameter_value": JavaScriptProxyMethodParameterValue, | 381 "js_proxy_method_parameter_value": JavaScriptProxyMethodParameterValue, |
| 367 "js_stub_method_parameter_value": JavaScriptStubMethodParameterValue, | 382 "js_stub_method_parameter_value": JavaScriptStubMethodParameterValue, |
| 368 "stylize_method": generator.StudlyCapsToCamel, | 383 "stylize_method": generator.StudlyCapsToCamel, |
| 369 "validate_array_params": JavaScriptValidateArrayParams, | 384 "validate_array_params": JavaScriptValidateArrayParams, |
| 385 "validate_enum_params": JavaScriptValidateEnumParams, |
| 370 "validate_handle_params": JavaScriptValidateHandleParams, | 386 "validate_handle_params": JavaScriptValidateHandleParams, |
| 371 "validate_interface_params": JavaScriptValidateInterfaceParams, | 387 "validate_interface_params": JavaScriptValidateInterfaceParams, |
| 372 "validate_map_params": JavaScriptValidateMapParams, | 388 "validate_map_params": JavaScriptValidateMapParams, |
| 373 "validate_string_params": JavaScriptValidateStringParams, | 389 "validate_string_params": JavaScriptValidateStringParams, |
| 374 "validate_struct_params": JavaScriptValidateStructParams, | 390 "validate_struct_params": JavaScriptValidateStructParams, |
| 375 "validate_union_params": JavaScriptValidateUnionParams, | 391 "validate_union_params": JavaScriptValidateUnionParams, |
| 376 } | 392 } |
| 377 | 393 |
| 378 def GetParameters(self): | 394 def GetParameters(self): |
| 379 return { | 395 return { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 return self.module.imports | 442 return self.module.imports |
| 427 | 443 |
| 428 def GetImportedInterfaces(self): | 444 def GetImportedInterfaces(self): |
| 429 interface_to_import = {}; | 445 interface_to_import = {}; |
| 430 for each_import in self.module.imports: | 446 for each_import in self.module.imports: |
| 431 for each_interface in each_import["module"].interfaces: | 447 for each_interface in each_import["module"].interfaces: |
| 432 name = each_interface.name | 448 name = each_interface.name |
| 433 interface_to_import[name] = each_import["unique_name"] + "." + name | 449 interface_to_import[name] = each_import["unique_name"] + "." + name |
| 434 return interface_to_import; | 450 return interface_to_import; |
| 435 | 451 |
| OLD | NEW |