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 Dart 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 |
11 | 11 |
12 _kind_to_javascript_default_value = { | 12 _kind_to_dart_default_value = { |
13 mojom.BOOL: "false", | 13 mojom.BOOL: "false", |
14 mojom.INT8: "0", | 14 mojom.INT8: "0", |
15 mojom.UINT8: "0", | 15 mojom.UINT8: "0", |
16 mojom.INT16: "0", | 16 mojom.INT16: "0", |
17 mojom.UINT16: "0", | 17 mojom.UINT16: "0", |
18 mojom.INT32: "0", | 18 mojom.INT32: "0", |
19 mojom.UINT32: "0", | 19 mojom.UINT32: "0", |
20 mojom.FLOAT: "0", | 20 mojom.FLOAT: "0", |
21 mojom.HANDLE: "null", | 21 mojom.HANDLE: "0", |
22 mojom.DCPIPE: "null", | 22 mojom.DCPIPE: "0", |
23 mojom.DPPIPE: "null", | 23 mojom.DPPIPE: "0", |
24 mojom.MSGPIPE: "null", | 24 mojom.MSGPIPE: "0", |
25 mojom.SHAREDBUFFER: "null", | 25 mojom.SHAREDBUFFER: "0", |
26 mojom.NULLABLE_HANDLE: "null", | 26 mojom.NULLABLE_HANDLE: "0", |
27 mojom.NULLABLE_DCPIPE: "null", | 27 mojom.NULLABLE_DCPIPE: "0", |
28 mojom.NULLABLE_DPPIPE: "null", | 28 mojom.NULLABLE_DPPIPE: "0", |
29 mojom.NULLABLE_MSGPIPE: "null", | 29 mojom.NULLABLE_MSGPIPE: "0", |
30 mojom.NULLABLE_SHAREDBUFFER: "null", | 30 mojom.NULLABLE_SHAREDBUFFER: "0", |
31 mojom.INT64: "0", | 31 mojom.INT64: "0", |
32 mojom.UINT64: "0", | 32 mojom.UINT64: "0", |
33 mojom.DOUBLE: "0", | 33 mojom.DOUBLE: "0", |
34 mojom.STRING: "null", | 34 mojom.STRING: "null", |
35 mojom.NULLABLE_STRING: "null" | 35 mojom.NULLABLE_STRING: "null" |
36 } | 36 } |
37 | 37 |
38 | 38 |
39 def JavaScriptType(kind): | 39 _kind_to_dart_decl_type = { |
| 40 mojom.BOOL: "bool", |
| 41 mojom.INT8: "int", |
| 42 mojom.UINT8: "int", |
| 43 mojom.INT16: "int", |
| 44 mojom.UINT16: "int", |
| 45 mojom.INT32: "int", |
| 46 mojom.UINT32: "int", |
| 47 mojom.FLOAT: "double", |
| 48 mojom.HANDLE: "int", |
| 49 mojom.DCPIPE: "int", |
| 50 mojom.DPPIPE: "int", |
| 51 mojom.MSGPIPE: "int", |
| 52 mojom.SHAREDBUFFER: "int", |
| 53 mojom.NULLABLE_HANDLE: "int", |
| 54 mojom.NULLABLE_DCPIPE: "int", |
| 55 mojom.NULLABLE_DPPIPE: "int", |
| 56 mojom.NULLABLE_MSGPIPE: "int", |
| 57 mojom.NULLABLE_SHAREDBUFFER: "int", |
| 58 mojom.INT64: "int", |
| 59 mojom.UINT64: "int", |
| 60 mojom.DOUBLE: "double", |
| 61 mojom.STRING: "String", |
| 62 mojom.NULLABLE_STRING: "String" |
| 63 } |
| 64 |
| 65 |
| 66 def DartType(kind): |
40 if kind.imported_from: | 67 if kind.imported_from: |
41 return kind.imported_from["unique_name"] + "." + kind.name | 68 return kind.imported_from["unique_name"] + "." + kind.name |
42 return kind.name | 69 return kind.name |
43 | 70 |
44 | 71 |
45 def JavaScriptDefaultValue(field): | 72 def DartDefaultValue(field): |
46 if field.default: | 73 if field.default: |
47 if mojom.IsStructKind(field.kind): | 74 if mojom.IsStructKind(field.kind): |
48 assert field.default == "default" | 75 assert field.default == "default" |
49 return "new %s()" % JavaScriptType(field.kind) | 76 return "new %s()" % DartType(field.kind) |
50 return ExpressionToText(field.default) | 77 return ExpressionToText(field.default) |
51 if field.kind in mojom.PRIMITIVES: | 78 if field.kind in mojom.PRIMITIVES: |
52 return _kind_to_javascript_default_value[field.kind] | 79 return _kind_to_dart_default_value[field.kind] |
53 if mojom.IsStructKind(field.kind): | 80 if mojom.IsStructKind(field.kind): |
54 return "null" | 81 return "null" |
55 if mojom.IsArrayKind(field.kind): | 82 if mojom.IsArrayKind(field.kind): |
56 return "null" | 83 return "null" |
57 if mojom.IsMapKind(field.kind): | 84 if mojom.IsMapKind(field.kind): |
58 return "null" | 85 return "null" |
59 if mojom.IsInterfaceKind(field.kind) or \ | 86 if mojom.IsInterfaceKind(field.kind) or \ |
60 mojom.IsInterfaceRequestKind(field.kind): | 87 mojom.IsInterfaceRequestKind(field.kind): |
61 return _kind_to_javascript_default_value[mojom.MSGPIPE] | 88 return _kind_to_dart_default_value[mojom.MSGPIPE] |
62 if mojom.IsEnumKind(field.kind): | 89 if mojom.IsEnumKind(field.kind): |
63 return "0" | 90 return "0" |
64 | 91 |
65 | 92 |
66 def JavaScriptPayloadSize(packed): | 93 def DartDeclType(kind): |
| 94 if kind in mojom.PRIMITIVES: |
| 95 return _kind_to_dart_decl_type[kind] |
| 96 if mojom.IsStructKind(kind): |
| 97 return DartType(kind) |
| 98 if mojom.IsArrayKind(kind): |
| 99 array_type = DartDeclType(kind.kind) |
| 100 return "List<" + array_type + ">" |
| 101 if mojom.IsMapKind(kind): |
| 102 key_type = DartDeclType(kind.key_kind) |
| 103 value_type = DartDeclType(kind.value_kind) |
| 104 return "Map<"+ key_type + ", " + value_type + ">" |
| 105 if mojom.IsInterfaceKind(kind) or \ |
| 106 mojom.IsInterfaceRequestKind(kind): |
| 107 return _kind_to_dart_decl_type[mojom.MSGPIPE] |
| 108 if mojom.IsEnumKind(kind): |
| 109 return "int" |
| 110 |
| 111 def DartPayloadSize(packed): |
67 packed_fields = packed.packed_fields | 112 packed_fields = packed.packed_fields |
68 if not packed_fields: | 113 if not packed_fields: |
69 return 0 | 114 return 0 |
70 last_field = packed_fields[-1] | 115 last_field = packed_fields[-1] |
71 offset = last_field.offset + last_field.size | 116 offset = last_field.offset + last_field.size |
72 pad = pack.GetPad(offset, 8) | 117 pad = pack.GetPad(offset, 8) |
73 return offset + pad | 118 return offset + pad |
74 | 119 |
75 | 120 |
76 _kind_to_codec_type = { | 121 _kind_to_codec_type = { |
77 mojom.BOOL: "codec.Uint8", | 122 mojom.BOOL: "bindings.Uint8", |
78 mojom.INT8: "codec.Int8", | 123 mojom.INT8: "bindings.Int8", |
79 mojom.UINT8: "codec.Uint8", | 124 mojom.UINT8: "bindings.Uint8", |
80 mojom.INT16: "codec.Int16", | 125 mojom.INT16: "bindings.Int16", |
81 mojom.UINT16: "codec.Uint16", | 126 mojom.UINT16: "bindings.Uint16", |
82 mojom.INT32: "codec.Int32", | 127 mojom.INT32: "bindings.Int32", |
83 mojom.UINT32: "codec.Uint32", | 128 mojom.UINT32: "bindings.Uint32", |
84 mojom.FLOAT: "codec.Float", | 129 mojom.FLOAT: "bindings.Float", |
85 mojom.HANDLE: "codec.Handle", | 130 mojom.HANDLE: "bindings.Handle", |
86 mojom.DCPIPE: "codec.Handle", | 131 mojom.DCPIPE: "bindings.Handle", |
87 mojom.DPPIPE: "codec.Handle", | 132 mojom.DPPIPE: "bindings.Handle", |
88 mojom.MSGPIPE: "codec.Handle", | 133 mojom.MSGPIPE: "bindings.Handle", |
89 mojom.SHAREDBUFFER: "codec.Handle", | 134 mojom.SHAREDBUFFER: "bindings.Handle", |
90 mojom.NULLABLE_HANDLE: "codec.NullableHandle", | 135 mojom.NULLABLE_HANDLE: "bindings.NullableHandle", |
91 mojom.NULLABLE_DCPIPE: "codec.NullableHandle", | 136 mojom.NULLABLE_DCPIPE: "bindings.NullableHandle", |
92 mojom.NULLABLE_DPPIPE: "codec.NullableHandle", | 137 mojom.NULLABLE_DPPIPE: "bindings.NullableHandle", |
93 mojom.NULLABLE_MSGPIPE: "codec.NullableHandle", | 138 mojom.NULLABLE_MSGPIPE: "bindings.NullableHandle", |
94 mojom.NULLABLE_SHAREDBUFFER: "codec.NullableHandle", | 139 mojom.NULLABLE_SHAREDBUFFER: "bindings.NullableHandle", |
95 mojom.INT64: "codec.Int64", | 140 mojom.INT64: "bindings.Int64", |
96 mojom.UINT64: "codec.Uint64", | 141 mojom.UINT64: "bindings.Uint64", |
97 mojom.DOUBLE: "codec.Double", | 142 mojom.DOUBLE: "bindings.Double", |
98 mojom.STRING: "codec.String", | 143 mojom.STRING: "bindings.MojoString", |
99 mojom.NULLABLE_STRING: "codec.NullableString", | 144 mojom.NULLABLE_STRING: "bindings.NullableMojoString", |
100 } | 145 } |
101 | 146 |
102 | 147 |
103 def CodecType(kind): | 148 def CodecType(kind): |
104 if kind in mojom.PRIMITIVES: | 149 if kind in mojom.PRIMITIVES: |
105 return _kind_to_codec_type[kind] | 150 return _kind_to_codec_type[kind] |
106 if mojom.IsStructKind(kind): | 151 if mojom.IsStructKind(kind): |
107 pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \ | 152 pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \ |
108 else "PointerTo" | 153 else "PointerTo" |
109 return "new codec.%s(%s)" % (pointer_type, JavaScriptType(kind)) | 154 return "new bindings.%s(%s)" % (pointer_type, DartType(kind)) |
110 if mojom.IsArrayKind(kind): | 155 if mojom.IsArrayKind(kind): |
111 array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" | 156 array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" |
112 array_length = "" if kind.length is None else ", %d" % kind.length | 157 array_length = "" if kind.length is None else ", %d" % kind.length |
113 element_type = "codec.PackedBool" if mojom.IsBoolKind(kind.kind) \ | 158 element_type = "bindings.PackedBool" if mojom.IsBoolKind(kind.kind) \ |
114 else CodecType(kind.kind) | 159 else CodecType(kind.kind) |
115 return "new codec.%s(%s%s)" % (array_type, element_type, array_length) | 160 return "new bindings.%s(%s%s)" % (array_type, element_type, array_length) |
116 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 161 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
117 return CodecType(mojom.MSGPIPE) | 162 return CodecType(mojom.MSGPIPE) |
118 if mojom.IsEnumKind(kind): | 163 if mojom.IsEnumKind(kind): |
119 return _kind_to_codec_type[mojom.INT32] | 164 return _kind_to_codec_type[mojom.INT32] |
120 return kind | 165 return kind |
121 | 166 |
122 def MapCodecType(kind): | 167 def MapCodecType(kind): |
123 return "codec.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) | 168 return "bindings.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) |
124 | 169 |
125 def JavaScriptDecodeSnippet(kind): | 170 def DartDecodeSnippet(kind): |
126 if kind in mojom.PRIMITIVES: | 171 if kind in mojom.PRIMITIVES: |
127 return "decodeStruct(%s)" % CodecType(kind) | 172 return "decodeStruct(%s)" % CodecType(kind) |
128 if mojom.IsStructKind(kind): | 173 if mojom.IsStructKind(kind): |
129 return "decodeStructPointer(%s)" % JavaScriptType(kind) | 174 return "decodeStructPointer(%s)" % DartType(kind) |
130 if mojom.IsMapKind(kind): | 175 if mojom.IsMapKind(kind): |
131 return "decodeMapPointer(%s, %s)" % \ | 176 return "decodeMapPointer(%s, %s)" % \ |
132 (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) | 177 (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) |
133 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 178 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
134 return "decodeArrayPointer(codec.PackedBool)" | 179 return "decodeArrayPointer(bindings.PackedBool)" |
135 if mojom.IsArrayKind(kind): | 180 if mojom.IsArrayKind(kind): |
136 return "decodeArrayPointer(%s)" % CodecType(kind.kind) | 181 return "decodeArrayPointer(%s)" % CodecType(kind.kind) |
137 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 182 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
138 return JavaScriptDecodeSnippet(mojom.MSGPIPE) | 183 return DartDecodeSnippet(mojom.MSGPIPE) |
139 if mojom.IsEnumKind(kind): | 184 if mojom.IsEnumKind(kind): |
140 return JavaScriptDecodeSnippet(mojom.INT32) | 185 return DartDecodeSnippet(mojom.INT32) |
141 | 186 |
142 | 187 |
143 def JavaScriptEncodeSnippet(kind): | 188 def DartEncodeSnippet(kind): |
144 if kind in mojom.PRIMITIVES: | 189 if kind in mojom.PRIMITIVES: |
145 return "encodeStruct(%s, " % CodecType(kind) | 190 return "encodeStruct(%s, " % CodecType(kind) |
146 if mojom.IsStructKind(kind): | 191 if mojom.IsStructKind(kind): |
147 return "encodeStructPointer(%s, " % JavaScriptType(kind) | 192 return "encodeStructPointer(%s, " % DartType(kind) |
148 if mojom.IsMapKind(kind): | 193 if mojom.IsMapKind(kind): |
149 return "encodeMapPointer(%s, %s, " % \ | 194 return "encodeMapPointer(%s, %s, " % \ |
150 (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) | 195 (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) |
151 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 196 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
152 return "encodeArrayPointer(codec.PackedBool, "; | 197 return "encodeArrayPointer(bindings.PackedBool, "; |
153 if mojom.IsArrayKind(kind): | 198 if mojom.IsArrayKind(kind): |
154 return "encodeArrayPointer(%s, " % CodecType(kind.kind) | 199 return "encodeArrayPointer(%s, " % CodecType(kind.kind) |
155 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 200 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
156 return JavaScriptEncodeSnippet(mojom.MSGPIPE) | 201 return DartEncodeSnippet(mojom.MSGPIPE) |
157 if mojom.IsEnumKind(kind): | 202 if mojom.IsEnumKind(kind): |
158 return JavaScriptEncodeSnippet(mojom.INT32) | 203 return DartEncodeSnippet(mojom.INT32) |
159 | 204 |
160 | 205 |
161 def JavaScriptFieldOffset(packed_field): | 206 def DartFieldOffset(packed_field): |
162 return "offset + codec.kStructHeaderSize + %s" % packed_field.offset | 207 return "offset + bindings.kStructHeaderSize + %s" % packed_field.offset |
163 | 208 |
164 | 209 |
165 def JavaScriptNullableParam(packed_field): | 210 def DartNullableParam(packed_field): |
166 return "true" if mojom.IsNullableKind(packed_field.field.kind) else "false" | 211 return "true" if mojom.IsNullableKind(packed_field.field.kind) else "false" |
167 | 212 |
168 | 213 |
169 def GetArrayExpectedDimensionSizes(kind): | 214 def GetArrayExpectedDimensionSizes(kind): |
170 expected_dimension_sizes = [] | 215 expected_dimension_sizes = [] |
171 while mojom.IsArrayKind(kind): | 216 while mojom.IsArrayKind(kind): |
172 expected_dimension_sizes.append(generator.ExpectedArraySize(kind) or 0) | 217 expected_dimension_sizes.append(generator.ExpectedArraySize(kind) or 0) |
173 kind = kind.kind | 218 kind = kind.kind |
174 # Strings are serialized as variable-length arrays. | 219 # Strings are serialized as variable-length arrays. |
175 if (mojom.IsStringKind(kind)): | 220 if (mojom.IsStringKind(kind)): |
176 expected_dimension_sizes.append(0) | 221 expected_dimension_sizes.append(0) |
177 return expected_dimension_sizes | 222 return expected_dimension_sizes |
178 | 223 |
179 | 224 |
180 def JavaScriptValidateArrayParams(packed_field): | 225 def DartValidateArrayParams(packed_field): |
181 nullable = JavaScriptNullableParam(packed_field) | 226 nullable = DartNullableParam(packed_field) |
182 field_offset = JavaScriptFieldOffset(packed_field) | 227 field_offset = DartFieldOffset(packed_field) |
183 element_kind = packed_field.field.kind.kind | 228 element_kind = packed_field.field.kind.kind |
184 element_size = pack.PackedField.GetSizeForKind(element_kind) | 229 element_size = pack.PackedField.GetSizeForKind(element_kind) |
185 expected_dimension_sizes = GetArrayExpectedDimensionSizes( | 230 expected_dimension_sizes = GetArrayExpectedDimensionSizes( |
186 packed_field.field.kind) | 231 packed_field.field.kind) |
187 element_type = "codec.PackedBool" if mojom.IsBoolKind(element_kind) \ | 232 element_type = "bindings.PackedBool" if mojom.IsBoolKind(element_kind) \ |
188 else CodecType(element_kind) | 233 else CodecType(element_kind) |
189 return "%s, %s, %s, %s, %s, 0" % \ | 234 return "%s, %s, %s, %s, %s, 0" % \ |
190 (field_offset, element_size, element_type, nullable, | 235 (field_offset, element_size, element_type, nullable, |
191 expected_dimension_sizes) | 236 expected_dimension_sizes) |
192 | 237 |
193 | 238 |
194 def JavaScriptValidateStructParams(packed_field): | 239 def DartValidateStructParams(packed_field): |
195 nullable = JavaScriptNullableParam(packed_field) | 240 nullable = DartNullableParam(packed_field) |
196 field_offset = JavaScriptFieldOffset(packed_field) | 241 field_offset = DartFieldOffset(packed_field) |
197 struct_type = JavaScriptType(packed_field.field.kind) | 242 struct_type = DartType(packed_field.field.kind) |
198 return "%s, %s, %s" % (field_offset, struct_type, nullable) | 243 return "%s, %s, %s" % (field_offset, struct_type, nullable) |
199 | 244 |
200 | 245 |
201 def JavaScriptValidateMapParams(packed_field): | 246 def DartValidateMapParams(packed_field): |
202 nullable = JavaScriptNullableParam(packed_field) | 247 nullable = DartNullableParam(packed_field) |
203 field_offset = JavaScriptFieldOffset(packed_field) | 248 field_offset = DartFieldOffset(packed_field) |
204 keys_type = MapCodecType(packed_field.field.kind.key_kind) | 249 keys_type = MapCodecType(packed_field.field.kind.key_kind) |
205 values_kind = packed_field.field.kind.value_kind; | 250 values_kind = packed_field.field.kind.value_kind; |
206 values_type = MapCodecType(values_kind) | 251 values_type = MapCodecType(values_kind) |
207 values_nullable = "true" if mojom.IsNullableKind(values_kind) else "false" | 252 values_nullable = "true" if mojom.IsNullableKind(values_kind) else "false" |
208 return "%s, %s, %s, %s, %s" % \ | 253 return "%s, %s, %s, %s, %s" % \ |
209 (field_offset, nullable, keys_type, values_type, values_nullable) | 254 (field_offset, nullable, keys_type, values_type, values_nullable) |
210 | 255 |
211 | 256 |
212 def JavaScriptValidateStringParams(packed_field): | 257 def DartValidateStringParams(packed_field): |
213 nullable = JavaScriptNullableParam(packed_field) | 258 nullable = DartNullableParam(packed_field) |
214 return "%s, %s" % (JavaScriptFieldOffset(packed_field), nullable) | 259 return "%s, %s" % (DartFieldOffset(packed_field), nullable) |
215 | 260 |
216 | 261 |
217 def JavaScriptValidateHandleParams(packed_field): | 262 def DartValidateHandleParams(packed_field): |
218 nullable = JavaScriptNullableParam(packed_field) | 263 nullable = DartNullableParam(packed_field) |
219 field_offset = JavaScriptFieldOffset(packed_field) | 264 field_offset = DartFieldOffset(packed_field) |
220 return "%s, %s" % (field_offset, nullable) | 265 return "%s, %s" % (field_offset, nullable) |
221 | 266 |
222 | 267 |
223 def TranslateConstants(token): | 268 def TranslateConstants(token): |
224 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): | 269 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): |
225 # Both variable and enum constants are constructed like: | 270 # Both variable and enum constants are constructed like: |
226 # NamespaceUid.Struct[.Enum].CONSTANT_NAME | 271 # NamespaceUid.Struct[.Enum].CONSTANT_NAME |
227 name = [] | 272 name = "" |
228 if token.imported_from: | 273 if token.imported_from: |
229 name.append(token.imported_from["unique_name"]) | 274 name = token.imported_from["unique_name"] + "." |
230 if token.parent_kind: | 275 if token.parent_kind: |
231 name.append(token.parent_kind.name) | 276 name = name + token.parent_kind.name + "." |
232 if isinstance(token, mojom.EnumValue): | 277 if isinstance(token, mojom.EnumValue): |
233 name.append(token.enum.name) | 278 name = name + token.enum.name + "_" |
234 name.append(token.name) | 279 return name + token.name |
235 return ".".join(name) | |
236 | 280 |
237 if isinstance(token, mojom.BuiltinValue): | 281 if isinstance(token, mojom.BuiltinValue): |
238 if token.value == "double.INFINITY" or token.value == "float.INFINITY": | 282 if token.value == "double.INFINITY" or token.value == "float.INFINITY": |
239 return "Infinity"; | 283 return "double.INFINITY"; |
240 if token.value == "double.NEGATIVE_INFINITY" or \ | 284 if token.value == "double.NEGATIVE_INFINITY" or \ |
241 token.value == "float.NEGATIVE_INFINITY": | 285 token.value == "float.NEGATIVE_INFINITY": |
242 return "-Infinity"; | 286 return "double.NEGATIVE_INFINITY"; |
243 if token.value == "double.NAN" or token.value == "float.NAN": | 287 if token.value == "double.NAN" or token.value == "float.NAN": |
244 return "NaN"; | 288 return "double.NAN"; |
245 | 289 |
246 return token | 290 return token |
247 | 291 |
248 | 292 |
249 def ExpressionToText(value): | 293 def ExpressionToText(value): |
250 return TranslateConstants(value) | 294 return TranslateConstants(value) |
251 | 295 |
252 | |
253 def IsArrayPointerField(field): | 296 def IsArrayPointerField(field): |
254 return mojom.IsArrayKind(field.kind) | 297 return mojom.IsArrayKind(field.kind) |
255 | 298 |
256 def IsStringPointerField(field): | 299 def IsStringPointerField(field): |
257 return mojom.IsStringKind(field.kind) | 300 return mojom.IsStringKind(field.kind) |
258 | 301 |
259 def IsStructPointerField(field): | 302 def IsStructPointerField(field): |
260 return mojom.IsStructKind(field.kind) | 303 return mojom.IsStructKind(field.kind) |
261 | 304 |
262 def IsMapPointerField(field): | 305 def IsMapPointerField(field): |
263 return mojom.IsMapKind(field.kind) | 306 return mojom.IsMapKind(field.kind) |
264 | 307 |
265 def IsHandleField(field): | 308 def IsHandleField(field): |
266 return mojom.IsAnyHandleKind(field.kind) | 309 return mojom.IsAnyHandleKind(field.kind) |
267 | 310 |
268 | 311 |
269 class Generator(generator.Generator): | 312 class Generator(generator.Generator): |
270 | 313 |
271 js_filters = { | 314 dart_filters = { |
272 "default_value": JavaScriptDefaultValue, | 315 "default_value": DartDefaultValue, |
273 "payload_size": JavaScriptPayloadSize, | 316 "payload_size": DartPayloadSize, |
274 "decode_snippet": JavaScriptDecodeSnippet, | 317 "decode_snippet": DartDecodeSnippet, |
275 "encode_snippet": JavaScriptEncodeSnippet, | 318 "encode_snippet": DartEncodeSnippet, |
276 "expression_to_text": ExpressionToText, | 319 "expression_to_text": ExpressionToText, |
277 "field_offset": JavaScriptFieldOffset, | 320 "field_offset": DartFieldOffset, |
278 "has_callbacks": mojom.HasCallbacks, | 321 "has_callbacks": mojom.HasCallbacks, |
279 "is_array_pointer_field": IsArrayPointerField, | 322 "is_array_pointer_field": IsArrayPointerField, |
280 "is_map_pointer_field": IsMapPointerField, | 323 "is_map_pointer_field": IsMapPointerField, |
281 "is_struct_pointer_field": IsStructPointerField, | 324 "is_struct_pointer_field": IsStructPointerField, |
282 "is_string_pointer_field": IsStringPointerField, | 325 "is_string_pointer_field": IsStringPointerField, |
283 "is_handle_field": IsHandleField, | 326 "is_handle_field": IsHandleField, |
284 "js_type": JavaScriptType, | 327 "dart_type": DartType, |
| 328 "dart_decl_type": DartDeclType, |
285 "stylize_method": generator.StudlyCapsToCamel, | 329 "stylize_method": generator.StudlyCapsToCamel, |
286 "validate_array_params": JavaScriptValidateArrayParams, | 330 "validate_array_params": DartValidateArrayParams, |
287 "validate_handle_params": JavaScriptValidateHandleParams, | 331 "validate_handle_params": DartValidateHandleParams, |
288 "validate_map_params": JavaScriptValidateMapParams, | 332 "validate_map_params": DartValidateMapParams, |
289 "validate_string_params": JavaScriptValidateStringParams, | 333 "validate_string_params": DartValidateStringParams, |
290 "validate_struct_params": JavaScriptValidateStructParams, | 334 "validate_struct_params": DartValidateStructParams, |
291 } | 335 } |
292 | 336 |
293 def GetParameters(self): | 337 def GetParameters(self): |
294 return { | 338 return { |
295 "namespace": self.module.namespace, | 339 "namespace": self.module.namespace, |
296 "imports": self.GetImports(), | 340 "imports": self.GetImports(), |
297 "kinds": self.module.kinds, | 341 "kinds": self.module.kinds, |
298 "enums": self.module.enums, | 342 "enums": self.module.enums, |
299 "module": self.module, | 343 "module": self.module, |
300 "structs": self.GetStructs() + self.GetStructsFromMethods(), | 344 "structs": self.GetStructs() + self.GetStructsFromMethods(), |
301 "interfaces": self.module.interfaces, | 345 "interfaces": self.module.interfaces, |
302 "imported_interfaces": self.GetImportedInterfaces(), | 346 "imported_interfaces": self.GetImportedInterfaces(), |
303 } | 347 } |
304 | 348 |
305 @UseJinja("js_templates/module.amd.tmpl", filters=js_filters) | 349 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) |
306 def GenerateAMDModule(self): | 350 def GenerateLibModule(self): |
307 return self.GetParameters() | |
308 | |
309 @UseJinja("js_templates/module.sky.tmpl", filters=js_filters) | |
310 def GenerateHTMLModule(self): | |
311 return self.GetParameters() | 351 return self.GetParameters() |
312 | 352 |
313 def GenerateFiles(self, args): | 353 def GenerateFiles(self, args): |
314 self.Write(self.GenerateAMDModule(), | 354 self.Write(self.GenerateLibModule(), |
315 self.MatchMojomFilePath("%s.js" % self.module.name)) | 355 self.MatchMojomFilePath("%s.dart" % self.module.name)) |
316 self.Write(self.GenerateHTMLModule(), | |
317 self.MatchMojomFilePath("%s.sky" % self.module.name)) | |
318 | 356 |
319 def GetImports(self): | 357 def GetImports(self): |
320 used_names = set() | 358 used_names = set() |
321 for each_import in self.module.imports: | 359 for each_import in self.module.imports: |
322 simple_name = each_import["module_name"].split(".")[0] | 360 simple_name = each_import["module_name"].split(".")[0] |
323 | 361 |
324 # Since each import is assigned a variable in JS, they need to have unique | 362 # Since each import is assigned a variable in JS, they need to have unique |
325 # names. | 363 # names. |
326 unique_name = simple_name | 364 unique_name = simple_name |
327 counter = 0 | 365 counter = 0 |
328 while unique_name in used_names: | 366 while unique_name in used_names: |
329 counter += 1 | 367 counter += 1 |
330 unique_name = simple_name + str(counter) | 368 unique_name = simple_name + str(counter) |
331 | 369 |
332 used_names.add(unique_name) | 370 used_names.add(unique_name) |
333 each_import["unique_name"] = unique_name | 371 each_import["unique_name"] = unique_name |
334 counter += 1 | 372 counter += 1 |
335 return self.module.imports | 373 return self.module.imports |
336 | 374 |
337 def GetImportedInterfaces(self): | 375 def GetImportedInterfaces(self): |
338 interface_to_import = {}; | 376 interface_to_import = {}; |
339 for each_import in self.module.imports: | 377 for each_import in self.module.imports: |
340 for each_interface in each_import["module"].interfaces: | 378 for each_interface in each_import["module"].interfaces: |
341 name = each_interface.name | 379 name = each_interface.name |
342 interface_to_import[name] = each_import["unique_name"] + "." + name | 380 interface_to_import[name] = each_import["unique_name"] + "." + name |
343 return interface_to_import; | 381 return interface_to_import; |
344 | 382 |
OLD | NEW |