| 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 18 matching lines...) Expand all Loading... |
| 29 mojom.STRING: '""', | 29 mojom.STRING: '""', |
| 30 } | 30 } |
| 31 | 31 |
| 32 | 32 |
| 33 def JavaScriptDefaultValue(field): | 33 def JavaScriptDefaultValue(field): |
| 34 if field.default: | 34 if field.default: |
| 35 raise Exception("Default values should've been handled in jinja.") | 35 raise Exception("Default values should've been handled in jinja.") |
| 36 if field.kind in mojom.PRIMITIVES: | 36 if field.kind in mojom.PRIMITIVES: |
| 37 return _kind_to_javascript_default_value[field.kind] | 37 return _kind_to_javascript_default_value[field.kind] |
| 38 if isinstance(field.kind, mojom.Struct): | 38 if isinstance(field.kind, mojom.Struct): |
| 39 return "null"; | 39 return "null" |
| 40 if isinstance(field.kind, mojom.Array): | 40 if isinstance(field.kind, mojom.Array): |
| 41 return "[]"; | 41 return "[]" |
| 42 if isinstance(field.kind, mojom.Interface): | 42 if isinstance(field.kind, mojom.Interface): |
| 43 return _kind_to_javascript_default_value[mojom.MSGPIPE] | 43 return _kind_to_javascript_default_value[mojom.MSGPIPE] |
| 44 if isinstance(field.kind, mojom.Enum): | 44 if isinstance(field.kind, mojom.Enum): |
| 45 return "0" | 45 return "0" |
| 46 | 46 |
| 47 | 47 |
| 48 def JavaScriptPayloadSize(packed): | 48 def JavaScriptPayloadSize(packed): |
| 49 packed_fields = packed.packed_fields | 49 packed_fields = packed.packed_fields |
| 50 if not packed_fields: | 50 if not packed_fields: |
| 51 return 0; | 51 return 0 |
| 52 last_field = packed_fields[-1] | 52 last_field = packed_fields[-1] |
| 53 offset = last_field.offset + last_field.size | 53 offset = last_field.offset + last_field.size |
| 54 pad = pack.GetPad(offset, 8) | 54 pad = pack.GetPad(offset, 8) |
| 55 return offset + pad; | 55 return offset + pad |
| 56 | 56 |
| 57 | 57 |
| 58 _kind_to_codec_type = { | 58 _kind_to_codec_type = { |
| 59 mojom.BOOL: "codec.Uint8", | 59 mojom.BOOL: "codec.Uint8", |
| 60 mojom.INT8: "codec.Int8", | 60 mojom.INT8: "codec.Int8", |
| 61 mojom.UINT8: "codec.Uint8", | 61 mojom.UINT8: "codec.Uint8", |
| 62 mojom.INT16: "codec.Int16", | 62 mojom.INT16: "codec.Int16", |
| 63 mojom.UINT16: "codec.Uint16", | 63 mojom.UINT16: "codec.Uint16", |
| 64 mojom.INT32: "codec.Int32", | 64 mojom.INT32: "codec.Int32", |
| 65 mojom.UINT32: "codec.Uint32", | 65 mojom.UINT32: "codec.Uint32", |
| (...skipping 19 matching lines...) Expand all Loading... |
| 85 return "new codec.ArrayOf(%s)" % CodecType(kind.kind) | 85 return "new codec.ArrayOf(%s)" % CodecType(kind.kind) |
| 86 if isinstance(kind, mojom.Interface): | 86 if isinstance(kind, mojom.Interface): |
| 87 return CodecType(mojom.MSGPIPE) | 87 return CodecType(mojom.MSGPIPE) |
| 88 if isinstance(kind, mojom.Enum): | 88 if isinstance(kind, mojom.Enum): |
| 89 return _kind_to_codec_type[mojom.INT32] | 89 return _kind_to_codec_type[mojom.INT32] |
| 90 return kind | 90 return kind |
| 91 | 91 |
| 92 | 92 |
| 93 def JavaScriptDecodeSnippet(kind): | 93 def JavaScriptDecodeSnippet(kind): |
| 94 if kind in mojom.PRIMITIVES: | 94 if kind in mojom.PRIMITIVES: |
| 95 return "decodeStruct(%s)" % CodecType(kind); | 95 return "decodeStruct(%s)" % CodecType(kind) |
| 96 if isinstance(kind, mojom.Struct): | 96 if isinstance(kind, mojom.Struct): |
| 97 return "decodeStructPointer(%s)" % CodecType(kind.name); | 97 return "decodeStructPointer(%s)" % CodecType(kind.name) |
| 98 if isinstance(kind, mojom.Array): | 98 if isinstance(kind, mojom.Array): |
| 99 return "decodeArrayPointer(%s)" % CodecType(kind.kind); | 99 return "decodeArrayPointer(%s)" % CodecType(kind.kind) |
| 100 if isinstance(kind, mojom.Interface): | 100 if isinstance(kind, mojom.Interface): |
| 101 return JavaScriptDecodeSnippet(mojom.MSGPIPE) | 101 return JavaScriptDecodeSnippet(mojom.MSGPIPE) |
| 102 if isinstance(kind, mojom.Enum): | 102 if isinstance(kind, mojom.Enum): |
| 103 return JavaScriptDecodeSnippet(mojom.INT32) | 103 return JavaScriptDecodeSnippet(mojom.INT32) |
| 104 | 104 |
| 105 | 105 |
| 106 def JavaScriptEncodeSnippet(kind): | 106 def JavaScriptEncodeSnippet(kind): |
| 107 if kind in mojom.PRIMITIVES: | 107 if kind in mojom.PRIMITIVES: |
| 108 return "encodeStruct(%s, " % CodecType(kind); | 108 return "encodeStruct(%s, " % CodecType(kind) |
| 109 if isinstance(kind, mojom.Struct): | 109 if isinstance(kind, mojom.Struct): |
| 110 return "encodeStructPointer(%s, " % CodecType(kind.name); | 110 return "encodeStructPointer(%s, " % CodecType(kind.name) |
| 111 if isinstance(kind, mojom.Array): | 111 if isinstance(kind, mojom.Array): |
| 112 return "encodeArrayPointer(%s, " % CodecType(kind.kind); | 112 return "encodeArrayPointer(%s, " % CodecType(kind.kind) |
| 113 if isinstance(kind, mojom.Interface): | 113 if isinstance(kind, mojom.Interface): |
| 114 return JavaScriptEncodeSnippet(mojom.MSGPIPE) | 114 return JavaScriptEncodeSnippet(mojom.MSGPIPE) |
| 115 if isinstance(kind, mojom.Enum): | 115 if isinstance(kind, mojom.Enum): |
| 116 return JavaScriptEncodeSnippet(mojom.INT32) | 116 return JavaScriptEncodeSnippet(mojom.INT32) |
| 117 | 117 |
| 118 |
| 118 def TranslateConstants(token, module): | 119 def TranslateConstants(token, module): |
| 119 if isinstance(token, mojom.Constant): | 120 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): |
| 120 # Enum constants are constructed like: | 121 # Both variable and enum constants are constructed like: |
| 121 # NamespaceUid.Struct_Enum.FIELD_NAME | 122 # NamespaceUid.Struct[.Enum].CONSTANT_NAME |
| 122 name = [] | 123 name = [] |
| 123 if token.imported_from: | 124 if token.imported_from: |
| 124 name.append(token.imported_from["unique_name"]) | 125 name.append(token.imported_from["unique_name"]) |
| 125 if token.parent_kind: | 126 if token.parent_kind: |
| 126 name.append(token.parent_kind.name + "_" + token.name[0]) | 127 name.append(token.parent_kind.name) |
| 127 else: | 128 if isinstance(token, mojom.EnumValue): |
| 128 name.append(token.name[0]) | 129 name.append(token.enum_name) |
| 129 name.append(token.name[1]) | 130 name.append(token.name) |
| 130 return ".".join(name) | 131 return ".".join(name) |
| 131 return token | 132 return token |
| 132 | 133 |
| 133 | 134 |
| 134 def ExpressionToText(value, module): | 135 def ExpressionToText(value, module): |
| 135 if value[0] != "EXPRESSION": | 136 if value[0] != "EXPRESSION": |
| 136 raise Exception("Expected EXPRESSION, got" + value) | 137 raise Exception("Expected EXPRESSION, got" + value) |
| 137 return "".join(generator.ExpressionMapper(value, | 138 return "".join(generator.ExpressionMapper(value, |
| 138 lambda token: TranslateConstants(token, module))) | 139 lambda token: TranslateConstants(token, module))) |
| 139 | 140 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) | 176 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) |
| 176 | 177 |
| 177 def GetImports(self): | 178 def GetImports(self): |
| 178 # Since each import is assigned a variable in JS, they need to have unique | 179 # Since each import is assigned a variable in JS, they need to have unique |
| 179 # names. | 180 # names. |
| 180 counter = 1 | 181 counter = 1 |
| 181 for each in self.module.imports: | 182 for each in self.module.imports: |
| 182 each["unique_name"] = "import" + str(counter) | 183 each["unique_name"] = "import" + str(counter) |
| 183 counter += 1 | 184 counter += 1 |
| 184 return self.module.imports | 185 return self.module.imports |
| OLD | NEW |