| 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 provides shared functionality for the system to generate | 6 """This module provides shared functionality for the system to generate |
| 7 Dart:html APIs from the IDL database.""" | 7 Dart:html APIs from the IDL database.""" |
| 8 | 8 |
| 9 from systemfrog import * | 9 from systemfrog import * |
| 10 from systeminterface import * | 10 from systeminterface import * |
| (...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 return sorted(event_names, key=lambda name: _html_event_names[name]) | 457 return sorted(event_names, key=lambda name: _html_event_names[name]) |
| 458 | 458 |
| 459 def DomToHtmlEvent(event_name): | 459 def DomToHtmlEvent(event_name): |
| 460 assert event_name in _html_event_names, \ | 460 assert event_name in _html_event_names, \ |
| 461 'No known html event name for event: ' + event_name | 461 'No known html event name for event: ' + event_name |
| 462 return _html_event_names[event_name] | 462 return _html_event_names[event_name] |
| 463 | 463 |
| 464 # ------------------------------------------------------------------------------ | 464 # ------------------------------------------------------------------------------ |
| 465 class HtmlSystemShared(object): | 465 class HtmlSystemShared(object): |
| 466 | 466 |
| 467 def __init__(self, database, generator): | 467 def __init__(self, database): |
| 468 self._event_classes = set() | 468 self._event_classes = set() |
| 469 self._seen_event_names = {} | 469 self._seen_event_names = {} |
| 470 self._database = database | 470 self._database = database |
| 471 self._generator = generator | 471 self._inheritance_closure = _ComputeInheritanceClosure(database) |
| 472 | 472 |
| 473 def _AllowInHtmlLibrary(self, interface, member, member_prefix): | 473 def _AllowInHtmlLibrary(self, interface, member, member_prefix): |
| 474 return not self._Matches(interface, member, member_prefix, | 474 return not self._Matches(interface, member, member_prefix, |
| 475 _html_library_remove) | 475 _html_library_remove) |
| 476 | 476 |
| 477 def _Matches(self, interface, member, member_prefix, candidates): | 477 def _Matches(self, interface, member, member_prefix, candidates): |
| 478 for interface_name in self._AllAncestorInterfaces(interface): | 478 for interface_name in self._AllAncestorInterfaces(interface): |
| 479 if (DartType(interface_name) + '.' + member in candidates or | 479 if (DartType(interface_name) + '.' + member in candidates or |
| 480 DartType(interface_name) + '.' + member_prefix + member in candidates)
: | 480 DartType(interface_name) + '.' + member_prefix + member in candidates)
: |
| 481 return True | 481 return True |
| 482 return False | 482 return False |
| 483 | 483 |
| 484 def _AllAncestorInterfaces(self, interface): | 484 def _AllAncestorInterfaces(self, interface): |
| 485 interfaces = ([interface.id] + | 485 return [interface.id] + self._inheritance_closure[interface.id] |
| 486 self._generator._AllImplementedInterfaces(interface)) | |
| 487 return interfaces | |
| 488 | 486 |
| 489 def RenameInHtmlLibrary(self, interface, member, member_prefix='', | 487 def RenameInHtmlLibrary(self, interface, member, member_prefix='', |
| 490 implementation_class=False): | 488 implementation_class=False): |
| 491 """ | 489 """ |
| 492 Returns the name of the member in the HTML library or None if the member is | 490 Returns the name of the member in the HTML library or None if the member is |
| 493 suppressed in the HTML library | 491 suppressed in the HTML library |
| 494 """ | 492 """ |
| 495 if not self._AllowInHtmlLibrary(interface, member, member_prefix): | 493 if not self._AllowInHtmlLibrary(interface, member, member_prefix): |
| 496 return None | 494 return None |
| 497 | 495 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 if events or interface.id in _html_explicit_event_classes: | 570 if events or interface.id in _html_explicit_event_classes: |
| 573 return True, events | 571 return True, events |
| 574 else: | 572 else: |
| 575 return False, None | 573 return False, None |
| 576 | 574 |
| 577 def IsPrivate(self, name): | 575 def IsPrivate(self, name): |
| 578 return name.startswith('_') | 576 return name.startswith('_') |
| 579 | 577 |
| 580 class HtmlSystem(System): | 578 class HtmlSystem(System): |
| 581 | 579 |
| 582 def __init__(self, templates, database, emitters, output_dir, generator): | 580 def __init__(self, templates, database, emitters, output_dir): |
| 583 super(HtmlSystem, self).__init__( | 581 super(HtmlSystem, self).__init__( |
| 584 templates, database, emitters, output_dir) | 582 templates, database, emitters, output_dir) |
| 585 self._shared = HtmlSystemShared(database, generator) | 583 self._shared = HtmlSystemShared(database) |
| 586 | 584 |
| 587 class HtmlInterfacesSystem(HtmlSystem): | 585 class HtmlInterfacesSystem(HtmlSystem): |
| 588 | 586 |
| 589 def __init__(self, templates, database, emitters, output_dir, generator): | 587 def __init__(self, templates, database, emitters, output_dir): |
| 590 super(HtmlInterfacesSystem, self).__init__( | 588 super(HtmlInterfacesSystem, self).__init__( |
| 591 templates, database, emitters, output_dir, generator) | 589 templates, database, emitters, output_dir) |
| 592 self._dart_interface_file_paths = [] | 590 self._dart_interface_file_paths = [] |
| 593 | 591 |
| 594 def InterfaceGenerator(self, | 592 def InterfaceGenerator(self, |
| 595 interface, | 593 interface, |
| 596 common_prefix, | 594 common_prefix, |
| 597 super_interface_name, | 595 super_interface_name, |
| 598 source_filter): | 596 source_filter): |
| 599 """.""" | 597 """.""" |
| 600 interface_name = interface.id | 598 interface_name = interface.id |
| 601 dart_interface_file_path = self._FilePathForDartInterface(interface_name) | 599 dart_interface_file_path = self._FilePathForDartInterface(interface_name) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 614 template, | 612 template, |
| 615 common_prefix, super_interface_name, | 613 common_prefix, super_interface_name, |
| 616 source_filter, self._shared) | 614 source_filter, self._shared) |
| 617 | 615 |
| 618 def ProcessCallback(self, interface, info): | 616 def ProcessCallback(self, interface, info): |
| 619 """Generates a typedef for the callback interface.""" | 617 """Generates a typedef for the callback interface.""" |
| 620 interface_name = interface.id | 618 interface_name = interface.id |
| 621 file_path = self._FilePathForDartInterface(interface_name) | 619 file_path = self._FilePathForDartInterface(interface_name) |
| 622 self._ProcessCallback(interface, info, file_path) | 620 self._ProcessCallback(interface, info, file_path) |
| 623 | 621 |
| 624 def GenerateLibraries(self, lib_dir): | 622 def GenerateLibraries(self): |
| 625 pass | 623 pass |
| 626 | 624 |
| 627 | 625 |
| 628 def _FilePathForDartInterface(self, interface_name): | 626 def _FilePathForDartInterface(self, interface_name): |
| 629 """Returns the file path of the Dart interface definition.""" | 627 """Returns the file path of the Dart interface definition.""" |
| 630 # TODO(jmesserly): is this the right path | 628 # TODO(jmesserly): is this the right path |
| 631 return os.path.join(self._output_dir, 'html', 'interface', | 629 return os.path.join(self._output_dir, 'html', 'interface', |
| 632 '%s.dart' % interface_name) | 630 '%s.dart' % interface_name) |
| 633 | 631 |
| 634 # ------------------------------------------------------------------------------ | 632 # ------------------------------------------------------------------------------ |
| (...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1101 | 1099 |
| 1102 def _EmitEventGetter(self, events_class): | 1100 def _EmitEventGetter(self, events_class): |
| 1103 self._members_emitter.Emit( | 1101 self._members_emitter.Emit( |
| 1104 '\n $TYPE get on() =>\n new $TYPE(this);\n', | 1102 '\n $TYPE get on() =>\n new $TYPE(this);\n', |
| 1105 TYPE=events_class) | 1103 TYPE=events_class) |
| 1106 | 1104 |
| 1107 # ------------------------------------------------------------------------------ | 1105 # ------------------------------------------------------------------------------ |
| 1108 | 1106 |
| 1109 class HtmlFrogSystem(HtmlSystem): | 1107 class HtmlFrogSystem(HtmlSystem): |
| 1110 | 1108 |
| 1111 def __init__(self, templates, database, emitters, output_dir, generator): | 1109 def __init__(self, templates, database, emitters, output_dir): |
| 1112 super(HtmlFrogSystem, self).__init__( | 1110 super(HtmlFrogSystem, self).__init__( |
| 1113 templates, database, emitters, output_dir, generator) | 1111 templates, database, emitters, output_dir) |
| 1114 self._dart_frog_file_paths = [] | 1112 self._dart_frog_file_paths = [] |
| 1115 | 1113 |
| 1116 | 1114 |
| 1117 def InterfaceGenerator(self, | 1115 def InterfaceGenerator(self, |
| 1118 interface, | 1116 interface, |
| 1119 common_prefix, | 1117 common_prefix, |
| 1120 super_interface_name, | 1118 super_interface_name, |
| 1121 source_filter): | 1119 source_filter): |
| 1122 """.""" | 1120 """.""" |
| 1123 template_file = 'impl_%s.darttemplate' % interface.id | 1121 template_file = 'impl_%s.darttemplate' % interface.id |
| 1124 template = self._templates.TryLoad(template_file) | 1122 template = self._templates.TryLoad(template_file) |
| 1125 if not template: | 1123 if not template: |
| 1126 template = self._templates.Load('frog_impl.darttemplate') | 1124 template = self._templates.Load('frog_impl.darttemplate') |
| 1127 | 1125 |
| 1128 dart_code = self._ImplFileEmitter(interface.id) | 1126 dart_code = self._ImplFileEmitter(interface.id) |
| 1129 return HtmlFrogClassGenerator(self, interface, template, | 1127 return HtmlFrogClassGenerator(self, interface, template, |
| 1130 super_interface_name, dart_code, self._shared) | 1128 super_interface_name, dart_code, self._shared) |
| 1131 | 1129 |
| 1132 def GenerateLibraries(self, lib_dir): | 1130 def GenerateLibraries(self): |
| 1133 self._GenerateLibFile( | 1131 self._GenerateLibFile( |
| 1134 'html_frog.darttemplate', | 1132 'html_frog.darttemplate', |
| 1135 os.path.join(lib_dir, 'html_frog.dart'), | 1133 os.path.join(self._output_dir, 'html_frog.dart'), |
| 1136 (self._interface_system._dart_interface_file_paths + | 1134 (self._interface_system._dart_interface_file_paths + |
| 1137 self._interface_system._dart_callback_file_paths + | 1135 self._interface_system._dart_callback_file_paths + |
| 1138 self._dart_frog_file_paths)) | 1136 self._dart_frog_file_paths)) |
| 1139 | 1137 |
| 1140 def Finish(self): | 1138 def Finish(self): |
| 1141 pass | 1139 pass |
| 1142 | 1140 |
| 1143 def _ImplFileEmitter(self, name): | 1141 def _ImplFileEmitter(self, name): |
| 1144 """Returns the file emitter of the Frog implementation file.""" | 1142 """Returns the file emitter of the Frog implementation file.""" |
| 1145 # TODO(jmesserly): is this the right path | 1143 # TODO(jmesserly): is this the right path |
| 1146 path = os.path.join(self._output_dir, 'html', 'frog', '%s.dart' % name) | 1144 path = os.path.join(self._output_dir, 'html', 'frog', '%s.dart' % name) |
| 1147 self._dart_frog_file_paths.append(path) | 1145 self._dart_frog_file_paths.append(path) |
| 1148 return self._emitters.FileEmitter(path) | 1146 return self._emitters.FileEmitter(path) |
| 1149 | 1147 |
| 1150 # ----------------------------------------------------------------------------- | 1148 # ----------------------------------------------------------------------------- |
| 1151 | 1149 |
| 1152 class HtmlDartiumSystem(HtmlSystem): | 1150 class HtmlDartiumSystem(HtmlSystem): |
| 1153 | 1151 |
| 1154 def __init__(self, templates, database, emitters, auxiliary_dir, output_dir, | 1152 def __init__(self, templates, database, emitters, auxiliary_dir, output_dir): |
| 1155 generator): | |
| 1156 """Prepared for generating wrapping implementation. | 1153 """Prepared for generating wrapping implementation. |
| 1157 | 1154 |
| 1158 - Creates emitter for Dart code. | 1155 - Creates emitter for Dart code. |
| 1159 """ | 1156 """ |
| 1160 super(HtmlDartiumSystem, self).__init__( | 1157 super(HtmlDartiumSystem, self).__init__( |
| 1161 templates, database, emitters, output_dir, generator) | 1158 templates, database, emitters, output_dir) |
| 1162 self._auxiliary_dir = auxiliary_dir | 1159 self._auxiliary_dir = auxiliary_dir |
| 1163 self._shared = HtmlSystemShared(database, generator) | 1160 self._shared = HtmlSystemShared(database) |
| 1164 self._dart_dartium_file_paths = [] | 1161 self._dart_dartium_file_paths = [] |
| 1165 self._wrap_cases = [] | 1162 self._wrap_cases = [] |
| 1166 | 1163 |
| 1167 def InterfaceGenerator(self, | 1164 def InterfaceGenerator(self, |
| 1168 interface, | 1165 interface, |
| 1169 common_prefix, | 1166 common_prefix, |
| 1170 super_interface_name, | 1167 super_interface_name, |
| 1171 source_filter): | 1168 source_filter): |
| 1172 """.""" | 1169 """.""" |
| 1173 template_file = 'impl_%s.darttemplate' % interface.id | 1170 template_file = 'impl_%s.darttemplate' % interface.id |
| 1174 template = self._templates.TryLoad(template_file) | 1171 template = self._templates.TryLoad(template_file) |
| 1175 # TODO(jacobr): change this name as it is confusing. | 1172 # TODO(jacobr): change this name as it is confusing. |
| 1176 if not template: | 1173 if not template: |
| 1177 template = self._templates.Load('frog_impl.darttemplate') | 1174 template = self._templates.Load('frog_impl.darttemplate') |
| 1178 | 1175 |
| 1179 dart_code = self._ImplFileEmitter(interface.id) | 1176 dart_code = self._ImplFileEmitter(interface.id) |
| 1180 return HtmlDartiumInterfaceGenerator(self, interface, template, | 1177 return HtmlDartiumInterfaceGenerator(self, interface, template, |
| 1181 super_interface_name, dart_code, self._BaseDefines(interface), | 1178 super_interface_name, dart_code, self._BaseDefines(interface), |
| 1182 self._shared) | 1179 self._shared) |
| 1183 | 1180 |
| 1184 def _ImplFileEmitter(self, name): | 1181 def _ImplFileEmitter(self, name): |
| 1185 """Returns the file emitter of the Dartium implementation file.""" | 1182 """Returns the file emitter of the Dartium implementation file.""" |
| 1186 path = os.path.join(self._output_dir, 'html', 'dartium', '%s.dart' % name) | 1183 path = os.path.join(self._output_dir, 'html', 'dartium', '%s.dart' % name) |
| 1187 self._dart_dartium_file_paths.append(path) | 1184 self._dart_dartium_file_paths.append(path) |
| 1188 return self._emitters.FileEmitter(path); | 1185 return self._emitters.FileEmitter(path); |
| 1189 | 1186 |
| 1190 def ProcessCallback(self, interface, info): | 1187 def ProcessCallback(self, interface, info): |
| 1191 pass | 1188 pass |
| 1192 | 1189 |
| 1193 def GenerateLibraries(self, lib_dir): | 1190 def GenerateLibraries(self): |
| 1194 # Library generated for implementation. | 1191 # Library generated for implementation. |
| 1195 auxiliary_dir = os.path.relpath(self._auxiliary_dir, self._output_dir) | 1192 auxiliary_dir = os.path.relpath(self._auxiliary_dir, self._output_dir) |
| 1196 | 1193 |
| 1197 self._GenerateLibFile( | 1194 self._GenerateLibFile( |
| 1198 'html_dartium.darttemplate', | 1195 'html_dartium.darttemplate', |
| 1199 os.path.join(lib_dir, 'html_dartium.dart'), | 1196 os.path.join(self._output_dir, 'html_dartium.dart'), |
| 1200 (self._interface_system._dart_interface_file_paths + | 1197 (self._interface_system._dart_interface_file_paths + |
| 1201 self._interface_system._dart_callback_file_paths + | 1198 self._interface_system._dart_callback_file_paths + |
| 1202 self._dart_dartium_file_paths | 1199 self._dart_dartium_file_paths |
| 1203 ), | 1200 ), |
| 1204 AUXILIARY_DIR=MassagePath(auxiliary_dir), | 1201 AUXILIARY_DIR=MassagePath(auxiliary_dir), |
| 1205 WRAPCASES='\n'.join(self._wrap_cases)) | 1202 WRAPCASES='\n'.join(self._wrap_cases)) |
| 1206 | 1203 |
| 1207 def Finish(self): | 1204 def Finish(self): |
| 1208 pass | 1205 pass |
| 1209 | 1206 |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1543 self._members_emitter.Emit( | 1540 self._members_emitter.Emit( |
| 1544 '\n' | 1541 '\n' |
| 1545 ' $TYPE $HTML_NAME($PARAMS) => $FUNCTION_CALL;\n', | 1542 ' $TYPE $HTML_NAME($PARAMS) => $FUNCTION_CALL;\n', |
| 1546 TYPE=info.type_name, | 1543 TYPE=info.type_name, |
| 1547 HTML_NAME=html_name, | 1544 HTML_NAME=html_name, |
| 1548 PARAMS=info.ParametersImplementationDeclaration(), | 1545 PARAMS=info.ParametersImplementationDeclaration(), |
| 1549 FUNCTION_CALL=function_call) | 1546 FUNCTION_CALL=function_call) |
| 1550 | 1547 |
| 1551 def AddStaticOperation(self, info): | 1548 def AddStaticOperation(self, info): |
| 1552 pass | 1549 pass |
| 1550 |
| 1551 def _ComputeInheritanceClosure(database): |
| 1552 def Collect(interface, seen, collected): |
| 1553 name = interface.id |
| 1554 if '<' in name: |
| 1555 # TODO(sra): Handle parameterized types. |
| 1556 return |
| 1557 if not name in seen: |
| 1558 seen.add(name) |
| 1559 collected.append(name) |
| 1560 for parent in interface.parents: |
| 1561 # TODO(sra): Handle parameterized types. |
| 1562 if not '<' in parent.type.id: |
| 1563 if database.HasInterface(parent.type.id): |
| 1564 Collect(database.GetInterface(parent.type.id), |
| 1565 seen, collected) |
| 1566 |
| 1567 inheritance_closure = {} |
| 1568 for interface in database.GetInterfaces(): |
| 1569 seen = set() |
| 1570 collected = [] |
| 1571 Collect(interface, seen, collected) |
| 1572 inheritance_closure[interface.id] = collected |
| 1573 return inheritance_closure |
| OLD | NEW |