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, \ |
11 DartDomNameOfAttribute, FindMatchingAttribute, IsPureInterface, \ | 11 DartDomNameOfAttribute, FindMatchingAttribute, IsPureInterface, \ |
12 TypeOrNothing, ConvertToFuture, GetCallbackInfo | 12 TypeOrNothing, ConvertToFuture, GetCallbackInfo |
13 from copy import deepcopy | 13 from copy import deepcopy |
14 from htmlrenamer import convert_to_future_members, custom_html_constructors, \ | 14 from htmlrenamer import convert_to_future_members, custom_html_constructors, \ |
15 keep_overloaded_members, overloaded_and_renamed, private_html_members, \ | 15 keep_overloaded_members, overloaded_and_renamed, private_html_members, \ |
16 renamed_html_members, renamed_overloads, removed_html_members | 16 renamed_html_members, renamed_overloads, removed_html_members |
| 17 from generator import TypeOrVar |
17 import logging | 18 import logging |
18 import monitored | 19 import monitored |
19 import sys | 20 import sys |
20 | 21 |
21 _logger = logging.getLogger('htmldartgenerator') | 22 _logger = logging.getLogger('htmldartgenerator') |
22 | 23 |
23 # Types that are accessible cross-frame in a limited fashion. | 24 # Types that are accessible cross-frame in a limited fashion. |
24 # In these cases, the base type (e.g., WindowBase) provides restricted access | 25 # In these cases, the base type (e.g., WindowBase) provides restricted access |
25 # while the subtype (e.g., Window) provides full access to the | 26 # while the subtype (e.g., Window) provides full access to the |
26 # corresponding objects if there are from the same frame. | 27 # corresponding objects if there are from the same frame. |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 member and not include invocation code. | 318 member and not include invocation code. |
318 """ | 319 """ |
319 # FIXME: When we pass in operations[0] below, we're assuming all | 320 # FIXME: When we pass in operations[0] below, we're assuming all |
320 # overloaded operations have the same security attributes. This | 321 # overloaded operations have the same security attributes. This |
321 # is currently true, but we should consider filtering earlier or | 322 # is currently true, but we should consider filtering earlier or |
322 # merging the relevant data into info itself. | 323 # merging the relevant data into info itself. |
323 method_name = self._renamer.RenameMember(self._interface.id, | 324 method_name = self._renamer.RenameMember(self._interface.id, |
324 info.operations[0], | 325 info.operations[0], |
325 info.name, | 326 info.name, |
326 'call:') | 327 'call:') |
| 328 |
327 if not method_name: | 329 if not method_name: |
328 if info.name == 'item': | 330 if info.name == 'item': |
329 # FIXME: item should be renamed to operator[], not removed. | 331 # FIXME: item should be renamed to operator[], not removed. |
330 self.EmitOperation(info, '_item') | 332 self.EmitOperation(info, '_item') |
331 return | 333 return |
332 | 334 |
333 if declare_only: | 335 if declare_only: |
334 self.DeclareOperation(info, | 336 self.DeclareOperation(info, |
335 self.SecureOutputType(info.type_name), method_name) | 337 self.SecureOutputType(info.type_name), method_name) |
336 else: | 338 else: |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 factory_name to call (calls an autogenerated FactoryProvider | 521 factory_name to call (calls an autogenerated FactoryProvider |
520 if unspecified) | 522 if unspecified) |
521 """ | 523 """ |
522 for constructor_info in constructors: | 524 for constructor_info in constructors: |
523 self._AddConstructor( | 525 self._AddConstructor( |
524 constructor_info, factory_name, factory_constructor_name) | 526 constructor_info, factory_name, factory_constructor_name) |
525 | 527 |
526 def _AddConstructor(self, | 528 def _AddConstructor(self, |
527 constructor_info, factory_name, factory_constructor_name): | 529 constructor_info, factory_name, factory_constructor_name): |
528 # Hack to ignore the Image constructor used by JavaScript. | 530 # Hack to ignore the Image constructor used by JavaScript. |
529 if (self._interface.id == 'HTMLImageElement' | 531 if ((self._interface.id == 'HTMLImageElement' or self._interface.id == 'Blob
') |
530 and not constructor_info.pure_dart_constructor): | 532 and not constructor_info.pure_dart_constructor): |
531 return | 533 return |
532 | 534 |
533 if self.GenerateCustomFactory(constructor_info): | 535 if self.GenerateCustomFactory(constructor_info): |
534 return | 536 return |
535 | 537 |
536 metadata = self._metadata.GetFormattedMetadata( | 538 metadata = self._metadata.GetFormattedMetadata( |
537 self._library_name, self._interface, self._interface.id, ' ') | 539 self._library_name, self._interface, self._interface.id, ' ') |
538 | 540 |
539 if not factory_constructor_name: | 541 if not factory_constructor_name: |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 custom_factory_ctr = self._interface.id in _custom_factories | 590 custom_factory_ctr = self._interface.id in _custom_factories |
589 constructor_full_name = constructor_info._ConstructorFullName( | 591 constructor_full_name = constructor_info._ConstructorFullName( |
590 self._DartType) | 592 self._DartType) |
591 | 593 |
592 def GenerateCall( | 594 def GenerateCall( |
593 stmts_emitter, call_emitter, | 595 stmts_emitter, call_emitter, |
594 version, signature_index, argument_count): | 596 version, signature_index, argument_count): |
595 name = emitter.Format('_create_$VERSION', VERSION=version) | 597 name = emitter.Format('_create_$VERSION', VERSION=version) |
596 arguments = constructor_info.idl_args[signature_index][:argument_count] | 598 arguments = constructor_info.idl_args[signature_index][:argument_count] |
597 args = None | 599 args = None |
| 600 call_template = '' |
598 if self._dart_use_blink: | 601 if self._dart_use_blink: |
599 type_ids = [p.type.id for p in arguments] | 602 type_ids = [p.type.id for p in arguments] |
600 base_name, rs = \ | 603 base_name, rs = \ |
601 self.DeriveNativeEntry("constructorCallback", 'Constructor', arg
ument_count) | 604 self.DeriveNativeEntry("constructorCallback", 'Constructor', arg
ument_count) |
602 qualified_name = \ | 605 qualified_name = \ |
603 self.DeriveQualifiedBlinkName(self._interface.id, | 606 self.DeriveQualifiedBlinkName(self._interface.id, |
604 base_name) | 607 base_name) |
605 args = constructor_info.ParametersAsArgumentList(argument_count) | 608 args = constructor_info.ParametersAsArgumentList(argument_count) |
| 609 |
| 610 # Handle converting Maps to Dictionaries, etc. |
| 611 (factory_params, converted_arguments) = self._ConvertArgumentTypes( |
| 612 stmts_emitter, arguments, argument_count, constructor_info) |
| 613 args = ', '.join(converted_arguments) |
| 614 call_template = 'wrap_jso($FACTORY_NAME($FACTORY_PARAMS))' |
606 else: | 615 else: |
607 qualified_name = emitter.Format( | 616 qualified_name = emitter.Format( |
608 '$FACTORY.$NAME', | 617 '$FACTORY.$NAME', |
609 FACTORY=factory_name, | 618 FACTORY=factory_name, |
610 NAME=name) | 619 NAME=name) |
611 (factory_params, converted_arguments) = self._ConvertArgumentTypes( | 620 (factory_params, converted_arguments) = self._ConvertArgumentTypes( |
612 stmts_emitter, arguments, argument_count, constructor_info) | 621 stmts_emitter, arguments, argument_count, constructor_info) |
613 args = ', '.join(converted_arguments) | 622 args = ', '.join(converted_arguments) |
614 call_emitter.Emit('$FACTORY_NAME($FACTORY_PARAMS)', | 623 call_template = '$FACTORY_NAME($FACTORY_PARAMS)' |
615 FACTORY_NAME=qualified_name, | 624 call_emitter.Emit(call_template, FACTORY_NAME=qualified_name, FACTORY_PA
RAMS=args) |
616 FACTORY_PARAMS=args) | |
617 self.EmitStaticFactoryOverload(constructor_info, name, arguments) | 625 self.EmitStaticFactoryOverload(constructor_info, name, arguments) |
618 | 626 |
619 def IsOptional(signature_index, argument): | 627 def IsOptional(signature_index, argument): |
620 return self.IsConstructorArgumentOptional(argument) | 628 return self.IsConstructorArgumentOptional(argument) |
621 | 629 |
622 entry_declaration = emitter.Format( | 630 entry_declaration = emitter.Format( |
623 '$(METADATA)$FACTORY_KEYWORD $CTOR($PARAMS)', | 631 '$(METADATA)$FACTORY_KEYWORD $CTOR($PARAMS)', |
624 FACTORY_KEYWORD=('factory' if not custom_factory_ctr else | 632 FACTORY_KEYWORD=('factory' if not custom_factory_ctr else |
625 'static %s' % constructor_full_name), | 633 'static %s' % constructor_full_name), |
626 CTOR=(('' if not custom_factory_ctr else '_factory') | 634 CTOR=(('' if not custom_factory_ctr else '_factory') |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 return self._type_registry.TypeInfo(type_name).narrow_dart_type() | 805 return self._type_registry.TypeInfo(type_name).narrow_dart_type() |
798 | 806 |
799 def _NarrowInputType(self, type_name): | 807 def _NarrowInputType(self, type_name): |
800 return self._NarrowToImplementationType(type_name) | 808 return self._NarrowToImplementationType(type_name) |
801 | 809 |
802 def _DartType(self, type_name): | 810 def _DartType(self, type_name): |
803 return self._type_registry.DartType(type_name) | 811 return self._type_registry.DartType(type_name) |
804 | 812 |
805 def _TypeInfo(self, type_name): | 813 def _TypeInfo(self, type_name): |
806 return self._type_registry.TypeInfo(type_name) | 814 return self._type_registry.TypeInfo(type_name) |
| 815 |
| 816 def _ConvertArgumentTypes(self, stmts_emitter, arguments, argument_count, info
): |
| 817 temp_version = [0] |
| 818 converted_arguments = [] |
| 819 target_parameters = [] |
| 820 for position, arg in enumerate(arguments[:argument_count]): |
| 821 conversion = self._InputConversion(arg.type.id, info.declared_name) |
| 822 param_name = arguments[position].id |
| 823 if conversion: |
| 824 temp_version[0] += 1 |
| 825 temp_name = '%s_%s' % (param_name, temp_version[0]) |
| 826 temp_type = conversion.output_type |
| 827 stmts_emitter.Emit( |
| 828 '$(INDENT)$TYPE $NAME = $CONVERT($ARG);\n', |
| 829 TYPE=TypeOrVar(temp_type), |
| 830 NAME=temp_name, |
| 831 CONVERT=conversion.function_name, |
| 832 ARG=info.param_infos[position].name) |
| 833 converted_arguments.append(temp_name) |
| 834 param_type = temp_type |
| 835 verified_type = temp_type # verified by assignment in checked mode. |
| 836 else: |
| 837 converted_arguments.append(info.param_infos[position].name) |
| 838 param_type = self._NarrowInputType(arg.type.id) |
| 839 # Verified by argument checking on entry to the dispatcher. |
| 840 |
| 841 verified_type = self._InputType( |
| 842 info.param_infos[position].type_id, info) |
| 843 # The native method does not need an argument type if we know the type. |
| 844 # But we do need the native methods to have correct function types, so |
| 845 # be conservative. |
| 846 if param_type == verified_type: |
| 847 if param_type in ['String', 'num', 'int', 'double', 'bool', 'Object']: |
| 848 param_type = 'dynamic' |
| 849 |
| 850 target_parameters.append( |
| 851 '%s%s' % (TypeOrNothing(param_type), param_name)) |
| 852 |
| 853 return target_parameters, converted_arguments |
| 854 |
| 855 def _InputType(self, type_name, info): |
| 856 conversion = self._InputConversion(type_name, info.declared_name) |
| 857 if conversion: |
| 858 return conversion.input_type |
| 859 else: |
| 860 return self._NarrowInputType(type_name) if type_name else 'dynamic' |
OLD | NEW |