| 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 systems to generate | 6 """This module provides shared functionality for the systems to generate |
| 7 frog binding from the IDL database.""" | 7 frog binding from the IDL database.""" |
| 8 | 8 |
| 9 import os | 9 import os |
| 10 from generator import * | 10 from generator import * |
| 11 from systembase import * | 11 from systembase import * |
| 12 | 12 |
| 13 class FrogSystem(System): | 13 class FrogSystem(System): |
| 14 | 14 |
| 15 def __init__(self, templates, database, emitters, output_dir): | 15 def __init__(self, templates, database, emitters, output_dir): |
| 16 super(FrogSystem, self).__init__( | 16 super(FrogSystem, self).__init__( |
| 17 templates, database, emitters, output_dir) | 17 templates, database, emitters, output_dir) |
| 18 self._dart_frog_file_paths = [] | 18 self._impl_file_paths = [] |
| 19 | 19 |
| 20 def InterfaceGenerator(self, | 20 def InterfaceGenerator(self, |
| 21 interface, | 21 interface, |
| 22 common_prefix, | 22 common_prefix, |
| 23 super_interface_name, | 23 super_interface_name, |
| 24 source_filter): | 24 source_filter): |
| 25 """.""" | 25 """.""" |
| 26 dart_frog_file_path = self._FilePathForFrogImpl(interface.id) | |
| 27 self._dart_frog_file_paths.append(dart_frog_file_path) | |
| 28 | |
| 29 template_file = 'impl_%s.darttemplate' % interface.id | 26 template_file = 'impl_%s.darttemplate' % interface.id |
| 30 template = self._templates.TryLoad(template_file) | 27 template = self._templates.TryLoad(template_file) |
| 31 if not template: | 28 if not template: |
| 32 template = self._templates.Load('frog_impl.darttemplate') | 29 template = self._templates.Load('frog_impl.darttemplate') |
| 33 | 30 |
| 34 dart_code = self._emitters.FileEmitter(dart_frog_file_path) | 31 dart_code = self._ImplFileEmitter(interface.id) |
| 35 return FrogInterfaceGenerator(self, interface, template, | 32 return FrogInterfaceGenerator(self, interface, template, |
| 36 super_interface_name, dart_code) | 33 super_interface_name, dart_code) |
| 37 | 34 |
| 38 def GenerateLibraries(self, lib_dir): | 35 def GenerateLibraries(self, lib_dir): |
| 39 self._GenerateLibFile( | 36 self._GenerateLibFile( |
| 40 'frog_dom.darttemplate', | 37 'frog_dom.darttemplate', |
| 41 os.path.join(lib_dir, 'dom_frog.dart'), | 38 os.path.join(lib_dir, 'dom_frog.dart'), |
| 42 (self._interface_system._dart_interface_file_paths + | 39 (self._interface_system._dart_interface_file_paths + |
| 43 self._interface_system._dart_callback_file_paths + | 40 self._interface_system._dart_callback_file_paths + |
| 44 self._dart_frog_file_paths)) | 41 self._impl_file_paths)) |
| 45 | 42 |
| 46 def Finish(self): | 43 def Finish(self): |
| 47 pass | 44 pass |
| 48 | 45 |
| 49 def _FilePathForFrogImpl(self, interface_name): | 46 def _ImplFileEmitter(self, name): |
| 50 """Returns the file path of the Frog implementation.""" | 47 """Returns the file emitter of the Frog implementation file.""" |
| 51 return os.path.join(self._output_dir, 'src', 'frog', | 48 path = os.path.join(self._output_dir, 'src', 'frog', '%s.dart' % name) |
| 52 '%s.dart' % interface_name) | 49 self._impl_file_paths.append(path) |
| 50 return self._emitters.FileEmitter(path) |
| 53 | 51 |
| 54 # ------------------------------------------------------------------------------ | 52 # ------------------------------------------------------------------------------ |
| 55 | 53 |
| 56 class FrogInterfaceGenerator(object): | 54 class FrogInterfaceGenerator(object): |
| 57 """Generates a Frog class for a DOM IDL interface.""" | 55 """Generates a Frog class for a DOM IDL interface.""" |
| 58 | 56 |
| 59 def __init__(self, system, interface, template, super_interface, dart_code): | 57 def __init__(self, system, interface, template, super_interface, dart_code): |
| 60 """Generates Dart code for the given interface. | 58 """Generates Dart code for the given interface. |
| 61 | 59 |
| 62 Args: | 60 Args: |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 #} | 113 #} |
| 116 CLASSNAME=self._class_name, | 114 CLASSNAME=self._class_name, |
| 117 EXTENDS=extends, | 115 EXTENDS=extends, |
| 118 IMPLEMENTS=' implements ' + ', '.join(implements), | 116 IMPLEMENTS=' implements ' + ', '.join(implements), |
| 119 NATIVESPEC=' native "' + native_spec + '"') | 117 NATIVESPEC=' native "' + native_spec + '"') |
| 120 | 118 |
| 121 element_type = MaybeTypedArrayElementType(interface) | 119 element_type = MaybeTypedArrayElementType(interface) |
| 122 if element_type: | 120 if element_type: |
| 123 self.AddTypedArrayConstructors(element_type) | 121 self.AddTypedArrayConstructors(element_type) |
| 124 | 122 |
| 123 # Emit a factory provider class for the constructor. |
| 124 constructor_info = AnalyzeConstructor(interface) |
| 125 if constructor_info: |
| 126 self._EmitFactoryProvider(interface_name, constructor_info) |
| 127 |
| 125 | 128 |
| 126 def FinishInterface(self): | 129 def FinishInterface(self): |
| 127 """.""" | 130 """.""" |
| 128 pass | 131 pass |
| 129 | 132 |
| 130 def _ImplClassName(self, type_name): | 133 def _ImplClassName(self, type_name): |
| 131 return '_' + type_name + 'Js' | 134 return '_' + type_name + 'Js' |
| 132 | 135 |
| 136 def _EmitFactoryProvider(self, interface_name, constructor_info): |
| 137 template_file = 'factoryprovider_%s.darttemplate' % interface_name |
| 138 template = self._system._templates.TryLoad(template_file) |
| 139 if not template: |
| 140 template = self._system._templates.Load('factoryprovider.darttemplate') |
| 141 |
| 142 factory_provider = '_' + interface_name + 'FactoryProvider' |
| 143 emitter = self._system._ImplFileEmitter(factory_provider) |
| 144 emitter.Emit( |
| 145 template, |
| 146 FACTORYPROVIDER=factory_provider, |
| 147 CONSTRUCTOR=interface_name, |
| 148 PARAMETERS=constructor_info.ParametersImplementationDeclaration(), |
| 149 NAMEDCONSTRUCTOR=constructor_info.name or interface_name, |
| 150 ARGUMENTS=constructor_info.ParametersAsArgumentList()) |
| 151 |
| 133 def _NarrowToImplementationType(self, type_name): | 152 def _NarrowToImplementationType(self, type_name): |
| 134 # TODO(sra): Move into the 'system' and cache the result. | 153 # TODO(sra): Move into the 'system' and cache the result. |
| 135 if type_name == 'EventListener': | 154 if type_name == 'EventListener': |
| 136 # Callbacks are typedef functions so don't have a class. | 155 # Callbacks are typedef functions so don't have a class. |
| 137 return type_name | 156 return type_name |
| 138 if self._system._database.HasInterface(type_name): | 157 if self._system._database.HasInterface(type_name): |
| 139 interface = self._system._database.GetInterface(type_name) | 158 interface = self._system._database.GetInterface(type_name) |
| 140 if RecognizeCallback(interface): | 159 if RecognizeCallback(interface): |
| 141 # Callbacks are typedef functions so don't have a class. | 160 # Callbacks are typedef functions so don't have a class. |
| 142 return type_name | 161 return type_name |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 if native_body: | 342 if native_body: |
| 324 native_body = " '''" + native_body + "'''" | 343 native_body = " '''" + native_body + "'''" |
| 325 | 344 |
| 326 self._members_emitter.Emit( | 345 self._members_emitter.Emit( |
| 327 '\n' | 346 '\n' |
| 328 ' $TYPE $NAME($PARAMS) native$NATIVESTRING;\n', | 347 ' $TYPE $NAME($PARAMS) native$NATIVESTRING;\n', |
| 329 TYPE=self._NarrowOutputType(info.type_name), | 348 TYPE=self._NarrowOutputType(info.type_name), |
| 330 NAME=info.name, | 349 NAME=info.name, |
| 331 PARAMS=params, | 350 PARAMS=params, |
| 332 NATIVESTRING=native_body) | 351 NATIVESTRING=native_body) |
| OLD | NEW |