| 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 the systems to generate | 6 """This module provides shared functionality for the systems to generate |
| 7 native binding from the IDL database.""" | 7 native binding from the IDL database.""" |
| 8 | 8 |
| 9 import emitter | 9 import emitter |
| 10 import os | 10 import os |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 ' exception = Dart_NewString("Failed to create an object"
);\n' | 297 ' exception = Dart_NewString("Failed to create an object"
);\n' |
| 298 ' goto fail;\n' | 298 ' goto fail;\n' |
| 299 ' }\n') | 299 ' }\n') |
| 300 arguments.append('context') | 300 arguments.append('context') |
| 301 else: | 301 else: |
| 302 raise Exception('Unsupported CallWith=%s attribute' % call_with) | 302 raise Exception('Unsupported CallWith=%s attribute' % call_with) |
| 303 | 303 |
| 304 # Process constructor arguments. | 304 # Process constructor arguments. |
| 305 for (i, argument) in enumerate(constructor_info.idl_args): | 305 for (i, argument) in enumerate(constructor_info.idl_args): |
| 306 argument_expression = self._GenerateParameterAdapter( | 306 argument_expression = self._GenerateParameterAdapter( |
| 307 parameter_definitions_emitter, argument, i - 1) | 307 parameter_definitions_emitter, argument, i) |
| 308 arguments.append(argument_expression) | 308 arguments.append(argument_expression) |
| 309 | 309 |
| 310 function_expression = '%s::%s' % (self._interface_type_info.native_type(), c
reate_function) | 310 function_expression = '%s::%s' % (self._interface_type_info.native_type(), c
reate_function) |
| 311 invocation = self._GenerateWebCoreInvocation(function_expression, arguments, | 311 invocation = self._GenerateWebCoreInvocation(function_expression, arguments, |
| 312 self._interface.id, self._interface.ext_attrs, raises_dom_exceptions) | 312 self._interface.id, self._interface.ext_attrs, raises_dom_exceptions) |
| 313 self._GenerateNativeCallback(callback_name='constructorCallback', | 313 self._GenerateNativeCallback(callback_name='constructorCallback', |
| 314 parameter_definitions=parameter_definitions_emitter.Fragments(), | 314 parameter_definitions=parameter_definitions_emitter.Fragments(), |
| 315 needs_receiver=False, invocation=invocation, | 315 needs_receiver=False, invocation=invocation, |
| 316 raises_exceptions=raises_exceptions) | 316 raises_exceptions=raises_exceptions) |
| 317 | 317 |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr)) | 531 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr)) |
| 532 else: | 532 else: |
| 533 webcore_function_name = re.sub(r'^(xml(?=[A-Z])|\w)', | 533 webcore_function_name = re.sub(r'^(xml(?=[A-Z])|\w)', |
| 534 lambda s: s.group(1).upper(), | 534 lambda s: s.group(1).upper(), |
| 535 attr.id) | 535 attr.id) |
| 536 webcore_function_name = 'set%s' % webcore_function_name | 536 webcore_function_name = 'set%s' % webcore_function_name |
| 537 if attr.type.id.startswith('SVGAnimated'): | 537 if attr.type.id.startswith('SVGAnimated'): |
| 538 webcore_function_name += 'Animated' | 538 webcore_function_name += 'Animated' |
| 539 | 539 |
| 540 argument_expression = self._GenerateParameterAdapter( | 540 argument_expression = self._GenerateParameterAdapter( |
| 541 parameter_definitions_emitter, attr, 0, adapter_name='value') | 541 parameter_definitions_emitter, attr, 1, adapter_name='value') |
| 542 arguments.append(argument_expression) | 542 arguments.append(argument_expression) |
| 543 | 543 |
| 544 parameter_definitions = parameter_definitions_emitter.Fragments() | 544 parameter_definitions = parameter_definitions_emitter.Fragments() |
| 545 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi
on_name, attr) | 545 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi
on_name, attr) |
| 546 invocation = self._GenerateWebCoreInvocation(function_expression, | 546 invocation = self._GenerateWebCoreInvocation(function_expression, |
| 547 arguments, 'void', attr.ext_attrs, attr.set_raises) | 547 arguments, 'void', attr.ext_attrs, attr.set_raises) |
| 548 | 548 |
| 549 self._GenerateNativeCallback(cpp_callback_name, parameter_definitions_emitte
r.Fragments(), | 549 self._GenerateNativeCallback(cpp_callback_name, parameter_definitions_emitte
r.Fragments(), |
| 550 True, invocation, raises_exceptions=True) | 550 True, invocation, raises_exceptions=True) |
| 551 | 551 |
| 552 def _HasNativeIndexGetter(self, interface): | 552 def _HasNativeIndexGetter(self, interface): |
| 553 return ('CustomIndexedGetter' in interface.ext_attrs or | 553 return ('CustomIndexedGetter' in interface.ext_attrs or |
| 554 'NumericIndexedGetter' in interface.ext_attrs) | 554 'NumericIndexedGetter' in interface.ext_attrs) |
| 555 | 555 |
| 556 def _EmitNativeIndexGetter(self, interface, element_type): | 556 def _EmitNativeIndexGetter(self, interface, element_type): |
| 557 dart_declaration = '%s operator[](int index)' % element_type | 557 dart_declaration = '%s operator[](int index)' % element_type |
| 558 self._GenerateNativeBinding('numericIndexGetter', 2, dart_declaration, | 558 self._GenerateNativeBinding('numericIndexGetter', 2, dart_declaration, |
| 559 'Callback', True) | 559 'Callback', True) |
| 560 | 560 |
| 561 def _EmitNativeIndexSetter(self, interface, element_type): | 561 def _EmitNativeIndexSetter(self, interface, element_type): |
| 562 dart_declaration = 'void operator[]=(int index, %s value)' % element_type | 562 dart_declaration = 'void operator[]=(int index, %s value)' % element_type |
| 563 self._GenerateNativeBinding('numericIndexSetter', 3, dart_declaration, | 563 self._GenerateNativeBinding('numericIndexSetter', 3, dart_declaration, |
| 564 'Callback', True) | 564 'Callback', True) |
| 565 | 565 |
| 566 def AddOperation(self, info): | 566 def _AddOperation(self, info): |
| 567 """ | 567 """ |
| 568 Arguments: | 568 Arguments: |
| 569 info: An OperationInfo object. | 569 info: An OperationInfo object. |
| 570 """ | 570 """ |
| 571 | 571 |
| 572 if 'CheckSecurityForNode' in info.overloads[0].ext_attrs: | 572 if 'CheckSecurityForNode' in info.overloads[0].ext_attrs: |
| 573 # FIXME: exclude from interface as well. | 573 # FIXME: exclude from interface as well. |
| 574 return | 574 return |
| 575 | 575 |
| 576 if 'Custom' in info.overloads[0].ext_attrs: | 576 if 'Custom' in info.overloads[0].ext_attrs: |
| 577 parameters = info.ParametersImplementationDeclaration() | 577 parameters = info.ParametersImplementationDeclaration() |
| 578 dart_declaration = '%s %s(%s)' % (info.type_name, info.name, parameters) | 578 dart_declaration = '%s %s(%s)' % (info.type_name, info.name, parameters) |
| 579 argument_count = 1 + len(info.param_infos) | 579 argument_count = 1 + len(info.param_infos) |
| 580 self._GenerateNativeBinding(info.name, argument_count, dart_declaration, | 580 self._GenerateNativeBinding(info.name, argument_count, dart_declaration, |
| 581 'Callback', True) | 581 'Callback', True) |
| 582 return | 582 return |
| 583 | 583 |
| 584 modifier = '' |
| 585 if info.IsStatic(): |
| 586 modifier = 'static ' |
| 584 body = self._members_emitter.Emit( | 587 body = self._members_emitter.Emit( |
| 585 '\n' | 588 '\n' |
| 586 ' $TYPE $NAME($PARAMETERS) {\n' | 589 ' $MODIFIER$TYPE $NAME($PARAMETERS) {\n' |
| 587 '$!BODY' | 590 '$!BODY' |
| 588 ' }\n', | 591 ' }\n', |
| 592 MODIFIER=modifier, |
| 589 TYPE=info.type_name, | 593 TYPE=info.type_name, |
| 590 NAME=info.name, | 594 NAME=info.name, |
| 591 PARAMETERS=info.ParametersImplementationDeclaration()) | 595 PARAMETERS=info.ParametersImplementationDeclaration()) |
| 592 | 596 |
| 593 # Process in order of ascending number of arguments to ensure missing | 597 # Process in order of ascending number of arguments to ensure missing |
| 594 # optional arguments are processed early. | 598 # optional arguments are processed early. |
| 595 overloads = sorted(info.overloads, | 599 overloads = sorted(info.overloads, |
| 596 key=lambda overload: len(overload.arguments)) | 600 key=lambda overload: len(overload.arguments)) |
| 597 self._native_version = 0 | 601 self._native_version = 0 |
| 598 fallthrough = self.GenerateDispatch(body, info, ' ', 0, overloads) | 602 fallthrough = self.GenerateDispatch(body, info, ' ', 0, overloads) |
| 599 if fallthrough: | 603 if fallthrough: |
| 600 body.Emit(' throw "Incorrect number or type of arguments";\n'); | 604 body.Emit(' throw "Incorrect number or type of arguments";\n'); |
| 601 | 605 |
| 606 def AddOperation(self, info): |
| 607 self._AddOperation(info) |
| 608 |
| 609 def AddStaticOperation(self, info): |
| 610 self._AddOperation(info) |
| 611 |
| 602 def GenerateSingleOperation(self, dispatch_emitter, info, indent, operation): | 612 def GenerateSingleOperation(self, dispatch_emitter, info, indent, operation): |
| 603 """Generates a call to a single operation. | 613 """Generates a call to a single operation. |
| 604 | 614 |
| 605 Arguments: | 615 Arguments: |
| 606 dispatch_emitter: an dispatch_emitter for the body of a block of code. | 616 dispatch_emitter: an dispatch_emitter for the body of a block of code. |
| 607 info: the compound information about the operation and its overloads. | 617 info: the compound information about the operation and its overloads. |
| 608 indent: an indentation string for generated code. | 618 indent: an indentation string for generated code. |
| 609 operation: the IDLOperation to call. | 619 operation: the IDLOperation to call. |
| 610 """ | 620 """ |
| 611 | 621 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 622 INDENT=indent, | 632 INDENT=indent, |
| 623 NATIVENAME=native_name, | 633 NATIVENAME=native_name, |
| 624 ARGS=argument_list) | 634 ARGS=argument_list) |
| 625 else: | 635 else: |
| 626 dispatch_emitter.Emit('$(INDENT)_$NATIVENAME($ARGS);\n' | 636 dispatch_emitter.Emit('$(INDENT)_$NATIVENAME($ARGS);\n' |
| 627 '$(INDENT)return;\n', | 637 '$(INDENT)return;\n', |
| 628 INDENT=indent, | 638 INDENT=indent, |
| 629 NATIVENAME=native_name, | 639 NATIVENAME=native_name, |
| 630 ARGS=argument_list) | 640 ARGS=argument_list) |
| 631 # Generate binding. | 641 # Generate binding. |
| 632 dart_declaration = '%s _%s(%s)' % (info.type_name, native_name, | 642 modifier = '' |
| 643 if operation.is_static: |
| 644 modifier = 'static ' |
| 645 dart_declaration = '%s%s _%s(%s)' % (modifier, info.type_name, native_name, |
| 633 argument_list) | 646 argument_list) |
| 634 is_custom = 'Custom' in operation.ext_attrs | 647 is_custom = 'Custom' in operation.ext_attrs |
| 635 cpp_callback_name = self._GenerateNativeBinding( | 648 cpp_callback_name = self._GenerateNativeBinding( |
| 636 native_name, 1 + len(operation.arguments), dart_declaration, 'Callback', | 649 native_name, 1 + len(operation.arguments), dart_declaration, 'Callback', |
| 637 is_custom) | 650 is_custom) |
| 638 if is_custom: | 651 if is_custom: |
| 639 return | 652 return |
| 640 | 653 |
| 641 # Generate callback. | 654 # Generate callback. |
| 642 webcore_function_name = operation.ext_attrs.get('ImplementedAs', operation.i
d) | 655 webcore_function_name = operation.ext_attrs.get('ImplementedAs', operation.i
d) |
| 643 | 656 |
| 644 parameter_definitions_emitter = emitter.Emitter() | 657 parameter_definitions_emitter = emitter.Emitter() |
| 645 arguments = [] | 658 arguments = [] |
| 646 raises_exceptions = self._GenerateCallWithHandling( | 659 raises_exceptions = self._GenerateCallWithHandling( |
| 647 operation, parameter_definitions_emitter, arguments) | 660 operation, parameter_definitions_emitter, arguments) |
| 648 raises_exceptions = raises_exceptions or len(operation.arguments) > 0 or ope
ration.raises | 661 raises_exceptions = raises_exceptions or len(operation.arguments) > 0 or ope
ration.raises |
| 649 | 662 |
| 650 # Process Dart arguments. | 663 # Process Dart arguments. |
| 664 start_index = 1 |
| 665 if operation.is_static: |
| 666 start_index = 0 |
| 651 for (i, argument) in enumerate(operation.arguments): | 667 for (i, argument) in enumerate(operation.arguments): |
| 652 if (i == len(operation.arguments) - 1 and | 668 if (i == len(operation.arguments) - 1 and |
| 653 self._interface.id == 'Console' and | 669 self._interface.id == 'Console' and |
| 654 argument.id == 'arg'): | 670 argument.id == 'arg'): |
| 655 # FIXME: we are skipping last argument here because it was added in | 671 # FIXME: we are skipping last argument here because it was added in |
| 656 # supplemental dart.idl. Cleanup dart.idl and remove this check. | 672 # supplemental dart.idl. Cleanup dart.idl and remove this check. |
| 657 break | 673 break |
| 658 argument_expression = self._GenerateParameterAdapter( | 674 argument_expression = self._GenerateParameterAdapter( |
| 659 parameter_definitions_emitter, argument, i) | 675 parameter_definitions_emitter, argument, start_index + i) |
| 660 arguments.append(argument_expression) | 676 arguments.append(argument_expression) |
| 661 | 677 |
| 662 if operation.id in ['addEventListener', 'removeEventListener']: | 678 if operation.id in ['addEventListener', 'removeEventListener']: |
| 663 # addEventListener's and removeEventListener's last argument is marked | 679 # addEventListener's and removeEventListener's last argument is marked |
| 664 # as optional in idl, but is not optional in webcore implementation. | 680 # as optional in idl, but is not optional in webcore implementation. |
| 665 if len(operation.arguments) == 2: | 681 if len(operation.arguments) == 2: |
| 666 arguments.append('false') | 682 arguments.append('false') |
| 667 | 683 |
| 668 if self._interface.id == 'CSSStyleDeclaration' and operation.id == 'setPrope
rty': | 684 if self._interface.id == 'CSSStyleDeclaration' and operation.id == 'setPrope
rty': |
| 669 # CSSStyleDeclaration.setProperty priority parameter is optional in Dart | 685 # CSSStyleDeclaration.setProperty priority parameter is optional in Dart |
| 670 # idl, but is not optional in webcore implementation. | 686 # idl, but is not optional in webcore implementation. |
| 671 if len(operation.arguments) == 2: | 687 if len(operation.arguments) == 2: |
| 672 arguments.append('String()') | 688 arguments.append('String()') |
| 673 | 689 |
| 674 if 'NeedsUserGestureCheck' in operation.ext_attrs: | 690 if 'NeedsUserGestureCheck' in operation.ext_attrs: |
| 675 arguments.append('DartUtilities::processingUserGesture') | 691 arguments.append('DartUtilities::processingUserGesture') |
| 676 | 692 |
| 677 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi
on_name, operation) | 693 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi
on_name, operation) |
| 678 invocation = self._GenerateWebCoreInvocation(function_expression, arguments, | 694 invocation = self._GenerateWebCoreInvocation(function_expression, arguments, |
| 679 operation.type.id, operation.ext_attrs, operation.raises) | 695 operation.type.id, operation.ext_attrs, operation.raises) |
| 680 self._GenerateNativeCallback(cpp_callback_name, | 696 self._GenerateNativeCallback(cpp_callback_name, |
| 681 parameter_definitions=parameter_definitions_emitter.Fragments(), | 697 parameter_definitions=parameter_definitions_emitter.Fragments(), |
| 682 needs_receiver=True, invocation=invocation, | 698 needs_receiver=not operation.is_static, invocation=invocation, |
| 683 raises_exceptions=raises_exceptions) | 699 raises_exceptions=raises_exceptions) |
| 684 | 700 |
| 685 def _GenerateNativeCallback(self, callback_name, parameter_definitions, | 701 def _GenerateNativeCallback(self, callback_name, parameter_definitions, |
| 686 needs_receiver, invocation, raises_exceptions): | 702 needs_receiver, invocation, raises_exceptions): |
| 687 | 703 |
| 688 if needs_receiver: | 704 if needs_receiver: |
| 689 parameter_definitions = emitter.Format( | 705 parameter_definitions = emitter.Format( |
| 690 ' $WEBCORE_CLASS_NAME* receiver = DartDOMWrapper::receiver< $WE
BCORE_CLASS_NAME >(args);\n' | 706 ' $WEBCORE_CLASS_NAME* receiver = DartDOMWrapper::receiver< $WE
BCORE_CLASS_NAME >(args);\n' |
| 691 ' $PARAMETER_DEFINITIONS\n', | 707 ' $PARAMETER_DEFINITIONS\n', |
| 692 WEBCORE_CLASS_NAME=self._interface_type_info.native_type(), | 708 WEBCORE_CLASS_NAME=self._interface_type_info.native_type(), |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 735 flags = ', DartUtilities::ConvertNullToDefaultValue' | 751 flags = ', DartUtilities::ConvertNullToDefaultValue' |
| 736 emitter.Emit( | 752 emitter.Emit( |
| 737 '\n' | 753 '\n' |
| 738 ' const $ADAPTER_TYPE $NAME(Dart_GetNativeArgument(args, $INDEX)$
FLAGS);\n' | 754 ' const $ADAPTER_TYPE $NAME(Dart_GetNativeArgument(args, $INDEX)$
FLAGS);\n' |
| 739 ' if (!$NAME.conversionSuccessful()) {\n' | 755 ' if (!$NAME.conversionSuccessful()) {\n' |
| 740 ' exception = $NAME.exception();\n' | 756 ' exception = $NAME.exception();\n' |
| 741 ' goto fail;\n' | 757 ' goto fail;\n' |
| 742 ' }\n', | 758 ' }\n', |
| 743 ADAPTER_TYPE=adapter_type, | 759 ADAPTER_TYPE=adapter_type, |
| 744 NAME=adapter_name, | 760 NAME=adapter_name, |
| 745 INDEX=index + 1, | 761 INDEX=index, |
| 746 FLAGS=flags) | 762 FLAGS=flags) |
| 747 | 763 |
| 748 conversion = '%s' | 764 conversion = '%s' |
| 749 if isinstance(type_info, SVGTearOffIDLTypeInfo) and not self._interface.id.e
ndswith('List'): | 765 if isinstance(type_info, SVGTearOffIDLTypeInfo) and not self._interface.id.e
ndswith('List'): |
| 750 conversion = '%s.get()->propertyReference()' | 766 conversion = '%s.get()->propertyReference()' |
| 751 elif type_info.idl_type() == 'SVGMatrix' and self._interface.id == 'SVGTrans
formList': | 767 elif type_info.idl_type() == 'SVGMatrix' and self._interface.id == 'SVGTrans
formList': |
| 752 conversion = '%s.get()' | 768 conversion = '%s.get()' |
| 753 return conversion % adapter_name | 769 return conversion % adapter_name |
| 754 | 770 |
| 755 def _GenerateNativeBinding(self, idl_name, argument_count, dart_declaration, | 771 def _GenerateNativeBinding(self, idl_name, argument_count, dart_declaration, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 785 if self._interface.id.startswith('SVG') and not attr.id in svg_exceptions: | 801 if self._interface.id.startswith('SVG') and not attr.id in svg_exceptions: |
| 786 namespace = 'SVGNames' | 802 namespace = 'SVGNames' |
| 787 self._cpp_impl_includes.add('"%s.h"' % namespace) | 803 self._cpp_impl_includes.add('"%s.h"' % namespace) |
| 788 | 804 |
| 789 attribute_name = attr.ext_attrs['Reflect'] or attr.id.lower() | 805 attribute_name = attr.ext_attrs['Reflect'] or attr.id.lower() |
| 790 return 'WebCore::%s::%sAttr' % (namespace, attribute_name) | 806 return 'WebCore::%s::%sAttr' % (namespace, attribute_name) |
| 791 | 807 |
| 792 def _GenerateWebCoreFunctionExpression(self, function_name, idl_node): | 808 def _GenerateWebCoreFunctionExpression(self, function_name, idl_node): |
| 793 if 'ImplementedBy' in idl_node.ext_attrs: | 809 if 'ImplementedBy' in idl_node.ext_attrs: |
| 794 return '%s::%s' % (idl_node.ext_attrs['ImplementedBy'], function_name) | 810 return '%s::%s' % (idl_node.ext_attrs['ImplementedBy'], function_name) |
| 811 if idl_node.is_static: |
| 812 return '%s::%s' % (self._interface_type_info.idl_type(), function_name) |
| 795 return '%s%s' % (self._interface_type_info.receiver(), function_name) | 813 return '%s%s' % (self._interface_type_info.receiver(), function_name) |
| 796 | 814 |
| 797 def _GenerateWebCoreInvocation(self, function_expression, arguments, | 815 def _GenerateWebCoreInvocation(self, function_expression, arguments, |
| 798 idl_return_type, attributes, raises_dom_exceptions): | 816 idl_return_type, attributes, raises_dom_exceptions): |
| 799 invocation_template = ' $FUNCTION_CALL;\n' | 817 invocation_template = ' $FUNCTION_CALL;\n' |
| 800 if idl_return_type != 'void': | 818 if idl_return_type != 'void': |
| 801 return_type_info = GetIDLTypeInfo(idl_return_type) | 819 return_type_info = GetIDLTypeInfo(idl_return_type) |
| 802 self._cpp_impl_includes |= set(return_type_info.conversion_includes()) | 820 self._cpp_impl_includes |= set(return_type_info.conversion_includes()) |
| 803 | 821 |
| 804 # Generate to Dart conversion of C++ value. | 822 # Generate to Dart conversion of C++ value. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 845 def _InstanceOfNode(database, interface): | 863 def _InstanceOfNode(database, interface): |
| 846 if interface.id == 'Node': | 864 if interface.id == 'Node': |
| 847 return True | 865 return True |
| 848 for parent in interface.parents: | 866 for parent in interface.parents: |
| 849 if not database.HasInterface(parent.type.id): | 867 if not database.HasInterface(parent.type.id): |
| 850 continue | 868 continue |
| 851 parent_interface = database.GetInterface(parent.type.id) | 869 parent_interface = database.GetInterface(parent.type.id) |
| 852 if _InstanceOfNode(database, parent_interface): | 870 if _InstanceOfNode(database, parent_interface): |
| 853 return True | 871 return True |
| 854 return False | 872 return False |
| OLD | NEW |