| 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 re | 9 import re |
| 10 | 10 |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 # name | 419 # name |
| 420 def TypeName(type_ids, interface): | 420 def TypeName(type_ids, interface): |
| 421 # Dynamically type this field for now. | 421 # Dynamically type this field for now. |
| 422 return 'Dynamic' | 422 return 'Dynamic' |
| 423 | 423 |
| 424 # ------------------------------------------------------------------------------ | 424 # ------------------------------------------------------------------------------ |
| 425 | 425 |
| 426 class IDLTypeInfo(object): | 426 class IDLTypeInfo(object): |
| 427 def __init__(self, idl_type, dart_type=None, | 427 def __init__(self, idl_type, dart_type=None, |
| 428 native_type=None, ref_counted=True, | 428 native_type=None, ref_counted=True, |
| 429 custom_parameter_adapter=False, conversion_template=None, | 429 custom_parameter_adapter=False, |
| 430 custom_to_dart=False, to_dart_includes=[]): | 430 custom_to_dart=False, to_dart_includes=[]): |
| 431 self._idl_type = idl_type | 431 self._idl_type = idl_type |
| 432 self._dart_type = dart_type | 432 self._dart_type = dart_type |
| 433 self._native_type = native_type | 433 self._native_type = native_type |
| 434 self._ref_counted = ref_counted | 434 self._ref_counted = ref_counted |
| 435 self._custom_parameter_adapter = custom_parameter_adapter | 435 self._custom_parameter_adapter = custom_parameter_adapter |
| 436 self._conversion_template = conversion_template | |
| 437 self._custom_to_dart = custom_to_dart | 436 self._custom_to_dart = custom_to_dart |
| 438 self._to_dart_includes = to_dart_includes | 437 self._to_dart_includes = to_dart_includes |
| 439 | 438 |
| 440 def idl_type(self): | 439 def idl_type(self): |
| 441 return self._idl_type | 440 return self._idl_type |
| 442 | 441 |
| 443 def dart_type(self): | 442 def dart_type(self): |
| 444 if self._dart_type: | 443 if self._dart_type: |
| 445 return self._dart_type | 444 return self._dart_type |
| 446 return self._idl_type | 445 return self._idl_type |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 return ['"%s.h"' % include] + _svg_supplemental_includes | 492 return ['"%s.h"' % include] + _svg_supplemental_includes |
| 494 | 493 |
| 495 def receiver(self): | 494 def receiver(self): |
| 496 return 'receiver->' | 495 return 'receiver->' |
| 497 | 496 |
| 498 def to_dart_includes(self): | 497 def to_dart_includes(self): |
| 499 return ['"Dart%s.h"' % include | 498 return ['"Dart%s.h"' % include |
| 500 for include in | 499 for include in |
| 501 [self.idl_type()] + self._to_dart_includes] | 500 [self.idl_type()] + self._to_dart_includes] |
| 502 | 501 |
| 503 def conversion_cast(self, expression): | 502 def to_dart_conversion(self, value, interface_name, attributes): |
| 504 if self._conversion_template: | 503 return 'toDartValue(%s)' % value |
| 505 return self._conversion_template % expression | |
| 506 return expression | |
| 507 | 504 |
| 508 def custom_to_dart(self): | 505 def custom_to_dart(self): |
| 509 return self._custom_to_dart | 506 return self._custom_to_dart |
| 510 | 507 |
| 511 | 508 |
| 512 class CompositeIDLTypeInfo(IDLTypeInfo): | 509 class CompositeIDLTypeInfo(IDLTypeInfo): |
| 513 def __init__(self, idl_type, collection_info, item_info): | 510 def __init__(self, idl_type, collection_info, item_info): |
| 514 super(CompositeIDLTypeInfo, self).__init__(idl_type) | 511 super(CompositeIDLTypeInfo, self).__init__(idl_type) |
| 515 self._collection_info = collection_info | 512 self._collection_info = collection_info |
| 516 self._item_info = item_info | 513 self._item_info = item_info |
| 517 | 514 |
| 518 def dart_type(self): | 515 def dart_type(self): |
| 519 collection_type = self._collection_info.dart_type() | 516 collection_type = self._collection_info.dart_type() |
| 520 item_type = self._item_info.dart_type() | 517 item_type = self._item_info.dart_type() |
| 521 return '%s<%s>' % (collection_type, item_type) | 518 return '%s<%s>' % (collection_type, item_type) |
| 522 | 519 |
| 523 def to_dart_includes(self): | 520 def to_dart_includes(self): |
| 524 return self._item_info.to_dart_includes() | 521 return self._item_info.to_dart_includes() |
| 525 | 522 |
| 526 | 523 |
| 527 class PrimitiveIDLTypeInfo(IDLTypeInfo): | 524 class PrimitiveIDLTypeInfo(IDLTypeInfo): |
| 528 def __init__(self, idl_type, dart_type, native_type=None, ref_counted=False, | 525 def __init__(self, idl_type, dart_type, native_type=None, ref_counted=False, |
| 529 conversion_template=None, | 526 needs_static_cast=False, |
| 530 webcore_getter_name='getAttribute', | 527 webcore_getter_name='getAttribute', |
| 531 webcore_setter_name='setAttribute'): | 528 webcore_setter_name='setAttribute'): |
| 532 super(PrimitiveIDLTypeInfo, self).__init__(idl_type, dart_type=dart_type, | 529 super(PrimitiveIDLTypeInfo, self).__init__(idl_type, dart_type=dart_type, |
| 533 native_type=native_type, ref_counted=ref_counted, | 530 native_type=native_type, ref_counted=ref_counted) |
| 534 conversion_template=conversion_template) | 531 self._needs_static_cast = needs_static_cast |
| 535 self._webcore_getter_name = webcore_getter_name | 532 self._webcore_getter_name = webcore_getter_name |
| 536 self._webcore_setter_name = webcore_setter_name | 533 self._webcore_setter_name = webcore_setter_name |
| 537 | 534 |
| 538 def parameter_adapter_info(self): | 535 def parameter_adapter_info(self): |
| 539 native_type = self.native_type() | 536 native_type = self.native_type() |
| 540 if self._ref_counted: | 537 if self._ref_counted: |
| 541 native_type = 'RefPtr< %s >' % native_type | 538 native_type = 'RefPtr< %s >' % native_type |
| 542 return ('ParameterAdapter< %s >' % native_type, None) | 539 return ('ParameterAdapter< %s >' % native_type, None) |
| 543 | 540 |
| 544 def parameter_type(self): | 541 def parameter_type(self): |
| 545 if self.native_type() == 'String': | 542 if self.native_type() == 'String': |
| 546 return 'const String&' | 543 return 'const String&' |
| 547 return self.native_type() | 544 return self.native_type() |
| 548 | 545 |
| 549 def to_dart_includes(self): | 546 def to_dart_includes(self): |
| 550 return [] | 547 return [] |
| 551 | 548 |
| 549 def to_dart_conversion(self, value, interface_name, attributes): |
| 550 if self._needs_static_cast: |
| 551 value = 'static_cast<%s>(%s)' % (self.native_type(), value) |
| 552 conversion_arguments = [value] |
| 553 if 'TreatReturnedNullStringAs' in attributes: |
| 554 conversion_arguments.append('ConvertDefaultToNull') |
| 555 return 'toDartValue(%s)' % ', '.join(conversion_arguments) |
| 556 |
| 552 def webcore_getter_name(self): | 557 def webcore_getter_name(self): |
| 553 return self._webcore_getter_name | 558 return self._webcore_getter_name |
| 554 | 559 |
| 555 def webcore_setter_name(self): | 560 def webcore_setter_name(self): |
| 556 return self._webcore_setter_name | 561 return self._webcore_setter_name |
| 557 | 562 |
| 558 class SVGTearOffIDLTypeInfo(IDLTypeInfo): | 563 class SVGTearOffIDLTypeInfo(IDLTypeInfo): |
| 559 def __init__(self, idl_type, native_type='', ref_counted=True): | 564 def __init__(self, idl_type, native_type='', ref_counted=True): |
| 560 super(SVGTearOffIDLTypeInfo, self).__init__(idl_type, | 565 super(SVGTearOffIDLTypeInfo, self).__init__(idl_type, |
| 561 native_type=native_type, | 566 native_type=native_type, |
| 562 ref_counted=ref_counted) | 567 ref_counted=ref_counted) |
| 563 | 568 |
| 564 def native_type(self): | 569 def native_type(self): |
| 565 if self._native_type: | 570 if self._native_type: |
| 566 return self._native_type | 571 return self._native_type |
| 567 tear_off_type = 'SVGPropertyTearOff' | 572 tear_off_type = 'SVGPropertyTearOff' |
| 568 if self._idl_type.endswith('List'): | 573 if self._idl_type.endswith('List'): |
| 569 tear_off_type = 'SVGListPropertyTearOff' | 574 tear_off_type = 'SVGListPropertyTearOff' |
| 570 return '%s<%s>' % (tear_off_type, self._idl_type) | 575 return '%s<%s>' % (tear_off_type, self._idl_type) |
| 571 | 576 |
| 572 def receiver(self): | 577 def receiver(self): |
| 573 if self._idl_type.endswith('List'): | 578 if self._idl_type.endswith('List'): |
| 574 return 'receiver->' | 579 return 'receiver->' |
| 575 return 'receiver->propertyReference().' | 580 return 'receiver->propertyReference().' |
| 576 | 581 |
| 582 def to_dart_conversion(self, value, interface_name, attributes): |
| 583 svg_primitive_types = ['SVGAngle', 'SVGLength', 'SVGMatrix', |
| 584 'SVGNumber', 'SVGPoint', 'SVGRect', 'SVGTransform'] |
| 585 conversion_cast = '%s::create(%s)' |
| 586 if interface_name.startswith('SVGAnimated'): |
| 587 conversion_cast = 'static_cast<%s*>(%s)' |
| 588 elif self.idl_type() == 'SVGStringList': |
| 589 conversion_cast = '%s::create(receiver, %s)' |
| 590 elif interface_name.endswith('List'): |
| 591 conversion_cast = 'static_cast<%s*>(%s.get())' |
| 592 elif self.idl_type() in svg_primitive_types: |
| 593 conversion_cast = '%s::create(%s)' |
| 594 else: |
| 595 conversion_cast = 'static_cast<%s*>(%s)' |
| 596 conversion_cast = conversion_cast % (self.native_type(), value) |
| 597 return 'toDartValue(%s)' % conversion_cast |
| 577 | 598 |
| 578 _idl_type_registry = { | 599 _idl_type_registry = { |
| 579 # There is GC3Dboolean which is not a bool, but unsigned char for OpenGL com
patibility. | 600 # There is GC3Dboolean which is not a bool, but unsigned char for OpenGL com
patibility. |
| 580 'boolean': PrimitiveIDLTypeInfo('boolean', dart_type='bool', native_type='bo
ol', | 601 'boolean': PrimitiveIDLTypeInfo('boolean', dart_type='bool', native_type='bo
ol', |
| 581 conversion_template='static_cast<bool>(%s)', | 602 needs_static_cast=True, |
| 582 webcore_getter_name='hasAttribute', | 603 webcore_getter_name='hasAttribute', |
| 583 webcore_setter_name='setBooleanAttribute'), | 604 webcore_setter_name='setBooleanAttribute'), |
| 584 # Some IDL's unsigned shorts/shorts are mapped to WebCore C++ enums, so we | 605 # Some IDL's unsigned shorts/shorts are mapped to WebCore C++ enums, so we |
| 585 # use a static_cast<int> here not to provide overloads for all enums. | 606 # use a static_cast<int> here not to provide overloads for all enums. |
| 586 'short': PrimitiveIDLTypeInfo('short', dart_type='int', native_type='int', | 607 'short': PrimitiveIDLTypeInfo('short', dart_type='int', native_type='int', |
| 587 conversion_template='static_cast<int>(%s)'), | 608 needs_static_cast=True), |
| 588 'unsigned short': PrimitiveIDLTypeInfo('unsigned short', dart_type='int', | 609 'unsigned short': PrimitiveIDLTypeInfo('unsigned short', dart_type='int', |
| 589 native_type='int', conversion_template='static_cast<int>(%s)'), | 610 native_type='int', needs_static_cast=True,), |
| 590 'int': PrimitiveIDLTypeInfo('int', dart_type='int'), | 611 'int': PrimitiveIDLTypeInfo('int', dart_type='int'), |
| 591 'unsigned int': PrimitiveIDLTypeInfo('unsigned int', dart_type='int', | 612 'unsigned int': PrimitiveIDLTypeInfo('unsigned int', dart_type='int', |
| 592 native_type='unsigned'), | 613 native_type='unsigned'), |
| 593 'long': PrimitiveIDLTypeInfo('long', dart_type='int', native_type='int', | 614 'long': PrimitiveIDLTypeInfo('long', dart_type='int', native_type='int', |
| 594 webcore_getter_name='getIntegralAttribute', | 615 webcore_getter_name='getIntegralAttribute', |
| 595 webcore_setter_name='setIntegralAttribute'), | 616 webcore_setter_name='setIntegralAttribute'), |
| 596 'unsigned long': PrimitiveIDLTypeInfo('unsigned long', dart_type='int', | 617 'unsigned long': PrimitiveIDLTypeInfo('unsigned long', dart_type='int', |
| 597 native_type='unsigned', | 618 native_type='unsigned', |
| 598 webcore_getter_name='getUnsignedIntegralAttribute', | 619 webcore_getter_name='getUnsignedIntegralAttribute', |
| 599 webcore_setter_name='setUnsignedIntegralAttribute'), | 620 webcore_setter_name='setUnsignedIntegralAttribute'), |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 '"SVGPathSegListPropertyTearOff.h"', | 687 '"SVGPathSegListPropertyTearOff.h"', |
| 667 ] | 688 ] |
| 668 | 689 |
| 669 def GetIDLTypeInfo(idl_type_name): | 690 def GetIDLTypeInfo(idl_type_name): |
| 670 match = re.match(r'(\w+)<(\w+)>$', idl_type_name) | 691 match = re.match(r'(\w+)<(\w+)>$', idl_type_name) |
| 671 if match: | 692 if match: |
| 672 collection_info = GetIDLTypeInfo(match.group(1)) | 693 collection_info = GetIDLTypeInfo(match.group(1)) |
| 673 item_info = GetIDLTypeInfo(match.group(2)) | 694 item_info = GetIDLTypeInfo(match.group(2)) |
| 674 return CompositeIDLTypeInfo(idl_type_name, collection_info, item_info) | 695 return CompositeIDLTypeInfo(idl_type_name, collection_info, item_info) |
| 675 return _idl_type_registry.get(idl_type_name, IDLTypeInfo(idl_type_name)) | 696 return _idl_type_registry.get(idl_type_name, IDLTypeInfo(idl_type_name)) |
| OLD | NEW |