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 from generate import mojom | 7 from generate import mojom |
8 from generate import mojom_pack | 8 from generate import mojom_pack |
9 from generate import mojom_generator | 9 from generate import mojom_generator |
10 | 10 |
11 from generate.template_expander import UseJinja | 11 from generate.template_expander import UseJinja |
12 | 12 |
13 _kind_to_javascript_default_value = { | 13 _kind_to_javascript_default_value = { |
14 mojom.BOOL: "false", | 14 mojom.BOOL: "false", |
15 mojom.INT8: "0", | 15 mojom.INT8: "0", |
16 mojom.UINT8: "0", | 16 mojom.UINT8: "0", |
17 mojom.INT16: "0", | 17 mojom.INT16: "0", |
18 mojom.UINT16: "0", | 18 mojom.UINT16: "0", |
19 mojom.INT32: "0", | 19 mojom.INT32: "0", |
20 mojom.UINT32: "0", | 20 mojom.UINT32: "0", |
21 mojom.FLOAT: "0", | 21 mojom.FLOAT: "0", |
22 mojom.HANDLE: "core.kInvalidHandle", | 22 mojom.HANDLE: "core.kInvalidHandle", |
23 mojom.DCPIPE: "core.kInvalidHandle", | 23 mojom.DCPIPE: "core.kInvalidHandle", |
24 mojom.DPPIPE: "core.kInvalidHandle", | 24 mojom.DPPIPE: "core.kInvalidHandle", |
25 mojom.MSGPIPE: "core.kInvalidHandle", | 25 mojom.MSGPIPE: "core.kInvalidHandle", |
26 mojom.INT64: "0", | 26 mojom.SHAREDBUFFER: "core.kInvalidHandle", |
27 mojom.UINT64: "0", | 27 mojom.INT64: "0", |
28 mojom.DOUBLE: "0", | 28 mojom.UINT64: "0", |
29 mojom.STRING: '""', | 29 mojom.DOUBLE: "0", |
| 30 mojom.STRING: '""', |
30 } | 31 } |
31 | 32 |
32 | 33 |
33 def JavaScriptDefaultValue(field): | 34 def JavaScriptDefaultValue(field): |
34 if field.default: | 35 if field.default: |
35 raise Exception("Default values should've been handled in jinja.") | 36 raise Exception("Default values should've been handled in jinja.") |
36 if field.kind in mojom.PRIMITIVES: | 37 if field.kind in mojom.PRIMITIVES: |
37 return _kind_to_javascript_default_value[field.kind] | 38 return _kind_to_javascript_default_value[field.kind] |
38 if isinstance(field.kind, mojom.Struct): | 39 if isinstance(field.kind, mojom.Struct): |
39 return "null"; | 40 return "null"; |
40 if isinstance(field.kind, mojom.Array): | 41 if isinstance(field.kind, mojom.Array): |
41 return "[]"; | 42 return "[]"; |
42 if isinstance(field.kind, mojom.Interface): | 43 if isinstance(field.kind, mojom.Interface): |
43 return _kind_to_javascript_default_value[mojom.MSGPIPE] | 44 return _kind_to_javascript_default_value[mojom.MSGPIPE] |
44 if isinstance(field.kind, mojom.Enum): | 45 if isinstance(field.kind, mojom.Enum): |
45 return "0" | 46 return "0" |
46 | 47 |
47 | 48 |
48 def JavaScriptPayloadSize(packed): | 49 def JavaScriptPayloadSize(packed): |
49 packed_fields = packed.packed_fields | 50 packed_fields = packed.packed_fields |
50 if not packed_fields: | 51 if not packed_fields: |
51 return 0; | 52 return 0; |
52 last_field = packed_fields[-1] | 53 last_field = packed_fields[-1] |
53 offset = last_field.offset + last_field.size | 54 offset = last_field.offset + last_field.size |
54 pad = mojom_pack.GetPad(offset, 8) | 55 pad = mojom_pack.GetPad(offset, 8) |
55 return offset + pad; | 56 return offset + pad; |
56 | 57 |
57 | 58 |
58 _kind_to_javascript_type = { | 59 _kind_to_javascript_type = { |
59 mojom.BOOL: "codec.Uint8", | 60 mojom.BOOL: "codec.Uint8", |
60 mojom.INT8: "codec.Int8", | 61 mojom.INT8: "codec.Int8", |
61 mojom.UINT8: "codec.Uint8", | 62 mojom.UINT8: "codec.Uint8", |
62 mojom.INT16: "codec.Int16", | 63 mojom.INT16: "codec.Int16", |
63 mojom.UINT16: "codec.Uint16", | 64 mojom.UINT16: "codec.Uint16", |
64 mojom.INT32: "codec.Int32", | 65 mojom.INT32: "codec.Int32", |
65 mojom.UINT32: "codec.Uint32", | 66 mojom.UINT32: "codec.Uint32", |
66 mojom.FLOAT: "codec.Float", | 67 mojom.FLOAT: "codec.Float", |
67 mojom.HANDLE: "codec.Handle", | 68 mojom.HANDLE: "codec.Handle", |
68 mojom.DCPIPE: "codec.Handle", | 69 mojom.DCPIPE: "codec.Handle", |
69 mojom.DPPIPE: "codec.Handle", | 70 mojom.DPPIPE: "codec.Handle", |
70 mojom.MSGPIPE: "codec.Handle", | 71 mojom.MSGPIPE: "codec.Handle", |
71 mojom.INT64: "codec.Int64", | 72 mojom.SHAREDBUFFER: "codec.Handle", |
72 mojom.UINT64: "codec.Uint64", | 73 mojom.INT64: "codec.Int64", |
73 mojom.DOUBLE: "codec.Double", | 74 mojom.UINT64: "codec.Uint64", |
74 mojom.STRING: "codec.String", | 75 mojom.DOUBLE: "codec.Double", |
| 76 mojom.STRING: "codec.String", |
75 } | 77 } |
76 | 78 |
77 | 79 |
78 def GetJavaScriptType(kind): | 80 def GetJavaScriptType(kind): |
79 if kind in mojom.PRIMITIVES: | 81 if kind in mojom.PRIMITIVES: |
80 return _kind_to_javascript_type[kind] | 82 return _kind_to_javascript_type[kind] |
81 if isinstance(kind, mojom.Struct): | 83 if isinstance(kind, mojom.Struct): |
82 return "new codec.PointerTo(%s)" % GetJavaScriptType(kind.name) | 84 return "new codec.PointerTo(%s)" % GetJavaScriptType(kind.name) |
83 if isinstance(kind, mojom.Array): | 85 if isinstance(kind, mojom.Array): |
84 return "new codec.ArrayOf(%s)" % GetJavaScriptType(kind.kind) | 86 return "new codec.ArrayOf(%s)" % GetJavaScriptType(kind.kind) |
85 if isinstance(kind, mojom.Interface): | 87 if isinstance(kind, mojom.Interface): |
86 return GetJavaScriptType(mojom.MSGPIPE) | 88 return GetJavaScriptType(mojom.MSGPIPE) |
87 if isinstance(kind, mojom.Enum): | 89 if isinstance(kind, mojom.Enum): |
88 return _kind_to_javascript_type[mojom.INT32] | 90 return _kind_to_javascript_type[mojom.INT32] |
89 return kind | 91 return kind |
90 | 92 |
91 | 93 |
92 _kind_to_javascript_decode_snippet = { | 94 _kind_to_javascript_decode_snippet = { |
93 mojom.BOOL: "read8() & 1", | 95 mojom.BOOL: "read8() & 1", |
94 mojom.INT8: "read8()", | 96 mojom.INT8: "read8()", |
95 mojom.UINT8: "read8()", | 97 mojom.UINT8: "read8()", |
96 mojom.INT16: "read16()", | 98 mojom.INT16: "read16()", |
97 mojom.UINT16: "read16()", | 99 mojom.UINT16: "read16()", |
98 mojom.INT32: "read32()", | 100 mojom.INT32: "read32()", |
99 mojom.UINT32: "read32()", | 101 mojom.UINT32: "read32()", |
100 mojom.FLOAT: "decodeFloat()", | 102 mojom.FLOAT: "decodeFloat()", |
101 mojom.HANDLE: "decodeHandle()", | 103 mojom.HANDLE: "decodeHandle()", |
102 mojom.DCPIPE: "decodeHandle()", | 104 mojom.DCPIPE: "decodeHandle()", |
103 mojom.DPPIPE: "decodeHandle()", | 105 mojom.DPPIPE: "decodeHandle()", |
104 mojom.MSGPIPE: "decodeHandle()", | 106 mojom.MSGPIPE: "decodeHandle()", |
105 mojom.INT64: "read64()", | 107 mojom.SHAREDBUFFER: "decodeHandle()", |
106 mojom.UINT64: "read64()", | 108 mojom.INT64: "read64()", |
107 mojom.DOUBLE: "decodeDouble()", | 109 mojom.UINT64: "read64()", |
108 mojom.STRING: "decodeStringPointer()", | 110 mojom.DOUBLE: "decodeDouble()", |
| 111 mojom.STRING: "decodeStringPointer()", |
109 } | 112 } |
110 | 113 |
111 | 114 |
112 def JavaScriptDecodeSnippet(kind): | 115 def JavaScriptDecodeSnippet(kind): |
113 if kind in mojom.PRIMITIVES: | 116 if kind in mojom.PRIMITIVES: |
114 return _kind_to_javascript_decode_snippet[kind] | 117 return _kind_to_javascript_decode_snippet[kind] |
115 if isinstance(kind, mojom.Struct): | 118 if isinstance(kind, mojom.Struct): |
116 return "decodeStructPointer(%s)" % GetJavaScriptType(kind.name); | 119 return "decodeStructPointer(%s)" % GetJavaScriptType(kind.name); |
117 if isinstance(kind, mojom.Array): | 120 if isinstance(kind, mojom.Array): |
118 return "decodeArrayPointer(%s)" % GetJavaScriptType(kind.kind); | 121 return "decodeArrayPointer(%s)" % GetJavaScriptType(kind.kind); |
119 if isinstance(kind, mojom.Interface): | 122 if isinstance(kind, mojom.Interface): |
120 return JavaScriptDecodeSnippet(mojom.MSGPIPE) | 123 return JavaScriptDecodeSnippet(mojom.MSGPIPE) |
121 if isinstance(kind, mojom.Enum): | 124 if isinstance(kind, mojom.Enum): |
122 return _kind_to_javascript_decode_snippet[mojom.INT32] | 125 return _kind_to_javascript_decode_snippet[mojom.INT32] |
123 | 126 |
124 | 127 |
125 _kind_to_javascript_encode_snippet = { | 128 _kind_to_javascript_encode_snippet = { |
126 mojom.BOOL: "write8(1 & ", | 129 mojom.BOOL: "write8(1 & ", |
127 mojom.INT8: "write8(", | 130 mojom.INT8: "write8(", |
128 mojom.UINT8: "write8(", | 131 mojom.UINT8: "write8(", |
129 mojom.INT16: "write16(", | 132 mojom.INT16: "write16(", |
130 mojom.UINT16: "write16(", | 133 mojom.UINT16: "write16(", |
131 mojom.INT32: "write32(", | 134 mojom.INT32: "write32(", |
132 mojom.UINT32: "write32(", | 135 mojom.UINT32: "write32(", |
133 mojom.FLOAT: "encodeFloat(", | 136 mojom.FLOAT: "encodeFloat(", |
134 mojom.HANDLE: "encodeHandle(", | 137 mojom.HANDLE: "encodeHandle(", |
135 mojom.DCPIPE: "encodeHandle(", | 138 mojom.DCPIPE: "encodeHandle(", |
136 mojom.DPPIPE: "encodeHandle(", | 139 mojom.DPPIPE: "encodeHandle(", |
137 mojom.MSGPIPE: "encodeHandle(", | 140 mojom.MSGPIPE: "encodeHandle(", |
138 mojom.INT64: "write64(", | 141 mojom.SHAREDBUFFER: "encodeHandle(", |
139 mojom.UINT64: "write64(", | 142 mojom.INT64: "write64(", |
140 mojom.DOUBLE: "encodeDouble(", | 143 mojom.UINT64: "write64(", |
141 mojom.STRING: "encodeStringPointer(", | 144 mojom.DOUBLE: "encodeDouble(", |
| 145 mojom.STRING: "encodeStringPointer(", |
142 } | 146 } |
143 | 147 |
144 | 148 |
145 def JavaScriptEncodeSnippet(kind): | 149 def JavaScriptEncodeSnippet(kind): |
146 if kind in mojom.PRIMITIVES: | 150 if kind in mojom.PRIMITIVES: |
147 return _kind_to_javascript_encode_snippet[kind] | 151 return _kind_to_javascript_encode_snippet[kind] |
148 if isinstance(kind, mojom.Struct): | 152 if isinstance(kind, mojom.Struct): |
149 return "encodeStructPointer(%s, " % GetJavaScriptType(kind.name); | 153 return "encodeStructPointer(%s, " % GetJavaScriptType(kind.name); |
150 if isinstance(kind, mojom.Array): | 154 if isinstance(kind, mojom.Array): |
151 return "encodeArrayPointer(%s, " % GetJavaScriptType(kind.kind); | 155 return "encodeArrayPointer(%s, " % GetJavaScriptType(kind.kind); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) | 218 self.Write(self.GenerateJsModule(), "%s.js" % self.module.name) |
215 | 219 |
216 def GetImports(self): | 220 def GetImports(self): |
217 # Since each import is assigned a variable in JS, they need to have unique | 221 # Since each import is assigned a variable in JS, they need to have unique |
218 # names. | 222 # names. |
219 counter = 1 | 223 counter = 1 |
220 for each in self.module.imports: | 224 for each in self.module.imports: |
221 each["unique_name"] = "import" + str(counter) | 225 each["unique_name"] = "import" + str(counter) |
222 counter += 1 | 226 counter += 1 |
223 return self.module.imports | 227 return self.module.imports |
OLD | NEW |