Chromium Code Reviews| 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, create_generator_backend): |
| 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._create_generator_backend = create_generator_backend |
| 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._create_generator_backend(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 Dart2JSGeneratorBackend(Dart2JSInterfaceGenerator): |
|
Anton Muhin
2012/09/26 14:20:36
if you want to keep both generator and backend, I
podivilov
2012/09/26 15:02:04
Renamed to Dart2JSBackend. Interface vs. Implement
| |
| 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(Dart2JSGeneratorBackend, self).__init__( |
| 770 system, interface, None, None) | 770 options, interface, None, None) |
| 771 self._html_interface_name = system._renamer.RenameInterface(self._interface) | 771 self._html_interface_name = options.renamer.RenameInterface(self._interface) |
| 772 | 772 |
| 773 def HasImplementation(self): | 773 def HasImplementation(self): |
| 774 return not (IsPureInterface(self._interface.id) or | 774 return not (IsPureInterface(self._interface.id) or |
| 775 self._interface.id in _merged_html_interfaces) | 775 self._interface.id in _merged_html_interfaces) |
| 776 | 776 |
| 777 def ImplementationClassName(self): | 777 def ImplementationClassName(self): |
| 778 return self._ImplClassName(self._html_interface_name) | 778 return self._ImplClassName(self._html_interface_name) |
| 779 | 779 |
| 780 def SetImplementationEmitter(self, implementation_emitter): | 780 def SetImplementationEmitter(self, implementation_emitter): |
| 781 self._dart_code = implementation_emitter | 781 self._dart_code = implementation_emitter |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 810 # TODO: Include all implemented interfaces, including other Lists. | 810 # TODO: Include all implemented interfaces, including other Lists. |
| 811 implements = [self._html_interface_name] | 811 implements = [self._html_interface_name] |
| 812 element_type = MaybeTypedArrayElementType(self._interface) | 812 element_type = MaybeTypedArrayElementType(self._interface) |
| 813 if element_type: | 813 if element_type: |
| 814 implements.append('List<%s>' % self._DartType(element_type)) | 814 implements.append('List<%s>' % self._DartType(element_type)) |
| 815 | 815 |
| 816 if self._HasJavaScriptIndexingBehaviour(): | 816 if self._HasJavaScriptIndexingBehaviour(): |
| 817 implements.append('JavaScriptIndexingBehavior') | 817 implements.append('JavaScriptIndexingBehavior') |
| 818 | 818 |
| 819 template_file = 'impl_%s.darttemplate' % self._html_interface_name | 819 template_file = 'impl_%s.darttemplate' % self._html_interface_name |
| 820 template = (self._system._templates.TryLoad(template_file) or | 820 template = (self._template_loader.TryLoad(template_file) or |
| 821 self._system._templates.Load('dart2js_impl.darttemplate')) | 821 self._template_loader.Load('dart2js_impl.darttemplate')) |
| 822 self._members_emitter = self._dart_code.Emit( | 822 self._members_emitter = self._dart_code.Emit( |
| 823 template, | 823 template, |
| 824 #class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | 824 #class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 825 #$!MEMBERS | 825 #$!MEMBERS |
| 826 #} | 826 #} |
| 827 CLASSNAME=self._class_name, | 827 CLASSNAME=self._class_name, |
| 828 EXTENDS=extends, | 828 EXTENDS=extends, |
| 829 IMPLEMENTS=' implements ' + ', '.join(implements), | 829 IMPLEMENTS=' implements ' + ', '.join(implements), |
| 830 NATIVESPEC=' native "' + native_spec + '"') | 830 NATIVESPEC=' native "' + native_spec + '"') |
| 831 if self._members_emitter == None: | 831 if self._members_emitter == None: |
| 832 raise Exception("Class %s doesn't use the $!MEMBERS variable" % | 832 raise Exception("Class %s doesn't use the $!MEMBERS variable" % |
| 833 self._class_name) | 833 self._class_name) |
| 834 | 834 |
| 835 return self._members_emitter | 835 return self._members_emitter |
| 836 | 836 |
| 837 def EmitFactoryProvider(self, constructor_info, factory_provider, emitter): | 837 def EmitFactoryProvider(self, constructor_info, factory_provider, emitter): |
| 838 template_file = ('factoryprovider_%s.darttemplate' % | 838 template_file = ('factoryprovider_%s.darttemplate' % |
| 839 self._html_interface_name) | 839 self._html_interface_name) |
| 840 template = self._system._templates.TryLoad(template_file) | 840 template = self._template_loader.TryLoad(template_file) |
| 841 if not template: | 841 if not template: |
| 842 template = self._system._templates.Load('factoryprovider.darttemplate') | 842 template = self._template_loader.Load('factoryprovider.darttemplate') |
| 843 | 843 |
| 844 emitter.Emit( | 844 emitter.Emit( |
| 845 template, | 845 template, |
| 846 FACTORYPROVIDER=factory_provider, | 846 FACTORYPROVIDER=factory_provider, |
| 847 CONSTRUCTOR=self._html_interface_name, | 847 CONSTRUCTOR=self._html_interface_name, |
| 848 PARAMETERS=constructor_info.ParametersImplementationDeclaration(self._Da rtType), | 848 PARAMETERS=constructor_info.ParametersImplementationDeclaration(self._Da rtType), |
| 849 NAMED_CONSTRUCTOR=constructor_info.name or self._html_interface_name, | 849 NAMED_CONSTRUCTOR=constructor_info.name or self._html_interface_name, |
| 850 ARGUMENTS=constructor_info.ParametersAsArgumentList()) | 850 ARGUMENTS=constructor_info.ParametersAsArgumentList()) |
| 851 | 851 |
| 852 def AddIndexer(self, element_type): | 852 def AddIndexer(self, element_type): |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 887 '\n' | 887 '\n' |
| 888 ' void operator[]=(int index, $TYPE value) {\n' | 888 ' void operator[]=(int index, $TYPE value) {\n' |
| 889 ' throw new UnsupportedOperationException("Cannot assign element of immutable List.");\n' | 889 ' throw new UnsupportedOperationException("Cannot assign element of immutable List.");\n' |
| 890 ' }\n', | 890 ' }\n', |
| 891 TYPE=self._NarrowInputType(element_type)) | 891 TYPE=self._NarrowInputType(element_type)) |
| 892 | 892 |
| 893 # TODO(sra): Use separate mixins for mutable implementations of List<T>. | 893 # TODO(sra): Use separate mixins for mutable implementations of List<T>. |
| 894 # TODO(sra): Use separate mixins for typed array implementations of List<T>. | 894 # TODO(sra): Use separate mixins for typed array implementations of List<T>. |
| 895 if self._interface.id != 'NodeList': | 895 if self._interface.id != 'NodeList': |
| 896 template_file = 'immutable_list_mixin.darttemplate' | 896 template_file = 'immutable_list_mixin.darttemplate' |
| 897 template = self._system._templates.Load(template_file) | 897 template = self._template_loader.Load(template_file) |
| 898 self._members_emitter.Emit(template, E=self._DartType(element_type)) | 898 self._members_emitter.Emit(template, E=self._DartType(element_type)) |
| 899 | 899 |
| 900 def AddAttribute(self, attribute, html_name, read_only): | 900 def AddAttribute(self, attribute, html_name, read_only): |
| 901 if self._HasCustomImplementation(attribute.id): | 901 if self._HasCustomImplementation(attribute.id): |
| 902 return | 902 return |
| 903 | 903 |
| 904 if attribute.id != html_name: | 904 if attribute.id != html_name: |
| 905 self._AddAttributeUsingProperties(attribute, html_name, read_only) | 905 self._AddAttributeUsingProperties(attribute, html_name, read_only) |
| 906 return | 906 return |
| 907 | 907 |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1210 member_name = '%s.%s' % (self._html_interface_name, member_name) | 1210 member_name = '%s.%s' % (self._html_interface_name, member_name) |
| 1211 return member_name in _js_custom_members | 1211 return member_name in _js_custom_members |
| 1212 | 1212 |
| 1213 def _HasJavaScriptIndexingBehaviour(self): | 1213 def _HasJavaScriptIndexingBehaviour(self): |
| 1214 """Returns True if the native object has an indexer and length property.""" | 1214 """Returns True if the native object has an indexer and length property.""" |
| 1215 (element_type, requires_indexer) = ListImplementationInfo( | 1215 (element_type, requires_indexer) = ListImplementationInfo( |
| 1216 self._interface, self._database) | 1216 self._interface, self._database) |
| 1217 if element_type and requires_indexer: return True | 1217 if element_type and requires_indexer: return True |
| 1218 return False | 1218 return False |
| 1219 | 1219 |
| 1220 | |
| 1220 # ------------------------------------------------------------------------------ | 1221 # ------------------------------------------------------------------------------ |
| 1221 | 1222 |
| 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(): | 1223 class DartLibraryEmitter(): |
| 1232 def __init__(self, emitters, template, dart_sources_dir): | 1224 def __init__(self, emitters, template, dart_sources_dir): |
| 1233 self._emitters = emitters | 1225 self._emitters = emitters |
| 1234 self._template = template | 1226 self._template = template |
| 1235 self._dart_sources_dir = dart_sources_dir | 1227 self._dart_sources_dir = dart_sources_dir |
| 1236 self._dart_sources_list = [] | 1228 self._dart_sources_list = [] |
| 1237 | 1229 |
| 1238 def CreateFileEmitter(self, filename): | 1230 def CreateFileEmitter(self, filename): |
| 1239 path = os.path.join(self._dart_sources_dir, filename) | 1231 path = os.path.join(self._dart_sources_dir, filename) |
| 1240 self._dart_sources_list.append(path) | 1232 self._dart_sources_list.append(path) |
| 1241 return self._emitters.FileEmitter(path) | 1233 return self._emitters.FileEmitter(path) |
| 1242 | 1234 |
| 1243 def EmitLibrary(self, library_file_path, auxiliary_dir): | 1235 def EmitLibrary(self, library_file_path, auxiliary_dir): |
| 1244 def massage_path(path): | 1236 def massage_path(path): |
| 1245 # The most robust way to emit path separators is to use / always. | 1237 # The most robust way to emit path separators is to use / always. |
| 1246 return path.replace('\\', '/') | 1238 return path.replace('\\', '/') |
| 1247 | 1239 |
| 1248 library_emitter = self._emitters.FileEmitter(library_file_path) | 1240 library_emitter = self._emitters.FileEmitter(library_file_path) |
| 1249 library_file_dir = os.path.dirname(library_file_path) | 1241 library_file_dir = os.path.dirname(library_file_path) |
| 1250 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir) | 1242 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir) |
| 1251 imports_emitter = library_emitter.Emit( | 1243 imports_emitter = library_emitter.Emit( |
| 1252 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir)) | 1244 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir)) |
| 1253 for path in sorted(self._dart_sources_list): | 1245 for path in sorted(self._dart_sources_list): |
| 1254 relpath = os.path.relpath(path, library_file_dir) | 1246 relpath = os.path.relpath(path, library_file_dir) |
| 1255 imports_emitter.Emit( | 1247 imports_emitter.Emit( |
| 1256 "#source('$PATH');\n", PATH=massage_path(relpath)) | 1248 "#source('$PATH');\n", PATH=massage_path(relpath)) |
| OLD | NEW |