| 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 re | 10 import re |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 type_id: Original type id. None for merged types. | 162 type_id: Original type id. None for merged types. |
| 163 dart_type: DartType of parameter. | 163 dart_type: DartType of parameter. |
| 164 is_optional: Parameter optionality. | 164 is_optional: Parameter optionality. |
| 165 """ | 165 """ |
| 166 def __init__(self, name, type_id, dart_type, is_optional): | 166 def __init__(self, name, type_id, dart_type, is_optional): |
| 167 self.name = name | 167 self.name = name |
| 168 self.type_id = type_id | 168 self.type_id = type_id |
| 169 self.dart_type = dart_type | 169 self.dart_type = dart_type |
| 170 self.is_optional = is_optional | 170 self.is_optional = is_optional |
| 171 | 171 |
| 172 def Copy(self): | |
| 173 return ParamInfo(self.name, self.type_id, self.dart_type, self.is_optional) | |
| 174 | |
| 175 def __repr__(self): | 172 def __repr__(self): |
| 176 content = 'name = %s, type_id = %s, dart_type = %s, is_optional = %s' % ( | 173 content = 'name = %s, type_id = %s, dart_type = %s, is_optional = %s' % ( |
| 177 self.name, self.type_id, self.dart_type, self.is_optional) | 174 self.name, self.type_id, self.dart_type, self.is_optional) |
| 178 return '<ParamInfo(%s)>' % content | 175 return '<ParamInfo(%s)>' % content |
| 179 | 176 |
| 180 | 177 |
| 181 # Given a list of overloaded arguments, render a dart argument. | 178 # Given a list of overloaded arguments, render a dart argument. |
| 182 def _DartArg(args, interface, constructor=False): | 179 def _DartArg(args, interface, constructor=False): |
| 183 # Given a list of overloaded arguments, choose a suitable name. | 180 # Given a list of overloaded arguments, choose a suitable name. |
| 184 def OverloadedName(args): | 181 def OverloadedName(args): |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 is_static = self.overloads[0].is_static | 406 is_static = self.overloads[0].is_static |
| 410 assert any([is_static == o.is_static for o in self.overloads]) | 407 assert any([is_static == o.is_static for o in self.overloads]) |
| 411 return is_static | 408 return is_static |
| 412 | 409 |
| 413 def ConstructorFullName(self): | 410 def ConstructorFullName(self): |
| 414 if self.constructor_name: | 411 if self.constructor_name: |
| 415 return self.type_name + '.' + self.constructor_name | 412 return self.type_name + '.' + self.constructor_name |
| 416 else: | 413 else: |
| 417 return self.type_name | 414 return self.type_name |
| 418 | 415 |
| 419 def CopyAndWidenDefaultParameters(self): | |
| 420 """Returns equivalent OperationInfo, but default parameters are Dynamic.""" | |
| 421 info = copy.copy(self) | |
| 422 info.param_infos = [param.Copy() for param in self.param_infos] | |
| 423 for param in info.param_infos: | |
| 424 if param.is_optional: | |
| 425 param.dart_type = 'Dynamic' | |
| 426 return info | |
| 427 | |
| 428 | 416 |
| 429 def ConstantOutputOrder(a, b): | 417 def ConstantOutputOrder(a, b): |
| 430 """Canonical output ordering for constants.""" | 418 """Canonical output ordering for constants.""" |
| 431 if a.id < b.id: return -1 | 419 if a.id < b.id: return -1 |
| 432 if a.id > b.id: return 1 | 420 if a.id > b.id: return 1 |
| 433 return 0 | 421 return 0 |
| 434 | 422 |
| 435 | 423 |
| 436 def _FormatNameList(names): | 424 def _FormatNameList(names): |
| 437 """Returns JavaScript array literal expression with one name per line.""" | 425 """Returns JavaScript array literal expression with one name per line.""" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 454 return ''.join(FormatLine(line) for line in text.split('\n')) | 442 return ''.join(FormatLine(line) for line in text.split('\n')) |
| 455 | 443 |
| 456 # Given a sorted sequence of type identifiers, return an appropriate type | 444 # Given a sorted sequence of type identifiers, return an appropriate type |
| 457 # name | 445 # name |
| 458 def TypeName(type_ids, interface): | 446 def TypeName(type_ids, interface): |
| 459 # Dynamically type this field for now. | 447 # Dynamically type this field for now. |
| 460 return 'Dynamic' | 448 return 'Dynamic' |
| 461 | 449 |
| 462 # ------------------------------------------------------------------------------ | 450 # ------------------------------------------------------------------------------ |
| 463 | 451 |
| 464 class Conversion(object): | |
| 465 """Represents a way of converting between types.""" | |
| 466 def __init__(self, name, input_type, output_type): | |
| 467 # input_type is the type of the API input (and the argument type of the | |
| 468 # conversion function) | |
| 469 # output_type is the type of the API output (and the result type of the | |
| 470 # conversion function) | |
| 471 self.function_name = name | |
| 472 self.input_type = input_type | |
| 473 self.output_type = output_type | |
| 474 | |
| 475 # TYPE -> "DIRECTION INTERFACE.MEMBER" -> conversion | |
| 476 # TYPE -> "DIRECTION INTERFACE.*" -> conversion | |
| 477 # TYPE -> "DIRECTION" -> conversion | |
| 478 # | |
| 479 # where DIRECTION is 'get' for getters and operation return values, 'set' for | |
| 480 # setters and operation arguments. INTERFACE and MEMBER are the idl names. | |
| 481 # | |
| 482 dart2js_conversions = { | |
| 483 'IDBKey': { | |
| 484 'get': | |
| 485 Conversion('_convertNativeToDart_IDBKey', 'Dynamic', 'Dynamic'), | |
| 486 'set': | |
| 487 Conversion('_convertDartToNative_IDBKey', 'Dynamic', 'Dynamic'), | |
| 488 }, | |
| 489 'ImageData': { | |
| 490 'get': | |
| 491 Conversion('_convertNativeToDart_ImageData', 'Dynamic', 'ImageData'), | |
| 492 'set': | |
| 493 Conversion('_convertDartToNative_ImageData', 'ImageData', 'Dynamic') | |
| 494 }, | |
| 495 'Dictionary': { | |
| 496 'get': | |
| 497 Conversion('_convertNativeToDart_Dictionary', 'Dynamic', 'Map'), | |
| 498 'set': | |
| 499 Conversion('_convertDartToNative_Dictionary', 'Map', 'Dynamic'), | |
| 500 }, | |
| 501 | |
| 502 'DOMString[]': { | |
| 503 'set': | |
| 504 Conversion( | |
| 505 '_convertDartToNative_StringArray', 'List<String>', 'List'), | |
| 506 }, | |
| 507 } | |
| 508 | |
| 509 def FindConversion(idl_type, direction, interface, member): | |
| 510 table = dart2js_conversions.get(idl_type) | |
| 511 if table: | |
| 512 return (table.get('%s %s.%s' % (direction, interface, member)) or | |
| 513 table.get('%s %s.*' % (direction, interface)) or | |
| 514 table.get(direction)) | |
| 515 return None | |
| 516 | |
| 517 # ------------------------------------------------------------------------------ | |
| 518 | |
| 519 class IDLTypeInfo(object): | 452 class IDLTypeInfo(object): |
| 520 def __init__(self, idl_type, data): | 453 def __init__(self, idl_type, data): |
| 521 self._idl_type = idl_type | 454 self._idl_type = idl_type |
| 522 self._data = data | 455 self._data = data |
| 523 | 456 |
| 524 def idl_type(self): | 457 def idl_type(self): |
| 525 return self._idl_type | 458 return self._idl_type |
| 526 | 459 |
| 527 def dart_type(self): | 460 def dart_type(self): |
| 528 return self._data.dart_type or self._idl_type | 461 return self._data.dart_type or self._idl_type |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 if match: | 780 if match: |
| 848 if type_name == 'DOMString[]': | 781 if type_name == 'DOMString[]': |
| 849 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt
ring')) | 782 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt
ring')) |
| 850 item_info = self.TypeInfo(match.group(1) or match.group(2)) | 783 item_info = self.TypeInfo(match.group(1) or match.group(2)) |
| 851 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) | 784 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) |
| 852 if not type_name in _idl_type_registry: | 785 if not type_name in _idl_type_registry: |
| 853 return InterfaceIDLTypeInfo(type_name, TypeData('Interface')) | 786 return InterfaceIDLTypeInfo(type_name, TypeData('Interface')) |
| 854 type_data = _idl_type_registry.get(type_name) | 787 type_data = _idl_type_registry.get(type_name) |
| 855 class_name = '%sIDLTypeInfo' % type_data.clazz | 788 class_name = '%sIDLTypeInfo' % type_data.clazz |
| 856 return globals()[class_name](type_name, type_data) | 789 return globals()[class_name](type_name, type_data) |
| OLD | NEW |