| 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 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 584 (self._common_prefix, DartType(parent.type.id))) | 584 (self._common_prefix, DartType(parent.type.id))) |
| 585 | 585 |
| 586 comment = ' extends' | 586 comment = ' extends' |
| 587 extends_str = '' | 587 extends_str = '' |
| 588 if extends: | 588 if extends: |
| 589 extends_str += ' extends ' + ', '.join(extends) | 589 extends_str += ' extends ' + ', '.join(extends) |
| 590 comment = ',' | 590 comment = ',' |
| 591 if suppressed_extends: | 591 if suppressed_extends: |
| 592 extends_str += ' /*%s %s */' % (comment, ', '.join(suppressed_extends)) | 592 extends_str += ' /*%s %s */' % (comment, ', '.join(suppressed_extends)) |
| 593 | 593 |
| 594 factory_provider = None |
| 595 constructor_info = AnalyzeConstructor(self._interface) |
| 596 if constructor_info: |
| 597 factory_provider = '_' + typename + 'FactoryProvider'; |
| 598 |
| 594 if typename in interface_factories: | 599 if typename in interface_factories: |
| 595 extends_str += ' default ' + interface_factories[typename] | 600 factory_provider = interface_factories[typename] |
| 601 |
| 602 if factory_provider: |
| 603 extends_str += ' default ' + factory_provider |
| 596 | 604 |
| 597 # TODO(vsm): Add appropriate package / namespace syntax. | 605 # TODO(vsm): Add appropriate package / namespace syntax. |
| 598 (self._members_emitter, | 606 (self._members_emitter, |
| 599 self._top_level_emitter) = self._emitter.Emit( | 607 self._top_level_emitter) = self._emitter.Emit( |
| 600 self._template + '$!TOP_LEVEL', | 608 self._template + '$!TOP_LEVEL', |
| 601 ID=typename, | 609 ID=typename, |
| 602 EXTENDS=extends_str) | 610 EXTENDS=extends_str) |
| 603 | 611 |
| 612 if constructor_info: |
| 613 self._members_emitter.Emit( |
| 614 '\n' |
| 615 ' $CTOR($PARAMS);\n', |
| 616 CTOR=typename, |
| 617 PARAMS=constructor_info.ParametersInterfaceDeclaration()); |
| 618 |
| 604 element_type = MaybeTypedArrayElementType(self._interface) | 619 element_type = MaybeTypedArrayElementType(self._interface) |
| 605 if element_type: | 620 if element_type: |
| 606 self._members_emitter.Emit( | 621 self._members_emitter.Emit( |
| 607 '\n' | 622 '\n' |
| 608 ' $CTOR(int length);\n' | 623 ' $CTOR(int length);\n' |
| 609 '\n' | 624 '\n' |
| 610 ' $CTOR.fromList(List<$TYPE> list);\n' | 625 ' $CTOR.fromList(List<$TYPE> list);\n' |
| 611 '\n' | 626 '\n' |
| 612 ' $CTOR.fromBuffer(ArrayBuffer buffer);\n', | 627 ' $CTOR.fromBuffer(ArrayBuffer buffer);\n', |
| 613 CTOR=self._interface.id, | 628 CTOR=self._interface.id, |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 731 #$!MEMBERS | 746 #$!MEMBERS |
| 732 #} | 747 #} |
| 733 CLASSNAME=self._class_name, | 748 CLASSNAME=self._class_name, |
| 734 EXTENDS=extends, | 749 EXTENDS=extends, |
| 735 IMPLEMENTS=' implements ' + ', '.join(implements), | 750 IMPLEMENTS=' implements ' + ', '.join(implements), |
| 736 NATIVESPEC=' native "' + native_spec + '"') | 751 NATIVESPEC=' native "' + native_spec + '"') |
| 737 | 752 |
| 738 if element_type: | 753 if element_type: |
| 739 self.AddTypedArrayConstructors(element_type) | 754 self.AddTypedArrayConstructors(element_type) |
| 740 | 755 |
| 756 # Emit a factory provider class for the constructor. |
| 757 constructor_info = AnalyzeConstructor(interface) |
| 758 if constructor_info: |
| 759 self._EmitFactoryProvider(interface_name, constructor_info) |
| 760 |
| 761 def _EmitFactoryProvider(self, interface_name, constructor_info): |
| 762 template_file = 'factoryprovider_%s.darttemplate' % interface_name |
| 763 template = self._system._templates.TryLoad(template_file) |
| 764 if not template: |
| 765 template = self._system._templates.Load('factoryprovider.darttemplate') |
| 766 |
| 767 factory_provider = '_' + interface_name + 'FactoryProvider' |
| 768 emitter = self._system._ImplFileEmitter(factory_provider) |
| 769 emitter.Emit( |
| 770 template, |
| 771 FACTORYPROVIDER=factory_provider, |
| 772 CONSTRUCTOR=interface_name, |
| 773 PARAMETERS=constructor_info.ParametersImplementationDeclaration(), |
| 774 NAMED_CONSTRUCTOR=constructor_info.name or interface_name, |
| 775 ARGUMENTS=constructor_info.ParametersAsArgumentList()) |
| 776 |
| 741 def AddIndexer(self, element_type): | 777 def AddIndexer(self, element_type): |
| 742 """Adds all the methods required to complete implementation of List.""" | 778 """Adds all the methods required to complete implementation of List.""" |
| 743 # We would like to simply inherit the implementation of everything except | 779 # We would like to simply inherit the implementation of everything except |
| 744 # get length(), [], and maybe []=. It is possible to extend from a base | 780 # get length(), [], and maybe []=. It is possible to extend from a base |
| 745 # array implementation class only when there is no other implementation | 781 # array implementation class only when there is no other implementation |
| 746 # inheritance. There might be no implementation inheritance other than | 782 # inheritance. There might be no implementation inheritance other than |
| 747 # DOMBaseWrapper for many classes, but there might be some where the | 783 # DOMBaseWrapper for many classes, but there might be some where the |
| 748 # array-ness is introduced by a non-root interface: | 784 # array-ness is introduced by a non-root interface: |
| 749 # | 785 # |
| 750 # interface Y extends X, List<T> ... | 786 # interface Y extends X, List<T> ... |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 993 templates, database, emitters, output_dir, generator) | 1029 templates, database, emitters, output_dir, generator) |
| 994 self._dart_frog_file_paths = [] | 1030 self._dart_frog_file_paths = [] |
| 995 | 1031 |
| 996 | 1032 |
| 997 def InterfaceGenerator(self, | 1033 def InterfaceGenerator(self, |
| 998 interface, | 1034 interface, |
| 999 common_prefix, | 1035 common_prefix, |
| 1000 super_interface_name, | 1036 super_interface_name, |
| 1001 source_filter): | 1037 source_filter): |
| 1002 """.""" | 1038 """.""" |
| 1003 dart_frog_file_path = self._FilePathForFrogImpl(interface.id) | |
| 1004 self._dart_frog_file_paths.append(dart_frog_file_path) | |
| 1005 | |
| 1006 template_file = 'impl_%s.darttemplate' % interface.id | 1039 template_file = 'impl_%s.darttemplate' % interface.id |
| 1007 template = self._templates.TryLoad(template_file) | 1040 template = self._templates.TryLoad(template_file) |
| 1008 if not template: | 1041 if not template: |
| 1009 template = self._templates.Load('frog_impl.darttemplate') | 1042 template = self._templates.Load('frog_impl.darttemplate') |
| 1010 | 1043 |
| 1011 dart_code = self._emitters.FileEmitter(dart_frog_file_path) | 1044 dart_code = self._ImplFileEmitter(interface.id) |
| 1012 return HtmlFrogClassGenerator(self, interface, template, | 1045 return HtmlFrogClassGenerator(self, interface, template, |
| 1013 super_interface_name, dart_code, self._shared) | 1046 super_interface_name, dart_code, self._shared) |
| 1014 | 1047 |
| 1015 def GenerateLibraries(self, lib_dir): | 1048 def GenerateLibraries(self, lib_dir): |
| 1016 self._GenerateLibFile( | 1049 self._GenerateLibFile( |
| 1017 'html_frog.darttemplate', | 1050 'html_frog.darttemplate', |
| 1018 os.path.join(lib_dir, 'html_frog.dart'), | 1051 os.path.join(lib_dir, 'html_frog.dart'), |
| 1019 (self._interface_system._dart_interface_file_paths + | 1052 (self._interface_system._dart_interface_file_paths + |
| 1020 self._interface_system._dart_callback_file_paths + | 1053 self._interface_system._dart_callback_file_paths + |
| 1021 self._dart_frog_file_paths)) | 1054 self._dart_frog_file_paths)) |
| 1022 | 1055 |
| 1023 def Finish(self): | 1056 def Finish(self): |
| 1024 pass | 1057 pass |
| 1025 | 1058 |
| 1026 def _FilePathForFrogImpl(self, interface_name): | 1059 def _ImplFileEmitter(self, name): |
| 1027 """Returns the file path of the Frog implementation.""" | 1060 """Returns the file emitter of the Frog implementation file.""" |
| 1028 # TODO(jmesserly): is this the right path | 1061 # TODO(jmesserly): is this the right path |
| 1029 return os.path.join(self._output_dir, 'html', 'frog', | 1062 path = os.path.join(self._output_dir, 'html', 'frog', '%s.dart' % name) |
| 1030 '%s.dart' % interface_name) | 1063 self._dart_frog_file_paths.append(path) |
| 1064 return self._emitters.FileEmitter(path) |
| 1031 | 1065 |
| 1032 # ----------------------------------------------------------------------------- | 1066 # ----------------------------------------------------------------------------- |
| 1033 | 1067 |
| 1034 class HtmlDartiumSystem(HtmlSystem): | 1068 class HtmlDartiumSystem(HtmlSystem): |
| 1035 | 1069 |
| 1036 def __init__(self, templates, database, emitters, output_dir, generator): | 1070 def __init__(self, templates, database, emitters, output_dir, generator): |
| 1037 """Prepared for generating wrapping implementation. | 1071 """Prepared for generating wrapping implementation. |
| 1038 | 1072 |
| 1039 - Creates emitter for Dart code. | 1073 - Creates emitter for Dart code. |
| 1040 """ | 1074 """ |
| 1041 super(HtmlDartiumSystem, self).__init__( | 1075 super(HtmlDartiumSystem, self).__init__( |
| 1042 templates, database, emitters, output_dir, generator) | 1076 templates, database, emitters, output_dir, generator) |
| 1043 self._shared = HtmlSystemShared(database, generator) | 1077 self._shared = HtmlSystemShared(database, generator) |
| 1044 self._dart_dartium_file_paths = [] | 1078 self._dart_dartium_file_paths = [] |
| 1045 self._wrap_cases = [] | 1079 self._wrap_cases = [] |
| 1046 | 1080 |
| 1047 def InterfaceGenerator(self, | 1081 def InterfaceGenerator(self, |
| 1048 interface, | 1082 interface, |
| 1049 common_prefix, | 1083 common_prefix, |
| 1050 super_interface_name, | 1084 super_interface_name, |
| 1051 source_filter): | 1085 source_filter): |
| 1052 """.""" | 1086 """.""" |
| 1053 dart_dartium_file_path = self._FilePathForImpl(interface.id) | |
| 1054 self._dart_dartium_file_paths.append(dart_dartium_file_path) | |
| 1055 | |
| 1056 template_file = 'impl_%s.darttemplate' % interface.id | 1087 template_file = 'impl_%s.darttemplate' % interface.id |
| 1057 template = self._templates.TryLoad(template_file) | 1088 template = self._templates.TryLoad(template_file) |
| 1058 # TODO(jacobr): change this name as it is confusing. | 1089 # TODO(jacobr): change this name as it is confusing. |
| 1059 if not template: | 1090 if not template: |
| 1060 template = self._templates.Load('frog_impl.darttemplate') | 1091 template = self._templates.Load('frog_impl.darttemplate') |
| 1061 | 1092 |
| 1062 dart_code = self._emitters.FileEmitter(dart_dartium_file_path) | 1093 dart_code = self._ImplFileEmitter(interface.id) |
| 1063 return HtmlDartiumInterfaceGenerator(self, interface, template, | 1094 return HtmlDartiumInterfaceGenerator(self, interface, template, |
| 1064 super_interface_name, dart_code, self._BaseDefines(interface), | 1095 super_interface_name, dart_code, self._BaseDefines(interface), |
| 1065 self._shared) | 1096 self._shared) |
| 1066 | 1097 |
| 1067 def _FilePathForImpl(self, interface_name): | 1098 def _ImplFileEmitter(self, name): |
| 1068 """Returns the file path of the Frog implementation.""" | 1099 """Returns the file emitter of the Dartium implementation file.""" |
| 1069 # TODO(jmesserly): is this the right path | 1100 path = os.path.join(self._output_dir, 'html', 'dartium', '%s.dart' % name) |
| 1070 return os.path.join(self._output_dir, 'html', 'dartium', | 1101 self._dart_dartium_file_paths.append(path) |
| 1071 '%s.dart' % interface_name) | 1102 return self._emitters.FileEmitter(path); |
| 1072 | 1103 |
| 1073 def ProcessCallback(self, interface, info): | 1104 def ProcessCallback(self, interface, info): |
| 1074 pass | 1105 pass |
| 1075 | 1106 |
| 1076 def GenerateLibraries(self, lib_dir): | 1107 def GenerateLibraries(self, lib_dir): |
| 1077 # Library generated for implementation. | 1108 # Library generated for implementation. |
| 1078 self._GenerateLibFile( | 1109 self._GenerateLibFile( |
| 1079 'html_dartium.darttemplate', | 1110 'html_dartium.darttemplate', |
| 1080 os.path.join(lib_dir, 'html_dartium.dart'), | 1111 os.path.join(lib_dir, 'html_dartium.dart'), |
| 1081 (self._interface_system._dart_interface_file_paths + | 1112 (self._interface_system._dart_interface_file_paths + |
| 1082 self._interface_system._dart_callback_file_paths + | 1113 self._interface_system._dart_callback_file_paths + |
| 1083 # FIXME: Move the implementation to a separate library. | |
| 1084 self._dart_dartium_file_paths | 1114 self._dart_dartium_file_paths |
| 1085 ), | 1115 ), |
| 1086 WRAPCASES='\n'.join(self._wrap_cases)) | 1116 WRAPCASES='\n'.join(self._wrap_cases)) |
| 1087 | 1117 |
| 1088 def Finish(self): | 1118 def Finish(self): |
| 1089 pass | 1119 pass |
| 1090 | 1120 |
| 1091 # ------------------------------------------------------------------------------ | 1121 # ------------------------------------------------------------------------------ |
| 1092 | 1122 |
| 1093 # TODO(jacobr): there is far too much duplicated code between these bindings | 1123 # TODO(jacobr): there is far too much duplicated code between these bindings |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1195 CLASSNAME=self._class_name, | 1225 CLASSNAME=self._class_name, |
| 1196 EXTENDS=extends, | 1226 EXTENDS=extends, |
| 1197 IMPLEMENTS=' implements ' + implements_str) | 1227 IMPLEMENTS=' implements ' + implements_str) |
| 1198 | 1228 |
| 1199 # Document requires a custom wrapper. | 1229 # Document requires a custom wrapper. |
| 1200 if dom_name != 'Document': | 1230 if dom_name != 'Document': |
| 1201 self._members_emitter.Emit( | 1231 self._members_emitter.Emit( |
| 1202 ' $(CLASSNAME)._wrap(ptr) : super._wrap(ptr);\n', | 1232 ' $(CLASSNAME)._wrap(ptr) : super._wrap(ptr);\n', |
| 1203 CLASSNAME=self._class_name) | 1233 CLASSNAME=self._class_name) |
| 1204 | 1234 |
| 1235 # Emit a factory provider class for the constructor. |
| 1236 constructor_info = AnalyzeConstructor(interface) |
| 1237 if constructor_info: |
| 1238 self._EmitFactoryProvider(interface_name, constructor_info) |
| 1239 |
| 1240 def _EmitFactoryProvider(self, interface_name, constructor_info): |
| 1241 template_file = 'factoryprovider_%s.darttemplate' % interface_name |
| 1242 template = self._system._templates.TryLoad(template_file) |
| 1243 if not template: |
| 1244 template = self._system._templates.Load('factoryprovider.darttemplate') |
| 1245 |
| 1246 factory_provider = '_' + interface_name + 'FactoryProvider' |
| 1247 emitter = self._system._ImplFileEmitter(factory_provider) |
| 1248 emitter.Emit( |
| 1249 template, |
| 1250 FACTORYPROVIDER=factory_provider, |
| 1251 CONSTRUCTOR=interface_name, |
| 1252 PARAMETERS=constructor_info.ParametersImplementationDeclaration(), |
| 1253 NAMED_CONSTRUCTOR=constructor_info.name or interface_name, |
| 1254 ARGUMENTS=constructor_info.ParametersAsArgumentList()) |
| 1255 |
| 1205 def _BaseClassName(self, interface): | 1256 def _BaseClassName(self, interface): |
| 1206 if not interface.parents: | 1257 if not interface.parents: |
| 1207 return '_DOMTypeBase' | 1258 return '_DOMTypeBase' |
| 1208 | 1259 |
| 1209 supertype = DartType(interface.parents[0].type.id) | 1260 supertype = DartType(interface.parents[0].type.id) |
| 1210 | 1261 |
| 1211 if IsDartListType(supertype) or IsDartCollectionType(supertype): | 1262 if IsDartListType(supertype) or IsDartCollectionType(supertype): |
| 1212 return 'DOMWrapperBase' | 1263 return 'DOMWrapperBase' |
| 1213 | 1264 |
| 1214 if supertype == 'EventTarget': | 1265 if supertype == 'EventTarget': |
| (...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1665 # dispatch has removed f(X), leaving only f(Y), but there is no guarantee | 1716 # dispatch has removed f(X), leaving only f(Y), but there is no guarantee |
| 1666 # that Y = Z-X, so we need to check for Y. | 1717 # that Y = Z-X, so we need to check for Y. |
| 1667 true_code = emitter.Emit( | 1718 true_code = emitter.Emit( |
| 1668 '$(INDENT)if ($COND) {\n' | 1719 '$(INDENT)if ($COND) {\n' |
| 1669 '$!TRUE' | 1720 '$!TRUE' |
| 1670 '$(INDENT)}\n', | 1721 '$(INDENT)}\n', |
| 1671 COND=test, INDENT=indent) | 1722 COND=test, INDENT=indent) |
| 1672 self.GenerateDispatch( | 1723 self.GenerateDispatch( |
| 1673 true_code, info, indent + ' ', position + 1, positive) | 1724 true_code, info, indent + ' ', position + 1, positive) |
| 1674 return True | 1725 return True |
| OLD | NEW |