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 import os | 10 import os |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 return (self._template_loader.TryLoad(template_file) or | 463 return (self._template_loader.TryLoad(template_file) or |
464 self._template_loader.Load('dart2js_impl.darttemplate')) | 464 self._template_loader.Load('dart2js_impl.darttemplate')) |
465 | 465 |
466 def StartInterface(self, members_emitter): | 466 def StartInterface(self, members_emitter): |
467 self._members_emitter = members_emitter | 467 self._members_emitter = members_emitter |
468 | 468 |
469 def FinishInterface(self): | 469 def FinishInterface(self): |
470 pass | 470 pass |
471 | 471 |
472 def EmitStaticFactory(self, constructor_info): | 472 def EmitStaticFactory(self, constructor_info): |
473 arguments = constructor_info.ParametersAsArgumentList() | 473 has_optional = any(param_info.is_optional |
474 if arguments: | 474 for param_info in constructor_info.param_infos) |
475 arguments = ', ' + arguments | 475 |
476 self._members_emitter.Emit( | 476 def FormatJS(index): |
477 " static $INTERFACE_NAME _create($PARAMETERS_DECLARATION) => JS(" | 477 arguments = constructor_info.ParametersAsArgumentList(index) |
478 "'$INTERFACE_NAME', " | 478 if arguments: |
479 "'new $CONSTRUCTOR_NAME($ARGUMENTS_PATTERN)'$ARGUMENTS);\n", | 479 arguments = ', ' + arguments |
480 INTERFACE_NAME=self._interface_type_info.interface_name(), | 480 return "JS('%s', 'new %s(%s)'%s)" % ( |
481 PARAMETERS_DECLARATION=constructor_info.ParametersDeclaration( | 481 self._interface_type_info.interface_name(), |
482 self._DartType), | 482 constructor_info.name or self._interface.doc_js_name, |
483 CONSTRUCTOR_NAME=constructor_info.name or self._interface.doc_js_name, | 483 ','.join(['#'] * index), |
484 ARGUMENTS_PATTERN=','.join(['#'] * len(constructor_info.param_infos)), | 484 arguments) |
485 ARGUMENTS=arguments) | 485 |
| 486 if not has_optional: |
| 487 self._members_emitter.Emit( |
| 488 " static $INTERFACE_NAME _create($PARAMETERS_DECLARATION) => $JS;\n", |
| 489 INTERFACE_NAME=self._interface_type_info.interface_name(), |
| 490 PARAMETERS_DECLARATION=constructor_info.ParametersDeclaration( |
| 491 self._DartType), |
| 492 JS=FormatJS(len(constructor_info.param_infos))) |
| 493 else: |
| 494 dispatcher_emitter = self._members_emitter.Emit( |
| 495 " static $INTERFACE_NAME _create($PARAMETERS_DECLARATION) {\n" |
| 496 "$!DISPATCHER" |
| 497 " return $JS;\n" |
| 498 " }\n", |
| 499 INTERFACE_NAME=self._interface_type_info.interface_name(), |
| 500 PARAMETERS_DECLARATION=constructor_info.ParametersDeclaration( |
| 501 self._DartType), |
| 502 JS=FormatJS(len(constructor_info.param_infos))) |
| 503 |
| 504 for index, param_info in enumerate(constructor_info.param_infos): |
| 505 if param_info.is_optional: |
| 506 dispatcher_emitter.Emit( |
| 507 " if (!?$OPT_PARAM_NAME) {\n" |
| 508 " return $JS;\n" |
| 509 " }\n", |
| 510 OPT_PARAM_NAME=constructor_info.param_infos[index].name, |
| 511 JS=FormatJS(index)) |
486 | 512 |
487 def SecondaryContext(self, interface): | 513 def SecondaryContext(self, interface): |
488 if interface is not self._current_secondary_parent: | 514 if interface is not self._current_secondary_parent: |
489 self._current_secondary_parent = interface | 515 self._current_secondary_parent = interface |
490 self._members_emitter.Emit('\n // From $WHERE\n', WHERE=interface.id) | 516 self._members_emitter.Emit('\n // From $WHERE\n', WHERE=interface.id) |
491 | 517 |
492 def AddIndexer(self, element_type): | 518 def AddIndexer(self, element_type): |
493 """Adds all the methods required to complete implementation of List.""" | 519 """Adds all the methods required to complete implementation of List.""" |
494 # We would like to simply inherit the implementation of everything except | 520 # We would like to simply inherit the implementation of everything except |
495 # length, [], and maybe []=. It is possible to extend from a base | 521 # length, [], and maybe []=. It is possible to extend from a base |
(...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1002 for library_name in libraries: | 1028 for library_name in libraries: |
1003 self._libraries[library_name] = DartLibrary( | 1029 self._libraries[library_name] = DartLibrary( |
1004 library_name, template_loader, library_type, output_dir) | 1030 library_name, template_loader, library_type, output_dir) |
1005 | 1031 |
1006 def AddFile(self, basename, library_name, path): | 1032 def AddFile(self, basename, library_name, path): |
1007 self._libraries[library_name].AddFile(path) | 1033 self._libraries[library_name].AddFile(path) |
1008 | 1034 |
1009 def Emit(self, emitter, auxiliary_dir): | 1035 def Emit(self, emitter, auxiliary_dir): |
1010 for lib in self._libraries.values(): | 1036 for lib in self._libraries.values(): |
1011 lib.Emit(emitter, auxiliary_dir) | 1037 lib.Emit(emitter, auxiliary_dir) |
OLD | NEW |