| 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 import emitter | 9 import emitter |
| 10 | 10 |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 if events or interface.id in _html_explicit_event_classes: | 431 if events or interface.id in _html_explicit_event_classes: |
| 432 return True, events | 432 return True, events |
| 433 else: | 433 else: |
| 434 return False, None | 434 return False, None |
| 435 | 435 |
| 436 def IsPrivate(self, name): | 436 def IsPrivate(self, name): |
| 437 return name.startswith('_') | 437 return name.startswith('_') |
| 438 | 438 |
| 439 | 439 |
| 440 class HtmlInterfacesSystem(System): | 440 class HtmlInterfacesSystem(System): |
| 441 def __init__(self, options, dart_library_generator, backend): | 441 def __init__(self, options, dart_library_generator, backend_factory): |
| 442 super(HtmlInterfacesSystem, self).__init__(options) | 442 super(HtmlInterfacesSystem, self).__init__(options) |
| 443 self._dart_library_generator = dart_library_generator | 443 self._dart_library_generator = dart_library_generator |
| 444 self._backend = backend | 444 self._backend_factory = backend_factory |
| 445 self._shared = HtmlSystemShared(options) | 445 self._shared = HtmlSystemShared(options) |
| 446 self._dart_file_paths = [] | |
| 447 self._elements_factory_emitter = None | 446 self._elements_factory_emitter = None |
| 448 | 447 |
| 449 def ProcessInterface(self, interface): | 448 def ProcessInterface(self, interface): |
| 450 HtmlDartInterfaceGenerator(self, interface).Generate() | 449 backend = self._backend_factory(interface) |
| 451 | 450 HtmlDartInterfaceGenerator(self, interface, backend).Generate() |
| 452 def ProcessCallback(self, interface, info): | |
| 453 """Generates a typedef for the callback interface.""" | |
| 454 code = self._CreateEmitter('%s.dart' % interface.id) | |
| 455 code.Emit(self._templates.Load('callback.darttemplate')) | |
| 456 code.Emit('typedef $TYPE $NAME($PARAMS);\n', | |
| 457 NAME=interface.id, | |
| 458 TYPE=DartType(info.type_name), | |
| 459 PARAMS=info.ParametersImplementationDeclaration(DartType)) | |
| 460 self._backend.ProcessCallback(interface, info) | |
| 461 | 451 |
| 462 def _CreateEmitter(self, filename): | 452 def _CreateEmitter(self, filename): |
| 463 return self._dart_library_generator.CreateFileEmitter(filename) | 453 return self._dart_library_generator.CreateFileEmitter(filename) |
| 464 | 454 |
| 465 # ------------------------------------------------------------------------------ | 455 # ------------------------------------------------------------------------------ |
| 466 | 456 |
| 467 class HtmlDartInterfaceGenerator(BaseGenerator): | 457 class HtmlDartInterfaceGenerator(BaseGenerator): |
| 468 """Generates dart interface and implementation for the DOM IDL interface.""" | 458 """Generates dart interface and implementation for the DOM IDL interface.""" |
| 469 | 459 |
| 470 def __init__(self, system, interface): | 460 def __init__(self, system, interface, backend): |
| 471 super(HtmlDartInterfaceGenerator, self).__init__( | 461 super(HtmlDartInterfaceGenerator, self).__init__( |
| 472 system._database, interface) | 462 system._database, system._type_registry, interface) |
| 473 self._system = system | 463 self._system = system |
| 464 self._backend = backend |
| 474 self._shared = system._shared | 465 self._shared = system._shared |
| 475 self._html_interface_name = system._renamer.RenameInterface(self._interface) | 466 self._html_interface_name = system._renamer.RenameInterface(self._interface) |
| 476 self._backend = system._backend.ImplementationGenerator(self._interface) | 467 |
| 468 def GenerateCallback(self, info): |
| 469 """Generates a typedef for the callback interface.""" |
| 470 code = self._system._CreateEmitter('%s.dart' % self._interface.id) |
| 471 code.Emit(self._system._templates.Load('callback.darttemplate')) |
| 472 code.Emit('typedef $TYPE $NAME($PARAMS);\n', |
| 473 NAME=self._interface.id, |
| 474 TYPE=DartType(info.type_name), |
| 475 PARAMS=info.ParametersImplementationDeclaration(DartType)) |
| 476 self._backend.GenerateCallback(info) |
| 477 | 477 |
| 478 def StartInterface(self): | 478 def StartInterface(self): |
| 479 if not self._interface.id in _merged_html_interfaces: | 479 if not self._interface.id in _merged_html_interfaces: |
| 480 path = '%s.dart' % self._html_interface_name | 480 path = '%s.dart' % self._html_interface_name |
| 481 self._interface_emitter = self._system._CreateEmitter(path) | 481 self._interface_emitter = self._system._CreateEmitter(path) |
| 482 else: | 482 else: |
| 483 self._interface_emitter = emitter.Emitter() | 483 self._interface_emitter = emitter.Emitter() |
| 484 | 484 |
| 485 template_file = 'interface_%s.darttemplate' % self._html_interface_name | 485 template_file = 'interface_%s.darttemplate' % self._html_interface_name |
| 486 interface_template = (self._system._templates.TryLoad(template_file) or | 486 interface_template = (self._system._templates.TryLoad(template_file) or |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 753 pass | 753 pass |
| 754 | 754 |
| 755 def AddOperation(self, info, html_name): | 755 def AddOperation(self, info, html_name): |
| 756 pass | 756 pass |
| 757 | 757 |
| 758 | 758 |
| 759 # ------------------------------------------------------------------------------ | 759 # ------------------------------------------------------------------------------ |
| 760 | 760 |
| 761 # TODO(jmesserly): inheritance is probably not the right way to factor this long | 761 # TODO(jmesserly): inheritance is probably not the right way to factor this long |
| 762 # term, but it makes merging better for now. | 762 # term, but it makes merging better for now. |
| 763 class HtmlDart2JSClassGenerator(Dart2JSInterfaceGenerator): | 763 class Dart2JSBackend(Dart2JSInterfaceGenerator): |
| 764 """Generates a dart2js class for the dart:html library from a DOM IDL | 764 """Generates a dart2js class for the dart:html library from a DOM IDL |
| 765 interface. | 765 interface. |
| 766 """ | 766 """ |
| 767 | 767 |
| 768 def __init__(self, system, interface): | 768 def __init__(self, interface, options): |
| 769 super(HtmlDart2JSClassGenerator, self).__init__( | 769 super(Dart2JSBackend, self).__init__(options, interface, None, None) |
| 770 system, interface, None, None) | 770 self._html_interface_name = options.renamer.RenameInterface(self._interface) |
| 771 self._html_interface_name = system._renamer.RenameInterface(self._interface) | |
| 772 | 771 |
| 773 def HasImplementation(self): | 772 def HasImplementation(self): |
| 774 return not (IsPureInterface(self._interface.id) or | 773 return not (IsPureInterface(self._interface.id) or |
| 775 self._interface.id in _merged_html_interfaces) | 774 self._interface.id in _merged_html_interfaces) |
| 776 | 775 |
| 777 def ImplementationClassName(self): | 776 def ImplementationClassName(self): |
| 778 return self._ImplClassName(self._html_interface_name) | 777 return self._ImplClassName(self._html_interface_name) |
| 779 | 778 |
| 780 def SetImplementationEmitter(self, implementation_emitter): | 779 def SetImplementationEmitter(self, implementation_emitter): |
| 781 self._dart_code = implementation_emitter | 780 self._dart_code = implementation_emitter |
| (...skipping 28 matching lines...) Expand all Loading... |
| 810 # TODO: Include all implemented interfaces, including other Lists. | 809 # TODO: Include all implemented interfaces, including other Lists. |
| 811 implements = [self._html_interface_name] | 810 implements = [self._html_interface_name] |
| 812 element_type = MaybeTypedArrayElementType(self._interface) | 811 element_type = MaybeTypedArrayElementType(self._interface) |
| 813 if element_type: | 812 if element_type: |
| 814 implements.append('List<%s>' % self._DartType(element_type)) | 813 implements.append('List<%s>' % self._DartType(element_type)) |
| 815 | 814 |
| 816 if self._HasJavaScriptIndexingBehaviour(): | 815 if self._HasJavaScriptIndexingBehaviour(): |
| 817 implements.append('JavaScriptIndexingBehavior') | 816 implements.append('JavaScriptIndexingBehavior') |
| 818 | 817 |
| 819 template_file = 'impl_%s.darttemplate' % self._html_interface_name | 818 template_file = 'impl_%s.darttemplate' % self._html_interface_name |
| 820 template = (self._system._templates.TryLoad(template_file) or | 819 template = (self._template_loader.TryLoad(template_file) or |
| 821 self._system._templates.Load('dart2js_impl.darttemplate')) | 820 self._template_loader.Load('dart2js_impl.darttemplate')) |
| 822 self._members_emitter = self._dart_code.Emit( | 821 self._members_emitter = self._dart_code.Emit( |
| 823 template, | 822 template, |
| 824 #class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | 823 #class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 825 #$!MEMBERS | 824 #$!MEMBERS |
| 826 #} | 825 #} |
| 827 CLASSNAME=self._class_name, | 826 CLASSNAME=self._class_name, |
| 828 EXTENDS=extends, | 827 EXTENDS=extends, |
| 829 IMPLEMENTS=' implements ' + ', '.join(implements), | 828 IMPLEMENTS=' implements ' + ', '.join(implements), |
| 830 NATIVESPEC=' native "' + native_spec + '"') | 829 NATIVESPEC=' native "' + native_spec + '"') |
| 831 if self._members_emitter == None: | 830 if self._members_emitter == None: |
| 832 raise Exception("Class %s doesn't use the $!MEMBERS variable" % | 831 raise Exception("Class %s doesn't use the $!MEMBERS variable" % |
| 833 self._class_name) | 832 self._class_name) |
| 834 | 833 |
| 835 return self._members_emitter | 834 return self._members_emitter |
| 836 | 835 |
| 837 def EmitFactoryProvider(self, constructor_info, factory_provider, emitter): | 836 def EmitFactoryProvider(self, constructor_info, factory_provider, emitter): |
| 838 template_file = ('factoryprovider_%s.darttemplate' % | 837 template_file = ('factoryprovider_%s.darttemplate' % |
| 839 self._html_interface_name) | 838 self._html_interface_name) |
| 840 template = self._system._templates.TryLoad(template_file) | 839 template = self._template_loader.TryLoad(template_file) |
| 841 if not template: | 840 if not template: |
| 842 template = self._system._templates.Load('factoryprovider.darttemplate') | 841 template = self._template_loader.Load('factoryprovider.darttemplate') |
| 843 | 842 |
| 844 emitter.Emit( | 843 emitter.Emit( |
| 845 template, | 844 template, |
| 846 FACTORYPROVIDER=factory_provider, | 845 FACTORYPROVIDER=factory_provider, |
| 847 CONSTRUCTOR=self._html_interface_name, | 846 CONSTRUCTOR=self._html_interface_name, |
| 848 PARAMETERS=constructor_info.ParametersImplementationDeclaration(self._Da
rtType), | 847 PARAMETERS=constructor_info.ParametersImplementationDeclaration(self._Da
rtType), |
| 849 NAMED_CONSTRUCTOR=constructor_info.name or self._html_interface_name, | 848 NAMED_CONSTRUCTOR=constructor_info.name or self._html_interface_name, |
| 850 ARGUMENTS=constructor_info.ParametersAsArgumentList()) | 849 ARGUMENTS=constructor_info.ParametersAsArgumentList()) |
| 851 | 850 |
| 852 def AddIndexer(self, element_type): | 851 def AddIndexer(self, element_type): |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 887 '\n' | 886 '\n' |
| 888 ' void operator[]=(int index, $TYPE value) {\n' | 887 ' void operator[]=(int index, $TYPE value) {\n' |
| 889 ' throw new UnsupportedOperationException("Cannot assign element
of immutable List.");\n' | 888 ' throw new UnsupportedOperationException("Cannot assign element
of immutable List.");\n' |
| 890 ' }\n', | 889 ' }\n', |
| 891 TYPE=self._NarrowInputType(element_type)) | 890 TYPE=self._NarrowInputType(element_type)) |
| 892 | 891 |
| 893 # TODO(sra): Use separate mixins for mutable implementations of List<T>. | 892 # TODO(sra): Use separate mixins for mutable implementations of List<T>. |
| 894 # TODO(sra): Use separate mixins for typed array implementations of List<T>. | 893 # TODO(sra): Use separate mixins for typed array implementations of List<T>. |
| 895 if self._interface.id != 'NodeList': | 894 if self._interface.id != 'NodeList': |
| 896 template_file = 'immutable_list_mixin.darttemplate' | 895 template_file = 'immutable_list_mixin.darttemplate' |
| 897 template = self._system._templates.Load(template_file) | 896 template = self._template_loader.Load(template_file) |
| 898 self._members_emitter.Emit(template, E=self._DartType(element_type)) | 897 self._members_emitter.Emit(template, E=self._DartType(element_type)) |
| 899 | 898 |
| 900 def AddAttribute(self, attribute, html_name, read_only): | 899 def AddAttribute(self, attribute, html_name, read_only): |
| 901 if self._HasCustomImplementation(attribute.id): | 900 if self._HasCustomImplementation(attribute.id): |
| 902 return | 901 return |
| 903 | 902 |
| 904 if attribute.id != html_name: | 903 if attribute.id != html_name: |
| 905 self._AddAttributeUsingProperties(attribute, html_name, read_only) | 904 self._AddAttributeUsingProperties(attribute, html_name, read_only) |
| 906 return | 905 return |
| 907 | 906 |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1210 member_name = '%s.%s' % (self._html_interface_name, member_name) | 1209 member_name = '%s.%s' % (self._html_interface_name, member_name) |
| 1211 return member_name in _js_custom_members | 1210 return member_name in _js_custom_members |
| 1212 | 1211 |
| 1213 def _HasJavaScriptIndexingBehaviour(self): | 1212 def _HasJavaScriptIndexingBehaviour(self): |
| 1214 """Returns True if the native object has an indexer and length property.""" | 1213 """Returns True if the native object has an indexer and length property.""" |
| 1215 (element_type, requires_indexer) = ListImplementationInfo( | 1214 (element_type, requires_indexer) = ListImplementationInfo( |
| 1216 self._interface, self._database) | 1215 self._interface, self._database) |
| 1217 if element_type and requires_indexer: return True | 1216 if element_type and requires_indexer: return True |
| 1218 return False | 1217 return False |
| 1219 | 1218 |
| 1219 |
| 1220 # ------------------------------------------------------------------------------ | 1220 # ------------------------------------------------------------------------------ |
| 1221 | 1221 |
| 1222 class HtmlDart2JSSystem(System): | |
| 1223 | |
| 1224 def __init__(self, options): | |
| 1225 super(HtmlDart2JSSystem, self).__init__(options) | |
| 1226 | |
| 1227 def ImplementationGenerator(self, interface): | |
| 1228 return HtmlDart2JSClassGenerator(self, interface) | |
| 1229 | |
| 1230 | |
| 1231 class DartLibraryEmitter(): | 1222 class DartLibraryEmitter(): |
| 1232 def __init__(self, emitters, template, dart_sources_dir): | 1223 def __init__(self, emitters, template, dart_sources_dir): |
| 1233 self._emitters = emitters | 1224 self._emitters = emitters |
| 1234 self._template = template | 1225 self._template = template |
| 1235 self._dart_sources_dir = dart_sources_dir | 1226 self._dart_sources_dir = dart_sources_dir |
| 1236 self._dart_sources_list = [] | 1227 self._dart_sources_list = [] |
| 1237 | 1228 |
| 1238 def CreateFileEmitter(self, filename): | 1229 def CreateFileEmitter(self, filename): |
| 1239 path = os.path.join(self._dart_sources_dir, filename) | 1230 path = os.path.join(self._dart_sources_dir, filename) |
| 1240 self._dart_sources_list.append(path) | 1231 self._dart_sources_list.append(path) |
| 1241 return self._emitters.FileEmitter(path) | 1232 return self._emitters.FileEmitter(path) |
| 1242 | 1233 |
| 1243 def EmitLibrary(self, library_file_path, auxiliary_dir): | 1234 def EmitLibrary(self, library_file_path, auxiliary_dir): |
| 1244 def massage_path(path): | 1235 def massage_path(path): |
| 1245 # The most robust way to emit path separators is to use / always. | 1236 # The most robust way to emit path separators is to use / always. |
| 1246 return path.replace('\\', '/') | 1237 return path.replace('\\', '/') |
| 1247 | 1238 |
| 1248 library_emitter = self._emitters.FileEmitter(library_file_path) | 1239 library_emitter = self._emitters.FileEmitter(library_file_path) |
| 1249 library_file_dir = os.path.dirname(library_file_path) | 1240 library_file_dir = os.path.dirname(library_file_path) |
| 1250 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir) | 1241 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir) |
| 1251 imports_emitter = library_emitter.Emit( | 1242 imports_emitter = library_emitter.Emit( |
| 1252 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir)) | 1243 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir)) |
| 1253 for path in sorted(self._dart_sources_list): | 1244 for path in sorted(self._dart_sources_list): |
| 1254 relpath = os.path.relpath(path, library_file_dir) | 1245 relpath = os.path.relpath(path, library_file_dir) |
| 1255 imports_emitter.Emit( | 1246 imports_emitter.Emit( |
| 1256 "#source('$PATH');\n", PATH=massage_path(relpath)) | 1247 "#source('$PATH');\n", PATH=massage_path(relpath)) |
| OLD | NEW |