Chromium Code Reviews| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 if field.kind in mojom.PRIMITIVES: | 55 if field.kind in mojom.PRIMITIVES: |
| 56 return _kind_to_javascript_default_value[field.kind] | 56 return _kind_to_javascript_default_value[field.kind] |
| 57 if mojom.IsStructKind(field.kind): | 57 if mojom.IsStructKind(field.kind): |
| 58 return "null" | 58 return "null" |
| 59 if mojom.IsUnionKind(field.kind): | 59 if mojom.IsUnionKind(field.kind): |
| 60 return "null" | 60 return "null" |
| 61 if mojom.IsArrayKind(field.kind): | 61 if mojom.IsArrayKind(field.kind): |
| 62 return "null" | 62 return "null" |
| 63 if mojom.IsMapKind(field.kind): | 63 if mojom.IsMapKind(field.kind): |
| 64 return "null" | 64 return "null" |
| 65 if mojom.IsInterfaceKind(field.kind) or \ | 65 if mojom.IsInterfaceKind(field.kind): |
| 66 mojom.IsInterfaceRequestKind(field.kind): | 66 return "new %sPtr()" % JavaScriptType(field.kind) |
|
yzshen1
2016/12/12 22:39:49
Because FooPtr and InterfaceRequest objects have t
Ken Rockot(use gerrit already)
2016/12/12 23:48:07
Makes sense
| |
| 67 return _kind_to_javascript_default_value[mojom.MSGPIPE] | 67 if mojom.IsInterfaceRequestKind(field.kind): |
| 68 return "new bindings.InterfaceRequest()" | |
| 68 if mojom.IsAssociatedKind(field.kind): | 69 if mojom.IsAssociatedKind(field.kind): |
| 69 return "null" | 70 return "null" |
| 70 if mojom.IsEnumKind(field.kind): | 71 if mojom.IsEnumKind(field.kind): |
| 71 return "0" | 72 return "0" |
| 72 raise Exception("No valid default: %s" % field) | 73 raise Exception("No valid default: %s" % field) |
| 73 | 74 |
| 74 | 75 |
| 75 def JavaScriptPayloadSize(packed): | 76 def JavaScriptPayloadSize(packed): |
| 76 packed_fields = packed.packed_fields | 77 packed_fields = packed.packed_fields |
| 77 if not packed_fields: | 78 if not packed_fields: |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 else "PointerTo" | 118 else "PointerTo" |
| 118 return "new codec.%s(%s)" % (pointer_type, JavaScriptType(kind)) | 119 return "new codec.%s(%s)" % (pointer_type, JavaScriptType(kind)) |
| 119 if mojom.IsUnionKind(kind): | 120 if mojom.IsUnionKind(kind): |
| 120 return JavaScriptType(kind) | 121 return JavaScriptType(kind) |
| 121 if mojom.IsArrayKind(kind): | 122 if mojom.IsArrayKind(kind): |
| 122 array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" | 123 array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" |
| 123 array_length = "" if kind.length is None else ", %d" % kind.length | 124 array_length = "" if kind.length is None else ", %d" % kind.length |
| 124 element_type = ElementCodecType(kind.kind) | 125 element_type = ElementCodecType(kind.kind) |
| 125 return "new codec.%s(%s%s)" % (array_type, element_type, array_length) | 126 return "new codec.%s(%s%s)" % (array_type, element_type, array_length) |
| 126 if mojom.IsInterfaceKind(kind): | 127 if mojom.IsInterfaceKind(kind): |
| 127 return "codec.%s" % ("NullableInterface" if mojom.IsNullableKind(kind) | 128 return "new codec.%s(%sPtr)" % ( |
| 128 else "Interface") | 129 "NullableInterface" if mojom.IsNullableKind(kind) else "Interface", |
| 130 JavaScriptType(kind)) | |
| 129 if mojom.IsInterfaceRequestKind(kind): | 131 if mojom.IsInterfaceRequestKind(kind): |
| 130 return CodecType(mojom.MSGPIPE) | 132 return "codec.%s" % ( |
| 133 "NullableInterfaceRequest" if mojom.IsNullableKind(kind) | |
| 134 else "InterfaceRequest") | |
| 131 if mojom.IsAssociatedInterfaceKind(kind): | 135 if mojom.IsAssociatedInterfaceKind(kind): |
| 132 return "codec.AssociatedInterfaceNotSupported" | 136 return "codec.AssociatedInterfaceNotSupported" |
| 133 if mojom.IsAssociatedInterfaceRequestKind(kind): | 137 if mojom.IsAssociatedInterfaceRequestKind(kind): |
| 134 return "codec.AssociatedInterfaceRequestNotSupported" | 138 return "codec.AssociatedInterfaceRequestNotSupported" |
| 135 if mojom.IsEnumKind(kind): | 139 if mojom.IsEnumKind(kind): |
| 136 return "new codec.Enum(%s)" % JavaScriptType(kind) | 140 return "new codec.Enum(%s)" % JavaScriptType(kind) |
| 137 if mojom.IsMapKind(kind): | 141 if mojom.IsMapKind(kind): |
| 138 map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf" | 142 map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf" |
| 139 key_type = ElementCodecType(kind.key_kind) | 143 key_type = ElementCodecType(kind.key_kind) |
| 140 value_type = ElementCodecType(kind.value_kind) | 144 value_type = ElementCodecType(kind.value_kind) |
| 141 return "new codec.%s(%s, %s)" % (map_type, key_type, value_type) | 145 return "new codec.%s(%s, %s)" % (map_type, key_type, value_type) |
| 142 raise Exception("No codec type for %s" % kind) | 146 raise Exception("No codec type for %s" % kind) |
| 143 | 147 |
| 144 | 148 |
| 145 def ElementCodecType(kind): | 149 def ElementCodecType(kind): |
| 146 return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) | 150 return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) |
| 147 | 151 |
| 148 | 152 |
| 149 def JavaScriptDecodeSnippet(kind): | 153 def JavaScriptDecodeSnippet(kind): |
| 150 if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or | 154 if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or |
| 151 mojom.IsInterfaceKind(kind) or mojom.IsAssociatedKind(kind)): | 155 mojom.IsAnyInterfaceKind(kind)): |
| 152 return "decodeStruct(%s)" % CodecType(kind) | 156 return "decodeStruct(%s)" % CodecType(kind) |
| 153 if mojom.IsStructKind(kind): | 157 if mojom.IsStructKind(kind): |
| 154 return "decodeStructPointer(%s)" % JavaScriptType(kind) | 158 return "decodeStructPointer(%s)" % JavaScriptType(kind) |
| 155 if mojom.IsMapKind(kind): | 159 if mojom.IsMapKind(kind): |
| 156 return "decodeMapPointer(%s, %s)" % \ | 160 return "decodeMapPointer(%s, %s)" % \ |
| 157 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) | 161 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) |
| 158 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 162 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
| 159 return "decodeArrayPointer(codec.PackedBool)" | 163 return "decodeArrayPointer(codec.PackedBool)" |
| 160 if mojom.IsArrayKind(kind): | 164 if mojom.IsArrayKind(kind): |
| 161 return "decodeArrayPointer(%s)" % CodecType(kind.kind) | 165 return "decodeArrayPointer(%s)" % CodecType(kind.kind) |
| 162 if mojom.IsUnionKind(kind): | 166 if mojom.IsUnionKind(kind): |
| 163 return "decodeUnion(%s)" % CodecType(kind) | 167 return "decodeUnion(%s)" % CodecType(kind) |
| 164 if mojom.IsInterfaceRequestKind(kind): | |
| 165 return JavaScriptDecodeSnippet(mojom.MSGPIPE) | |
| 166 if mojom.IsEnumKind(kind): | 168 if mojom.IsEnumKind(kind): |
| 167 return JavaScriptDecodeSnippet(mojom.INT32) | 169 return JavaScriptDecodeSnippet(mojom.INT32) |
| 168 raise Exception("No decode snippet for %s" % kind) | 170 raise Exception("No decode snippet for %s" % kind) |
| 169 | 171 |
| 170 | 172 |
| 171 def JavaScriptEncodeSnippet(kind): | 173 def JavaScriptEncodeSnippet(kind): |
| 172 if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or | 174 if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or |
| 173 mojom.IsInterfaceKind(kind) or mojom.IsAssociatedKind(kind)): | 175 mojom.IsAnyInterfaceKind(kind)): |
| 174 return "encodeStruct(%s, " % CodecType(kind) | 176 return "encodeStruct(%s, " % CodecType(kind) |
| 175 if mojom.IsUnionKind(kind): | 177 if mojom.IsUnionKind(kind): |
| 176 return "encodeStruct(%s, " % JavaScriptType(kind) | 178 return "encodeStruct(%s, " % JavaScriptType(kind) |
| 177 if mojom.IsStructKind(kind): | 179 if mojom.IsStructKind(kind): |
| 178 return "encodeStructPointer(%s, " % JavaScriptType(kind) | 180 return "encodeStructPointer(%s, " % JavaScriptType(kind) |
| 179 if mojom.IsMapKind(kind): | 181 if mojom.IsMapKind(kind): |
| 180 return "encodeMapPointer(%s, %s, " % \ | 182 return "encodeMapPointer(%s, %s, " % \ |
| 181 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) | 183 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) |
| 182 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 184 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
| 183 return "encodeArrayPointer(codec.PackedBool, "; | 185 return "encodeArrayPointer(codec.PackedBool, "; |
| 184 if mojom.IsArrayKind(kind): | 186 if mojom.IsArrayKind(kind): |
| 185 return "encodeArrayPointer(%s, " % CodecType(kind.kind) | 187 return "encodeArrayPointer(%s, " % CodecType(kind.kind) |
| 186 if mojom.IsInterfaceRequestKind(kind): | |
| 187 return JavaScriptEncodeSnippet(mojom.MSGPIPE) | |
| 188 if mojom.IsEnumKind(kind): | 188 if mojom.IsEnumKind(kind): |
| 189 return JavaScriptEncodeSnippet(mojom.INT32) | 189 return JavaScriptEncodeSnippet(mojom.INT32) |
| 190 raise Exception("No encode snippet for %s" % kind) | 190 raise Exception("No encode snippet for %s" % kind) |
| 191 | 191 |
| 192 | 192 |
| 193 def JavaScriptUnionDecodeSnippet(kind): | 193 def JavaScriptUnionDecodeSnippet(kind): |
| 194 if mojom.IsUnionKind(kind): | 194 if mojom.IsUnionKind(kind): |
| 195 return "decodeStructPointer(%s)" % JavaScriptType(kind) | 195 return "decodeStructPointer(%s)" % JavaScriptType(kind) |
| 196 return JavaScriptDecodeSnippet(kind) | 196 return JavaScriptDecodeSnippet(kind) |
| 197 | 197 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 def JavaScriptValidateMapParams(field): | 251 def JavaScriptValidateMapParams(field): |
| 252 nullable = JavaScriptNullableParam(field) | 252 nullable = JavaScriptNullableParam(field) |
| 253 keys_type = ElementCodecType(field.kind.key_kind) | 253 keys_type = ElementCodecType(field.kind.key_kind) |
| 254 values_kind = field.kind.value_kind; | 254 values_kind = field.kind.value_kind; |
| 255 values_type = ElementCodecType(values_kind) | 255 values_type = ElementCodecType(values_kind) |
| 256 values_nullable = "true" if mojom.IsNullableKind(values_kind) else "false" | 256 values_nullable = "true" if mojom.IsNullableKind(values_kind) else "false" |
| 257 return "%s, %s, %s, %s" % \ | 257 return "%s, %s, %s, %s" % \ |
| 258 (nullable, keys_type, values_type, values_nullable) | 258 (nullable, keys_type, values_type, values_nullable) |
| 259 | 259 |
| 260 | 260 |
| 261 def JavaScriptValidateStringParams(field): | |
| 262 nullable = JavaScriptNullableParam(field) | |
| 263 return "%s" % (nullable) | |
| 264 | |
| 265 | |
| 266 def JavaScriptValidateHandleParams(field): | |
| 267 nullable = JavaScriptNullableParam(field) | |
| 268 return "%s" % (nullable) | |
| 269 | |
| 270 def JavaScriptValidateInterfaceParams(field): | |
| 271 return JavaScriptValidateHandleParams(field) | |
| 272 | |
| 273 def JavaScriptProxyMethodParameterValue(parameter): | |
| 274 name = parameter.name; | |
| 275 if (mojom.IsInterfaceKind(parameter.kind)): | |
| 276 type = JavaScriptType(parameter.kind) | |
| 277 return "core.isHandle(%s) ? %s : connection.bindImpl" \ | |
| 278 "(%s, %s)" % (name, name, name, type) | |
| 279 if (mojom.IsInterfaceRequestKind(parameter.kind)): | |
| 280 type = JavaScriptType(parameter.kind.kind) | |
| 281 return "core.isHandle(%s) ? %s : connection.bindProxy" \ | |
| 282 "(%s, %s)" % (name, name, name, type) | |
| 283 return name; | |
| 284 | |
| 285 | |
| 286 def JavaScriptStubMethodParameterValue(parameter): | |
| 287 name = parameter.name; | |
| 288 if (mojom.IsInterfaceKind(parameter.kind)): | |
| 289 type = JavaScriptType(parameter.kind) | |
| 290 return "connection.bindHandleToProxy(%s, %s)" % (name, type) | |
| 291 if (mojom.IsInterfaceRequestKind(parameter.kind)): | |
| 292 type = JavaScriptType(parameter.kind.kind) | |
| 293 return "connection.bindHandleToStub(%s, %s)" % (name, type) | |
| 294 return name; | |
| 295 | |
| 296 | |
| 297 def TranslateConstants(token): | 261 def TranslateConstants(token): |
| 298 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): | 262 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): |
| 299 # Both variable and enum constants are constructed like: | 263 # Both variable and enum constants are constructed like: |
| 300 # NamespaceUid.Struct[.Enum].CONSTANT_NAME | 264 # NamespaceUid.Struct[.Enum].CONSTANT_NAME |
| 301 name = [] | 265 name = [] |
| 302 if token.imported_from: | 266 if token.imported_from: |
| 303 name.append(token.imported_from["unique_name"]) | 267 name.append(token.imported_from["unique_name"]) |
| 304 if token.parent_kind: | 268 if token.parent_kind: |
| 305 name.append(token.parent_kind.name) | 269 name.append(token.parent_kind.name) |
| 306 if isinstance(token, mojom.EnumValue): | 270 if isinstance(token, mojom.EnumValue): |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 377 "is_bool_field": IsBoolField, | 341 "is_bool_field": IsBoolField, |
| 378 "is_enum_field": IsEnumField, | 342 "is_enum_field": IsEnumField, |
| 379 "is_handle_field": IsHandleField, | 343 "is_handle_field": IsHandleField, |
| 380 "is_interface_field": IsInterfaceField, | 344 "is_interface_field": IsInterfaceField, |
| 381 "is_interface_request_field": IsInterfaceRequestField, | 345 "is_interface_request_field": IsInterfaceRequestField, |
| 382 "is_map_pointer_field": IsMapPointerField, | 346 "is_map_pointer_field": IsMapPointerField, |
| 383 "is_object_field": IsObjectField, | 347 "is_object_field": IsObjectField, |
| 384 "is_string_pointer_field": IsStringPointerField, | 348 "is_string_pointer_field": IsStringPointerField, |
| 385 "is_struct_pointer_field": IsStructPointerField, | 349 "is_struct_pointer_field": IsStructPointerField, |
| 386 "is_union_field": IsUnionField, | 350 "is_union_field": IsUnionField, |
| 387 "js_proxy_method_parameter_value": JavaScriptProxyMethodParameterValue, | |
| 388 "js_stub_method_parameter_value": JavaScriptStubMethodParameterValue, | |
| 389 "js_type": JavaScriptType, | 351 "js_type": JavaScriptType, |
| 390 "payload_size": JavaScriptPayloadSize, | 352 "payload_size": JavaScriptPayloadSize, |
| 391 "stylize_method": generator.StudlyCapsToCamel, | 353 "stylize_method": generator.StudlyCapsToCamel, |
| 392 "union_decode_snippet": JavaScriptUnionDecodeSnippet, | 354 "union_decode_snippet": JavaScriptUnionDecodeSnippet, |
| 393 "union_encode_snippet": JavaScriptUnionEncodeSnippet, | 355 "union_encode_snippet": JavaScriptUnionEncodeSnippet, |
| 394 "validate_array_params": JavaScriptValidateArrayParams, | 356 "validate_array_params": JavaScriptValidateArrayParams, |
| 395 "validate_enum_params": JavaScriptValidateEnumParams, | 357 "validate_enum_params": JavaScriptValidateEnumParams, |
| 396 "validate_handle_params": JavaScriptValidateHandleParams, | |
| 397 "validate_interface_params": JavaScriptValidateInterfaceParams, | |
| 398 "validate_map_params": JavaScriptValidateMapParams, | 358 "validate_map_params": JavaScriptValidateMapParams, |
| 399 "validate_string_params": JavaScriptValidateStringParams, | 359 "validate_nullable_params": JavaScriptNullableParam, |
| 400 "validate_struct_params": JavaScriptValidateStructParams, | 360 "validate_struct_params": JavaScriptValidateStructParams, |
| 401 "validate_union_params": JavaScriptValidateUnionParams, | 361 "validate_union_params": JavaScriptValidateUnionParams, |
| 402 } | 362 } |
| 403 | 363 |
| 404 def GetParameters(self): | 364 def GetParameters(self): |
| 405 return { | 365 return { |
| 406 "namespace": self.module.namespace, | 366 "namespace": self.module.namespace, |
| 407 "imports": self.GetImports(), | 367 "imports": self.GetImports(), |
| 408 "kinds": self.module.kinds, | 368 "kinds": self.module.kinds, |
| 409 "enums": self.module.enums, | 369 "enums": self.module.enums, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 452 return self.module.imports | 412 return self.module.imports |
| 453 | 413 |
| 454 def GetImportedInterfaces(self): | 414 def GetImportedInterfaces(self): |
| 455 interface_to_import = {}; | 415 interface_to_import = {}; |
| 456 for each_import in self.module.imports: | 416 for each_import in self.module.imports: |
| 457 for each_interface in each_import["module"].interfaces: | 417 for each_interface in each_import["module"].interfaces: |
| 458 name = each_interface.name | 418 name = each_interface.name |
| 459 interface_to_import[name] = each_import["unique_name"] + "." + name | 419 interface_to_import[name] = each_import["unique_name"] + "." + name |
| 460 return interface_to_import; | 420 return interface_to_import; |
| 461 | 421 |
| OLD | NEW |