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 Dart 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 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 def CodecType(kind): | 148 def CodecType(kind): |
149 if kind in mojom.PRIMITIVES: | 149 if kind in mojom.PRIMITIVES: |
150 return _kind_to_codec_type[kind] | 150 return _kind_to_codec_type[kind] |
151 if mojom.IsStructKind(kind): | 151 if mojom.IsStructKind(kind): |
152 pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \ | 152 pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \ |
153 else "PointerTo" | 153 else "PointerTo" |
154 return "new bindings.%s(%s)" % (pointer_type, DartType(kind)) | 154 return "new bindings.%s(%s)" % (pointer_type, DartType(kind)) |
155 if mojom.IsArrayKind(kind): | 155 if mojom.IsArrayKind(kind): |
156 array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" | 156 array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf" |
157 array_length = "" if kind.length is None else ", %d" % kind.length | 157 array_length = "" if kind.length is None else ", %d" % kind.length |
158 element_type = "bindings.PackedBool" if mojom.IsBoolKind(kind.kind) \ | 158 element_type = ElementCodecType(kind.kind) |
159 else CodecType(kind.kind) | |
160 return "new bindings.%s(%s%s)" % (array_type, element_type, array_length) | 159 return "new bindings.%s(%s%s)" % (array_type, element_type, array_length) |
161 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 160 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
162 return CodecType(mojom.MSGPIPE) | 161 return CodecType(mojom.MSGPIPE) |
163 if mojom.IsEnumKind(kind): | 162 if mojom.IsEnumKind(kind): |
164 return _kind_to_codec_type[mojom.INT32] | 163 return _kind_to_codec_type[mojom.INT32] |
| 164 if mojom.IsMapKind(kind): |
| 165 map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf" |
| 166 key_type = ElementCodecType(kind.key_kind) |
| 167 value_type = ElementCodecType(kind.value_kind) |
| 168 return "new bindings.%s(%s, %s)" % (map_type, key_type, value_type) |
165 return kind | 169 return kind |
166 | 170 |
167 def MapCodecType(kind): | 171 def ElementCodecType(kind): |
168 return "bindings.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) | 172 return "bindings.PackedBool" if mojom.IsBoolKind(kind) else CodecType(kind) |
169 | 173 |
170 def DartDecodeSnippet(kind): | 174 def DartDecodeSnippet(kind): |
171 if kind in mojom.PRIMITIVES: | 175 if kind in mojom.PRIMITIVES: |
172 return "decodeStruct(%s)" % CodecType(kind) | 176 return "decodeStruct(%s)" % CodecType(kind) |
173 if mojom.IsStructKind(kind): | 177 if mojom.IsStructKind(kind): |
174 return "decodeStructPointer(%s)" % DartType(kind) | 178 return "decodeStructPointer(%s)" % DartType(kind) |
175 if mojom.IsMapKind(kind): | 179 if mojom.IsMapKind(kind): |
176 return "decodeMapPointer(%s, %s)" % \ | 180 return "decodeMapPointer(%s, %s)" % \ |
177 (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) | 181 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) |
178 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 182 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
179 return "decodeArrayPointer(bindings.PackedBool)" | 183 return "decodeArrayPointer(bindings.PackedBool)" |
180 if mojom.IsArrayKind(kind): | 184 if mojom.IsArrayKind(kind): |
181 return "decodeArrayPointer(%s)" % CodecType(kind.kind) | 185 return "decodeArrayPointer(%s)" % CodecType(kind.kind) |
182 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 186 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
183 return DartDecodeSnippet(mojom.MSGPIPE) | 187 return DartDecodeSnippet(mojom.MSGPIPE) |
184 if mojom.IsEnumKind(kind): | 188 if mojom.IsEnumKind(kind): |
185 return DartDecodeSnippet(mojom.INT32) | 189 return DartDecodeSnippet(mojom.INT32) |
186 | 190 |
187 | 191 |
188 def DartEncodeSnippet(kind): | 192 def DartEncodeSnippet(kind): |
189 if kind in mojom.PRIMITIVES: | 193 if kind in mojom.PRIMITIVES: |
190 return "encodeStruct(%s, " % CodecType(kind) | 194 return "encodeStruct(%s, " % CodecType(kind) |
191 if mojom.IsStructKind(kind): | 195 if mojom.IsStructKind(kind): |
192 return "encodeStructPointer(%s, " % DartType(kind) | 196 return "encodeStructPointer(%s, " % DartType(kind) |
193 if mojom.IsMapKind(kind): | 197 if mojom.IsMapKind(kind): |
194 return "encodeMapPointer(%s, %s, " % \ | 198 return "encodeMapPointer(%s, %s, " % \ |
195 (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind)) | 199 (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) |
196 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): | 200 if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): |
197 return "encodeArrayPointer(bindings.PackedBool, "; | 201 return "encodeArrayPointer(bindings.PackedBool, "; |
198 if mojom.IsArrayKind(kind): | 202 if mojom.IsArrayKind(kind): |
199 return "encodeArrayPointer(%s, " % CodecType(kind.kind) | 203 return "encodeArrayPointer(%s, " % CodecType(kind.kind) |
200 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): | 204 if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind): |
201 return DartEncodeSnippet(mojom.MSGPIPE) | 205 return DartEncodeSnippet(mojom.MSGPIPE) |
202 if mojom.IsEnumKind(kind): | 206 if mojom.IsEnumKind(kind): |
203 return DartEncodeSnippet(mojom.INT32) | 207 return DartEncodeSnippet(mojom.INT32) |
204 | 208 |
205 | 209 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 def GetParameters(self): | 255 def GetParameters(self): |
252 return { | 256 return { |
253 "namespace": self.module.namespace, | 257 "namespace": self.module.namespace, |
254 "imports": self.GetImports(), | 258 "imports": self.GetImports(), |
255 "kinds": self.module.kinds, | 259 "kinds": self.module.kinds, |
256 "enums": self.module.enums, | 260 "enums": self.module.enums, |
257 "module": self.module, | 261 "module": self.module, |
258 "structs": self.GetStructs() + self.GetStructsFromMethods(), | 262 "structs": self.GetStructs() + self.GetStructsFromMethods(), |
259 "interfaces": self.module.interfaces, | 263 "interfaces": self.module.interfaces, |
260 "imported_interfaces": self.GetImportedInterfaces(), | 264 "imported_interfaces": self.GetImportedInterfaces(), |
| 265 "imported_from": self.ImportedFrom(), |
261 } | 266 } |
262 | 267 |
263 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) | 268 @UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters) |
264 def GenerateLibModule(self): | 269 def GenerateLibModule(self): |
265 return self.GetParameters() | 270 return self.GetParameters() |
266 | 271 |
267 def GenerateFiles(self, args): | 272 def GenerateFiles(self, args): |
268 self.Write(self.GenerateLibModule(), | 273 self.Write(self.GenerateLibModule(), |
269 self.MatchMojomFilePath("%s.dart" % self.module.name)) | 274 self.MatchMojomFilePath("%s.dart" % self.module.name)) |
270 | 275 |
271 def GetImports(self): | 276 def GetImports(self): |
272 used_names = set() | 277 used_names = set() |
273 for each_import in self.module.imports: | 278 for each_import in self.module.imports: |
274 simple_name = each_import["module_name"].split(".")[0] | 279 simple_name = each_import["module_name"].split(".")[0] |
275 | 280 |
276 # Since each import is assigned a variable in JS, they need to have unique | 281 # Since each import is assigned a variable in JS, they need to have unique |
277 # names. | 282 # names. |
278 unique_name = simple_name | 283 unique_name = simple_name |
279 counter = 0 | 284 counter = 0 |
280 while unique_name in used_names: | 285 while unique_name in used_names: |
281 counter += 1 | 286 counter += 1 |
282 unique_name = simple_name + str(counter) | 287 unique_name = simple_name + str(counter) |
283 | 288 |
284 used_names.add(unique_name) | 289 used_names.add(unique_name) |
285 each_import["unique_name"] = unique_name | 290 each_import["unique_name"] = unique_name |
286 counter += 1 | 291 counter += 1 |
287 return self.module.imports | 292 return self.module.imports |
288 | 293 |
289 def GetImportedInterfaces(self): | 294 def GetImportedInterfaces(self): |
290 interface_to_import = {}; | 295 interface_to_import = {} |
291 for each_import in self.module.imports: | 296 for each_import in self.module.imports: |
292 for each_interface in each_import["module"].interfaces: | 297 for each_interface in each_import["module"].interfaces: |
293 name = each_interface.name | 298 name = each_interface.name |
294 interface_to_import[name] = each_import["unique_name"] + "." + name | 299 interface_to_import[name] = each_import["unique_name"] + "." + name |
295 return interface_to_import; | 300 return interface_to_import |
| 301 |
| 302 def ImportedFrom(self): |
| 303 interface_to_import = {} |
| 304 for each_import in self.module.imports: |
| 305 for each_interface in each_import["module"].interfaces: |
| 306 name = each_interface.name |
| 307 interface_to_import[name] = each_import["unique_name"] + "." |
| 308 return interface_to_import |
OLD | NEW |