| 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 native binding from the IDL database.""" | 7 native binding from the IDL database.""" |
| 8 | 8 |
| 9 import emitter | 9 import emitter |
| 10 import os | 10 import os |
| 11 from generator import * | 11 from generator import * |
| 12 from systembase import * | 12 from systembase import * |
| 13 from systemhtml import DomToHtmlEvent, DomToHtmlEvents, HtmlSystemShared | 13 from systemhtml import DomToHtmlEvent, DomToHtmlEvents, HtmlSystemShared |
| 14 | 14 |
| 15 class NativeImplementationSystem(System): | 15 class NativeImplementationSystem(System): |
| 16 | 16 |
| 17 def __init__(self, templates, database, html_database, html_renames, | 17 def __init__(self, templates, database, html_database, html_renames, |
| 18 emitters, auxiliary_dir, output_dir): | 18 emitters, output_dir): |
| 19 super(NativeImplementationSystem, self).__init__( | 19 super(NativeImplementationSystem, self).__init__( |
| 20 templates, database, emitters, output_dir) | 20 templates, database, emitters, output_dir) |
| 21 | 21 |
| 22 self._html_renames = html_renames | 22 self._html_renames = html_renames |
| 23 self._auxiliary_dir = auxiliary_dir | |
| 24 self._dom_public_files = [] | |
| 25 self._dom_impl_files = [] | 23 self._dom_impl_files = [] |
| 26 self._cpp_header_files = [] | 24 self._cpp_header_files = [] |
| 27 self._cpp_impl_files = [] | 25 self._cpp_impl_files = [] |
| 28 self._html_system = HtmlSystemShared(html_database) | 26 self._html_system = HtmlSystemShared(html_database) |
| 29 | 27 |
| 30 def InterfaceGenerator(self, | 28 def InterfaceGenerator(self, |
| 31 interface, | 29 interface, |
| 32 common_prefix, | 30 common_prefix, |
| 33 super_interface_name, | 31 super_interface_name, |
| 34 source_filter): | 32 source_filter): |
| 35 interface_name = interface.id | 33 interface_name = interface.id |
| 36 | 34 |
| 37 dart_interface_path = self._FilePathForDartInterface(interface_name) | |
| 38 self._dom_public_files.append(dart_interface_path) | |
| 39 | |
| 40 if IsPureInterface(interface_name): | 35 if IsPureInterface(interface_name): |
| 41 return None | 36 return None |
| 42 | 37 |
| 43 dart_impl_path = self._FilePathForDartImplementation(interface_name) | 38 dart_impl_path = self._FilePathForDartImplementation(interface_name) |
| 44 self._dom_impl_files.append(dart_impl_path) | 39 self._dom_impl_files.append(dart_impl_path) |
| 45 | 40 |
| 46 cpp_header_path = self._FilePathForCppHeader(interface_name) | 41 cpp_header_path = self._FilePathForCppHeader(interface_name) |
| 47 self._cpp_header_files.append(cpp_header_path) | 42 self._cpp_header_files.append(cpp_header_path) |
| 48 | 43 |
| 49 cpp_impl_path = self._FilePathForCppImplementation(interface_name) | 44 cpp_impl_path = self._FilePathForCppImplementation(interface_name) |
| 50 self._cpp_impl_files.append(cpp_impl_path) | 45 self._cpp_impl_files.append(cpp_impl_path) |
| 51 | 46 |
| 52 return NativeImplementationGenerator(self, interface, | 47 return NativeImplementationGenerator(self, interface, |
| 53 self._emitters.FileEmitter(dart_impl_path), | 48 self._emitters.FileEmitter(dart_impl_path), |
| 54 self._emitters.FileEmitter(cpp_header_path), | 49 self._emitters.FileEmitter(cpp_header_path), |
| 55 self._emitters.FileEmitter(cpp_impl_path), | 50 self._emitters.FileEmitter(cpp_impl_path), |
| 56 self._BaseDefines(interface), | 51 self._BaseDefines(interface), |
| 57 self._templates) | 52 self._templates) |
| 58 | 53 |
| 59 def ProcessCallback(self, interface, info): | 54 def ProcessCallback(self, interface, info): |
| 60 self._interface = interface | 55 self._interface = interface |
| 61 | 56 |
| 62 dart_interface_path = self._FilePathForDartInterface(self._interface.id) | |
| 63 self._dom_public_files.append(dart_interface_path) | |
| 64 | |
| 65 if IsPureInterface(self._interface.id): | 57 if IsPureInterface(self._interface.id): |
| 66 return None | 58 return None |
| 67 | 59 |
| 68 cpp_impl_includes = set() | 60 cpp_impl_includes = set() |
| 69 cpp_header_handlers_emitter = emitter.Emitter() | 61 cpp_header_handlers_emitter = emitter.Emitter() |
| 70 cpp_impl_handlers_emitter = emitter.Emitter() | 62 cpp_impl_handlers_emitter = emitter.Emitter() |
| 71 class_name = 'Dart%s' % self._interface.id | 63 class_name = 'Dart%s' % self._interface.id |
| 72 for operation in interface.operations: | 64 for operation in interface.operations: |
| 73 if operation.type.id == 'void': | 65 if operation.type.id == 'void': |
| 74 return_prefix = '' | 66 return_prefix = '' |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 cpp_impl_path = self._FilePathForCppImplementation(self._interface.id) | 116 cpp_impl_path = self._FilePathForCppImplementation(self._interface.id) |
| 125 self._cpp_impl_files.append(cpp_impl_path) | 117 self._cpp_impl_files.append(cpp_impl_path) |
| 126 cpp_impl_emitter = self._emitters.FileEmitter(cpp_impl_path) | 118 cpp_impl_emitter = self._emitters.FileEmitter(cpp_impl_path) |
| 127 cpp_impl_emitter.Emit( | 119 cpp_impl_emitter.Emit( |
| 128 self._templates.Load('cpp_callback_implementation.template'), | 120 self._templates.Load('cpp_callback_implementation.template'), |
| 129 INCLUDES=_GenerateCPPIncludes(cpp_impl_includes), | 121 INCLUDES=_GenerateCPPIncludes(cpp_impl_includes), |
| 130 INTERFACE=self._interface.id, | 122 INTERFACE=self._interface.id, |
| 131 HANDLERS=cpp_impl_handlers_emitter.Fragments()) | 123 HANDLERS=cpp_impl_handlers_emitter.Fragments()) |
| 132 | 124 |
| 133 def GenerateLibraries(self): | 125 def GenerateLibraries(self): |
| 134 auxiliary_dir = os.path.relpath(self._auxiliary_dir, self._output_dir) | |
| 135 | |
| 136 # Generate dom_public.dart. | |
| 137 self._GenerateLibFile( | |
| 138 'dom_public.darttemplate', | |
| 139 os.path.join(self._output_dir, 'dom_public.dart'), | |
| 140 self._dom_public_files, | |
| 141 AUXILIARY_DIR=MassagePath(auxiliary_dir)); | |
| 142 | |
| 143 # Generate dom_impl.dart. | |
| 144 self._GenerateLibFile( | |
| 145 'dom_impl.darttemplate', | |
| 146 os.path.join(self._output_dir, 'dom_impl.dart'), | |
| 147 self._dom_impl_files, | |
| 148 AUXILIARY_DIR=MassagePath(auxiliary_dir)); | |
| 149 | |
| 150 # Generate DartDerivedSourcesXX.cpp. | 126 # Generate DartDerivedSourcesXX.cpp. |
| 151 partitions = 20 # FIXME: this should be configurable. | 127 partitions = 20 # FIXME: this should be configurable. |
| 152 sources_count = len(self._cpp_impl_files) | 128 sources_count = len(self._cpp_impl_files) |
| 153 for i in range(0, partitions): | 129 for i in range(0, partitions): |
| 154 derived_sources_path = os.path.join(self._output_dir, | 130 derived_sources_path = os.path.join(self._output_dir, |
| 155 'DartDerivedSources%02i.cpp' % (i + 1)) | 131 'DartDerivedSources%02i.cpp' % (i + 1)) |
| 156 | 132 |
| 157 includes_emitter = emitter.Emitter() | 133 includes_emitter = emitter.Emitter() |
| 158 for impl_file in self._cpp_impl_files[i::partitions]: | 134 for impl_file in self._cpp_impl_files[i::partitions]: |
| 159 path = os.path.relpath(impl_file, os.path.dirname(derived_sources_path
)) | 135 path = os.path.relpath(impl_file, os.path.dirname(derived_sources_path
)) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 179 | 155 |
| 180 cpp_resolver_emitter = self._emitters.FileEmitter(cpp_resolver_path) | 156 cpp_resolver_emitter = self._emitters.FileEmitter(cpp_resolver_path) |
| 181 cpp_resolver_emitter.Emit( | 157 cpp_resolver_emitter.Emit( |
| 182 self._templates.Load('cpp_resolver.template'), | 158 self._templates.Load('cpp_resolver.template'), |
| 183 INCLUDES=includes_emitter.Fragments(), | 159 INCLUDES=includes_emitter.Fragments(), |
| 184 RESOLVER_BODY=resolver_body_emitter.Fragments()) | 160 RESOLVER_BODY=resolver_body_emitter.Fragments()) |
| 185 | 161 |
| 186 def Finish(self): | 162 def Finish(self): |
| 187 pass | 163 pass |
| 188 | 164 |
| 189 def _FilePathForDartInterface(self, interface_name): | |
| 190 return os.path.join(self._output_dir, 'src', 'interface', | |
| 191 '%s.dart' % interface_name) | |
| 192 | |
| 193 def _FilePathForDartImplementation(self, interface_name): | 165 def _FilePathForDartImplementation(self, interface_name): |
| 194 return os.path.join(self._output_dir, 'dart', | 166 return os.path.join(self._output_dir, 'dart', |
| 195 '%sImplementation.dart' % interface_name) | 167 '%sImplementation.dart' % interface_name) |
| 196 | 168 |
| 197 def _FilePathForDartFactoryProvider(self, interface_name): | |
| 198 return os.path.join(self._output_dir, 'dart', | |
| 199 '_%sFactoryProvider.dart' % interface_name) | |
| 200 | |
| 201 def _FilePathForDartFactoryProviderImplementation(self, interface_name): | 169 def _FilePathForDartFactoryProviderImplementation(self, interface_name): |
| 202 return os.path.join(self._output_dir, 'dart', | 170 return os.path.join(self._output_dir, 'dart', |
| 203 '%sFactoryProviderImplementation.dart' % interface_name) | 171 '%sFactoryProviderImplementation.dart' % interface_name) |
| 204 | 172 |
| 205 def _FilePathForCppHeader(self, interface_name): | 173 def _FilePathForCppHeader(self, interface_name): |
| 206 return os.path.join(self._output_dir, 'cpp', 'Dart%s.h' % interface_name) | 174 return os.path.join(self._output_dir, 'cpp', 'Dart%s.h' % interface_name) |
| 207 | 175 |
| 208 def _FilePathForCppImplementation(self, interface_name): | 176 def _FilePathForCppImplementation(self, interface_name): |
| 209 return os.path.join(self._output_dir, 'cpp', 'Dart%s.cpp' % interface_name) | 177 return os.path.join(self._output_dir, 'cpp', 'Dart%s.cpp' % interface_name) |
| 210 | 178 |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 return self._ImplClassName(supertype) | 402 return self._ImplClassName(supertype) |
| 435 | 403 |
| 436 def _HTMLInterfaceName(self, interface_name): | 404 def _HTMLInterfaceName(self, interface_name): |
| 437 return self._system._html_renames.get(interface_name, interface_name) | 405 return self._system._html_renames.get(interface_name, interface_name) |
| 438 | 406 |
| 439 def _IsConstructable(self): | 407 def _IsConstructable(self): |
| 440 # FIXME: support ConstructorTemplate. | 408 # FIXME: support ConstructorTemplate. |
| 441 return set(['CustomConstructor', 'V8CustomConstructor', 'Constructor', 'Name
dConstructor']) & set(self._interface.ext_attrs) | 409 return set(['CustomConstructor', 'V8CustomConstructor', 'Constructor', 'Name
dConstructor']) & set(self._interface.ext_attrs) |
| 442 | 410 |
| 443 def _EmitFactoryProvider(self, interface_name, constructor_info): | 411 def _EmitFactoryProvider(self, interface_name, constructor_info): |
| 444 factory_provider = '_%sFactoryProvider' % interface_name | |
| 445 implementation_class = '_%sFactoryProviderImpl' % interface_name | 412 implementation_class = '_%sFactoryProviderImpl' % interface_name |
| 446 implementation_function = 'create' + interface_name | 413 implementation_function = 'create' + interface_name |
| 447 native_implementation_function = '%s_constructor_Callback' % interface_name | 414 native_implementation_function = '%s_constructor_Callback' % interface_name |
| 448 | 415 |
| 449 # Emit private factory provider in public library. | |
| 450 template_file = 'factoryprovider_%s.darttemplate' % interface_name | |
| 451 template = self._system._templates.TryLoad(template_file) | |
| 452 if not template: | |
| 453 template = self._system._templates.Load('factoryprovider.darttemplate') | |
| 454 | |
| 455 dart_impl_path = self._system._FilePathForDartFactoryProvider( | |
| 456 interface_name) | |
| 457 self._system._dom_public_files.append(dart_impl_path) | |
| 458 | |
| 459 parameters = constructor_info.ParametersImplementationDeclaration(self._Dart
Type) | |
| 460 | |
| 461 emitter = self._system._emitters.FileEmitter(dart_impl_path) | |
| 462 emitter.Emit( | |
| 463 template, | |
| 464 FACTORY_PROVIDER=factory_provider, | |
| 465 CONSTRUCTOR=interface_name, | |
| 466 PARAMETERS=parameters, | |
| 467 IMPL_CLASS=implementation_class, | |
| 468 IMPL_FUNCTION=implementation_function, | |
| 469 ARGUMENTS=constructor_info.ParametersAsArgumentList()) | |
| 470 | |
| 471 # Emit public implementation in implementation libary. | 416 # Emit public implementation in implementation libary. |
| 472 dart_impl_path = self._system._FilePathForDartFactoryProviderImplementation( | 417 dart_impl_path = self._system._FilePathForDartFactoryProviderImplementation( |
| 473 interface_name) | 418 interface_name) |
| 474 self._system._dom_impl_files.append(dart_impl_path) | 419 self._system._dom_impl_files.append(dart_impl_path) |
| 420 |
| 421 parameters = constructor_info.ParametersImplementationDeclaration(self._Dart
Type) |
| 475 emitter = self._system._emitters.FileEmitter(dart_impl_path) | 422 emitter = self._system._emitters.FileEmitter(dart_impl_path) |
| 476 emitter.Emit( | 423 emitter.Emit( |
| 477 'class $IMPL_CLASS {\n' | 424 'class $IMPL_CLASS {\n' |
| 478 ' static $TYPE $IMPL_FUNCTION($PARAMETERS)\n' | 425 ' static $TYPE $IMPL_FUNCTION($PARAMETERS)\n' |
| 479 ' native "$NATIVE_NAME";\n' | 426 ' native "$NATIVE_NAME";\n' |
| 480 '}', | 427 '}', |
| 481 PARAMETERS=parameters, | 428 PARAMETERS=parameters, |
| 482 IMPL_CLASS=implementation_class, | 429 IMPL_CLASS=implementation_class, |
| 483 TYPE=self._ImplClassName(interface_name), | 430 TYPE=self._ImplClassName(interface_name), |
| 484 IMPL_FUNCTION=implementation_function, | 431 IMPL_FUNCTION=implementation_function, |
| (...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1299 for parent in interface.parents: | 1246 for parent in interface.parents: |
| 1300 parent_name = parent.type.id | 1247 parent_name = parent.type.id |
| 1301 if not database.HasInterface(parent.type.id): | 1248 if not database.HasInterface(parent.type.id): |
| 1302 continue | 1249 continue |
| 1303 parent_interface = database.GetInterface(parent.type.id) | 1250 parent_interface = database.GetInterface(parent.type.id) |
| 1304 if callback(parent_interface): | 1251 if callback(parent_interface): |
| 1305 return parent_interface | 1252 return parent_interface |
| 1306 parent_interface = _FindParent(parent_interface, database, callback) | 1253 parent_interface = _FindParent(parent_interface, database, callback) |
| 1307 if parent_interface: | 1254 if parent_interface: |
| 1308 return parent_interface | 1255 return parent_interface |
| OLD | NEW |