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 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 # Nodes with different tags in different browsers can be listed as multiple | 107 # Nodes with different tags in different browsers can be listed as multiple |
108 # tags here provided there is not conflict in usage (e.g. browser X has tag | 108 # tags here provided there is not conflict in usage (e.g. browser X has tag |
109 # T and no other browser has tag T). | 109 # T and no other browser has tag T). |
110 | 110 |
111 'AnalyserNode': 'AnalyserNode,RealtimeAnalyserNode', | 111 'AnalyserNode': 'AnalyserNode,RealtimeAnalyserNode', |
112 'AudioContext': 'AudioContext,webkitAudioContext', | 112 'AudioContext': 'AudioContext,webkitAudioContext', |
113 | 113 |
114 'ChannelMergerNode': 'ChannelMergerNode,AudioChannelMerger', | 114 'ChannelMergerNode': 'ChannelMergerNode,AudioChannelMerger', |
115 'ChannelSplitterNode': 'ChannelSplitterNode,AudioChannelSplitter', | 115 'ChannelSplitterNode': 'ChannelSplitterNode,AudioChannelSplitter', |
116 | 116 |
117 'ClientRect': 'ClientRect,DOMRect', | |
118 'ClientRectList': 'ClientRectList,DOMRectList', | 117 'ClientRectList': 'ClientRectList,DOMRectList', |
119 | 118 |
120 'CSSStyleDeclaration': | 119 'CSSStyleDeclaration': |
121 # IE Firefox | 120 # IE Firefox |
122 'CSSStyleDeclaration,MSStyleCSSProperties,CSS2Properties', | 121 'CSSStyleDeclaration,MSStyleCSSProperties,CSS2Properties', |
123 | 122 |
124 'Clipboard': 'Clipboard,DataTransfer', | 123 'Clipboard': 'Clipboard,DataTransfer', |
125 | 124 |
126 'ApplicationCache': | 125 'ApplicationCache': |
127 'ApplicationCache,DOMApplicationCache,OfflineResourceList', | 126 'ApplicationCache,DOMApplicationCache,OfflineResourceList', |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 return argument.optional | 231 return argument.optional |
233 if 'DartForceOptional' in argument.ext_attrs: | 232 if 'DartForceOptional' in argument.ext_attrs: |
234 return True | 233 return True |
235 return False | 234 return False |
236 | 235 |
237 # Given a list of overloaded arguments, choose a suitable name. | 236 # Given a list of overloaded arguments, choose a suitable name. |
238 def OverloadedName(args): | 237 def OverloadedName(args): |
239 return '_OR_'.join(sorted(set(arg.id for arg in args))) | 238 return '_OR_'.join(sorted(set(arg.id for arg in args))) |
240 | 239 |
241 def DartType(idl_type_name): | 240 def DartType(idl_type_name): |
242 if idl_type_name in _idl_type_registry: | 241 if idl_type_name in _idl_type_registry: |
243 return _idl_type_registry[idl_type_name].dart_type or idl_type_name | 242 return _idl_type_registry[idl_type_name].dart_type or idl_type_name |
244 return idl_type_name | 243 return idl_type_name |
245 | 244 |
246 # Given a list of overloaded arguments, choose a suitable type. | 245 # Given a list of overloaded arguments, choose a suitable type. |
247 def OverloadedType(args): | 246 def OverloadedType(args): |
248 type_ids = sorted(set(arg.type.id for arg in args)) | 247 type_ids = sorted(set(arg.type.id for arg in args)) |
249 if len(set(DartType(arg.type.id) for arg in args)) == 1: | 248 if len(set(DartType(arg.type.id) for arg in args)) == 1: |
250 return type_ids[0] | 249 return type_ids[0] |
251 else: | 250 else: |
252 return None | 251 return None |
253 | 252 |
254 result = [] | 253 result = [] |
255 | 254 |
256 is_optional = False | 255 is_optional = False |
257 for arg_tuple in map(lambda *x: x, *args): | 256 for arg_tuple in map(lambda *x: x, *args): |
258 is_optional = is_optional or any(arg is None or IsOptional(arg) for arg in a
rg_tuple) | 257 is_optional = is_optional or any(arg is None or IsOptional(arg) for arg in a
rg_tuple) |
259 | 258 |
260 filtered = filter(None, arg_tuple) | 259 filtered = filter(None, arg_tuple) |
261 type_id = OverloadedType(filtered) | 260 type_id = OverloadedType(filtered) |
262 name = OverloadedName(filtered) | 261 name = OverloadedName(filtered) |
263 result.append(ParamInfo(name, type_id, is_optional)) | 262 result.append(ParamInfo(name, type_id, is_optional)) |
264 | 263 |
265 return result | 264 return result |
266 | 265 |
267 # Argument default value set (literal value or null). | 266 # Argument default value is one that we suppress |
268 def HasDefault(argument): | 267 # FIXME(leafp) We may wish to eliminate this special treatment of optional |
269 return (argument.default_value != None) or argument.default_value_is_null | 268 # arguments entirely, since default values are being used more pervasively |
| 269 # in the IDL now. |
| 270 def HasSuppressedOptionalDefault(argument): |
| 271 return (argument.default_value == 'Undefined') or argument.default_value_is_nu
ll |
270 | 272 |
271 def IsOptional(argument): | 273 def IsOptional(argument): |
272 return argument.optional and (not(HasDefault(argument))) \ | 274 return argument.optional and (not(HasSuppressedOptionalDefault(argument))) \ |
273 or 'DartForceOptional' in argument.ext_attrs | 275 or 'DartForceOptional' in argument.ext_attrs |
274 | 276 |
275 def AnalyzeOperation(interface, operations): | 277 def AnalyzeOperation(interface, operations): |
276 """Makes operation calling convention decision for a set of overloads. | 278 """Makes operation calling convention decision for a set of overloads. |
277 | 279 |
278 Returns: An OperationInfo object. | 280 Returns: An OperationInfo object. |
279 """ | 281 """ |
280 | 282 |
281 # split operations with optional args into multiple operations | 283 # split operations with optional args into multiple operations |
282 split_operations = [] | 284 split_operations = [] |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' | 445 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' |
444 return (TypeOrNothing(dart_type, param.type_id), param.name) | 446 return (TypeOrNothing(dart_type, param.type_id), param.name) |
445 required = [] | 447 required = [] |
446 optional = [] | 448 optional = [] |
447 for param_info in self.param_infos: | 449 for param_info in self.param_infos: |
448 if param_info.is_optional: | 450 if param_info.is_optional: |
449 optional.append(FormatParam(param_info)) | 451 optional.append(FormatParam(param_info)) |
450 else: | 452 else: |
451 if optional: | 453 if optional: |
452 raise Exception('Optional parameters cannot precede required ones: ' | 454 raise Exception('Optional parameters cannot precede required ones: ' |
453 + str(params)) | 455 + str(param_info)) |
454 required.append(FormatParam(param_info)) | 456 required.append(FormatParam(param_info)) |
455 needs_named = optional and self.requires_named_arguments and not force_optio
nal | 457 needs_named = optional and self.requires_named_arguments and not force_optio
nal |
456 return (required, optional, needs_named) | 458 return (required, optional, needs_named) |
457 | 459 |
458 def ParametersAsDecStringList(self, rename_type, force_optional=False): | 460 def ParametersAsDecStringList(self, rename_type, force_optional=False): |
459 """Returns a list of strings where each string corresponds to a parameter | 461 """Returns a list of strings where each string corresponds to a parameter |
460 declaration. All of the optional/named parameters if any will appear as | 462 declaration. All of the optional/named parameters if any will appear as |
461 a single entry at the end of the list. | 463 a single entry at the end of the list. |
462 """ | 464 """ |
463 (required, optional, needs_named) = \ | 465 (required, optional, needs_named) = \ |
(...skipping 802 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1266 | 1268 |
1267 def TypeInfo(self, type_name): | 1269 def TypeInfo(self, type_name): |
1268 if not type_name in self._cache: | 1270 if not type_name in self._cache: |
1269 self._cache[type_name] = self._TypeInfo(type_name) | 1271 self._cache[type_name] = self._TypeInfo(type_name) |
1270 return self._cache[type_name] | 1272 return self._cache[type_name] |
1271 | 1273 |
1272 def DartType(self, type_name): | 1274 def DartType(self, type_name): |
1273 return self.TypeInfo(type_name).dart_type() | 1275 return self.TypeInfo(type_name).dart_type() |
1274 | 1276 |
1275 def _TypeInfo(self, type_name): | 1277 def _TypeInfo(self, type_name): |
1276 match = re.match(r'(?:sequence<(\w+)>|(\w+)\[\])$', type_name) | 1278 match = re.match(r'(?:sequence<([\w ]+)>|(\w+)\[\])$', type_name) |
1277 if match: | 1279 if match: |
1278 type_data = TypeData('Sequence') | 1280 type_data = TypeData('Sequence') |
1279 item_info = self.TypeInfo(match.group(1) or match.group(2)) | 1281 item_info = self.TypeInfo(match.group(1) or match.group(2)) |
1280 # TODO(vsm): Generalize this code. | 1282 # TODO(vsm): Generalize this code. |
1281 if 'SourceInfo' in type_name: | 1283 if 'SourceInfo' in type_name: |
1282 type_data.native_type = 'const Vector<RefPtr<SourceInfo> >& ' | 1284 type_data.native_type = 'const Vector<RefPtr<SourceInfo> >& ' |
1283 return SequenceIDLTypeInfo(type_name, type_data, item_info) | 1285 return SequenceIDLTypeInfo(type_name, type_data, item_info) |
1284 | 1286 |
1285 if not type_name in _idl_type_registry: | 1287 if not type_name in _idl_type_registry: |
1286 if self._database.HasEnum(type_name): | 1288 if self._database.HasEnum(type_name): |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1323 if type_data.clazz == 'BasicTypedList': | 1325 if type_data.clazz == 'BasicTypedList': |
1324 if type_name == 'ArrayBuffer': | 1326 if type_name == 'ArrayBuffer': |
1325 dart_interface_name = 'ByteBuffer' | 1327 dart_interface_name = 'ByteBuffer' |
1326 else: | 1328 else: |
1327 dart_interface_name = self._renamer.RenameInterfaceId(type_name) | 1329 dart_interface_name = self._renamer.RenameInterfaceId(type_name) |
1328 return BasicTypedListIDLTypeInfo( | 1330 return BasicTypedListIDLTypeInfo( |
1329 type_name, type_data, dart_interface_name, self) | 1331 type_name, type_data, dart_interface_name, self) |
1330 | 1332 |
1331 class_name = '%sIDLTypeInfo' % type_data.clazz | 1333 class_name = '%sIDLTypeInfo' % type_data.clazz |
1332 return globals()[class_name](type_name, type_data) | 1334 return globals()[class_name](type_name, type_data) |
OLD | NEW |