Chromium Code Reviews| 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 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 else: | 486 else: |
| 487 include = self._idl_type | 487 include = self._idl_type |
| 488 return ['"%s.h"' % include] + _svg_supplemental_includes | 488 return ['"%s.h"' % include] + _svg_supplemental_includes |
| 489 | 489 |
| 490 def receiver(self): | 490 def receiver(self): |
| 491 return 'receiver->' | 491 return 'receiver->' |
| 492 | 492 |
| 493 def conversion_includes(self): | 493 def conversion_includes(self): |
| 494 return ['"Dart%s.h"' % include for include in self._conversion_includes] | 494 return ['"Dart%s.h"' % include for include in self._conversion_includes] |
| 495 | 495 |
| 496 def to_dart_conversion(self, value, interface_name, attributes): | 496 def to_dart_conversion(self, value, interface_name=None, attributes=None): |
| 497 return 'toDartValue(%s)' % value | 497 return 'toDartValue(%s)' % value |
| 498 | 498 |
| 499 def custom_to_dart(self): | 499 def custom_to_dart(self): |
| 500 return self._custom_to_dart | 500 return self._custom_to_dart |
| 501 | 501 |
| 502 | 502 |
| 503 class SequenceIDLTypeInfo(IDLTypeInfo): | 503 class SequenceIDLTypeInfo(IDLTypeInfo): |
| 504 def __init__(self, idl_type, item_info): | 504 def __init__(self, idl_type, item_info): |
| 505 super(SequenceIDLTypeInfo, self).__init__(idl_type) | 505 super(SequenceIDLTypeInfo, self).__init__(idl_type) |
| 506 self._item_info = item_info | 506 self._item_info = item_info |
| 507 | 507 |
| 508 def dart_type(self): | 508 def dart_type(self): |
| 509 return 'List<%s>' % self._item_info.dart_type() | 509 return 'List<%s>' % self._item_info.dart_type() |
| 510 | 510 |
| 511 def conversion_includes(self): | 511 def conversion_includes(self): |
| 512 return self._item_info.conversion_includes() | 512 return self._item_info.conversion_includes() |
| 513 | 513 |
| 514 | 514 |
| 515 class PrimitiveIDLTypeInfo(IDLTypeInfo): | 515 class PrimitiveIDLTypeInfo(IDLTypeInfo): |
| 516 def __init__(self, idl_type, dart_type, native_type=None, ref_counted=False, | 516 def __init__(self, idl_type, dart_type, native_type=None, ref_counted=False, |
| 517 needs_static_cast=False, | |
| 518 webcore_getter_name='getAttribute', | 517 webcore_getter_name='getAttribute', |
| 519 webcore_setter_name='setAttribute'): | 518 webcore_setter_name='setAttribute'): |
| 520 super(PrimitiveIDLTypeInfo, self).__init__(idl_type, dart_type=dart_type, | 519 super(PrimitiveIDLTypeInfo, self).__init__(idl_type, dart_type=dart_type, |
| 521 native_type=native_type, ref_counted=ref_counted) | 520 native_type=native_type, ref_counted=ref_counted) |
| 522 self._needs_static_cast = needs_static_cast | |
| 523 self._webcore_getter_name = webcore_getter_name | 521 self._webcore_getter_name = webcore_getter_name |
| 524 self._webcore_setter_name = webcore_setter_name | 522 self._webcore_setter_name = webcore_setter_name |
| 525 | 523 |
| 526 def parameter_adapter_info(self): | 524 def parameter_adapter_info(self): |
| 527 native_type = self.native_type() | 525 native_type = self.native_type() |
| 528 if self._ref_counted: | 526 if self._ref_counted: |
| 529 native_type = 'RefPtr< %s >' % native_type | 527 native_type = 'RefPtr< %s >' % native_type |
| 530 return ('ParameterAdapter< %s >' % native_type, None) | 528 return ('ParameterAdapter< %s >' % native_type, None) |
| 531 | 529 |
| 532 def parameter_type(self): | 530 def parameter_type(self): |
| 533 if self.native_type() == 'String': | 531 if self.native_type() == 'String': |
| 534 return 'const String&' | 532 return 'const String&' |
| 535 return self.native_type() | 533 return self.native_type() |
| 536 | 534 |
| 537 def conversion_includes(self): | 535 def conversion_includes(self): |
| 538 return [] | 536 return [] |
| 539 | 537 |
| 540 def to_dart_conversion(self, value, interface_name, attributes): | 538 def to_dart_conversion(self, value, interface_name=None, attributes=None): |
| 541 if self._needs_static_cast: | |
| 542 value = 'static_cast<%s>(%s)' % (self.native_type(), value) | |
| 543 conversion_arguments = [value] | 539 conversion_arguments = [value] |
| 544 if 'TreatReturnedNullStringAs' in attributes: | 540 if attributes and 'TreatReturnedNullStringAs' in attributes: |
| 545 conversion_arguments.append('ConvertDefaultToNull') | 541 conversion_arguments.append('DartUtilities::ConvertNullToDefaultValue') |
| 546 return 'toDartValue(%s)' % ', '.join(conversion_arguments) | 542 function_name = 'toDartValue' |
| 543 # FIXME: implement DartUtilities::toDart for other primitive types and | |
| 544 # remove this list. | |
| 545 if self.native_type() in ['String', 'bool', 'int', 'unsigned', 'long long', 'unsigned long long', 'double']: | |
| 546 words = self.native_type().split(' ') | |
|
Anton Muhin
2012/04/19 19:45:02
maybe use string.capwords: http://docs.python.org/
podivilov
2012/04/20 13:11:07
Don't think it could help here.
| |
| 547 words[0] = words[0][0].lower() + words[0][1:] | |
| 548 for i in range(1, len(words)): | |
| 549 words[i] = words[i].capitalize() | |
| 550 function_name = 'DartUtilities::%sToDart' % ''.join(words) | |
| 551 return '%s(%s)' % (function_name, ', '.join(conversion_arguments)) | |
| 547 | 552 |
| 548 def webcore_getter_name(self): | 553 def webcore_getter_name(self): |
| 549 return self._webcore_getter_name | 554 return self._webcore_getter_name |
| 550 | 555 |
| 551 def webcore_setter_name(self): | 556 def webcore_setter_name(self): |
| 552 return self._webcore_setter_name | 557 return self._webcore_setter_name |
| 553 | 558 |
| 554 class SVGTearOffIDLTypeInfo(IDLTypeInfo): | 559 class SVGTearOffIDLTypeInfo(IDLTypeInfo): |
| 555 def __init__(self, idl_type, native_type='', ref_counted=True): | 560 def __init__(self, idl_type, native_type='', ref_counted=True): |
| 556 super(SVGTearOffIDLTypeInfo, self).__init__(idl_type, | 561 super(SVGTearOffIDLTypeInfo, self).__init__(idl_type, |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 581 elif interface_name.endswith('List'): | 586 elif interface_name.endswith('List'): |
| 582 conversion_cast = 'static_cast<%s*>(%s.get())' | 587 conversion_cast = 'static_cast<%s*>(%s.get())' |
| 583 elif self.idl_type() in svg_primitive_types: | 588 elif self.idl_type() in svg_primitive_types: |
| 584 conversion_cast = '%s::create(%s)' | 589 conversion_cast = '%s::create(%s)' |
| 585 else: | 590 else: |
| 586 conversion_cast = 'static_cast<%s*>(%s)' | 591 conversion_cast = 'static_cast<%s*>(%s)' |
| 587 conversion_cast = conversion_cast % (self.native_type(), value) | 592 conversion_cast = conversion_cast % (self.native_type(), value) |
| 588 return 'toDartValue(%s)' % conversion_cast | 593 return 'toDartValue(%s)' % conversion_cast |
| 589 | 594 |
| 590 _idl_type_registry = { | 595 _idl_type_registry = { |
| 591 # There is GC3Dboolean which is not a bool, but unsigned char for OpenGL com patibility. | |
| 592 'boolean': PrimitiveIDLTypeInfo('boolean', dart_type='bool', native_type='bo ol', | 596 'boolean': PrimitiveIDLTypeInfo('boolean', dart_type='bool', native_type='bo ol', |
| 593 needs_static_cast=True, | |
| 594 webcore_getter_name='hasAttribute', | 597 webcore_getter_name='hasAttribute', |
| 595 webcore_setter_name='setBooleanAttribute'), | 598 webcore_setter_name='setBooleanAttribute'), |
| 596 # Some IDL's unsigned shorts/shorts are mapped to WebCore C++ enums, so we | 599 'short': PrimitiveIDLTypeInfo('short', dart_type='int', native_type='int'), |
| 597 # use a static_cast<int> here not to provide overloads for all enums. | |
| 598 'short': PrimitiveIDLTypeInfo('short', dart_type='int', native_type='int', | |
| 599 needs_static_cast=True), | |
| 600 'unsigned short': PrimitiveIDLTypeInfo('unsigned short', dart_type='int', | 600 'unsigned short': PrimitiveIDLTypeInfo('unsigned short', dart_type='int', |
| 601 native_type='int', needs_static_cast=True,), | 601 native_type='int'), |
| 602 'int': PrimitiveIDLTypeInfo('int', dart_type='int'), | 602 'int': PrimitiveIDLTypeInfo('int', dart_type='int'), |
| 603 'unsigned int': PrimitiveIDLTypeInfo('unsigned int', dart_type='int', | 603 'unsigned int': PrimitiveIDLTypeInfo('unsigned int', dart_type='int', |
| 604 native_type='unsigned'), | 604 native_type='unsigned'), |
| 605 'long': PrimitiveIDLTypeInfo('long', dart_type='int', native_type='int', | 605 'long': PrimitiveIDLTypeInfo('long', dart_type='int', native_type='int', |
| 606 webcore_getter_name='getIntegralAttribute', | 606 webcore_getter_name='getIntegralAttribute', |
| 607 webcore_setter_name='setIntegralAttribute'), | 607 webcore_setter_name='setIntegralAttribute'), |
| 608 'unsigned long': PrimitiveIDLTypeInfo('unsigned long', dart_type='int', | 608 'unsigned long': PrimitiveIDLTypeInfo('unsigned long', dart_type='int', |
| 609 native_type='unsigned', | 609 native_type='unsigned', |
| 610 webcore_getter_name='getUnsignedIntegralAttribute', | 610 webcore_getter_name='getUnsignedIntegralAttribute', |
| 611 webcore_setter_name='setUnsignedIntegralAttribute'), | 611 webcore_setter_name='setUnsignedIntegralAttribute'), |
| 612 'long long': PrimitiveIDLTypeInfo('long long', dart_type='int'), | 612 'long long': PrimitiveIDLTypeInfo('long long', dart_type='int'), |
| 613 'unsigned long long': PrimitiveIDLTypeInfo('unsigned long long', dart_type=' int'), | 613 'unsigned long long': PrimitiveIDLTypeInfo('unsigned long long', dart_type=' int'), |
| 614 'float': PrimitiveIDLTypeInfo('float', dart_type='num', native_type='double' ), | 614 'float': PrimitiveIDLTypeInfo('float', dart_type='num', native_type='double' ), |
| 615 'double': PrimitiveIDLTypeInfo('double', dart_type='num'), | 615 'double': PrimitiveIDLTypeInfo('double', dart_type='num'), |
| 616 | 616 |
| 617 'any': PrimitiveIDLTypeInfo('any', dart_type='Object'), | 617 'any': PrimitiveIDLTypeInfo('any', dart_type='Object'), |
| 618 'any[]': PrimitiveIDLTypeInfo('any[]', dart_type='List'), | 618 'any[]': PrimitiveIDLTypeInfo('any[]', dart_type='List'), |
| 619 'Array': PrimitiveIDLTypeInfo('Array', dart_type='List'), | 619 'Array': PrimitiveIDLTypeInfo('Array', dart_type='List'), |
| 620 'custom': PrimitiveIDLTypeInfo('custom', dart_type='Dynamic'), | 620 'custom': PrimitiveIDLTypeInfo('custom', dart_type='Dynamic'), |
| 621 'Date': PrimitiveIDLTypeInfo('Date', dart_type='Date', native_type='double') , | 621 'Date': PrimitiveIDLTypeInfo('Date', dart_type='Date', native_type='double') , |
| 622 'DOMObject': PrimitiveIDLTypeInfo('DOMObject', dart_type='Object', native_ty pe='ScriptValue'), | 622 'DOMObject': PrimitiveIDLTypeInfo('DOMObject', dart_type='Object', native_ty pe='ScriptValue'), |
| 623 'DOMString': PrimitiveIDLTypeInfo('DOMString', dart_type='String', native_ty pe='String'), | 623 'DOMString': PrimitiveIDLTypeInfo('DOMString', dart_type='String', native_ty pe='String'), |
| 624 # TODO(sra): Flags is really a dictionary: {create:bool, exclusive:bool} | 624 # TODO(sra): Flags is really a dictionary: {create:bool, exclusive:bool} |
| 625 # http://dev.w3.org/2009/dap/file-system/file-dir-sys.html#the-flags-interfa ce | 625 # http://dev.w3.org/2009/dap/file-system/file-dir-sys.html#the-flags-interfa ce |
| 626 'Flags': PrimitiveIDLTypeInfo('Flags', dart_type='Object'), | 626 'Flags': PrimitiveIDLTypeInfo('Flags', dart_type='Object'), |
| 627 'DOMTimeStamp': PrimitiveIDLTypeInfo('DOMTimeStamp', dart_type='int'), | 627 'DOMTimeStamp': PrimitiveIDLTypeInfo('DOMTimeStamp', dart_type='int', native _type='unsigned long long'), |
| 628 'object': PrimitiveIDLTypeInfo('object', dart_type='Object', native_type='Sc riptValue'), | 628 'object': PrimitiveIDLTypeInfo('object', dart_type='Object', native_type='Sc riptValue'), |
| 629 # TODO(sra): Come up with some meaningful name so that where this appears in | 629 # TODO(sra): Come up with some meaningful name so that where this appears in |
| 630 # the documentation, the user is made aware that only a limited subset of | 630 # the documentation, the user is made aware that only a limited subset of |
| 631 # serializable types are actually permitted. | 631 # serializable types are actually permitted. |
| 632 'SerializedScriptValue': PrimitiveIDLTypeInfo('SerializedScriptValue', dart_ type='Dynamic', ref_counted=True), | 632 'SerializedScriptValue': PrimitiveIDLTypeInfo('SerializedScriptValue', dart_ type='Dynamic', ref_counted=True), |
| 633 # TODO(sra): Flags is really a dictionary: {create:bool, exclusive:bool} | 633 # TODO(sra): Flags is really a dictionary: {create:bool, exclusive:bool} |
| 634 # http://dev.w3.org/2009/dap/file-system/file-dir-sys.html#the-flags-interfa ce | 634 # http://dev.w3.org/2009/dap/file-system/file-dir-sys.html#the-flags-interfa ce |
| 635 'WebKitFlags': PrimitiveIDLTypeInfo('WebKitFlags', dart_type='Object'), | 635 'WebKitFlags': PrimitiveIDLTypeInfo('WebKitFlags', dart_type='Object'), |
| 636 | 636 |
| 637 'DOMStringList': PrimitiveIDLTypeInfo('DOMStringList', dart_type='List<Strin g>'), | 637 'DOMStringList': PrimitiveIDLTypeInfo('DOMStringList', dart_type='List<Strin g>'), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 676 '"SVGAnimatedListPropertyTearOff.h"', | 676 '"SVGAnimatedListPropertyTearOff.h"', |
| 677 '"SVGTransformListPropertyTearOff.h"', | 677 '"SVGTransformListPropertyTearOff.h"', |
| 678 '"SVGPathSegListPropertyTearOff.h"', | 678 '"SVGPathSegListPropertyTearOff.h"', |
| 679 ] | 679 ] |
| 680 | 680 |
| 681 def GetIDLTypeInfo(idl_type_name): | 681 def GetIDLTypeInfo(idl_type_name): |
| 682 match = re.match(r'sequence<(\w+)>$', idl_type_name) | 682 match = re.match(r'sequence<(\w+)>$', idl_type_name) |
| 683 if match: | 683 if match: |
| 684 return SequenceIDLTypeInfo(idl_type_name, GetIDLTypeInfo(match.group(1))) | 684 return SequenceIDLTypeInfo(idl_type_name, GetIDLTypeInfo(match.group(1))) |
| 685 return _idl_type_registry.get(idl_type_name, IDLTypeInfo(idl_type_name)) | 685 return _idl_type_registry.get(idl_type_name, IDLTypeInfo(idl_type_name)) |
| OLD | NEW |