| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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_javascript_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", |
| 66 mojom.FLOAT: "codec.Float", | 66 mojom.FLOAT: "codec.Float", |
| 67 mojom.HANDLE: "codec.Handle", | 67 mojom.HANDLE: "codec.Handle", |
| 68 mojom.DCPIPE: "codec.Handle", | 68 mojom.DCPIPE: "codec.Handle", |
| 69 mojom.DPPIPE: "codec.Handle", | 69 mojom.DPPIPE: "codec.Handle", |
| 70 mojom.MSGPIPE: "codec.Handle", | 70 mojom.MSGPIPE: "codec.Handle", |
| 71 mojom.SHAREDBUFFER: "codec.Handle", | 71 mojom.SHAREDBUFFER: "codec.Handle", |
| 72 mojom.INT64: "codec.Int64", | 72 mojom.INT64: "codec.Int64", |
| 73 mojom.UINT64: "codec.Uint64", | 73 mojom.UINT64: "codec.Uint64", |
| 74 mojom.DOUBLE: "codec.Double", | 74 mojom.DOUBLE: "codec.Double", |
| 75 mojom.STRING: "codec.String", | 75 mojom.STRING: "codec.String", |
| 76 } | 76 } |
| 77 | 77 |
| 78 | 78 |
| 79 def GetJavaScriptType(kind): | 79 def CodecType(kind): |
| 80 if kind in mojom.PRIMITIVES: | 80 if kind in mojom.PRIMITIVES: |
| 81 return _kind_to_javascript_type[kind] | 81 return _kind_to_codec_type[kind] |
| 82 if isinstance(kind, mojom.Struct): | 82 if isinstance(kind, mojom.Struct): |
| 83 return "new codec.PointerTo(%s)" % GetJavaScriptType(kind.name) | 83 return "new codec.PointerTo(%s)" % CodecType(kind.name) |
| 84 if isinstance(kind, mojom.Array): | 84 if isinstance(kind, mojom.Array): |
| 85 return "new codec.ArrayOf(%s)" % GetJavaScriptType(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 GetJavaScriptType(mojom.MSGPIPE) | 87 return CodecType(mojom.MSGPIPE) |
| 88 if isinstance(kind, mojom.Enum): | 88 if isinstance(kind, mojom.Enum): |
| 89 return _kind_to_javascript_type[mojom.INT32] | 89 return _kind_to_codec_type[mojom.INT32] |
| 90 return kind | 90 return kind |
| 91 | 91 |
| 92 | 92 |
| 93 _kind_to_javascript_decode_snippet = { | |
| 94 mojom.BOOL: "readUint8() & 1", | |
| 95 mojom.INT8: "readInt8()", | |
| 96 mojom.UINT8: "readUint8()", | |
| 97 mojom.INT16: "readInt16()", | |
| 98 mojom.UINT16: "readUint16()", | |
| 99 mojom.INT32: "readInt32()", | |
| 100 mojom.UINT32: "readUint32()", | |
| 101 mojom.FLOAT: "readFloat()", | |
| 102 mojom.HANDLE: "decodeHandle()", | |
| 103 mojom.DCPIPE: "decodeHandle()", | |
| 104 mojom.DPPIPE: "decodeHandle()", | |
| 105 mojom.MSGPIPE: "decodeHandle()", | |
| 106 mojom.SHAREDBUFFER: "decodeHandle()", | |
| 107 mojom.INT64: "readInt64()", | |
| 108 mojom.UINT64: "readUint64()", | |
| 109 mojom.DOUBLE: "readDouble()", | |
| 110 mojom.STRING: "decodeStringPointer()", | |
| 111 } | |
| 112 | |
| 113 | |
| 114 def JavaScriptDecodeSnippet(kind): | 93 def JavaScriptDecodeSnippet(kind): |
| 115 if kind in mojom.PRIMITIVES: | 94 if kind in mojom.PRIMITIVES: |
| 116 return _kind_to_javascript_decode_snippet[kind] | 95 return "decodeStruct(%s)" % CodecType(kind); |
| 117 if isinstance(kind, mojom.Struct): | 96 if isinstance(kind, mojom.Struct): |
| 118 return "decodeStructPointer(%s)" % GetJavaScriptType(kind.name); | 97 return "decodeStructPointer(%s)" % CodecType(kind.name); |
| 119 if isinstance(kind, mojom.Array): | 98 if isinstance(kind, mojom.Array): |
| 120 return "decodeArrayPointer(%s)" % GetJavaScriptType(kind.kind); | 99 return "decodeArrayPointer(%s)" % CodecType(kind.kind); |
| 121 if isinstance(kind, mojom.Interface): | 100 if isinstance(kind, mojom.Interface): |
| 122 return JavaScriptDecodeSnippet(mojom.MSGPIPE) | 101 return JavaScriptDecodeSnippet(mojom.MSGPIPE) |
| 123 if isinstance(kind, mojom.Enum): | 102 if isinstance(kind, mojom.Enum): |
| 124 return _kind_to_javascript_decode_snippet[mojom.INT32] | 103 return JavaScriptDecodeSnippet(mojom.INT32) |
| 125 | |
| 126 | |
| 127 _kind_to_javascript_encode_snippet = { | |
| 128 mojom.BOOL: "writeUint8(1 & ", | |
| 129 mojom.INT8: "writeInt8(", | |
| 130 mojom.UINT8: "writeUint8(", | |
| 131 mojom.INT16: "writeInt16(", | |
| 132 mojom.UINT16: "writeUint16(", | |
| 133 mojom.INT32: "writeInt32(", | |
| 134 mojom.UINT32: "writeUint32(", | |
| 135 mojom.FLOAT: "writeFloat(", | |
| 136 mojom.HANDLE: "encodeHandle(", | |
| 137 mojom.DCPIPE: "encodeHandle(", | |
| 138 mojom.DPPIPE: "encodeHandle(", | |
| 139 mojom.MSGPIPE: "encodeHandle(", | |
| 140 mojom.SHAREDBUFFER: "encodeHandle(", | |
| 141 mojom.INT64: "writeInt64(", | |
| 142 mojom.UINT64: "writeUint64(", | |
| 143 mojom.DOUBLE: "writeDouble(", | |
| 144 mojom.STRING: "encodeStringPointer(", | |
| 145 } | |
| 146 | 104 |
| 147 | 105 |
| 148 def JavaScriptEncodeSnippet(kind): | 106 def JavaScriptEncodeSnippet(kind): |
| 149 if kind in mojom.PRIMITIVES: | 107 if kind in mojom.PRIMITIVES: |
| 150 return _kind_to_javascript_encode_snippet[kind] | 108 return "encodeStruct(%s, " % CodecType(kind); |
| 151 if isinstance(kind, mojom.Struct): | 109 if isinstance(kind, mojom.Struct): |
| 152 return "encodeStructPointer(%s, " % GetJavaScriptType(kind.name); | 110 return "encodeStructPointer(%s, " % CodecType(kind.name); |
| 153 if isinstance(kind, mojom.Array): | 111 if isinstance(kind, mojom.Array): |
| 154 return "encodeArrayPointer(%s, " % GetJavaScriptType(kind.kind); | 112 return "encodeArrayPointer(%s, " % CodecType(kind.kind); |
| 155 if isinstance(kind, mojom.Interface): | 113 if isinstance(kind, mojom.Interface): |
| 156 return JavaScriptEncodeSnippet(mojom.MSGPIPE) | 114 return JavaScriptEncodeSnippet(mojom.MSGPIPE) |
| 157 if isinstance(kind, mojom.Enum): | 115 if isinstance(kind, mojom.Enum): |
| 158 return _kind_to_javascript_encode_snippet[mojom.INT32] | 116 return JavaScriptEncodeSnippet(mojom.INT32) |
| 159 | 117 |
| 160 def TranslateConstants(token, module): | 118 def TranslateConstants(token, module): |
| 161 if isinstance(token, mojom.Constant): | 119 if isinstance(token, mojom.Constant): |
| 162 # Enum constants are constructed like: | 120 # Enum constants are constructed like: |
| 163 # NamespaceUid.Struct_Enum.FIELD_NAME | 121 # NamespaceUid.Struct_Enum.FIELD_NAME |
| 164 name = [] | 122 name = [] |
| 165 if token.imported_from: | 123 if token.imported_from: |
| 166 name.append(token.imported_from["unique_name"]) | 124 name.append(token.imported_from["unique_name"]) |
| 167 if token.parent_kind: | 125 if token.parent_kind: |
| 168 name.append(token.parent_kind.name + "_" + token.name[0]) | 126 name.append(token.parent_kind.name + "_" + token.name[0]) |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) | 175 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) |
| 218 | 176 |
| 219 def GetImports(self): | 177 def GetImports(self): |
| 220 # Since each import is assigned a variable in JS, they need to have unique | 178 # Since each import is assigned a variable in JS, they need to have unique |
| 221 # names. | 179 # names. |
| 222 counter = 1 | 180 counter = 1 |
| 223 for each in self.module.imports: | 181 for each in self.module.imports: |
| 224 each["unique_name"] = "import" + str(counter) | 182 each["unique_name"] = "import" + str(counter) |
| 225 counter += 1 | 183 counter += 1 |
| 226 return self.module.imports | 184 return self.module.imports |
| OLD | NEW |