OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 """This module generates Dart APIs from the IDL database.""" | 6 """This module generates Dart APIs from the IDL database.""" |
7 | 7 |
8 import emitter | 8 import emitter |
9 import idlnode | 9 import idlnode |
10 import logging | 10 import logging |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 interface.parents = filter(HasAnnotations, interface.parents) | 358 interface.parents = filter(HasAnnotations, interface.parents) |
359 else: | 359 else: |
360 database.DeleteInterface(interface.id) | 360 database.DeleteInterface(interface.id) |
361 | 361 |
362 self.FilterMembersWithUnidentifiedTypes(database) | 362 self.FilterMembersWithUnidentifiedTypes(database) |
363 | 363 |
364 | 364 |
365 def Generate(self, database, output_dir, | 365 def Generate(self, database, output_dir, |
366 module_source_preference=[], source_filter=None, | 366 module_source_preference=[], source_filter=None, |
367 super_database=None, common_prefix=None, super_map={}, | 367 super_database=None, common_prefix=None, super_map={}, |
368 lib_dir = None): | 368 lib_dir=None, native=False): |
sra1
2012/01/26 18:32:16
I would like eventually (i.e. not this CL)
to move
podivilov
2012/01/30 19:23:50
I suggest that we pass systems list here, and even
| |
369 """Generates Dart and JS files for the loaded interfaces. | 369 """Generates Dart and JS files for the loaded interfaces. |
370 | 370 |
371 Args: | 371 Args: |
372 database -- database containing interfaces to generate code for. | 372 database -- database containing interfaces to generate code for. |
373 output_dir -- directory to write generated files to. | 373 output_dir -- directory to write generated files to. |
374 module_source_preference -- priority order list of source annotations to | 374 module_source_preference -- priority order list of source annotations to |
375 use when choosing a module name, if none specified uses the module name | 375 use when choosing a module name, if none specified uses the module name |
376 from the database. | 376 from the database. |
377 source_filter -- if specified, only outputs interfaces that have one of | 377 source_filter -- if specified, only outputs interfaces that have one of |
378 these source annotation and rewrites the names of superclasses not | 378 these source annotation and rewrites the names of superclasses not |
379 marked with this source to use the common prefix. | 379 marked with this source to use the common prefix. |
380 super_database -- database containing super interfaces that the generated | 380 super_database -- database containing super interfaces that the generated |
381 interfaces should extend. | 381 interfaces should extend. |
382 common_prefix -- prefix for the common library, if any. | 382 common_prefix -- prefix for the common library, if any. |
383 lib_file_path -- filename for generated .lib file, None if not required. | 383 lib_file_path -- filename for generated .lib file, None if not required. |
384 lib_template -- template file in this directory for generated lib file. | 384 lib_template -- template file in this directory for generated lib file. |
385 """ | 385 """ |
386 | 386 |
387 self._emitters = multiemitter.MultiEmitter() | 387 self._emitters = multiemitter.MultiEmitter() |
388 self._database = database | 388 self._database = database |
389 self._output_dir = output_dir | 389 self._output_dir = output_dir |
390 | 390 |
391 self._ComputeInheritanceClosure() | 391 self._ComputeInheritanceClosure() |
392 | 392 |
393 self._systems = [] | |
394 | |
393 interface_system = InterfacesSystem( | 395 interface_system = InterfacesSystem( |
394 TemplateLoader(self._template_dir, ['dom/interface', 'dom', '']), | 396 TemplateLoader(self._template_dir, ['dom/interface', 'dom', '']), |
395 self._database, self._emitters, self._output_dir) | 397 self._database, self._emitters, self._output_dir) |
398 self._systems.append(interface_system) | |
396 | 399 |
397 wrapping_system = WrappingImplementationSystem( | 400 if native: |
398 TemplateLoader(self._template_dir, ['dom/wrapping', 'dom', '']), | 401 native_system = NativeImplementationSystem( |
399 self._database, self._emitters, self._output_dir) | 402 TemplateLoader(self._template_dir, ['dom/native', 'dom', '']), |
403 self._database, self._emitters, self._auxiliary_dir, | |
404 self._output_dir) | |
405 self._systems.append(native_system) | |
406 else: | |
407 wrapping_system = WrappingImplementationSystem( | |
408 TemplateLoader(self._template_dir, ['dom/wrapping', 'dom', '']), | |
409 self._database, self._emitters, self._output_dir) | |
400 | 410 |
401 # Makes interface files available for listing in the library for the | 411 # Makes interface files available for listing in the library for the |
402 # wrapping implementation. | 412 # wrapping implementation. |
403 | 413 |
404 wrapping_system._interface_system = interface_system | 414 wrapping_system._interface_system = interface_system |
415 self._systems.append(wrapping_system) | |
405 | 416 |
406 frog_system = FrogSystem( | 417 frog_system = FrogSystem( |
407 TemplateLoader(self._template_dir, ['dom/frog', 'dom', '']), | 418 TemplateLoader(self._template_dir, ['dom/frog', 'dom', '']), |
408 self._database, self._emitters, self._output_dir) | 419 self._database, self._emitters, self._output_dir) |
409 | 420 |
410 frog_system._interface_system = interface_system | 421 frog_system._interface_system = interface_system |
422 self._systems.append(frog_system) | |
411 | 423 |
412 self._systems = [interface_system, | |
413 wrapping_system, | |
414 frog_system] | |
415 | 424 |
416 # Render all interfaces into Dart and save them in files. | 425 # Render all interfaces into Dart and save them in files. |
417 for interface in database.GetInterfaces(): | 426 for interface in database.GetInterfaces(): |
418 | 427 |
419 super_interface = None | 428 super_interface = None |
420 super_name = interface.id | 429 super_name = interface.id |
421 | 430 |
422 if not _MatchSourceFilter(source_filter, interface): | 431 if not _MatchSourceFilter(source_filter, interface): |
423 # Skip this interface since it's not present in the required source | 432 # Skip this interface since it's not present in the required source |
424 _logger.info('Omitting interface - %s' % interface.id) | 433 _logger.info('Omitting interface - %s' % interface.id) |
(...skipping 873 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1298 | 1307 |
1299 | 1308 |
1300 def StartInterface(self): | 1309 def StartInterface(self): |
1301 interface = self._interface | 1310 interface = self._interface |
1302 interface_name = interface.id | 1311 interface_name = interface.id |
1303 | 1312 |
1304 self._class_name = self._ImplClassName(interface_name) | 1313 self._class_name = self._ImplClassName(interface_name) |
1305 self._type_map.Emit(' "$INTERFACE": native_$(CLASS)_create_$(CLASS),\n', | 1314 self._type_map.Emit(' "$INTERFACE": native_$(CLASS)_create_$(CLASS),\n', |
1306 INTERFACE=interface_name, CLASS=self._class_name) | 1315 INTERFACE=interface_name, CLASS=self._class_name) |
1307 | 1316 |
1308 base = 'DOMWrapperBase' | 1317 base = self._BaseClassName(interface) |
1309 if interface.parents: | |
1310 supertype = interface.parents[0].type.id | |
1311 # FIXME: We're currently injecting List<..> and EventTarget as | |
1312 # supertypes in dart.idl. We should annotate/preserve as | |
1313 # attributes instead. For now, this hack lets the interfaces | |
1314 # inherit, but not the classes. | |
1315 if (not _IsDartListType(supertype) and | |
1316 not supertype == 'EventTarget'): | |
1317 base = self._ImplClassName(supertype) | |
1318 if _IsDartCollectionType(supertype): | |
1319 # List methods are injected in AddIndexer. | |
1320 pass | |
1321 elif supertype == 'EventTarget': | |
1322 # Most implementors of EventTarget specify the EventListener operations | |
1323 # again. If the operations are not specified, try to inherit from the | |
1324 # EventTarget implementation. | |
1325 # | |
1326 # Applies to MessagePort. | |
1327 if not [op for op in interface.operations if op.id == 'addEventListener' ]: | |
1328 base = self._ImplClassName(supertype) | |
1329 else: | |
1330 base = self._ImplClassName(supertype) | |
1331 | 1318 |
1332 (self._members_emitter, | 1319 (self._members_emitter, |
1333 self._top_level_emitter) = self._dart_code.Emit( | 1320 self._top_level_emitter) = self._dart_code.Emit( |
1334 '\n' | 1321 '\n' |
1335 'class $CLASS extends $BASE implements $INTERFACE {\n' | 1322 'class $CLASS extends $BASE implements $INTERFACE {\n' |
1336 ' $CLASS() : super() {}\n' | 1323 ' $CLASS() : super() {}\n' |
1337 '\n' | 1324 '\n' |
1338 ' static create_$CLASS() native {\n' | 1325 ' static create_$CLASS() native {\n' |
1339 ' return new $CLASS();\n' | 1326 ' return new $CLASS();\n' |
1340 ' }\n' | 1327 ' }\n' |
1341 '$!MEMBERS' | 1328 '$!MEMBERS' |
1342 '\n' | 1329 '\n' |
1343 ' String get typeName() { return "$INTERFACE"; }\n' | 1330 ' String get typeName() { return "$INTERFACE"; }\n' |
1344 '}\n' | 1331 '}\n' |
1345 '$!TOP_LEVEL', | 1332 '$!TOP_LEVEL', |
1346 CLASS=self._class_name, BASE=base, INTERFACE=interface_name) | 1333 CLASS=self._class_name, BASE=base, INTERFACE=interface_name) |
1347 | 1334 |
1348 def _ImplClassName(self, type_name): | 1335 def _ImplClassName(self, type_name): |
1349 return '_' + type_name + 'WrappingImplementation' | 1336 return '_' + type_name + 'WrappingImplementation' |
1350 | 1337 |
1338 def _BaseClassName(self, interface): | |
1339 if not interface.parents: | |
1340 return 'DOMWrapperBase' | |
1341 | |
1342 supertype = interface.parents[0].type.id | |
1343 | |
1344 # FIXME: We're currently injecting List<..> and EventTarget as | |
1345 # supertypes in dart.idl. We should annotate/preserve as | |
1346 # attributes instead. For now, this hack lets the interfaces | |
1347 # inherit, but not the classes. | |
1348 # List methods are injected in AddIndexer. | |
1349 if _IsDartListType(supertype) or _IsDartCollectionType(supertype): | |
1350 return 'DOMWrapperBase' | |
1351 | |
1352 if supertype == 'EventTarget': | |
1353 # Most implementors of EventTarget specify the EventListener operations | |
1354 # again. If the operations are not specified, try to inherit from the | |
1355 # EventTarget implementation. | |
1356 # | |
1357 # Applies to MessagePort. | |
1358 if not [op for op in interface.operations if op.id == 'addEventListener']: | |
1359 return self._ImplClassName(supertype) | |
1360 return 'DOMWrapperBase' | |
1361 | |
1362 return self._ImplClassName(supertype) | |
1363 | |
1351 def FinishInterface(self): | 1364 def FinishInterface(self): |
1352 """.""" | 1365 """.""" |
1353 pass | 1366 pass |
1354 | 1367 |
1355 def AddConstant(self, constant): | 1368 def AddConstant(self, constant): |
1356 # Constants are already defined on the interface. | 1369 # Constants are already defined on the interface. |
1357 pass | 1370 pass |
1358 | 1371 |
1359 def _MethodName(self, prefix, name): | 1372 def _MethodName(self, prefix, name): |
1360 method_name = prefix + name | 1373 method_name = prefix + name |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1434 # interface Y extends X, List<T> ... | 1447 # interface Y extends X, List<T> ... |
1435 # | 1448 # |
1436 # In the non-root case we have to choose between: | 1449 # In the non-root case we have to choose between: |
1437 # | 1450 # |
1438 # class YImpl extends XImpl { add List<T> methods; } | 1451 # class YImpl extends XImpl { add List<T> methods; } |
1439 # | 1452 # |
1440 # and | 1453 # and |
1441 # | 1454 # |
1442 # class YImpl extends ListBase<T> { copies of transitive XImpl methods; } | 1455 # class YImpl extends ListBase<T> { copies of transitive XImpl methods; } |
1443 # | 1456 # |
1444 if ('HasIndexGetter' in self._interface.ext_attrs or | 1457 if self._HasNativeIndexGetter(self._interface): |
1445 'HasNumericIndexGetter' in self._interface.ext_attrs): | 1458 self._EmitNativeIndexGetter(self._interface, element_type) |
1446 method_name = '_index' | |
1447 self._members_emitter.Emit( | |
1448 '\n' | |
1449 ' $TYPE operator[](int index) { return $METHOD(this, index); }\n' | |
1450 ' static $TYPE $METHOD(var _this, int index) native;\n', | |
1451 TYPE=element_type, METHOD=method_name) | |
1452 self._js_code.Emit( | |
1453 '\n' | |
1454 'function native_$(CLASS)_$(METHOD)(_this, index) {\n' | |
1455 ' try {\n' | |
1456 ' return __dom_wrap(_this.$dom[index]);\n' | |
1457 ' } catch (e) {\n' | |
1458 ' throw __dom_wrap_exception(e);\n' | |
1459 ' }\n' | |
1460 '}\n', | |
1461 CLASS=self._class_name, METHOD=method_name) | |
1462 else: | 1459 else: |
1463 self._members_emitter.Emit( | 1460 self._members_emitter.Emit( |
1464 '\n' | 1461 '\n' |
1465 ' $TYPE operator[](int index) {\n' | 1462 ' $TYPE operator[](int index) {\n' |
1466 ' return item(index);\n' | 1463 ' return item(index);\n' |
1467 ' }\n', | 1464 ' }\n', |
1468 TYPE=element_type) | 1465 TYPE=element_type) |
1469 | 1466 |
1470 | 1467 if self._HasNativeIndexSetter(self._interface): |
1471 if 'HasCustomIndexSetter' in self._interface.ext_attrs: | 1468 self._EmitNativeIndexSetter(self._interface, element_type) |
1472 method_name = '_set_index' | |
1473 self._members_emitter.Emit( | |
1474 '\n' | |
1475 ' void operator[]=(int index, $TYPE value) {\n' | |
1476 ' return $METHOD(this, index, value);\n' | |
1477 ' }\n' | |
1478 ' static $METHOD(_this, index, value) native;\n', | |
1479 TYPE=element_type, METHOD=method_name) | |
1480 self._js_code.Emit( | |
1481 '\n' | |
1482 'function native_$(CLASS)_$(METHOD)(_this, index, value) {\n' | |
1483 ' try {\n' | |
1484 ' return _this.$dom[index] = __dom_unwrap(value);\n' | |
1485 ' } catch (e) {\n' | |
1486 ' throw __dom_wrap_exception(e);\n' | |
1487 ' }\n' | |
1488 '}\n', | |
1489 CLASS=self._class_name, METHOD=method_name) | |
1490 else: | 1469 else: |
1491 self._members_emitter.Emit( | 1470 self._members_emitter.Emit( |
1492 '\n' | 1471 '\n' |
1493 ' void operator[]=(int index, $TYPE value) {\n' | 1472 ' void operator[]=(int index, $TYPE value) {\n' |
1494 ' throw new UnsupportedOperationException("Cannot assign element of immutable List.");\n' | 1473 ' throw new UnsupportedOperationException("Cannot assign element of immutable List.");\n' |
1495 ' }\n', | 1474 ' }\n', |
1496 TYPE=element_type) | 1475 TYPE=element_type) |
1497 | 1476 |
1498 self._members_emitter.Emit( | 1477 self._members_emitter.Emit( |
1499 '\n' | 1478 '\n' |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1577 '\n' | 1556 '\n' |
1578 ' bool isEmpty() {\n' | 1557 ' bool isEmpty() {\n' |
1579 ' return length == 0;\n' | 1558 ' return length == 0;\n' |
1580 ' }\n' | 1559 ' }\n' |
1581 '\n' | 1560 '\n' |
1582 ' Iterator<$TYPE> iterator() {\n' | 1561 ' Iterator<$TYPE> iterator() {\n' |
1583 ' return new _FixedSizeListIterator<$TYPE>(this);\n' | 1562 ' return new _FixedSizeListIterator<$TYPE>(this);\n' |
1584 ' }\n', | 1563 ' }\n', |
1585 TYPE=element_type) | 1564 TYPE=element_type) |
1586 | 1565 |
1566 def _HasNativeIndexGetter(self, interface): | |
1567 return ('HasIndexGetter' in interface.ext_attrs or | |
1568 'HasNumericIndexGetter' in interface.ext_attrs) | |
1569 | |
1570 def _EmitNativeIndexGetter(self, interface, element_type): | |
1571 method_name = '_index' | |
1572 self._members_emitter.Emit( | |
1573 '\n' | |
1574 ' $TYPE operator[](int index) { return $METHOD(this, index); }\n' | |
1575 ' static $TYPE $METHOD(var _this, int index) native;\n', | |
1576 TYPE=element_type, METHOD=method_name) | |
1577 self._js_code.Emit( | |
1578 '\n' | |
1579 'function native_$(CLASS)_$(METHOD)(_this, index) {\n' | |
1580 ' try {\n' | |
1581 ' return __dom_wrap(_this.$dom[index]);\n' | |
1582 ' } catch (e) {\n' | |
1583 ' throw __dom_wrap_exception(e);\n' | |
1584 ' }\n' | |
1585 '}\n', | |
1586 CLASS=self._class_name, METHOD=method_name) | |
1587 | |
1588 def _HasNativeIndexSetter(self, interface): | |
1589 return 'HasCustomIndexSetter' in interface.ext_attrs | |
1590 | |
1591 def _EmitNativeIndexSetter(self, interface, element_type): | |
1592 method_name = '_set_index' | |
1593 self._members_emitter.Emit( | |
1594 '\n' | |
1595 ' void operator[]=(int index, $TYPE value) {\n' | |
1596 ' return $METHOD(this, index, value);\n' | |
1597 ' }\n' | |
1598 ' static $METHOD(_this, index, value) native;\n', | |
1599 TYPE=element_type, METHOD=method_name) | |
1600 self._js_code.Emit( | |
1601 '\n' | |
1602 'function native_$(CLASS)_$(METHOD)(_this, index, value) {\n' | |
1603 ' try {\n' | |
1604 ' return _this.$dom[index] = __dom_unwrap(value);\n' | |
1605 ' } catch (e) {\n' | |
1606 ' throw __dom_wrap_exception(e);\n' | |
1607 ' }\n' | |
1608 '}\n', | |
1609 CLASS=self._class_name, METHOD=method_name) | |
1610 | |
1587 def AddOperation(self, info): | 1611 def AddOperation(self, info): |
1588 """ | 1612 """ |
1589 Arguments: | 1613 Arguments: |
1590 info: An OperationInfo object. | 1614 info: An OperationInfo object. |
1591 """ | 1615 """ |
1592 body = self._members_emitter.Emit( | 1616 body = self._members_emitter.Emit( |
1593 '\n' | 1617 '\n' |
1594 ' $TYPE $NAME($PARAMS) {\n' | 1618 ' $TYPE $NAME($PARAMS) {\n' |
1595 '$!BODY' | 1619 '$!BODY' |
1596 ' }\n', | 1620 ' }\n', |
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2027 info: An OperationInfo object. | 2051 info: An OperationInfo object. |
2028 """ | 2052 """ |
2029 # TODO(vsm): Handle overloads. | 2053 # TODO(vsm): Handle overloads. |
2030 self._members_emitter.Emit( | 2054 self._members_emitter.Emit( |
2031 '\n' | 2055 '\n' |
2032 ' $TYPE $NAME($PARAMS) native;\n', | 2056 ' $TYPE $NAME($PARAMS) native;\n', |
2033 TYPE=self._NarrowOutputType(info.type_name), | 2057 TYPE=self._NarrowOutputType(info.type_name), |
2034 NAME=info.name, | 2058 NAME=info.name, |
2035 PARAMS=info.ParametersImplementationDeclaration( | 2059 PARAMS=info.ParametersImplementationDeclaration( |
2036 lambda type_name: self._NarrowInputType(type_name))) | 2060 lambda type_name: self._NarrowInputType(type_name))) |
2061 | |
2062 | |
2063 # ------------------------------------------------------------------------------ | |
2064 | |
2065 class NativeImplementationSystem(System): | |
2066 | |
2067 def __init__(self, templates, database, emitters, auxiliary_dir, output_dir): | |
2068 super(NativeImplementationSystem, self).__init__( | |
2069 templates, database, emitters, output_dir) | |
2070 | |
2071 self._auxiliary_dir = auxiliary_dir | |
2072 | |
2073 self._dom_public_imports_emitter = emitter.Emitter() | |
sra1
2012/01/26 18:32:16
The other generators use the pattern of generating
podivilov
2012/01/30 19:23:50
Replaced emitters with files lists like it's done
| |
2074 self._dom_impl_imports_emitter = emitter.Emitter() | |
2075 | |
2076 def InterfaceGenerator(self, | |
2077 interface, | |
2078 common_prefix, | |
2079 super_interface_name, | |
2080 source_filter): | |
2081 interface_name = interface.id | |
2082 | |
2083 self._dom_public_imports_emitter.Emit('#source("$PATH");\n', | |
2084 PATH=self._FilePathForDartInterface(interface_name)) | |
2085 self._dom_impl_imports_emitter.Emit('#source("$PATH");\n', | |
2086 PATH=self._FilePathForDartImpl(interface_name)) | |
2087 | |
2088 dart_impl_path = os.path.join(self._output_dir, | |
2089 self._FilePathForDartImpl(interface_name)) | |
2090 dart_impl_emitter = self._emitters.FileEmitter(dart_impl_path) | |
2091 | |
2092 return NativeImplementationGenerator(interface, super_interface_name, | |
2093 dart_impl_emitter, | |
2094 self._BaseDefines(interface), | |
2095 self._templates) | |
2096 | |
2097 def ProcessCallback(self, interface, info): | |
2098 self._dom_public_imports_emitter.Emit('#source("$PATH");\n', | |
2099 PATH=self._FilePathForDartInterface(interface.id)) | |
2100 | |
2101 def GenerateLibraries(self, lib_dir): | |
2102 auxiliary_dir = os.path.relpath(self._auxiliary_dir, self._output_dir) | |
2103 | |
2104 # Generate dom_public.dart. | |
2105 dom_public_path = os.path.join(self._output_dir, 'dom_public.dart') | |
2106 dom_public_emitter = self._emitters.FileEmitter(dom_public_path) | |
2107 dom_public_emitter.Emit(self._templates.Load('dom_public.darttemplate'), | |
2108 AUXILIARY_DIR=auxiliary_dir, | |
2109 SOURCES=self._dom_public_imports_emitter.Fragments()) | |
2110 | |
2111 # Generate dom_impl.dart. | |
2112 dom_impl_path = os.path.join(self._output_dir, 'dom_impl.dart') | |
2113 dom_impl_emitter = self._emitters.FileEmitter(dom_impl_path) | |
2114 dom_impl_emitter.Emit(self._templates.Load('dom_impl.darttemplate'), | |
2115 AUXILIARY_DIR=auxiliary_dir, | |
2116 SOURCES=self._dom_impl_imports_emitter.Fragments()) | |
2117 | |
2118 def Finish(self): | |
2119 pass | |
2120 | |
2121 def _FilePathForDartInterface(self, interface_name): | |
2122 return os.path.join('src', 'interface', '%s.dart' % interface_name) | |
2123 | |
2124 def _FilePathForDartImpl(self, interface_name): | |
2125 return os.path.join('dart', '%sImplementation.dart' % interface_name) | |
2126 | |
2127 | |
2128 class NativeImplementationGenerator(WrappingInterfaceGenerator): | |
2129 """Generates Dart implementation for one DOM IDL interface.""" | |
2130 | |
2131 def __init__(self, interface, super_interface, dart_impl_emitter, | |
2132 base_members, templates): | |
2133 """Generates Dart code for the given interface. | |
2134 | |
2135 Args: | |
2136 | |
2137 interface: an IDLInterface instance. It is assumed that all types have | |
2138 been converted to Dart types (e.g. int, String), unless they are in | |
2139 the same package as the interface. | |
2140 super_interface: A string or None, the name of the common interface that | |
2141 this interface implements, if any. | |
2142 dart_impl_emitter: an Emitter for the file containing the Dart | |
2143 implementation class. | |
2144 base_members: a set of names of members defined in a base class. This is | |
2145 used to avoid static member 'overriding' in the generated Dart code. | |
2146 """ | |
2147 self._interface = interface | |
2148 self._super_interface = super_interface | |
2149 self._dart_impl_emitter = dart_impl_emitter | |
2150 self._base_members = base_members | |
2151 self._templates = templates | |
2152 self._current_secondary_parent = None | |
2153 | |
2154 def StartInterface(self): | |
2155 self._class_name = self._ImplClassName(self._interface.id) | |
2156 self._members_emitter = emitter.Emitter() | |
2157 | |
2158 def _ImplClassName(self, type_name): | |
2159 return type_name + 'Implementation' | |
2160 | |
2161 def FinishInterface(self): | |
2162 interface = self._interface | |
2163 interface_name = interface.id | |
2164 | |
2165 base = self._BaseClassName(interface) | |
2166 self._dart_impl_emitter.Emit( | |
2167 self._templates.Load('dart_implementation.darttemplate'), | |
2168 CLASS=self._class_name, BASE=base, INTERFACE=interface_name, | |
2169 MEMBERS=self._members_emitter.Fragments()) | |
2170 | |
2171 def AddGetter(self, attr): | |
2172 self._members_emitter.Emit( | |
2173 '\n' | |
2174 ' $TYPE get $NAME() native "$(INTERFACE)_$(NAME)_Getter";\n', | |
2175 NAME=attr.id, TYPE=attr.type.id, INTERFACE=self._interface.id) | |
2176 | |
2177 def AddSetter(self, attr): | |
2178 self._members_emitter.Emit( | |
2179 '\n' | |
2180 ' void set $NAME($TYPE) native "$(INTERFACE)_$(NAME)_Setter";\n', | |
2181 NAME=attr.id, TYPE=attr.type.id, INTERFACE=self._interface.id) | |
2182 | |
2183 def _HasNativeIndexGetter(self, interface): | |
2184 return ('HasCustomIndexGetter' in interface.ext_attrs or | |
2185 'HasNumericIndexGetter' in interface.ext_attrs) | |
2186 | |
2187 def _EmitNativeIndexGetter(self, interface, element_type): | |
2188 native_binding = '%s_numericIndexGetter_Callback' % interface.id | |
2189 self._members_emitter.Emit( | |
2190 '\n' | |
2191 ' $TYPE operator[](int index) native "$NATIVE_BINDING";\n', | |
2192 TYPE=element_type, NATIVE_BINDING=native_binding) | |
2193 | |
2194 def _EmitNativeIndexSetter(self, interface, element_type): | |
2195 native_binding = '%s_numericIndexSetter_Callback' % self._interface.id | |
2196 self._members_emitter.Emit( | |
2197 '\n' | |
2198 ' void operator[]=(int index, $TYPE value) native "$NATIVE_BINDING";\n' , | |
2199 TYPE=element_type, NATIVE_BINDING=native_binding) | |
2200 | |
2201 def AddOperation(self, info): | |
2202 """ | |
2203 Arguments: | |
2204 info: An OperationInfo object. | |
2205 """ | |
2206 | |
2207 if 'Custom' in info.overloads[0].ext_attrs: | |
2208 self._members_emitter.Emit( | |
2209 '\n' | |
2210 ' $TYPE $NAME($PARAMETERS) native "$(INTERFACE)_$(NAME)_Callback";\ n', | |
2211 TYPE=info.type_name, | |
2212 NAME=info.name, | |
2213 PARAMETERS=info.ParametersImplementationDeclaration(), | |
2214 INTERFACE=self._interface.id) | |
2215 return | |
2216 | |
2217 body = self._members_emitter.Emit( | |
2218 '\n' | |
2219 ' $TYPE $NAME($PARAMETERS) {\n' | |
2220 '$!BODY' | |
2221 ' }\n', | |
2222 TYPE=info.type_name, | |
2223 NAME=info.name, | |
2224 PARAMETERS=info.ParametersImplementationDeclaration()) | |
2225 | |
2226 # Process in order of ascending number of arguments to ensure missing | |
2227 # optional arguments are processed early. | |
2228 overloads = sorted(info.overloads, | |
2229 key=lambda overload: len(overload.arguments)) | |
2230 self._native_version = 0 | |
2231 fallthrough = self.GenerateDispatch(body, info, ' ', 0, overloads) | |
2232 if fallthrough: | |
2233 body.Emit(' throw "Incorrect number or type of arguments";\n'); | |
2234 | |
2235 def GenerateSingleOperation(self, emitter, info, indent, operation): | |
2236 """Generates a call to a single operation. | |
2237 | |
2238 Arguments: | |
2239 emitter: an Emitter for the body of a block of code. | |
2240 info: the compound information about the operation and its overloads. | |
2241 indent: an indentation string for generated code. | |
2242 operation: the IDLOperation to call. | |
2243 """ | |
2244 | |
2245 arg_names = [info.arg_infos[i][0] | |
2246 for (i, arg) in enumerate(operation.arguments)] | |
2247 | |
2248 self._native_version += 1 | |
2249 native_name = '_%s' % info.name | |
2250 if self._native_version > 1: | |
2251 native_name = '%s_%s' % (native_name, self._native_version) | |
2252 | |
2253 argument_expressions = ', '.join(arg_names) | |
2254 if info.type_name != 'void': | |
2255 emitter.Emit('$(INDENT)return $NATIVENAME($ARGS);\n', | |
2256 INDENT=indent, | |
2257 NATIVENAME=native_name, | |
2258 ARGS=argument_expressions) | |
2259 else: | |
2260 emitter.Emit('$(INDENT)$NATIVENAME($ARGS);\n' | |
2261 '$(INDENT)return;\n', | |
2262 INDENT=indent, | |
2263 NATIVENAME=native_name, | |
2264 ARGS=argument_expressions) | |
2265 | |
2266 self._members_emitter.Emit(' $TYPE $NATIVE_NAME($PARAMS) native "$(INTERFAC E)$(NATIVE_NAME)_Callback";\n', | |
sra1
2012/01/26 18:32:16
Line too long.
Sometimes this is unavoidable, but
podivilov
2012/01/30 19:23:50
Done.
| |
2267 NATIVE_NAME=native_name, | |
2268 TYPE=info.type_name, | |
2269 PARAMS=', '.join(arg_names), | |
2270 INTERFACE=self._interface.id) | |
OLD | NEW |