| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2011, 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 generates Dart APIs from the IDL database.""" | 6 """This module generates Dart APIs from the IDL database.""" |
| 7 | 7 |
| 8 import emitter | 8 import emitter |
| 9 import idlnode | 9 import idlnode |
| 10 import logging | 10 import logging |
| 11 import multiemitter | 11 import multiemitter |
| 12 import os | 12 import os |
| 13 import re | 13 import re |
| 14 import shutil | 14 import shutil |
| 15 import snippet_manager | |
| 16 | 15 |
| 17 _logger = logging.getLogger('dartgenerator') | 16 _logger = logging.getLogger('dartgenerator') |
| 18 | 17 |
| 19 # IDL->Dart primitive types conversion. | 18 # IDL->Dart primitive types conversion. |
| 20 _idl_to_dart_type_conversions = { | 19 _idl_to_dart_type_conversions = { |
| 21 'any': 'Object', | 20 'any': 'Object', |
| 22 'custom': 'Dynamic', | 21 'custom': 'Dynamic', |
| 23 'boolean': 'bool', | 22 'boolean': 'bool', |
| 24 'DOMObject': 'Object', | 23 'DOMObject': 'Object', |
| 25 'DOMString': 'String', | 24 'DOMString': 'String', |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 def _IsDartListType(type): | 145 def _IsDartListType(type): |
| 147 return type == 'List' or type.startswith('List<') | 146 return type == 'List' or type.startswith('List<') |
| 148 | 147 |
| 149 def _IsDartCollectionType(type): | 148 def _IsDartCollectionType(type): |
| 150 return _IsDartListType(type) | 149 return _IsDartListType(type) |
| 151 | 150 |
| 152 | 151 |
| 153 class DartGenerator(object): | 152 class DartGenerator(object): |
| 154 """Utilities to generate Dart APIs and corresponding JavaScript.""" | 153 """Utilities to generate Dart APIs and corresponding JavaScript.""" |
| 155 | 154 |
| 156 def __init__(self, auxiliary_dir, snippet_dir, base_package): | 155 def __init__(self, auxiliary_dir, base_package): |
| 157 """Constructor for the DartGenerator. | 156 """Constructor for the DartGenerator. |
| 158 | 157 |
| 159 Args: | 158 Args: |
| 160 auxiliary_dir -- location of auxiliary handwritten classes | 159 auxiliary_dir -- location of auxiliary handwritten classes |
| 161 snippet_dir -- location of implementation snippets | |
| 162 base_package -- the base package name for the generated code. | 160 base_package -- the base package name for the generated code. |
| 163 """ | 161 """ |
| 164 self._auxiliary_dir = auxiliary_dir | 162 self._auxiliary_dir = auxiliary_dir |
| 165 self._snippet_manager = snippet_manager.SnippetManager(snippet_dir) | |
| 166 self._base_package = base_package | 163 self._base_package = base_package |
| 167 self._auxiliary_files = {} | 164 self._auxiliary_files = {} |
| 168 self._dart_templates_re = re.compile(r'[\w.:]+<([\w\.<>:]+)>') | 165 self._dart_templates_re = re.compile(r'[\w.:]+<([\w\.<>:]+)>') |
| 169 | 166 |
| 170 self._emitters = None # set later | 167 self._emitters = None # set later |
| 171 | 168 |
| 172 | 169 |
| 173 def _StripModules(self, type_name): | 170 def _StripModules(self, type_name): |
| 174 return type_name.split('::')[-1] | 171 return type_name.split('::')[-1] |
| 175 | 172 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 for a in and_annotations: | 341 for a in and_annotations: |
| 345 if a not in idl_node.annotations: | 342 if a not in idl_node.annotations: |
| 346 return False | 343 return False |
| 347 return True | 344 return True |
| 348 | 345 |
| 349 if HasAnnotations(interface): | 346 if HasAnnotations(interface): |
| 350 interface.constants = filter(HasAnnotations, interface.constants) | 347 interface.constants = filter(HasAnnotations, interface.constants) |
| 351 interface.attributes = filter(HasAnnotations, interface.attributes) | 348 interface.attributes = filter(HasAnnotations, interface.attributes) |
| 352 interface.operations = filter(HasAnnotations, interface.operations) | 349 interface.operations = filter(HasAnnotations, interface.operations) |
| 353 interface.parents = filter(HasAnnotations, interface.parents) | 350 interface.parents = filter(HasAnnotations, interface.parents) |
| 354 interface.snippets = filter(HasAnnotations, interface.snippets) | |
| 355 else: | 351 else: |
| 356 database.DeleteInterface(interface.id) | 352 database.DeleteInterface(interface.id) |
| 357 | 353 |
| 358 self.FilterMembersWithUnidentifiedTypes(database) | 354 self.FilterMembersWithUnidentifiedTypes(database) |
| 359 | 355 |
| 360 | 356 |
| 361 def Generate(self, database, output_dir, | 357 def Generate(self, database, output_dir, |
| 362 module_source_preference=[], source_filter=None, | 358 module_source_preference=[], source_filter=None, |
| 363 super_database=None, common_prefix=None, super_map={}, | 359 super_database=None, common_prefix=None, super_map={}, |
| 364 lib_dir = None): | 360 lib_dir = None): |
| (...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 753 | 749 |
| 754 self._dart_interface_file_paths.append(dart_interface_file_path) | 750 self._dart_interface_file_paths.append(dart_interface_file_path) |
| 755 | 751 |
| 756 dart_interface_code = self._emitters.FileEmitter(dart_interface_file_path) | 752 dart_interface_code = self._emitters.FileEmitter(dart_interface_file_path) |
| 757 | 753 |
| 758 template_file = 'template_interface_%s.darttemplate' % interface_name | 754 template_file = 'template_interface_%s.darttemplate' % interface_name |
| 759 if not os.path.exists(template_file): | 755 if not os.path.exists(template_file): |
| 760 template_file = 'template_interface.darttemplate' | 756 template_file = 'template_interface.darttemplate' |
| 761 template = ''.join(open(template_file).readlines()) | 757 template = ''.join(open(template_file).readlines()) |
| 762 | 758 |
| 763 snippet = self._snippet_manager.snippet_map.get(interface_name) | |
| 764 | |
| 765 return DartInterfaceGenerator( | 759 return DartInterfaceGenerator( |
| 766 interface, dart_interface_code, | 760 interface, dart_interface_code, |
| 767 template, | 761 template, |
| 768 snippet, common_prefix, super_interface_name, | 762 common_prefix, super_interface_name, |
| 769 source_filter) | 763 source_filter) |
| 770 | 764 |
| 771 | 765 |
| 772 def _StartGenerateWrappingImpl(self, output_dir): | 766 def _StartGenerateWrappingImpl(self, output_dir): |
| 773 """Prepared for generating wrapping implementation. | 767 """Prepared for generating wrapping implementation. |
| 774 | 768 |
| 775 - Creates emitter for JS code. | 769 - Creates emitter for JS code. |
| 776 - Creates emitter for Dart code. | 770 - Creates emitter for Dart code. |
| 777 """ | 771 """ |
| 778 js_file_name = os.path.join(output_dir, 'wrapping_dom.js') | 772 js_file_name = os.path.join(output_dir, 'wrapping_dom.js') |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1067 else: | 1061 else: |
| 1068 return '\n' | 1062 return '\n' |
| 1069 return ''.join(FormatLine(line) for line in text.split('\n')) | 1063 return ''.join(FormatLine(line) for line in text.split('\n')) |
| 1070 | 1064 |
| 1071 # ------------------------------------------------------------------------------ | 1065 # ------------------------------------------------------------------------------ |
| 1072 | 1066 |
| 1073 class DartInterfaceGenerator(object): | 1067 class DartInterfaceGenerator(object): |
| 1074 """Generates Dart Interface definition for one DOM IDL interface.""" | 1068 """Generates Dart Interface definition for one DOM IDL interface.""" |
| 1075 | 1069 |
| 1076 def __init__(self, interface, emitter, template, | 1070 def __init__(self, interface, emitter, template, |
| 1077 extra_snippets, common_prefix, super_interface, source_filter): | 1071 common_prefix, super_interface, source_filter): |
| 1078 """Generates Dart code for the given interface. | 1072 """Generates Dart code for the given interface. |
| 1079 | 1073 |
| 1080 Args: | 1074 Args: |
| 1081 interface -- an IDLInterface instance. It is assumed that all types have | 1075 interface -- an IDLInterface instance. It is assumed that all types have |
| 1082 been converted to Dart types (e.g. int, String), unless they are in the | 1076 been converted to Dart types (e.g. int, String), unless they are in the |
| 1083 same package as the interface. | 1077 same package as the interface. |
| 1084 extra_snippets -- additional snippet text with method signatures that were | |
| 1085 extracted from the implementation snippet file | |
| 1086 common_prefix -- the prefix for the common library, if any. | 1078 common_prefix -- the prefix for the common library, if any. |
| 1087 super_interface -- the name of the common interface that this interface | 1079 super_interface -- the name of the common interface that this interface |
| 1088 implements, if any. | 1080 implements, if any. |
| 1089 source_filter -- if specified, rewrites the names of any superinterfaces | 1081 source_filter -- if specified, rewrites the names of any superinterfaces |
| 1090 that are not from these sources to use the common prefix. | 1082 that are not from these sources to use the common prefix. |
| 1091 """ | 1083 """ |
| 1092 self._interface = interface | 1084 self._interface = interface |
| 1093 self._emitter = emitter | 1085 self._emitter = emitter |
| 1094 self._template = template | 1086 self._template = template |
| 1095 self._extra_snippets = extra_snippets | |
| 1096 self._common_prefix = common_prefix | 1087 self._common_prefix = common_prefix |
| 1097 self._super_interface = super_interface | 1088 self._super_interface = super_interface |
| 1098 self._source_filter = source_filter | 1089 self._source_filter = source_filter |
| 1099 | 1090 |
| 1100 | 1091 |
| 1101 def StartInterface(self): | 1092 def StartInterface(self): |
| 1102 if self._super_interface: | 1093 if self._super_interface: |
| 1103 typename = self._super_interface | 1094 typename = self._super_interface |
| 1104 else: | 1095 else: |
| 1105 typename = self._interface.id | 1096 typename = self._interface.id |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1146 ' $CTOR(int length);\n' | 1137 ' $CTOR(int length);\n' |
| 1147 '\n' | 1138 '\n' |
| 1148 ' $CTOR.fromList(List<$TYPE> list);\n' | 1139 ' $CTOR.fromList(List<$TYPE> list);\n' |
| 1149 '\n' | 1140 '\n' |
| 1150 ' $CTOR.fromBuffer(ArrayBuffer buffer);\n', | 1141 ' $CTOR.fromBuffer(ArrayBuffer buffer);\n', |
| 1151 CTOR=self._interface.id, | 1142 CTOR=self._interface.id, |
| 1152 TYPE=element_type) | 1143 TYPE=element_type) |
| 1153 | 1144 |
| 1154 | 1145 |
| 1155 def FinishInterface(self): | 1146 def FinishInterface(self): |
| 1156 # Write snippet text that was inlined in the IDL. | |
| 1157 for snippet in self._interface.snippets: | |
| 1158 self._members_emitter.Emit('\n$LINES', | |
| 1159 LINES=IndentText(snippets.text, ' ')) | |
| 1160 | |
| 1161 # TODO(vsm): Test if snippets are extra methods or extra types. | |
| 1162 # Since Dart doesn't permit inner types, append after the interface. | |
| 1163 # Consider moving these types to auxilary classes instead. | |
| 1164 if self._extra_snippets is not None: | |
| 1165 if 'interface' in self._extra_snippets: | |
| 1166 self._top_level_emitter.Emit('\n$TEXT', TEXT=self._extra_snippets) | |
| 1167 else: | |
| 1168 self._members_emitter.Emit('\n$TEXT', | |
| 1169 TEXT=IndentText(self._extra_snippets, ' ')) | |
| 1170 | |
| 1171 # TODO(vsm): Use typedef if / when that is supported in Dart. | 1147 # TODO(vsm): Use typedef if / when that is supported in Dart. |
| 1172 # Define variant as subtype. | 1148 # Define variant as subtype. |
| 1173 if (self._super_interface and | 1149 if (self._super_interface and |
| 1174 self._interface.id is not self._super_interface): | 1150 self._interface.id is not self._super_interface): |
| 1175 consts_emitter = self._top_level_emitter.Emit( | 1151 consts_emitter = self._top_level_emitter.Emit( |
| 1176 '\n' | 1152 '\n' |
| 1177 'interface $NAME extends $BASE {\n' | 1153 'interface $NAME extends $BASE {\n' |
| 1178 '$!CONSTS' | 1154 '$!CONSTS' |
| 1179 '}\n', | 1155 '}\n', |
| 1180 NAME=self._interface.id, | 1156 NAME=self._interface.id, |
| (...skipping 795 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1976 Arguments: | 1952 Arguments: |
| 1977 info: An OperationInfo object. | 1953 info: An OperationInfo object. |
| 1978 """ | 1954 """ |
| 1979 # TODO(vsm): Handle overloads. | 1955 # TODO(vsm): Handle overloads. |
| 1980 self._members_emitter.Emit( | 1956 self._members_emitter.Emit( |
| 1981 '\n' | 1957 '\n' |
| 1982 ' $TYPE $NAME($ARGS) native;\n', | 1958 ' $TYPE $NAME($ARGS) native;\n', |
| 1983 TYPE=info.type_name, | 1959 TYPE=info.type_name, |
| 1984 NAME=info.name, | 1960 NAME=info.name, |
| 1985 ARGS=info.arg_implementation_declaration) | 1961 ARGS=info.arg_implementation_declaration) |
| OLD | NEW |