| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 if mojom.IsStructKind(field.kind): | 47 if mojom.IsStructKind(field.kind): |
| 48 assert field.default == "default" | 48 assert field.default == "default" |
| 49 return "new %s()" % JavaScriptType(field.kind) | 49 return "new %s()" % JavaScriptType(field.kind) |
| 50 return ExpressionToText(field.default) | 50 return ExpressionToText(field.default) |
| 51 if field.kind in mojom.PRIMITIVES: | 51 if field.kind in mojom.PRIMITIVES: |
| 52 return _kind_to_javascript_default_value[field.kind] | 52 return _kind_to_javascript_default_value[field.kind] |
| 53 if mojom.IsStructKind(field.kind): | 53 if mojom.IsStructKind(field.kind): |
| 54 return "null" | 54 return "null" |
| 55 if mojom.IsAnyArrayKind(field.kind): | 55 if mojom.IsAnyArrayKind(field.kind): |
| 56 return "null" | 56 return "null" |
| 57 if mojom.IsMapKind(field.kind): |
| 58 return "null" |
| 57 if mojom.IsInterfaceKind(field.kind) or \ | 59 if mojom.IsInterfaceKind(field.kind) or \ |
| 58 mojom.IsInterfaceRequestKind(field.kind): | 60 mojom.IsInterfaceRequestKind(field.kind): |
| 59 return _kind_to_javascript_default_value[mojom.MSGPIPE] | 61 return _kind_to_javascript_default_value[mojom.MSGPIPE] |
| 60 if mojom.IsEnumKind(field.kind): | 62 if mojom.IsEnumKind(field.kind): |
| 61 return "0" | 63 return "0" |
| 62 | 64 |
| 63 | 65 |
| 64 def JavaScriptPayloadSize(packed): | 66 def JavaScriptPayloadSize(packed): |
| 65 packed_fields = packed.packed_fields | 67 packed_fields = packed.packed_fields |
| 66 if not packed_fields: | 68 if not packed_fields: |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" | 111 array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" |
| 110 element_type = "codec.PackedBool" if mojom.IsBoolKind(kind.kind) \ | 112 element_type = "codec.PackedBool" if mojom.IsBoolKind(kind.kind) \ |
| 111 else CodecType(kind.kind) | 113 else CodecType(kind.kind) |
| 112 return "new codec.%s(%s)" % (array_type, element_type) | 114 return "new codec.%s(%s)" % (array_type, element_type) |
| 113 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 115 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
| 114 return CodecType(mojom.MSGPIPE) | 116 return CodecType(mojom.MSGPIPE) |
| 115 if mojom.IsEnumKind(kind): | 117 if mojom.IsEnumKind(kind): |
| 116 return _kind_to_codec_type[mojom.INT32] | 118 return _kind_to_codec_type[mojom.INT32] |
| 117 return kind | 119 return kind |
| 118 | 120 |
| 121 def MapCodecType(kind): |
| 122 return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) |
| 119 | 123 |
| 120 def JavaScriptDecodeSnippet(kind): | 124 def JavaScriptDecodeSnippet(kind): |
| 121 if kind in mojom.PRIMITIVES: | 125 if kind in mojom.PRIMITIVES: |
| 122 return "decodeStruct(%s)" % CodecType(kind) | 126 return "decodeStruct(%s)" % CodecType(kind) |
| 123 if mojom.IsStructKind(kind): | 127 if mojom.IsStructKind(kind): |
| 124 return "decodeStructPointer(%s)" % JavaScriptType(kind) | 128 return "decodeStructPointer(%s)" % JavaScriptType(kind) |
| 129 if mojom.IsMapKind(kind): |
| 130 return "decodeMapPointer(%s, %s)" % \ |
| 131 (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) |
| 125 if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 132 if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
| 126 return "decodeArrayPointer(codec.PackedBool)" | 133 return "decodeArrayPointer(codec.PackedBool)" |
| 127 if mojom.IsAnyArrayKind(kind): | 134 if mojom.IsAnyArrayKind(kind): |
| 128 return "decodeArrayPointer(%s)" % CodecType(kind.kind) | 135 return "decodeArrayPointer(%s)" % CodecType(kind.kind) |
| 129 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 136 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
| 130 return JavaScriptDecodeSnippet(mojom.MSGPIPE) | 137 return JavaScriptDecodeSnippet(mojom.MSGPIPE) |
| 131 if mojom.IsEnumKind(kind): | 138 if mojom.IsEnumKind(kind): |
| 132 return JavaScriptDecodeSnippet(mojom.INT32) | 139 return JavaScriptDecodeSnippet(mojom.INT32) |
| 133 | 140 |
| 134 | 141 |
| 135 def JavaScriptEncodeSnippet(kind): | 142 def JavaScriptEncodeSnippet(kind): |
| 136 if kind in mojom.PRIMITIVES: | 143 if kind in mojom.PRIMITIVES: |
| 137 return "encodeStruct(%s, " % CodecType(kind) | 144 return "encodeStruct(%s, " % CodecType(kind) |
| 138 if mojom.IsStructKind(kind): | 145 if mojom.IsStructKind(kind): |
| 139 return "encodeStructPointer(%s, " % JavaScriptType(kind) | 146 return "encodeStructPointer(%s, " % JavaScriptType(kind) |
| 147 if mojom.IsMapKind(kind): |
| 148 return "encodeMapPointer(%s, %s, " % \ |
| 149 (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) |
| 140 if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 150 if mojom.IsAnyArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
| 141 return "encodeArrayPointer(codec.PackedBool, "; | 151 return "encodeArrayPointer(codec.PackedBool, "; |
| 142 if mojom.IsAnyArrayKind(kind): | 152 if mojom.IsAnyArrayKind(kind): |
| 143 return "encodeArrayPointer(%s, " % CodecType(kind.kind) | 153 return "encodeArrayPointer(%s, " % CodecType(kind.kind) |
| 144 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 154 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
| 145 return JavaScriptEncodeSnippet(mojom.MSGPIPE) | 155 return JavaScriptEncodeSnippet(mojom.MSGPIPE) |
| 146 if mojom.IsEnumKind(kind): | 156 if mojom.IsEnumKind(kind): |
| 147 return JavaScriptEncodeSnippet(mojom.INT32) | 157 return JavaScriptEncodeSnippet(mojom.INT32) |
| 148 | 158 |
| 149 | 159 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 return self.module.imports | 303 return self.module.imports |
| 294 | 304 |
| 295 def GetImportedInterfaces(self): | 305 def GetImportedInterfaces(self): |
| 296 interface_to_import = {}; | 306 interface_to_import = {}; |
| 297 for each_import in self.module.imports: | 307 for each_import in self.module.imports: |
| 298 for each_interface in each_import["module"].interfaces: | 308 for each_interface in each_import["module"].interfaces: |
| 299 name = each_interface.name | 309 name = each_interface.name |
| 300 interface_to_import[name] = each_import["unique_name"] + "." + name | 310 interface_to_import[name] = each_import["unique_name"] + "." + name |
| 301 return interface_to_import; | 311 return interface_to_import; |
| 302 | 312 |
| OLD | NEW |