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