| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 'DataView.setFloat32', | 54 'DataView.setFloat32', |
| 55 'DataView.setFloat64', | 55 'DataView.setFloat64', |
| 56 'DataView.setInt16', | 56 'DataView.setInt16', |
| 57 'DataView.setInt32', | 57 'DataView.setInt32', |
| 58 'DataView.setInt8', | 58 'DataView.setInt8', |
| 59 'DataView.setUint16', | 59 'DataView.setUint16', |
| 60 'DataView.setUint32', | 60 'DataView.setUint32', |
| 61 'DataView.setUint8', | 61 'DataView.setUint8', |
| 62 'DirectoryEntry.getDirectory', | 62 'DirectoryEntry.getDirectory', |
| 63 'DirectoryEntry.getFile', | 63 'DirectoryEntry.getFile', |
| 64 'Entry.copyTo', | |
| 65 'Entry.moveTo', | |
| 66 'HTMLInputElement.setRangeText', | |
| 67 'XMLHttpRequest.open', | |
| 68 ]) | 64 ]) |
| 69 | 65 |
| 70 # | 66 # |
| 71 # Renames for attributes that have names that are not legal Dart names. | 67 # Renames for attributes that have names that are not legal Dart names. |
| 72 # | 68 # |
| 73 _dart_attribute_renames = monitored.Dict('generator._dart_attribute_renames', { | 69 _dart_attribute_renames = monitored.Dict('generator._dart_attribute_renames', { |
| 74 'default': 'defaultValue', | 70 'default': 'defaultValue', |
| 75 }) | 71 }) |
| 76 | 72 |
| 77 # | 73 # |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 self.is_optional = is_optional | 129 self.is_optional = is_optional |
| 134 | 130 |
| 135 def Copy(self): | 131 def Copy(self): |
| 136 return ParamInfo(self.name, self.type_id, self.is_optional) | 132 return ParamInfo(self.name, self.type_id, self.is_optional) |
| 137 | 133 |
| 138 def __repr__(self): | 134 def __repr__(self): |
| 139 content = 'name = %s, type_id = %s, is_optional = %s' % ( | 135 content = 'name = %s, type_id = %s, is_optional = %s' % ( |
| 140 self.name, self.type_id, self.is_optional) | 136 self.name, self.type_id, self.is_optional) |
| 141 return '<ParamInfo(%s)>' % content | 137 return '<ParamInfo(%s)>' % content |
| 142 | 138 |
| 143 def GetCallbackInfo(interface): | |
| 144 """For the given interface, find operations that take callbacks (for use in | |
| 145 auto-transforming callbacks into futures).""" | |
| 146 callback_handlers = [operation for operation in interface.operations | |
| 147 if operation.id == 'handleEvent'] | |
| 148 return AnalyzeOperation(interface, callback_handlers) | |
| 149 | 139 |
| 150 # Given a list of overloaded arguments, render dart arguments. | 140 # Given a list of overloaded arguments, render dart arguments. |
| 151 def _BuildArguments(args, interface, constructor=False): | 141 def _BuildArguments(args, interface, constructor=False): |
| 152 def IsOptional(argument): | 142 def IsOptional(argument): |
| 153 if 'Callback' in argument.ext_attrs: | 143 if 'Callback' in argument.ext_attrs: |
| 154 # Callbacks with 'Optional=XXX' are treated as optional arguments. | 144 # Callbacks with 'Optional=XXX' are treated as optional arguments. |
| 155 return 'Optional' in argument.ext_attrs | 145 return 'Optional' in argument.ext_attrs |
| 156 if constructor: | 146 if constructor: |
| 157 # FIXME: Constructors with 'Optional=XXX' shouldn't be treated as | 147 # FIXME: Constructors with 'Optional=XXX' shouldn't be treated as |
| 158 # optional arguments. | 148 # optional arguments. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 info.operations = operations | 205 info.operations = operations |
| 216 info.overloads = split_operations | 206 info.overloads = split_operations |
| 217 info.declared_name = operations[0].id | 207 info.declared_name = operations[0].id |
| 218 info.name = operations[0].ext_attrs.get('DartName', info.declared_name) | 208 info.name = operations[0].ext_attrs.get('DartName', info.declared_name) |
| 219 info.constructor_name = None | 209 info.constructor_name = None |
| 220 info.js_name = info.declared_name | 210 info.js_name = info.declared_name |
| 221 info.type_name = operations[0].type.id # TODO: widen. | 211 info.type_name = operations[0].type.id # TODO: widen. |
| 222 info.param_infos = _BuildArguments([op.arguments for op in split_operations],
interface) | 212 info.param_infos = _BuildArguments([op.arguments for op in split_operations],
interface) |
| 223 full_name = '%s.%s' % (interface.id, info.declared_name) | 213 full_name = '%s.%s' % (interface.id, info.declared_name) |
| 224 info.requires_named_arguments = full_name in _methods_with_named_formals | 214 info.requires_named_arguments = full_name in _methods_with_named_formals |
| 225 # The arguments in that the original operation took as callbacks (for | |
| 226 # conversion to futures). | |
| 227 info.callback_args = [] | |
| 228 return info | 215 return info |
| 229 | 216 |
| 230 def ConvertToFuture(info): | |
| 231 """Given an OperationInfo object, convert the operation's signature so that it | |
| 232 instead uses futures instead of callbacks.""" | |
| 233 new_info = copy.deepcopy(info) | |
| 234 def IsNotCallbackType(param): | |
| 235 return 'Callback' not in param.type_id | |
| 236 # Success callback is the first argument (change if this no longer holds). | |
| 237 new_info.callback_args = filter( | |
| 238 lambda x: not IsNotCallbackType(x), new_info.param_infos) | |
| 239 new_info.param_infos = filter(IsNotCallbackType, new_info.param_infos) | |
| 240 new_info.type_name = 'Future' | |
| 241 | |
| 242 return new_info | |
| 243 | |
| 244 | 217 |
| 245 def AnalyzeConstructor(interface): | 218 def AnalyzeConstructor(interface): |
| 246 """Returns an OperationInfo object for the constructor. | 219 """Returns an OperationInfo object for the constructor. |
| 247 | 220 |
| 248 Returns None if the interface has no Constructor. | 221 Returns None if the interface has no Constructor. |
| 249 """ | 222 """ |
| 250 if 'Constructor' in interface.ext_attrs: | 223 if 'Constructor' in interface.ext_attrs: |
| 251 name = None | 224 name = None |
| 252 overloads = interface.ext_attrs['Constructor'] | 225 overloads = interface.ext_attrs['Constructor'] |
| 253 idl_args = [[] if f is None else f.arguments for f in overloads] | 226 idl_args = [[] if f is None else f.arguments for f in overloads] |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 argtexts = map(FormatParam, required) | 334 argtexts = map(FormatParam, required) |
| 362 if optional: | 335 if optional: |
| 363 needs_named = self.requires_named_arguments and not force_optional | 336 needs_named = self.requires_named_arguments and not force_optional |
| 364 left_bracket, right_bracket = '{}' if needs_named else '[]' | 337 left_bracket, right_bracket = '{}' if needs_named else '[]' |
| 365 argtexts.append( | 338 argtexts.append( |
| 366 left_bracket + | 339 left_bracket + |
| 367 ', '.join(map(FormatParam, optional)) + | 340 ', '.join(map(FormatParam, optional)) + |
| 368 right_bracket) | 341 right_bracket) |
| 369 return ', '.join(argtexts) | 342 return ', '.join(argtexts) |
| 370 | 343 |
| 371 def ParametersAsArgumentList(self, parameter_count=None): | 344 def ParametersAsArgumentList(self, parameter_count = None): |
| 372 """Returns a string of the parameter names suitable for passing the | 345 """Returns a string of the parameter names suitable for passing the |
| 373 parameters as arguments. | 346 parameters as arguments. |
| 374 """ | 347 """ |
| 375 def param_name(param_info): | |
| 376 if self.requires_named_arguments and param_info.is_optional: | |
| 377 return '%s : %s' % (param_info.name, param_info.name) | |
| 378 else: | |
| 379 return param_info.name | |
| 380 | |
| 381 if parameter_count is None: | 348 if parameter_count is None: |
| 382 parameter_count = len(self.param_infos) | 349 parameter_count = len(self.param_infos) |
| 383 return ', '.join(map(param_name, self.param_infos[:parameter_count])) | 350 return ', '.join(map( |
| 351 lambda param_info: param_info.name, |
| 352 self.param_infos[:parameter_count])) |
| 384 | 353 |
| 385 def IsStatic(self): | 354 def IsStatic(self): |
| 386 is_static = self.overloads[0].is_static | 355 is_static = self.overloads[0].is_static |
| 387 assert any([is_static == o.is_static for o in self.overloads]) | 356 assert any([is_static == o.is_static for o in self.overloads]) |
| 388 return is_static | 357 return is_static |
| 389 | 358 |
| 390 def _ConstructorFullName(self, rename_type): | 359 def _ConstructorFullName(self, rename_type): |
| 391 if self.constructor_name: | 360 if self.constructor_name: |
| 392 return rename_type(self.type_name) + '.' + self.constructor_name | 361 return rename_type(self.type_name) + '.' + self.constructor_name |
| 393 else: | 362 else: |
| (...skipping 1021 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1415 'DOMMimeTypeArray': TypeData(clazz='Interface', item_type='DOMMimeType'), | 1384 'DOMMimeTypeArray': TypeData(clazz='Interface', item_type='DOMMimeType'), |
| 1416 'DOMPluginArray': TypeData(clazz='Interface', item_type='DOMPlugin'), | 1385 'DOMPluginArray': TypeData(clazz='Interface', item_type='DOMPlugin'), |
| 1417 'DOMStringList': TypeData(clazz='Interface', item_type='DOMString', | 1386 'DOMStringList': TypeData(clazz='Interface', item_type='DOMString', |
| 1418 dart_type='List<String>', custom_to_native=True), | 1387 dart_type='List<String>', custom_to_native=True), |
| 1419 'EntryArray': TypeData(clazz='Interface', item_type='Entry', | 1388 'EntryArray': TypeData(clazz='Interface', item_type='Entry', |
| 1420 suppress_interface=True), | 1389 suppress_interface=True), |
| 1421 'EntryArraySync': TypeData(clazz='Interface', item_type='EntrySync', | 1390 'EntryArraySync': TypeData(clazz='Interface', item_type='EntrySync', |
| 1422 suppress_interface=True), | 1391 suppress_interface=True), |
| 1423 'FileList': TypeData(clazz='Interface', item_type='File', | 1392 'FileList': TypeData(clazz='Interface', item_type='File', |
| 1424 dart_type='List<File>'), | 1393 dart_type='List<File>'), |
| 1425 'Future': TypeData(clazz='Interface', dart_type='Future'), | |
| 1426 'GamepadList': TypeData(clazz='Interface', item_type='Gamepad', | 1394 'GamepadList': TypeData(clazz='Interface', item_type='Gamepad', |
| 1427 suppress_interface=True), | 1395 suppress_interface=True), |
| 1428 'HTMLAllCollection': TypeData(clazz='Interface', item_type='Node'), | 1396 'HTMLAllCollection': TypeData(clazz='Interface', item_type='Node'), |
| 1429 'HTMLCollection': TypeData(clazz='Interface', item_type='Node'), | 1397 'HTMLCollection': TypeData(clazz='Interface', item_type='Node'), |
| 1430 'MediaStreamList': TypeData(clazz='Interface', | 1398 'MediaStreamList': TypeData(clazz='Interface', |
| 1431 item_type='MediaStream', suppress_interface=True), | 1399 item_type='MediaStream', suppress_interface=True), |
| 1432 'NamedNodeMap': TypeData(clazz='Interface', item_type='Node'), | 1400 'NamedNodeMap': TypeData(clazz='Interface', item_type='Node'), |
| 1433 'NodeList': TypeData(clazz='Interface', item_type='Node', | 1401 'NodeList': TypeData(clazz='Interface', item_type='Node', |
| 1434 suppress_interface=False, dart_type='List<Node>'), | 1402 suppress_interface=False, dart_type='List<Node>'), |
| 1435 'SVGElementInstanceList': TypeData(clazz='Interface', | 1403 'SVGElementInstanceList': TypeData(clazz='Interface', |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1530 self) | 1498 self) |
| 1531 | 1499 |
| 1532 if type_data.clazz == 'SVGTearOff': | 1500 if type_data.clazz == 'SVGTearOff': |
| 1533 dart_interface_name = self._renamer.RenameInterface( | 1501 dart_interface_name = self._renamer.RenameInterface( |
| 1534 self._database.GetInterface(type_name)) | 1502 self._database.GetInterface(type_name)) |
| 1535 return SVGTearOffIDLTypeInfo( | 1503 return SVGTearOffIDLTypeInfo( |
| 1536 type_name, type_data, dart_interface_name, self) | 1504 type_name, type_data, dart_interface_name, self) |
| 1537 | 1505 |
| 1538 class_name = '%sIDLTypeInfo' % type_data.clazz | 1506 class_name = '%sIDLTypeInfo' % type_data.clazz |
| 1539 return globals()[class_name](type_name, type_data) | 1507 return globals()[class_name](type_name, type_data) |
| OLD | NEW |