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 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 self.is_optional = is_optional | 394 self.is_optional = is_optional |
395 | 395 |
396 def Copy(self): | 396 def Copy(self): |
397 return ParamInfo(self.name, self.type_id, self.is_optional) | 397 return ParamInfo(self.name, self.type_id, self.is_optional) |
398 | 398 |
399 def __repr__(self): | 399 def __repr__(self): |
400 content = 'name = %s, type_id = %s, is_optional = %s' % ( | 400 content = 'name = %s, type_id = %s, is_optional = %s' % ( |
401 self.name, self.type_id, self.is_optional) | 401 self.name, self.type_id, self.is_optional) |
402 return '<ParamInfo(%s)>' % content | 402 return '<ParamInfo(%s)>' % content |
403 | 403 |
404 def GetCallbackInfo(interface): | 404 def GetCallbackHandlers(interface): |
405 """For the given interface, find operations that take callbacks (for use in | 405 callback_handlers = [] |
406 auto-transforming callbacks into futures).""" | |
407 callback_handlers = [operation for operation in interface.operations | 406 callback_handlers = [operation for operation in interface.operations |
408 if operation.id == 'handleEvent'] | 407 if operation.id == 'handleEvent'] |
409 if callback_handlers == []: | 408 if callback_handlers == []: |
410 callback_handlers = [operation for operation in interface.operations | 409 callback_handlers = [operation for operation in interface.operations |
411 if operation.id == 'handleItem'] | 410 if operation.id == 'handleItem'] |
| 411 return callback_handlers |
| 412 |
| 413 def GetCallbackInfo(interface): |
| 414 """For the given interface, find operations that take callbacks (for use in |
| 415 auto-transforming callbacks into futures).""" |
| 416 callback_handlers = GetCallbackHandlers(interface) |
412 return AnalyzeOperation(interface, callback_handlers) | 417 return AnalyzeOperation(interface, callback_handlers) |
413 | 418 |
414 # Given a list of overloaded arguments, render dart arguments. | 419 # Given a list of overloaded arguments, render dart arguments. |
415 def _BuildArguments(args, interface, constructor=False): | 420 def _BuildArguments(args, interface, constructor=False): |
416 def IsOptional(argument): | 421 def IsOptional(argument): |
417 if 'Callback' in argument.ext_attrs: | 422 if 'Callback' in argument.ext_attrs: |
418 # Optional callbacks arguments are treated as optional arguments. | 423 # Optional callbacks arguments are treated as optional arguments. |
419 return argument.optional | 424 return argument.optional |
420 if constructor: | 425 if constructor: |
421 # FIXME: Optional constructors arguments should not be treated as | 426 # FIXME: Optional constructors arguments should not be treated as |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 # The arguments in that the original operation took as callbacks (for | 502 # The arguments in that the original operation took as callbacks (for |
498 # conversion to futures). | 503 # conversion to futures). |
499 info.callback_args = [] | 504 info.callback_args = [] |
500 return info | 505 return info |
501 | 506 |
502 def ConvertToFuture(info): | 507 def ConvertToFuture(info): |
503 """Given an OperationInfo object, convert the operation's signature so that it | 508 """Given an OperationInfo object, convert the operation's signature so that it |
504 instead uses futures instead of callbacks.""" | 509 instead uses futures instead of callbacks.""" |
505 new_info = copy.deepcopy(info) | 510 new_info = copy.deepcopy(info) |
506 def IsNotCallbackType(param): | 511 def IsNotCallbackType(param): |
507 return 'Callback' not in param.type_id | 512 type_id = param.type_id |
| 513 if type_id is None: |
| 514 return False |
| 515 else: |
| 516 return 'Callback' not in type_id |
| 517 |
508 # Success callback is the first argument (change if this no longer holds). | 518 # Success callback is the first argument (change if this no longer holds). |
509 new_info.callback_args = filter( | 519 new_info.callback_args = filter( |
510 lambda x: not IsNotCallbackType(x), new_info.param_infos) | 520 lambda x: not IsNotCallbackType(x), new_info.param_infos) |
511 new_info.param_infos = filter(IsNotCallbackType, new_info.param_infos) | 521 new_info.param_infos = filter(IsNotCallbackType, new_info.param_infos) |
512 new_info.type_name = 'Future' | 522 new_info.type_name = 'Future' |
513 | 523 |
514 return new_info | 524 return new_info |
515 | 525 |
516 | 526 |
517 def AnalyzeConstructor(interface): | 527 def AnalyzeConstructor(interface): |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 """ | 644 """ |
635 def FormatParam(param): | 645 def FormatParam(param): |
636 # Is the type a typedef if so it's a union so it's dynamic. | 646 # Is the type a typedef if so it's a union so it's dynamic. |
637 # TODO(terry): This may have to change for dart2js for code shaking the | 647 # TODO(terry): This may have to change for dart2js for code shaking the |
638 # return types (unions) needs to be emitted with @create | 648 # return types (unions) needs to be emitted with @create |
639 # annotations and/or with JS('type1|type2',...) | 649 # annotations and/or with JS('type1|type2',...) |
640 if hasattr(rename_type, 'im_self') and rename_type.im_self._database.HasTy
peDef(param.type_id): | 650 if hasattr(rename_type, 'im_self') and rename_type.im_self._database.HasTy
peDef(param.type_id): |
641 dart_type = 'dynamic' | 651 dart_type = 'dynamic' |
642 else: | 652 else: |
643 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' | 653 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' |
| 654 # Special handling for setlike IDL forEach operation. |
| 655 if dart_type is None and param.type_id.endswith('ForEachCallback'): |
| 656 dart_type = param.type_id |
644 return (TypeOrNothing(dart_type, param.type_id), param.name) | 657 return (TypeOrNothing(dart_type, param.type_id), param.name) |
645 required = [] | 658 required = [] |
646 optional = [] | 659 optional = [] |
647 for param_info in self.param_infos: | 660 for param_info in self.param_infos: |
648 if param_info.is_optional: | 661 if param_info.is_optional: |
649 optional.append(FormatParam(param_info)) | 662 optional.append(FormatParam(param_info)) |
650 else: | 663 else: |
651 if optional: | 664 if optional: |
652 raise Exception('Optional parameters cannot precede required ones: ' | 665 raise Exception('Optional parameters cannot precede required ones: ' |
653 + str(param_info)) | 666 + str(param_info)) |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1173 if not matched: | 1186 if not matched: |
1174 return None | 1187 return None |
1175 return matched.group(1) | 1188 return matched.group(1) |
1176 | 1189 |
1177 class SequenceIDLTypeInfo(IDLTypeInfo): | 1190 class SequenceIDLTypeInfo(IDLTypeInfo): |
1178 def __init__(self, idl_type, data, item_info): | 1191 def __init__(self, idl_type, data, item_info): |
1179 super(SequenceIDLTypeInfo, self).__init__(idl_type, data) | 1192 super(SequenceIDLTypeInfo, self).__init__(idl_type, data) |
1180 self._item_info = item_info | 1193 self._item_info = item_info |
1181 | 1194 |
1182 def dart_type(self): | 1195 def dart_type(self): |
1183 return 'List<%s>' % self._item_info.dart_type() | 1196 darttype = self._item_info.dart_type() |
| 1197 return 'List' if darttype is None else 'List<%s>' % darttype |
1184 | 1198 |
1185 def interface_name(self): | 1199 def interface_name(self): |
1186 return self.dart_type() | 1200 return self.dart_type() |
1187 | 1201 |
1188 def implementation_name(self): | 1202 def implementation_name(self): |
1189 return self.dart_type() | 1203 return self.dart_type() |
1190 | 1204 |
1191 def vector_to_dart_template_parameter(self): | 1205 def vector_to_dart_template_parameter(self): |
1192 raise Exception('sequences of sequences are not supported yet') | 1206 raise Exception('sequences of sequences are not supported yet') |
1193 | 1207 |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1510 'GLushort': TypeData(clazz='Primitive', dart_type='int', native_type='int'), | 1524 'GLushort': TypeData(clazz='Primitive', dart_type='int', native_type='int'), |
1511 'GLuint': TypeData(clazz='Primitive', dart_type='int', | 1525 'GLuint': TypeData(clazz='Primitive', dart_type='int', |
1512 native_type='unsigned'), | 1526 native_type='unsigned'), |
1513 'GLfloat': TypeData(clazz='Primitive', dart_type='num', native_type='float')
, | 1527 'GLfloat': TypeData(clazz='Primitive', dart_type='num', native_type='float')
, |
1514 'GLclampf': TypeData(clazz='Primitive', dart_type='num', native_type='float'
), | 1528 'GLclampf': TypeData(clazz='Primitive', dart_type='num', native_type='float'
), |
1515 'HTMLCollection': TypeData(clazz='Interface', item_type='Node', | 1529 'HTMLCollection': TypeData(clazz='Interface', item_type='Node', |
1516 dart_type='List<Node>'), | 1530 dart_type='List<Node>'), |
1517 'NamedNodeMap': TypeData(clazz='Interface', item_type='Node'), | 1531 'NamedNodeMap': TypeData(clazz='Interface', item_type='Node'), |
1518 'NodeList': TypeData(clazz='Interface', item_type='Node', | 1532 'NodeList': TypeData(clazz='Interface', item_type='Node', |
1519 suppress_interface=False, dart_type='List<Node>'), | 1533 suppress_interface=False, dart_type='List<Node>'), |
| 1534 'NotificationAction': TypedListTypeData(''), |
1520 'SVGElementInstanceList': TypeData(clazz='Interface', | 1535 'SVGElementInstanceList': TypeData(clazz='Interface', |
1521 item_type='SVGElementInstance', suppress_interface=True), | 1536 item_type='SVGElementInstance', suppress_interface=True), |
1522 'SourceBufferList': TypeData(clazz='Interface', item_type='SourceBuffer'), | 1537 'SourceBufferList': TypeData(clazz='Interface', item_type='SourceBuffer'), |
1523 'SpeechGrammarList': TypeData(clazz='Interface', item_type='SpeechGrammar'), | 1538 'SpeechGrammarList': TypeData(clazz='Interface', item_type='SpeechGrammar'), |
1524 'SpeechInputResultList': TypeData(clazz='Interface', | 1539 'SpeechInputResultList': TypeData(clazz='Interface', |
1525 item_type='SpeechInputResult', suppress_interface=True), | 1540 item_type='SpeechInputResult', suppress_interface=True), |
1526 'SpeechRecognitionResultList': TypeData(clazz='Interface', | 1541 'SpeechRecognitionResultList': TypeData(clazz='Interface', |
1527 item_type='SpeechRecognitionResult', suppress_interface=True), | 1542 item_type='SpeechRecognitionResult', suppress_interface=True), |
1528 'SQLResultSetRowList': TypeData(clazz='Interface', item_type='Dictionary'), | 1543 'SQLResultSetRowList': TypeData(clazz='Interface', item_type='Dictionary'), |
1529 'StyleSheetList': TypeData(clazz='Interface', | 1544 'StyleSheetList': TypeData(clazz='Interface', |
(...skipping 25 matching lines...) Expand all Loading... |
1555 native_type='SVGPathSegListPropertyTearOff'), | 1570 native_type='SVGPathSegListPropertyTearOff'), |
1556 'SVGPoint': TypeData(clazz='SVGTearOff', native_type='SVGPointTearOff'), | 1571 'SVGPoint': TypeData(clazz='SVGTearOff', native_type='SVGPointTearOff'), |
1557 'SVGPointList': TypeData(clazz='SVGTearOff', native_type='SVGPointListTearOf
f'), | 1572 'SVGPointList': TypeData(clazz='SVGTearOff', native_type='SVGPointListTearOf
f'), |
1558 'SVGPreserveAspectRatio': TypeData(clazz='SVGTearOff', native_type='SVGPrese
rveAspectRatioTearOff'), | 1573 'SVGPreserveAspectRatio': TypeData(clazz='SVGTearOff', native_type='SVGPrese
rveAspectRatioTearOff'), |
1559 'SVGRect': TypeData(clazz='SVGTearOff', native_type='SVGRectTearOff'), | 1574 'SVGRect': TypeData(clazz='SVGTearOff', native_type='SVGRectTearOff'), |
1560 'SVGStringList': TypeData(clazz='SVGTearOff', item_type='DOMString', | 1575 'SVGStringList': TypeData(clazz='SVGTearOff', item_type='DOMString', |
1561 native_type='SVGStringListTearOff'), | 1576 native_type='SVGStringListTearOff'), |
1562 'SVGTransform': TypeData(clazz='SVGTearOff', native_type="SVGPropertyTearOff
<SVGTransform>"), | 1577 'SVGTransform': TypeData(clazz='SVGTearOff', native_type="SVGPropertyTearOff
<SVGTransform>"), |
1563 'SVGTransformList': TypeData(clazz='SVGTearOff', item_type='SVGTransform', | 1578 'SVGTransformList': TypeData(clazz='SVGTearOff', item_type='SVGTransform', |
1564 native_type='SVGTransformListPropertyTearOff'), | 1579 native_type='SVGTransformListPropertyTearOff'), |
| 1580 |
| 1581 # Add any setlike forEach Callback types here. |
| 1582 'FontFaceSetForEachCallback': TypeData(clazz='Interface', item_type='FontFac
eSetForEachCallback'), |
1565 }) | 1583 }) |
1566 | 1584 |
1567 _svg_supplemental_includes = [ | 1585 _svg_supplemental_includes = [ |
1568 '"core/svg/properties/SVGPropertyTraits.h"', | 1586 '"core/svg/properties/SVGPropertyTraits.h"', |
1569 ] | 1587 ] |
1570 | 1588 |
1571 class TypeRegistry(object): | 1589 class TypeRegistry(object): |
1572 def __init__(self, database, renamer=None): | 1590 def __init__(self, database, renamer=None): |
1573 self._database = database | 1591 self._database = database |
1574 self._renamer = renamer | 1592 self._renamer = renamer |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1702 return_type == 'Rectangle') | 1720 return_type == 'Rectangle') |
1703 | 1721 |
1704 def wrap_return_type_blink(return_type, type_name, type_registry): | 1722 def wrap_return_type_blink(return_type, type_name, type_registry): |
1705 """Returns True if we should wrap the returned value. This checks | 1723 """Returns True if we should wrap the returned value. This checks |
1706 a number of different variations, calling the more basic functions | 1724 a number of different variations, calling the more basic functions |
1707 above.""" | 1725 above.""" |
1708 return (wrap_unwrap_type_blink(return_type, type_registry) or | 1726 return (wrap_unwrap_type_blink(return_type, type_registry) or |
1709 wrap_unwrap_type_blink(type_name, type_registry) or | 1727 wrap_unwrap_type_blink(type_name, type_registry) or |
1710 wrap_type_blink(return_type, type_registry) or | 1728 wrap_type_blink(return_type, type_registry) or |
1711 wrap_unwrap_list_blink(return_type, type_registry)) | 1729 wrap_unwrap_list_blink(return_type, type_registry)) |
OLD | NEW |