| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 | 76 |
| 77 for attr in sorted(interface.attributes, ConstantOutputOrder): | 77 for attr in sorted(interface.attributes, ConstantOutputOrder): |
| 78 if attr.type.id != 'EventHandler' and attr.type.id != 'EventListener': | 78 if attr.type.id != 'EventHandler' and attr.type.id != 'EventListener': |
| 79 self.AddAttribute(attr, declare_only) | 79 self.AddAttribute(attr, declare_only) |
| 80 | 80 |
| 81 # The implementation should define an indexer if the interface directly | 81 # The implementation should define an indexer if the interface directly |
| 82 # extends List. | 82 # extends List. |
| 83 element_type = None | 83 element_type = None |
| 84 requires_indexer = False | 84 requires_indexer = False |
| 85 if self._interface_type_info.list_item_type(): | 85 if self._interface_type_info.list_item_type(): |
| 86 self.AddIndexer(self._interface_type_info.list_item_type()) | 86 self.AddIndexer(self._interface_type_info.list_item_type(), |
| 87 self._interface_type_info.list_item_type_nullable()) |
| 87 else: | 88 else: |
| 88 for parent in self._database.Hierarchy(self._interface): | 89 for parent in self._database.Hierarchy(self._interface): |
| 89 if parent == self._interface: | 90 if parent == self._interface: |
| 90 continue | 91 continue |
| 91 parent_type_info = self._type_registry.TypeInfo(parent.id) | 92 parent_type_info = self._type_registry.TypeInfo(parent.id) |
| 92 if parent_type_info.list_item_type(): | 93 if parent_type_info.list_item_type(): |
| 93 self.AmendIndexer(parent_type_info.list_item_type()) | 94 self.AmendIndexer(parent_type_info.list_item_type()) |
| 94 break | 95 break |
| 95 | 96 |
| 96 # Group overloaded operations by name. | 97 # Group overloaded operations by name. |
| (...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 return | 753 return |
| 753 else: | 754 else: |
| 754 template = '\n $TYPE $NAME($PARAMS);\n' | 755 template = '\n $TYPE $NAME($PARAMS);\n' |
| 755 | 756 |
| 756 self._members_emitter.Emit( | 757 self._members_emitter.Emit( |
| 757 template, | 758 template, |
| 758 TYPE=return_type_name, | 759 TYPE=return_type_name, |
| 759 NAME=method_name, | 760 NAME=method_name, |
| 760 PARAMS=operation.ParametersAsDeclaration(self._DartType)) | 761 PARAMS=operation.ParametersAsDeclaration(self._DartType)) |
| 761 | 762 |
| 762 def EmitListMixin(self, element_name): | 763 def EmitListMixin(self, element_name, nullable): |
| 763 # TODO(sra): Use separate mixins for mutable implementations of List<T>. | 764 # TODO(sra): Use separate mixins for mutable implementations of List<T>. |
| 764 # TODO(sra): Use separate mixins for typed array implementations of List<T>. | 765 # TODO(sra): Use separate mixins for typed array implementations of List<T>. |
| 765 template_file = 'immutable_list_mixin.darttemplate' | 766 template_file = 'immutable_list_mixin.darttemplate' |
| 766 has_length = False | 767 has_length = False |
| 767 has_length_setter = False | 768 has_length_setter = False |
| 768 | 769 |
| 769 def _HasExplicitIndexedGetter(self): | 770 def _HasExplicitIndexedGetter(self): |
| 770 return any(op.id == 'getItem' for op in self._interface.operations) | 771 return any(op.id == 'getItem' for op in self._interface.operations) |
| 771 | 772 |
| 772 def _HasCustomIndexedGetter(self): | 773 def _HasCustomIndexedGetter(self): |
| (...skipping 15 matching lines...) Expand all Loading... |
| 788 has_num_items = any(attr.id == 'numberOfItems' | 789 has_num_items = any(attr.id == 'numberOfItems' |
| 789 for attr in self._interface.attributes) | 790 for attr in self._interface.attributes) |
| 790 | 791 |
| 791 template = self._template_loader.Load( | 792 template = self._template_loader.Load( |
| 792 template_file, | 793 template_file, |
| 793 { | 794 { |
| 794 'DEFINE_LENGTH_AS_NUM_ITEMS': not has_length and has_num_items, | 795 'DEFINE_LENGTH_AS_NUM_ITEMS': not has_length and has_num_items, |
| 795 'DEFINE_LENGTH_SETTER': not has_length_setter, | 796 'DEFINE_LENGTH_SETTER': not has_length_setter, |
| 796 'USE_NATIVE_INDEXED_GETTER': _HasNativeIndexedGetter(self) or _HasExpl
icitIndexedGetter(self), | 797 'USE_NATIVE_INDEXED_GETTER': _HasNativeIndexedGetter(self) or _HasExpl
icitIndexedGetter(self), |
| 797 }) | 798 }) |
| 798 self._members_emitter.Emit(template, E=element_name, GETTER=getter_name) | 799 if nullable: |
| 800 element_js = element_name + "|Null" |
| 801 else: |
| 802 element_js = element_name |
| 803 self._members_emitter.Emit(template, E=element_name, EJS=element_js, |
| 804 GETTER=getter_name) |
| 799 | 805 |
| 800 def SecureOutputType(self, type_name, is_dart_type=False, | 806 def SecureOutputType(self, type_name, is_dart_type=False, |
| 801 can_narrow_type=False): | 807 can_narrow_type=False): |
| 802 """ Converts the type name to the secure type name for return types. | 808 """ Converts the type name to the secure type name for return types. |
| 803 Arguments: | 809 Arguments: |
| 804 can_narrow_type - True if the output type can be narrowed further than | 810 can_narrow_type - True if the output type can be narrowed further than |
| 805 what would be accepted for input, used to narrow num APIs down to double | 811 what would be accepted for input, used to narrow num APIs down to double |
| 806 or int. | 812 or int. |
| 807 """ | 813 """ |
| 808 if is_dart_type: | 814 if is_dart_type: |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 converted_arguments.append(temp_name) | 869 converted_arguments.append(temp_name) |
| 864 param_type = temp_type | 870 param_type = temp_type |
| 865 verified_type = temp_type # verified by assignment in checked mode. | 871 verified_type = temp_type # verified by assignment in checked mode. |
| 866 else: | 872 else: |
| 867 converted_arguments.append(info.param_infos[position].name) | 873 converted_arguments.append(info.param_infos[position].name) |
| 868 if self._database.HasTypeDef(arg.type.id): | 874 if self._database.HasTypeDef(arg.type.id): |
| 869 param_type = 'dynamic' | 875 param_type = 'dynamic' |
| 870 else: | 876 else: |
| 871 param_type = self._NarrowInputType(arg.type.id) | 877 param_type = self._NarrowInputType(arg.type.id) |
| 872 # Verified by argument checking on entry to the dispatcher. | 878 # Verified by argument checking on entry to the dispatcher. |
| 873 | 879 |
| 874 verified_type = self._InputType( | 880 verified_type = self._InputType( |
| 875 info.param_infos[position].type_id, info) | 881 info.param_infos[position].type_id, info) |
| 876 # The native method does not need an argument type if we know the type
. | 882 # The native method does not need an argument type if we know the type
. |
| 877 # But we do need the native methods to have correct function types, so | 883 # But we do need the native methods to have correct function types, so |
| 878 # be conservative. | 884 # be conservative. |
| 879 if param_type == verified_type: | 885 if param_type == verified_type: |
| 880 if param_type in ['String', 'num', 'int', 'double', 'bool', 'Object'
]: | 886 if param_type in ['String', 'num', 'int', 'double', 'bool', 'Object'
]: |
| 881 param_type = 'dynamic' | 887 param_type = 'dynamic' |
| 882 | 888 |
| 883 target_parameters.append( | 889 target_parameters.append( |
| 884 '%s%s' % (TypeOrNothing(param_type), param_name)) | 890 '%s%s' % (TypeOrNothing(param_type), param_name)) |
| 885 | 891 |
| 886 return target_parameters, converted_arguments | 892 return target_parameters, converted_arguments |
| 887 | 893 |
| 888 def _InputType(self, type_name, info): | 894 def _InputType(self, type_name, info): |
| 889 conversion = self._InputConversion(type_name, info.declared_name) | 895 conversion = self._InputConversion(type_name, info.declared_name) |
| 890 if conversion: | 896 if conversion: |
| 891 return conversion.input_type | 897 return conversion.input_type |
| 892 else: | 898 else: |
| 893 # If typedef it's a union return dynamic. | 899 # If typedef it's a union return dynamic. |
| 894 if self._database.HasTypeDef(type_name): | 900 if self._database.HasTypeDef(type_name): |
| 895 return 'dynamic' | 901 return 'dynamic' |
| 896 else: | 902 else: |
| 897 return self._NarrowInputType(type_name) if type_name else 'dynamic' | 903 return self._NarrowInputType(type_name) if type_name else 'dynamic' |
| OLD | NEW |