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 systems to generate | 6 """This module provides shared functionality for systems to generate |
7 Dart APIs from the IDL database.""" | 7 Dart APIs from the IDL database.""" |
8 | 8 |
9 import copy | 9 import copy |
10 import json | 10 import json |
11 import monitored | 11 import monitored |
12 import os | 12 import os |
13 import re | 13 import re |
14 from htmlrenamer import html_interface_renames, typed_array_renames | 14 from htmlrenamer import html_interface_renames, typed_array_renames |
15 | 15 |
16 _pure_interfaces = monitored.Set('generator._pure_interfaces', [ | 16 _pure_interfaces = monitored.Set('generator._pure_interfaces', [ |
17 # TODO(sra): DOMStringMap should be a class implementing Map<String,String>. | 17 # TODO(sra): DOMStringMap should be a class implementing Map<String,String>. |
18 'DOMStringMap', | 18 'DOMStringMap', |
19 'ChildNode', | 19 'ChildNode', |
20 'EventListener', | 20 'EventListener', |
| 21 'EventHandler', |
21 'MediaQueryListListener', | 22 'MediaQueryListListener', |
22 'MutationCallback', | 23 'MutationCallback', |
23 'ParentNode', | 24 'ParentNode', |
24 'SVGExternalResourcesRequired', | 25 'SVGExternalResourcesRequired', |
25 'SVGFilterPrimitiveStandardAttributes', | 26 'SVGFilterPrimitiveStandardAttributes', |
26 'SVGFitToViewBox', | 27 'SVGFitToViewBox', |
27 'SVGTests', | 28 'SVGTests', |
28 'SVGURIReference', | 29 'SVGURIReference', |
29 'SVGZoomAndPan', | 30 'SVGZoomAndPan', |
30 'TimeoutHandler', | 31 'TimeoutHandler', |
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 | 777 |
777 def merged_into(self): | 778 def merged_into(self): |
778 return self._data.merged_into | 779 return self._data.merged_into |
779 | 780 |
780 | 781 |
781 class CallbackIDLTypeInfo(IDLTypeInfo): | 782 class CallbackIDLTypeInfo(IDLTypeInfo): |
782 def __init__(self, idl_type, data): | 783 def __init__(self, idl_type, data): |
783 super(CallbackIDLTypeInfo, self).__init__(idl_type, data) | 784 super(CallbackIDLTypeInfo, self).__init__(idl_type, data) |
784 | 785 |
785 | 786 |
| 787 def array_type(data_type): |
| 788 matched = re.match(r'([\w\d_\s]+)\[\]', data_type) |
| 789 if not matched: |
| 790 return None |
| 791 return matched.group(1) |
| 792 |
786 class SequenceIDLTypeInfo(IDLTypeInfo): | 793 class SequenceIDLTypeInfo(IDLTypeInfo): |
787 def __init__(self, idl_type, data, item_info): | 794 def __init__(self, idl_type, data, item_info): |
788 super(SequenceIDLTypeInfo, self).__init__(idl_type, data) | 795 super(SequenceIDLTypeInfo, self).__init__(idl_type, data) |
789 self._item_info = item_info | 796 self._item_info = item_info |
790 | 797 |
791 def dart_type(self): | 798 def dart_type(self): |
792 return 'List<%s>' % self._item_info.dart_type() | 799 return 'List<%s>' % self._item_info.dart_type() |
793 | 800 |
794 def interface_name(self): | 801 def interface_name(self): |
795 return self.dart_type() | 802 return self.dart_type() |
796 | 803 |
797 def implementation_name(self): | 804 def implementation_name(self): |
798 return self.dart_type() | 805 return self.dart_type() |
799 | 806 |
800 def vector_to_dart_template_parameter(self): | 807 def vector_to_dart_template_parameter(self): |
801 raise Exception('sequences of sequences are not supported yet') | 808 raise Exception('sequences of sequences are not supported yet') |
802 | 809 |
803 def to_native_info(self, idl_node, interface_name): | 810 def to_native_info(self, idl_node, interface_name): |
804 item_native_type = self._item_info.vector_to_dart_template_parameter() | 811 item_native_type = self._item_info.vector_to_dart_template_parameter() |
805 if isinstance(self._item_info, PrimitiveIDLTypeInfo): | 812 if isinstance(self._item_info, PrimitiveIDLTypeInfo): |
806 return '%s', 'Vector<%s>' % item_native_type, 'DartUtilities', 'toNativeVe
ctor<%s>' % item_native_type | 813 return '%s', 'Vector<%s>' % item_native_type, 'DartUtilities', 'toNativeVe
ctor<%s>' % item_native_type |
807 return '%s', 'Vector< RefPtr<%s> >' % item_native_type, 'DartUtilities', 'to
NativeVector< RefPtr<%s> >' % item_native_type | 814 return '%s', 'Vector< RefPtr<%s> >' % item_native_type, 'DartUtilities', 'to
NativeVector< RefPtr<%s> >' % item_native_type |
808 | 815 |
809 def parameter_type(self): | 816 def parameter_type(self): |
810 return self.native_type() | 817 native_type = self.native_type() |
| 818 if array_type(native_type): |
| 819 return 'const Vector<RefPtr<%s> > &' % array_type(native_type) |
| 820 |
| 821 return native_type |
811 | 822 |
812 def pass_native_by_ref(self): return True | 823 def pass_native_by_ref(self): return True |
813 | 824 |
814 def to_dart_conversion(self, value, interface_name=None, attributes=None): | 825 def to_dart_conversion(self, value, interface_name=None, attributes=None): |
815 if isinstance(self._item_info, PrimitiveIDLTypeInfo): | 826 if isinstance(self._item_info, PrimitiveIDLTypeInfo): |
816 return 'DartDOMWrapper::vectorToDart(%s)' % value | 827 return 'DartDOMWrapper::vectorToDart(%s)' % value |
817 return 'DartDOMWrapper::vectorToDart<%s>(%s)' % (self._item_info.bindings_cl
ass(), value) | 828 return 'DartDOMWrapper::vectorToDart<%s>(%s)' % (self._item_info.bindings_cl
ass(), value) |
818 | 829 |
819 def conversion_includes(self): | 830 def conversion_includes(self): |
820 return self._item_info.conversion_includes() | 831 return self._item_info.conversion_includes() |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1023 'SerializedScriptValue': TypeData(clazz='Primitive', dart_type='dynamic'), | 1034 'SerializedScriptValue': TypeData(clazz='Primitive', dart_type='dynamic'), |
1024 'sequence': TypeData(clazz='Primitive', dart_type='List'), | 1035 'sequence': TypeData(clazz='Primitive', dart_type='List'), |
1025 'void': TypeData(clazz='Primitive', dart_type='void'), | 1036 'void': TypeData(clazz='Primitive', dart_type='void'), |
1026 | 1037 |
1027 'CSSRule': TypeData(clazz='Interface', conversion_includes=['CSSImportRule']
), | 1038 'CSSRule': TypeData(clazz='Interface', conversion_includes=['CSSImportRule']
), |
1028 'DOMStringMap': TypeData(clazz='Interface', dart_type='Map<String, String>')
, | 1039 'DOMStringMap': TypeData(clazz='Interface', dart_type='Map<String, String>')
, |
1029 'Window': TypeData(clazz='Interface', custom_to_dart=True), | 1040 'Window': TypeData(clazz='Interface', custom_to_dart=True), |
1030 'Element': TypeData(clazz='Interface', merged_interface='HTMLElement', | 1041 'Element': TypeData(clazz='Interface', merged_interface='HTMLElement', |
1031 custom_to_dart=True), | 1042 custom_to_dart=True), |
1032 'EventListener': TypeData(clazz='Interface', custom_to_native=True), | 1043 'EventListener': TypeData(clazz='Interface', custom_to_native=True), |
| 1044 'EventHandler': TypeData(clazz='Interface', custom_to_native=True), |
1033 'EventTarget': TypeData(clazz='Interface', custom_to_native=True), | 1045 'EventTarget': TypeData(clazz='Interface', custom_to_native=True), |
1034 'HTMLElement': TypeData(clazz='Interface', merged_into='Element', | 1046 'HTMLElement': TypeData(clazz='Interface', merged_into='Element', |
1035 custom_to_dart=True), | 1047 custom_to_dart=True), |
1036 'IDBAny': TypeData(clazz='Interface', dart_type='dynamic', custom_to_native=
True), | 1048 'IDBAny': TypeData(clazz='Interface', dart_type='dynamic', custom_to_native=
True), |
1037 'MutationRecordArray': TypeData(clazz='Interface', # C++ pass by pointer. | 1049 'MutationRecordArray': TypeData(clazz='Interface', # C++ pass by pointer. |
1038 native_type='MutationRecordArray', dart_type='List<MutationRecord>'), | 1050 native_type='MutationRecordArray', dart_type='List<MutationRecord>'), |
1039 'StyleSheet': TypeData(clazz='Interface', conversion_includes=['CSSStyleShee
t']), | 1051 'StyleSheet': TypeData(clazz='Interface', conversion_includes=['CSSStyleShee
t']), |
1040 'SVGElement': TypeData(clazz='Interface', custom_to_dart=True), | 1052 'SVGElement': TypeData(clazz='Interface', custom_to_dart=True), |
1041 | 1053 |
1042 'ClientRectList': TypeData(clazz='Interface', | 1054 'ClientRectList': TypeData(clazz='Interface', |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1135 def DartType(self, type_name): | 1147 def DartType(self, type_name): |
1136 return self.TypeInfo(type_name).dart_type() | 1148 return self.TypeInfo(type_name).dart_type() |
1137 | 1149 |
1138 def _TypeInfo(self, type_name): | 1150 def _TypeInfo(self, type_name): |
1139 match = re.match(r'(?:sequence<(\w+)>|(\w+)\[\])$', type_name) | 1151 match = re.match(r'(?:sequence<(\w+)>|(\w+)\[\])$', type_name) |
1140 if match: | 1152 if match: |
1141 type_data = TypeData('Sequence') | 1153 type_data = TypeData('Sequence') |
1142 item_info = self.TypeInfo(match.group(1) or match.group(2)) | 1154 item_info = self.TypeInfo(match.group(1) or match.group(2)) |
1143 # TODO(vsm): Generalize this code. | 1155 # TODO(vsm): Generalize this code. |
1144 if 'SourceInfo' in type_name: | 1156 if 'SourceInfo' in type_name: |
1145 type_data.native_type = 'Vector<RefPtr<SourceInfo> >' | 1157 type_data.native_type = 'const Vector<RefPtr<SourceInfo> >& ' |
1146 return SequenceIDLTypeInfo(type_name, type_data, item_info) | 1158 return SequenceIDLTypeInfo(type_name, type_data, item_info) |
1147 | 1159 |
1148 if not type_name in _idl_type_registry: | 1160 if not type_name in _idl_type_registry: |
1149 if self._database.HasEnum(type_name): | 1161 if self._database.HasEnum(type_name): |
1150 return PrimitiveIDLTypeInfo( | 1162 return PrimitiveIDLTypeInfo( |
1151 type_name, | 1163 type_name, |
1152 TypeData(clazz='Primitive', dart_type='String', native_type='String'
)) | 1164 TypeData(clazz='Primitive', dart_type='String', native_type='String'
)) |
1153 | 1165 |
1154 interface = self._database.GetInterface(type_name) | 1166 interface = self._database.GetInterface(type_name) |
1155 if 'Callback' in interface.ext_attrs: | 1167 if 'Callback' in interface.ext_attrs: |
(...skipping 16 matching lines...) Expand all Loading... |
1172 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name, | 1184 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name, |
1173 self) | 1185 self) |
1174 | 1186 |
1175 if type_data.clazz == 'SVGTearOff': | 1187 if type_data.clazz == 'SVGTearOff': |
1176 dart_interface_name = self._renamer.RenameInterface( | 1188 dart_interface_name = self._renamer.RenameInterface( |
1177 self._database.GetInterface(type_name)) | 1189 self._database.GetInterface(type_name)) |
1178 return SVGTearOffIDLTypeInfo( | 1190 return SVGTearOffIDLTypeInfo( |
1179 type_name, type_data, dart_interface_name, self) | 1191 type_name, type_data, dart_interface_name, self) |
1180 | 1192 |
1181 if type_data.clazz == 'TypedList': | 1193 if type_data.clazz == 'TypedList': |
1182 dart_interface_name = self._renamer.RenameInterface( | 1194 dart_interface_name = self._renamer.RenameInterfaceId(type_name) |
1183 self._database.GetInterface(type_name)) | |
1184 return TypedListIDLTypeInfo( | 1195 return TypedListIDLTypeInfo( |
1185 type_name, type_data, dart_interface_name, self) | 1196 type_name, type_data, dart_interface_name, self) |
1186 | 1197 |
1187 if type_data.clazz == 'BasicTypedList': | 1198 if type_data.clazz == 'BasicTypedList': |
1188 if type_name == 'ArrayBuffer': | 1199 if type_name == 'ArrayBuffer': |
1189 dart_interface_name = 'ByteBuffer' | 1200 dart_interface_name = 'ByteBuffer' |
1190 else: | 1201 else: |
1191 dart_interface_name = self._renamer.RenameInterface( | 1202 dart_interface_name = self._renamer.RenameInterfaceId(type_name) |
1192 self._database.GetInterface(type_name)) | |
1193 return BasicTypedListIDLTypeInfo( | 1203 return BasicTypedListIDLTypeInfo( |
1194 type_name, type_data, dart_interface_name, self) | 1204 type_name, type_data, dart_interface_name, self) |
1195 | 1205 |
1196 class_name = '%sIDLTypeInfo' % type_data.clazz | 1206 class_name = '%sIDLTypeInfo' % type_data.clazz |
1197 return globals()[class_name](type_name, type_data) | 1207 return globals()[class_name](type_name, type_data) |
OLD | NEW |