| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2011, 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 generates Dart APIs from the IDL database.""" | 6 """This module generates Dart APIs from the IDL database.""" |
| 7 | 7 |
| 8 import emitter | 8 import emitter |
| 9 import idlnode | 9 import idlnode |
| 10 import logging | 10 import logging |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 super_database -- database containing super interfaces that the generated | 351 super_database -- database containing super interfaces that the generated |
| 352 interfaces should extend. | 352 interfaces should extend. |
| 353 common_prefix -- prefix for the common library, if any. | 353 common_prefix -- prefix for the common library, if any. |
| 354 lib_file_path -- filename for generated .lib file, None if not required. | 354 lib_file_path -- filename for generated .lib file, None if not required. |
| 355 lib_template -- template file in this directory for generated lib file. | 355 lib_template -- template file in this directory for generated lib file. |
| 356 """ | 356 """ |
| 357 | 357 |
| 358 self._emitters = multiemitter.MultiEmitter() | 358 self._emitters = multiemitter.MultiEmitter() |
| 359 self._database = database | 359 self._database = database |
| 360 self._output_dir = output_dir | 360 self._output_dir = output_dir |
| 361 self._dart_callback_file_paths = [] |
| 361 | 362 |
| 362 self._StartGenerateInterfaceLibrary() | 363 self._StartGenerateInterfaceLibrary() |
| 363 self._StartGenerateJavaScriptMonkeyImpl(database, output_dir) | 364 self._StartGenerateJavaScriptMonkeyImpl(database, output_dir) |
| 364 self._StartGenerateWrappingImpl(output_dir) | 365 self._StartGenerateWrappingImpl(output_dir) |
| 365 self._StartGenerateFrogImpl(output_dir) | 366 self._StartGenerateFrogImpl(output_dir) |
| 366 | 367 |
| 367 # Render all interfaces into Dart and save them in files. | 368 # Render all interfaces into Dart and save them in files. |
| 368 dart_file_paths = [] | 369 dart_file_paths = [] |
| 369 processed_interfaces = [] | 370 processed_interfaces = [] |
| 370 for interface in database.GetInterfaces(): | 371 for interface in database.GetInterfaces(): |
| (...skipping 13 matching lines...) Expand all Loading... |
| 384 super_database.HasInterface(super_name)): | 385 super_database.HasInterface(super_name)): |
| 385 super_interface = super_name | 386 super_interface = super_name |
| 386 | 387 |
| 387 interface_name = interface.id | 388 interface_name = interface.id |
| 388 auxiliary_file = self._auxiliary_files.get(interface_name) | 389 auxiliary_file = self._auxiliary_files.get(interface_name) |
| 389 if auxiliary_file is not None: | 390 if auxiliary_file is not None: |
| 390 _logger.info('Skipping %s because %s exists' % ( | 391 _logger.info('Skipping %s because %s exists' % ( |
| 391 interface_name, auxiliary_file)) | 392 interface_name, auxiliary_file)) |
| 392 continue | 393 continue |
| 393 | 394 |
| 394 self._ProcessInterface(interface, super_interface, | 395 |
| 395 source_filter, common_prefix) | 396 info = self._RecognizeCallback(interface) |
| 397 if info: |
| 398 self._ProcessCallback(interface, info) |
| 399 else: |
| 400 if 'Callback' in interface.ext_attrs: |
| 401 _logger.info('Malformed callback: %s' % interface.id) |
| 402 self._ProcessInterface(interface, super_interface, |
| 403 source_filter, common_prefix) |
| 396 processed_interfaces.append(interface) | 404 processed_interfaces.append(interface) |
| 397 | 405 |
| 398 self._GenerateBrowserAnalysis(processed_interfaces, output_dir) | 406 self._GenerateBrowserAnalysis(processed_interfaces, output_dir) |
| 399 | 407 |
| 400 | 408 |
| 401 # Libraries | 409 # Libraries |
| 402 if lib_dir: | 410 if lib_dir: |
| 403 # New version: Monkey-patching interface library. | 411 # New version: Monkey-patching interface library. |
| 404 self.GenerateLibFile('template_monkey_dom.darttemplate', | 412 self.GenerateLibFile('template_monkey_dom.darttemplate', |
| 405 os.path.join(lib_dir, 'monkey_dom.dart'), | 413 os.path.join(lib_dir, 'monkey_dom.dart'), |
| 406 self._dart_interface_file_paths) | 414 self._dart_interface_file_paths + |
| 415 self._dart_callback_file_paths) |
| 407 | 416 |
| 408 # New version: Wrapping implementation combined interface and | 417 # New version: Wrapping implementation combined interface and |
| 409 # implementation library. | 418 # implementation library. |
| 410 self.GenerateLibFile('template_wrapping_dom.darttemplate', | 419 self.GenerateLibFile('template_wrapping_dom.darttemplate', |
| 411 os.path.join(lib_dir, 'wrapping_dom.dart'), | 420 os.path.join(lib_dir, 'wrapping_dom.dart'), |
| 412 (self._dart_interface_file_paths + | 421 (self._dart_interface_file_paths + |
| 422 self._dart_callback_file_paths + |
| 413 # FIXME: Move the implementation to a separate | 423 # FIXME: Move the implementation to a separate |
| 414 # library. | 424 # library. |
| 415 self._dart_wrapping_file_paths)) | 425 self._dart_wrapping_file_paths)) |
| 416 | 426 |
| 417 # New version: Frog | 427 # New version: Frog |
| 418 self.GenerateLibFile('template_frog_dom.darttemplate', | 428 self.GenerateLibFile('template_frog_dom.darttemplate', |
| 419 os.path.join(lib_dir, 'frog_dom.dart'), | 429 os.path.join(lib_dir, 'frog_dom.dart'), |
| 420 self._dart_frog_file_paths) | 430 self._dart_frog_file_paths + |
| 431 self._dart_callback_file_paths) |
| 421 | 432 |
| 422 | 433 |
| 423 # JavaScript externs files | 434 # JavaScript externs files |
| 424 self._GenerateJavaScriptExternsMonkey(database, output_dir) | 435 self._GenerateJavaScriptExternsMonkey(database, output_dir) |
| 425 self._GenerateJavaScriptExternsWrapping(database, output_dir) | 436 self._GenerateJavaScriptExternsWrapping(database, output_dir) |
| 426 | 437 |
| 427 | 438 |
| 439 def _RecognizeCallback(self, interface): |
| 440 """Returns the info for the callback method if the interface smells like a |
| 441 callback. |
| 442 """ |
| 443 if 'Callback' not in interface.ext_attrs: return None |
| 444 handlers = [op for op in interface.operations if op.id == 'handleEvent'] |
| 445 if not handlers: return None |
| 446 if not (handlers == interface.operations): return None |
| 447 return self._AnalyzeOperation(interface, handlers) |
| 448 |
| 449 def _ProcessCallback(self, interface, info): |
| 450 """Generates a typedef for the callback interface.""" |
| 451 interface_name = interface.id |
| 452 file_path = self.FilePathForDartInterface(interface_name) |
| 453 self._dart_callback_file_paths.append(file_path) |
| 454 code = self._emitters.FileEmitter(file_path) |
| 455 |
| 456 template_file = 'template_callback.darttemplate' |
| 457 code.Emit(''.join(open(template_file).readlines())) |
| 458 code.Emit('typedef $TYPE $NAME($ARGS);\n', |
| 459 NAME=interface.id, |
| 460 TYPE=info.type_name, |
| 461 ARGS=info.arg_implementation_declaration) |
| 462 |
| 463 |
| 464 |
| 428 def _ProcessInterface(self, interface, super_interface_name, | 465 def _ProcessInterface(self, interface, super_interface_name, |
| 429 source_filter, | 466 source_filter, |
| 430 common_prefix): | 467 common_prefix): |
| 431 """.""" | 468 """.""" |
| 432 _logger.info('Generating %s' % interface.id) | 469 _logger.info('Generating %s' % interface.id) |
| 433 | 470 |
| 434 dart_interface_generator = self._MakeDartInterfaceGenerator( | 471 dart_interface_generator = self._MakeDartInterfaceGenerator( |
| 435 interface, | 472 interface, |
| 436 common_prefix, | 473 common_prefix, |
| 437 super_interface_name, | 474 super_interface_name, |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 799 """.""" | 836 """.""" |
| 800 interface_name = interface.id | 837 interface_name = interface.id |
| 801 dart_frog_file_path = self.FilePathForFrogImpl(interface_name) | 838 dart_frog_file_path = self.FilePathForFrogImpl(interface_name) |
| 802 | 839 |
| 803 self._dart_frog_file_paths.append(dart_frog_file_path) | 840 self._dart_frog_file_paths.append(dart_frog_file_path) |
| 804 | 841 |
| 805 dart_code = self._emitters.FileEmitter(dart_frog_file_path) | 842 dart_code = self._emitters.FileEmitter(dart_frog_file_path) |
| 806 dart_code.Emit( | 843 dart_code.Emit( |
| 807 ''.join(open('template_frog_impl.darttemplate').readlines())) | 844 ''.join(open('template_frog_impl.darttemplate').readlines())) |
| 808 return FrogInterfaceGenerator(interface, super_interface_name, | 845 return FrogInterfaceGenerator(interface, super_interface_name, |
| 809 dart_code) | 846 dart_code) |
| 810 | 847 |
| 811 | 848 |
| 812 def _StartGenerateJavaScriptMonkeyImpl(self, database, output_dir): | 849 def _StartGenerateJavaScriptMonkeyImpl(self, database, output_dir): |
| 813 """Generate a JavaScript implementation that is coherent with | 850 """Generate a JavaScript implementation that is coherent with |
| 814 generated Dart APIs. | 851 generated Dart APIs. |
| 815 """ | 852 """ |
| 816 self._ComputeInheritanceClosure(database) | 853 self._ComputeInheritanceClosure(database) |
| 817 js_file_name = os.path.join(output_dir, 'monkey_dom.js') | 854 js_file_name = os.path.join(output_dir, 'monkey_dom.js') |
| 818 code = self._emitters.FileEmitter(js_file_name) | 855 code = self._emitters.FileEmitter(js_file_name) |
| 819 | 856 |
| (...skipping 1267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2087 Arguments: | 2124 Arguments: |
| 2088 info: An OperationInfo object. | 2125 info: An OperationInfo object. |
| 2089 """ | 2126 """ |
| 2090 # TODO(vsm): Handle overloads. | 2127 # TODO(vsm): Handle overloads. |
| 2091 self._members_emitter.Emit( | 2128 self._members_emitter.Emit( |
| 2092 '\n' | 2129 '\n' |
| 2093 ' $TYPE $NAME($ARGS) native;\n', | 2130 ' $TYPE $NAME($ARGS) native;\n', |
| 2094 TYPE=info.type_name, | 2131 TYPE=info.type_name, |
| 2095 NAME=info.name, | 2132 NAME=info.name, |
| 2096 ARGS=info.arg_implementation_declaration) | 2133 ARGS=info.arg_implementation_declaration) |
| OLD | NEW |