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, \ | 11 DartDomNameOfAttribute, FindMatchingAttribute, \ |
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, private_html_members, renamed_html_members, \ | 15 keep_overloaded_members, overloaded_and_renamed, private_html_members, \ |
16 renamed_overloads, removed_html_members | 16 renamed_html_members, renamed_overloads, removed_html_members |
17 import logging | 17 import logging |
18 import monitored | 18 import monitored |
19 import sys | 19 import sys |
20 | 20 |
21 _logger = logging.getLogger('htmldartgenerator') | 21 _logger = logging.getLogger('htmldartgenerator') |
22 | 22 |
23 # Types that are accessible cross-frame in a limited fashion. | 23 # Types that are accessible cross-frame in a limited fashion. |
24 # In these cases, the base type (e.g., WindowBase) provides restricted access | 24 # In these cases, the base type (e.g., WindowBase) provides restricted access |
25 # while the subtype (e.g., Window) provides full access to the | 25 # while the subtype (e.g., Window) provides full access to the |
26 # corresponding objects if there are from the same frame. | 26 # corresponding objects if there are from the same frame. |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 if operation.id in operationsByName: | 148 if operation.id in operationsByName: |
149 operations = operationsByName[operation.id] | 149 operations = operationsByName[operation.id] |
150 for existing_operation in operations: | 150 for existing_operation in operations: |
151 if existing_operation.SameSignatureAs(operation): | 151 if existing_operation.SameSignatureAs(operation): |
152 del operationsByName[operation.id] | 152 del operationsByName[operation.id] |
153 | 153 |
154 def _AddRenamedOverloads(self, interface): | 154 def _AddRenamedOverloads(self, interface): |
155 """The IDL has a number of functions with the same name but that accept | 155 """The IDL has a number of functions with the same name but that accept |
156 different types. This is fine for JavaScript, but results in vague type | 156 different types. This is fine for JavaScript, but results in vague type |
157 signatures for Dart. We rename some of these (by adding a new identical | 157 signatures for Dart. We rename some of these (by adding a new identical |
158 operation with a different DartName), and leave the original version in a | 158 operation with a different DartName), but leave the original version as |
159 few specific instances.""" | 159 well in some cases.""" |
160 potential_added_operations = set() | 160 potential_added_operations = set() |
161 operations_by_name = self._OperationsByName(interface) | 161 operations_by_name = self._OperationsByName(interface) |
162 already_renamed = [operation.ext_attrs['DartName'] if 'DartName' in | 162 already_renamed = [operation.ext_attrs['DartName'] if 'DartName' in |
163 operation.ext_attrs else '' for operation in interface.operations] | 163 operation.ext_attrs else '' for operation in interface.operations] |
164 | 164 |
| 165 added_operations = [] |
165 for operation in interface.operations: | 166 for operation in interface.operations: |
166 full_operation_str = self._GetStringRepresentation(interface, operation) | 167 full_operation_str = self._GetStringRepresentation(interface, operation) |
167 if (full_operation_str in renamed_overloads and | 168 if (full_operation_str in renamed_overloads and |
168 renamed_overloads[full_operation_str] not in already_renamed): | 169 renamed_overloads[full_operation_str] not in already_renamed): |
169 dart_name = renamed_overloads[full_operation_str] | 170 if '%s.%s' % (interface.id, operation.id) in overloaded_and_renamed: |
170 if not dart_name: | 171 cloned_operation = deepcopy(operation) |
171 continue | 172 cloned_operation.ext_attrs['DartName'] = renamed_overloads[ |
| 173 full_operation_str] |
| 174 added_operations.append(cloned_operation) |
| 175 else: |
| 176 dart_name = renamed_overloads[full_operation_str] |
| 177 if not dart_name: |
| 178 continue |
172 | 179 |
173 operation.ext_attrs['DartName'] = dart_name | 180 operation.ext_attrs['DartName'] = dart_name |
174 potential_added_operations.add(operation.id) | 181 potential_added_operations.add(operation.id) |
175 self._EnsureNoMultipleTypeSignatures(interface, operation, | 182 self._EnsureNoMultipleTypeSignatures(interface, operation, |
176 operations_by_name) | 183 operations_by_name) |
| 184 interface.operations += added_operations |
177 self._AddDesiredOverloadedOperations(potential_added_operations, interface, | 185 self._AddDesiredOverloadedOperations(potential_added_operations, interface, |
178 operations_by_name) | 186 operations_by_name) |
179 | 187 |
180 def _AddDesiredOverloadedOperations(self, potential_added_operations, | 188 def _AddDesiredOverloadedOperations(self, potential_added_operations, |
181 interface, original_operations_by_name): | 189 interface, original_operations_by_name): |
182 """For some cases we desire to keep the overloaded version in dart, for | 190 """For some cases we desire to keep the overloaded version in dart, for |
183 simplicity of API, and explain the parameters accepted in documentation.""" | 191 simplicity of API, and explain the parameters accepted in documentation.""" |
184 updated_operations_by_name = self._OperationsByName(interface) | 192 updated_operations_by_name = self._OperationsByName(interface) |
185 for operation_id in potential_added_operations: | 193 for operation_id in potential_added_operations: |
186 if (operation_id not in updated_operations_by_name and | 194 if (operation_id not in updated_operations_by_name and |
187 '%s.%s' % (interface.id, operation_id) in keep_overloaded_members): | 195 '%s.%s' % (interface.id, operation_id) in keep_overloaded_members): |
188 for operation in original_operations_by_name[operation_id]: | 196 for operation in original_operations_by_name[operation_id]: |
189 cloned_operation = deepcopy(operation) | 197 cloned_operation = deepcopy(operation) |
190 cloned_operation.ext_attrs['DartName'] = operation_id | 198 cloned_operation.ext_attrs['DartName'] = operation_id |
191 interface.operations.append(cloned_operation) | 199 interface.operations.append(cloned_operation) |
192 | 200 |
193 def _EnsureNoMultipleTypeSignatures(self, interface, operation, | 201 def _EnsureNoMultipleTypeSignatures(self, interface, operation, |
194 operations_by_name): | 202 operations_by_name): |
195 """Make sure that there is now at most one operation with a particular | 203 """Make sure that there is now at most one operation with a particular |
196 operation.id. If not, stop library generation, and throw an error, requiring | 204 operation.id. If not, stop library generation, and throw an error, requiring |
197 programmer input about the best name change before proceeding.""" | 205 programmer input about the best name change before proceeding.""" |
198 operation_str = '%s.%s' % (interface.id, operation.id) | 206 operation_str = '%s.%s' % (interface.id, operation.id) |
199 if (operation.id in operations_by_name and | 207 if (operation.id in operations_by_name and |
200 len(operations_by_name[operation.id]) > 1 and | 208 len(operations_by_name[operation.id]) > 1 and |
201 len(filter(lambda overload: overload.startswith(operation_str), | 209 len(filter(lambda overload: overload.startswith(operation_str), |
202 renamed_overloads.keys())) == 0 and | 210 renamed_overloads.keys())) == 0 and |
203 operation_str not in keep_overloaded_members and | 211 operation_str not in keep_overloaded_members and |
| 212 operation_str not in overloaded_and_renamed and |
204 operation_str not in renamed_html_members and | 213 operation_str not in renamed_html_members and |
205 operation_str not in private_html_members and | 214 operation_str not in private_html_members and |
206 operation_str not in removed_html_members and | 215 operation_str not in removed_html_members and |
207 operation.id != '__getter__' and | 216 operation.id != '__getter__' and |
208 operation.id != '__setter__' and | 217 operation.id != '__setter__' and |
209 operation.id != '__delete__'): | 218 operation.id != '__delete__'): |
210 _logger.error('Multiple type signatures for %s.%s' % ( | 219 _logger.error('Multiple type signatures for %s.%s. Please file a bug with' |
| 220 ' the dart:html team to determine if one of these functions should be' |
| 221 ' renamed.' % ( |
211 interface.id, operation.id)) | 222 interface.id, operation.id)) |
212 raise Exception('Rename one of the methods in renamed_overloads or add it' | |
213 ' to keep_overloaded_members.\n' | |
214 'Generation UNsuccessful.') | |
215 | 223 |
216 def _GetStringRepresentation(self, interface, operation): | 224 def _GetStringRepresentation(self, interface, operation): |
217 """Given an IDLOperation, return a object-independent representation of the | 225 """Given an IDLOperation, return a object-independent representation of the |
218 operations's signature.""" | 226 operations's signature.""" |
219 return '%s.%s(%s)' % (interface.id, operation.id, ', '.join( | 227 return '%s.%s(%s)' % (interface.id, operation.id, ', '.join( |
220 ['%s %s' % (arg.type.id, arg.id) for arg in operation.arguments])) | 228 ['%s %s' % (arg.type.id, arg.id) for arg in operation.arguments])) |
221 | 229 |
222 def _OperationsByName(self, interface): | 230 def _OperationsByName(self, interface): |
223 operationsByName = {} | 231 operationsByName = {} |
224 for operation in interface.operations: | 232 for operation in interface.operations: |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 | 701 |
694 def SecureBaseName(self, type_name): | 702 def SecureBaseName(self, type_name): |
695 if type_name in _secure_base_types: | 703 if type_name in _secure_base_types: |
696 return _secure_base_types[type_name] | 704 return _secure_base_types[type_name] |
697 | 705 |
698 def _DartType(self, type_name): | 706 def _DartType(self, type_name): |
699 return self._type_registry.DartType(type_name) | 707 return self._type_registry.DartType(type_name) |
700 | 708 |
701 def _TypeInfo(self, type_name): | 709 def _TypeInfo(self, type_name): |
702 return self._type_registry.TypeInfo(type_name) | 710 return self._type_registry.TypeInfo(type_name) |
OLD | NEW |