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 |
(...skipping 28 matching lines...) Expand all Loading... |
39 | 39 |
40 dart_impl_path = self._FilePathForDartImplementation(interface_name) | 40 dart_impl_path = self._FilePathForDartImplementation(interface_name) |
41 self._dom_impl_files.append(dart_impl_path) | 41 self._dom_impl_files.append(dart_impl_path) |
42 | 42 |
43 cpp_header_path = self._FilePathForCppHeader(interface_name) | 43 cpp_header_path = self._FilePathForCppHeader(interface_name) |
44 self._cpp_header_files.append(cpp_header_path) | 44 self._cpp_header_files.append(cpp_header_path) |
45 | 45 |
46 cpp_impl_path = self._FilePathForCppImplementation(interface_name) | 46 cpp_impl_path = self._FilePathForCppImplementation(interface_name) |
47 self._cpp_impl_files.append(cpp_impl_path) | 47 self._cpp_impl_files.append(cpp_impl_path) |
48 | 48 |
49 return NativeImplementationGenerator(interface, super_interface_name, | 49 return NativeImplementationGenerator(self, interface, super_interface_name, |
50 self._emitters.FileEmitter(dart_impl_path), | 50 self._emitters.FileEmitter(dart_impl_path), |
51 self._emitters.FileEmitter(cpp_header_path), | 51 self._emitters.FileEmitter(cpp_header_path), |
52 self._emitters.FileEmitter(cpp_impl_path), | 52 self._emitters.FileEmitter(cpp_impl_path), |
53 self._BaseDefines(interface), | 53 self._BaseDefines(interface), |
54 self._templates) | 54 self._templates) |
55 | 55 |
56 def ProcessCallback(self, interface, info): | 56 def ProcessCallback(self, interface, info): |
57 self._interface = interface | 57 self._interface = interface |
58 | 58 |
59 dart_interface_path = self._FilePathForDartInterface(self._interface.id) | 59 dart_interface_path = self._FilePathForDartInterface(self._interface.id) |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 pass | 170 pass |
171 | 171 |
172 def _FilePathForDartInterface(self, interface_name): | 172 def _FilePathForDartInterface(self, interface_name): |
173 return os.path.join(self._output_dir, 'src', 'interface', | 173 return os.path.join(self._output_dir, 'src', 'interface', |
174 '%s.dart' % interface_name) | 174 '%s.dart' % interface_name) |
175 | 175 |
176 def _FilePathForDartImplementation(self, interface_name): | 176 def _FilePathForDartImplementation(self, interface_name): |
177 return os.path.join(self._output_dir, 'dart', | 177 return os.path.join(self._output_dir, 'dart', |
178 '%sImplementation.dart' % interface_name) | 178 '%sImplementation.dart' % interface_name) |
179 | 179 |
| 180 def _FilePathForDartFactoryProvider(self, interface_name): |
| 181 return os.path.join(self._output_dir, 'dart', |
| 182 '_%sFactoryProvider.dart' % interface_name) |
| 183 |
| 184 def _FilePathForDartFactoryProviderImplementation(self, interface_name): |
| 185 return os.path.join(self._output_dir, 'dart', |
| 186 '%sFactoryProviderImplementation.dart' % interface_name) |
| 187 |
180 def _FilePathForCppHeader(self, interface_name): | 188 def _FilePathForCppHeader(self, interface_name): |
181 return os.path.join(self._output_dir, 'cpp', 'Dart%s.h' % interface_name) | 189 return os.path.join(self._output_dir, 'cpp', 'Dart%s.h' % interface_name) |
182 | 190 |
183 def _FilePathForCppImplementation(self, interface_name): | 191 def _FilePathForCppImplementation(self, interface_name): |
184 return os.path.join(self._output_dir, 'cpp', 'Dart%s.cpp' % interface_name) | 192 return os.path.join(self._output_dir, 'cpp', 'Dart%s.cpp' % interface_name) |
185 | 193 |
186 | 194 |
187 class NativeImplementationGenerator(systemwrapping.WrappingInterfaceGenerator): | 195 class NativeImplementationGenerator(systemwrapping.WrappingInterfaceGenerator): |
188 """Generates Dart implementation for one DOM IDL interface.""" | 196 """Generates Dart implementation for one DOM IDL interface.""" |
189 | 197 |
190 def __init__(self, interface, super_interface, | 198 def __init__(self, system, interface, super_interface, |
191 dart_impl_emitter, cpp_header_emitter, cpp_impl_emitter, | 199 dart_impl_emitter, cpp_header_emitter, cpp_impl_emitter, |
192 base_members, templates): | 200 base_members, templates): |
193 """Generates Dart and C++ code for the given interface. | 201 """Generates Dart and C++ code for the given interface. |
194 | 202 |
195 Args: | 203 Args: |
196 | 204 system: The NativeImplementationSystem. |
197 interface: an IDLInterface instance. It is assumed that all types have | 205 interface: an IDLInterface instance. It is assumed that all types have |
198 been converted to Dart types (e.g. int, String), unless they are in | 206 been converted to Dart types (e.g. int, String), unless they are in |
199 the same package as the interface. | 207 the same package as the interface. |
200 super_interface: A string or None, the name of the common interface that | 208 super_interface: A string or None, the name of the common interface that |
201 this interface implements, if any. | 209 this interface implements, if any. |
202 dart_impl_emitter: an Emitter for the file containing the Dart | 210 dart_impl_emitter: an Emitter for the file containing the Dart |
203 implementation class. | 211 implementation class. |
204 cpp_header_emitter: an Emitter for the file containing the C++ header. | 212 cpp_header_emitter: an Emitter for the file containing the C++ header. |
205 cpp_impl_emitter: an Emitter for the file containing the C++ | 213 cpp_impl_emitter: an Emitter for the file containing the C++ |
206 implementation. | 214 implementation. |
207 base_members: a set of names of members defined in a base class. This is | 215 base_members: a set of names of members defined in a base class. This is |
208 used to avoid static member 'overriding' in the generated Dart code. | 216 used to avoid static member 'overriding' in the generated Dart code. |
209 """ | 217 """ |
| 218 self._system = system |
210 self._interface = interface | 219 self._interface = interface |
211 self._super_interface = super_interface | 220 self._super_interface = super_interface |
212 self._dart_impl_emitter = dart_impl_emitter | 221 self._dart_impl_emitter = dart_impl_emitter |
213 self._cpp_header_emitter = cpp_header_emitter | 222 self._cpp_header_emitter = cpp_header_emitter |
214 self._cpp_impl_emitter = cpp_impl_emitter | 223 self._cpp_impl_emitter = cpp_impl_emitter |
215 self._base_members = base_members | 224 self._base_members = base_members |
216 self._templates = templates | 225 self._templates = templates |
217 self._current_secondary_parent = None | 226 self._current_secondary_parent = None |
218 | 227 |
219 def StartInterface(self): | 228 def StartInterface(self): |
(...skipping 13 matching lines...) Expand all Loading... |
233 | 242 |
234 # TODO(antonm): currently we don't have information about number of argument
s expected by | 243 # TODO(antonm): currently we don't have information about number of argument
s expected by |
235 # the constructor, so name only dispatch. | 244 # the constructor, so name only dispatch. |
236 self._cpp_resolver_emitter.Emit( | 245 self._cpp_resolver_emitter.Emit( |
237 ' if (name == "$(INTERFACE_NAME)_constructor_Callback")\n' | 246 ' if (name == "$(INTERFACE_NAME)_constructor_Callback")\n' |
238 ' return Dart$(INTERFACE_NAME)Internal::constructorCallback;\n', | 247 ' return Dart$(INTERFACE_NAME)Internal::constructorCallback;\n', |
239 INTERFACE_NAME=self._interface.id) | 248 INTERFACE_NAME=self._interface.id) |
240 | 249 |
241 | 250 |
242 constructor_info = AnalyzeConstructor(self._interface) | 251 constructor_info = AnalyzeConstructor(self._interface) |
| 252 if constructor_info: |
| 253 self._EmitFactoryProvider(self._interface.id, constructor_info) |
| 254 |
243 if constructor_info is None: | 255 if constructor_info is None: |
244 # We have a custom implementation for it. | 256 # We have a custom implementation for it. |
245 self._cpp_declarations_emitter.Emit( | 257 self._cpp_declarations_emitter.Emit( |
246 '\n' | 258 '\n' |
247 'void constructorCallback(Dart_NativeArguments);\n') | 259 'void constructorCallback(Dart_NativeArguments);\n') |
248 return | 260 return |
249 | 261 |
250 raises_dom_exceptions = 'ConstructorRaisesException' in self._interface.ext_
attrs | 262 raises_dom_exceptions = 'ConstructorRaisesException' in self._interface.ext_
attrs |
251 raises_exceptions = raises_dom_exceptions or len(constructor_info.idl_args)
> 0 | 263 raises_exceptions = raises_dom_exceptions or len(constructor_info.idl_args)
> 0 |
252 arguments = [] | 264 arguments = [] |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 needs_receiver=False, invocation=invocation, | 303 needs_receiver=False, invocation=invocation, |
292 raises_exceptions=raises_exceptions) | 304 raises_exceptions=raises_exceptions) |
293 | 305 |
294 def _ImplClassName(self, interface_name): | 306 def _ImplClassName(self, interface_name): |
295 return interface_name + 'Implementation' | 307 return interface_name + 'Implementation' |
296 | 308 |
297 def _IsConstructable(self): | 309 def _IsConstructable(self): |
298 # FIXME: support ConstructorTemplate. | 310 # FIXME: support ConstructorTemplate. |
299 return set(['CustomConstructor', 'V8CustomConstructor', 'Constructor', 'Name
dConstructor']) & set(self._interface.ext_attrs) | 311 return set(['CustomConstructor', 'V8CustomConstructor', 'Constructor', 'Name
dConstructor']) & set(self._interface.ext_attrs) |
300 | 312 |
| 313 def _EmitFactoryProvider(self, interface_name, constructor_info): |
| 314 factory_provider = '_' + interface_name + 'FactoryProvider' |
| 315 implementation_class = interface_name + 'FactoryProviderImplementation' |
| 316 implementation_function = 'create' + interface_name |
| 317 native_implementation_function = '%s_constructor_Callback' % interface_name |
| 318 |
| 319 # Emit private factory provider in public library. |
| 320 template_file = 'factoryprovider_%s.darttemplate' % interface_name |
| 321 template = self._system._templates.TryLoad(template_file) |
| 322 if not template: |
| 323 template = self._system._templates.Load('factoryprovider.darttemplate') |
| 324 |
| 325 dart_impl_path = self._system._FilePathForDartFactoryProvider( |
| 326 interface_name) |
| 327 self._system._dom_public_files.append(dart_impl_path) |
| 328 |
| 329 emitter = self._system._emitters.FileEmitter(dart_impl_path) |
| 330 emitter.Emit( |
| 331 template, |
| 332 FACTORY_PROVIDER=factory_provider, |
| 333 CONSTRUCTOR=interface_name, |
| 334 PARAMETERS=constructor_info.ParametersImplementationDeclaration(), |
| 335 IMPL_CLASS=implementation_class, |
| 336 IMPL_FUNCTION=implementation_function, |
| 337 ARGUMENTS=constructor_info.ParametersAsArgumentList()) |
| 338 |
| 339 # Emit public implementation in implementation libary. |
| 340 dart_impl_path = self._system._FilePathForDartFactoryProviderImplementation( |
| 341 interface_name) |
| 342 self._system._dom_impl_files.append(dart_impl_path) |
| 343 emitter = self._system._emitters.FileEmitter(dart_impl_path) |
| 344 emitter.Emit( |
| 345 'class $IMPL_CLASS {\n' |
| 346 ' static $INTERFACE_NAME $IMPL_FUNCTION($PARAMETERS)\n' |
| 347 ' native "$NATIVE_NAME";\n' |
| 348 '}', |
| 349 INTERFACE_NAME=interface_name, |
| 350 PARAMETERS=constructor_info.ParametersImplementationDeclaration(), |
| 351 IMPL_CLASS=implementation_class, |
| 352 IMPL_FUNCTION=implementation_function, |
| 353 NATIVE_NAME=native_implementation_function) |
| 354 |
301 def FinishInterface(self): | 355 def FinishInterface(self): |
302 base = self._BaseClassName(self._interface) | 356 base = self._BaseClassName(self._interface) |
303 self._dart_impl_emitter.Emit( | 357 self._dart_impl_emitter.Emit( |
304 self._templates.Load('dart_implementation.darttemplate'), | 358 self._templates.Load('dart_implementation.darttemplate'), |
305 CLASS=self._class_name, BASE=base, INTERFACE=self._interface.id, | 359 CLASS=self._class_name, BASE=base, INTERFACE=self._interface.id, |
306 MEMBERS=self._members_emitter.Fragments()) | 360 MEMBERS=self._members_emitter.Fragments()) |
307 | 361 |
308 self._GenerateCppHeader() | 362 self._GenerateCppHeader() |
309 | 363 |
310 self._cpp_impl_emitter.Emit( | 364 self._cpp_impl_emitter.Emit( |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 | 831 |
778 if 'ImplementedBy' in attributes: | 832 if 'ImplementedBy' in attributes: |
779 arguments.insert(0, 'receiver') | 833 arguments.insert(0, 'receiver') |
780 self._cpp_impl_includes.add(attributes['ImplementedBy']) | 834 self._cpp_impl_includes.add(attributes['ImplementedBy']) |
781 | 835 |
782 return emitter.Format(invocation_template, | 836 return emitter.Format(invocation_template, |
783 FUNCTION_CALL='%s(%s)' % (function_expression, ', '.join(arguments))) | 837 FUNCTION_CALL='%s(%s)' % (function_expression, ', '.join(arguments))) |
784 | 838 |
785 def _GenerateCPPIncludes(includes): | 839 def _GenerateCPPIncludes(includes): |
786 return ''.join(['#include "%s.h"\n' % include for include in includes]) | 840 return ''.join(['#include "%s.h"\n' % include for include in includes]) |
OLD | NEW |