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 from generator import AnalyzeOperation, ConstantOutputOrder, \ | 10 from generator import AnalyzeOperation, ConstantOutputOrder, \ |
(...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 factory_constructor_name = '_create' | 540 factory_constructor_name = '_create' |
541 factory_parameters = constructor_info.ParametersAsArgumentList() | 541 factory_parameters = constructor_info.ParametersAsArgumentList() |
542 else: | 542 else: |
543 factory_parameters = ', '.join(constructor_info.factory_parameters) | 543 factory_parameters = ', '.join(constructor_info.factory_parameters) |
544 | 544 |
545 if constructor_info.pure_dart_constructor: | 545 if constructor_info.pure_dart_constructor: |
546 # TODO(antonm): use common dispatcher generation for this case as well. | 546 # TODO(antonm): use common dispatcher generation for this case as well. |
547 has_optional = any(param_info.is_optional | 547 has_optional = any(param_info.is_optional |
548 for param_info in constructor_info.param_infos) | 548 for param_info in constructor_info.param_infos) |
549 | 549 |
| 550 factory_expression = self.FactoryConstructorExpression(constructor_info, |
| 551 factory_name, factory_constructor_name, factory_parameters) |
| 552 |
550 if not has_optional: | 553 if not has_optional: |
551 self._members_emitter.Emit( | 554 self._members_emitter.Emit( |
552 '\n $(METADATA)' | 555 '\n $(METADATA)' |
553 'factory $CTOR($PARAMS) => ' | 556 'factory $CTOR($PARAMS) => $FACTORY_EXPRESSION;\n', |
554 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', | |
555 CTOR=constructor_info._ConstructorFullName(self._DartType), | 557 CTOR=constructor_info._ConstructorFullName(self._DartType), |
556 PARAMS=constructor_info.ParametersAsDeclaration(self._DartType), | 558 PARAMS=constructor_info.ParametersAsDeclaration(self._DartType), |
557 FACTORY=factory_name, | 559 FACTORY_EXPRESSION=factory_expression, |
558 METADATA=metadata, | 560 METADATA=metadata) |
559 CTOR_FACTORY_NAME=factory_constructor_name, | |
560 FACTORY_PARAMS=factory_parameters) | |
561 else: | 561 else: |
562 inits = self._members_emitter.Emit( | 562 inits = self._members_emitter.Emit( |
563 '\n $(METADATA)' | 563 '\n $(METADATA)' |
564 'factory $CONSTRUCTOR($PARAMS) {\n' | 564 'factory $CONSTRUCTOR($PARAMS) {\n' |
565 ' var e = $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | 565 ' var e = $FACTORY_EXPRESSION;\n' |
566 '$!INITS' | 566 '$!INITS' |
567 ' return e;\n' | 567 ' return e;\n' |
568 ' }\n', | 568 ' }\n', |
569 CONSTRUCTOR=constructor_info._ConstructorFullName(self._DartType), | 569 CONSTRUCTOR=constructor_info._ConstructorFullName(self._DartType), |
570 METADATA=metadata, | 570 METADATA=metadata, |
571 FACTORY=factory_name, | 571 FACTORY_EXPRESSION=factory_expression, |
572 CTOR_FACTORY_NAME=factory_constructor_name, | 572 PARAMS=constructor_info.ParametersAsDeclaration(self._DartType)) |
573 PARAMS=constructor_info.ParametersAsDeclaration(self._DartType), | |
574 FACTORY_PARAMS=factory_parameters) | |
575 | 573 |
576 for index, param_info in enumerate(constructor_info.param_infos): | 574 for index, param_info in enumerate(constructor_info.param_infos): |
577 if param_info.is_optional: | 575 if param_info.is_optional: |
578 inits.Emit(' if ($E != null) e.$E = $E;\n', E=param_info.name) | 576 inits.Emit(' if ($E != null) e.$E = $E;\n', E=param_info.name) |
579 else: | 577 else: |
580 custom_factory_ctr = self._interface.id in _custom_factories | 578 custom_factory_ctr = self._interface.id in _custom_factories |
581 constructor_full_name = constructor_info._ConstructorFullName( | 579 constructor_full_name = constructor_info._ConstructorFullName( |
582 self._DartType) | 580 self._DartType) |
583 | 581 |
584 def GenerateCall( | 582 def GenerateCall( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 | 619 |
622 self._GenerateOverloadDispatcher( | 620 self._GenerateOverloadDispatcher( |
623 constructor_info, | 621 constructor_info, |
624 constructor_info.idl_args, | 622 constructor_info.idl_args, |
625 False, | 623 False, |
626 overload_declaration, | 624 overload_declaration, |
627 GenerateCall, | 625 GenerateCall, |
628 IsOptional, | 626 IsOptional, |
629 overload_emitter) | 627 overload_emitter) |
630 | 628 |
| 629 def FactoryConstructorExpression(self, constructor_info, |
| 630 factory_name, factory_constructor_name, factory_parameters): |
| 631 return emitter.Format( |
| 632 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS)', |
| 633 FACTORY=factory_name, |
| 634 CTOR_FACTORY_NAME=factory_constructor_name, |
| 635 FACTORY_PARAMS=factory_parameters); |
| 636 |
631 def _AddFutureifiedOperation(self, info, html_name): | 637 def _AddFutureifiedOperation(self, info, html_name): |
632 """Given a API function that uses callbacks, convert it to using Futures. | 638 """Given a API function that uses callbacks, convert it to using Futures. |
633 | 639 |
634 This conversion assumes the success callback is always provided before the | 640 This conversion assumes the success callback is always provided before the |
635 error callback (and so far in the DOM API, this is the case).""" | 641 error callback (and so far in the DOM API, this is the case).""" |
636 callback_info = GetCallbackInfo( | 642 callback_info = GetCallbackInfo( |
637 self._database.GetInterface(info.callback_args[0].type_id)) | 643 self._database.GetInterface(info.callback_args[0].type_id)) |
638 | 644 |
639 param_list = info.ParametersAsArgumentList() | 645 param_list = info.ParametersAsArgumentList() |
640 metadata = '' | 646 metadata = '' |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
778 return self._type_registry.TypeInfo(type_name).narrow_dart_type() | 784 return self._type_registry.TypeInfo(type_name).narrow_dart_type() |
779 | 785 |
780 def _NarrowInputType(self, type_name): | 786 def _NarrowInputType(self, type_name): |
781 return self._NarrowToImplementationType(type_name) | 787 return self._NarrowToImplementationType(type_name) |
782 | 788 |
783 def _DartType(self, type_name): | 789 def _DartType(self, type_name): |
784 return self._type_registry.DartType(type_name) | 790 return self._type_registry.DartType(type_name) |
785 | 791 |
786 def _TypeInfo(self, type_name): | 792 def _TypeInfo(self, type_name): |
787 return self._type_registry.TypeInfo(type_name) | 793 return self._type_registry.TypeInfo(type_name) |
OLD | NEW |