| 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 709 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 if 'synthesizedV8EnabledAtRuntime' in ext_attrs: | 720 if 'synthesizedV8EnabledAtRuntime' in ext_attrs: |
| 721 raises_exceptions = True | 721 raises_exceptions = True |
| 722 self._cpp_impl_includes.add('"RuntimeEnabledFeatures.h"') | 722 self._cpp_impl_includes.add('"RuntimeEnabledFeatures.h"') |
| 723 runtime_check = emitter.Format( | 723 runtime_check = emitter.Format( |
| 724 ' if (!RuntimeEnabledFeatures::$(FEATURE)Enabled()) {\n' | 724 ' if (!RuntimeEnabledFeatures::$(FEATURE)Enabled()) {\n' |
| 725 ' exception = Dart_NewString("Feature $FEATURE is not enabl
ed");\n' | 725 ' exception = Dart_NewString("Feature $FEATURE is not enabl
ed");\n' |
| 726 ' goto fail;\n' | 726 ' goto fail;\n' |
| 727 ' }', | 727 ' }', |
| 728 FEATURE=_ToWebKitName(ext_attrs['synthesizedV8EnabledAtRuntime'])) | 728 FEATURE=_ToWebKitName(ext_attrs['synthesizedV8EnabledAtRuntime'])) |
| 729 | 729 |
| 730 head_emitter = emitter.Emitter() | 730 body_emitter = self._cpp_definitions_emitter.Emit( |
| 731 '\n' |
| 732 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n' |
| 733 '{\n' |
| 734 ' DartApiScope dartApiScope;\n' |
| 735 '$!BODY' |
| 736 '}\n', |
| 737 CALLBACK_NAME=callback_name) |
| 738 |
| 739 if raises_exceptions: |
| 740 body_emitter = body_emitter.Emit( |
| 741 ' Dart_Handle exception = 0;\n' |
| 742 '$!BODY' |
| 743 '\n' |
| 744 'fail:\n' |
| 745 ' Dart_ThrowException(exception);\n' |
| 746 ' ASSERT_NOT_REACHED();\n') |
| 747 |
| 748 body_emitter = body_emitter.Emit( |
| 749 ' {\n' |
| 750 '$!BODY' |
| 751 ' return;\n' |
| 752 ' }\n') |
| 731 | 753 |
| 732 if requires_v8_scope: | 754 if requires_v8_scope: |
| 733 head_emitter.Emit( | 755 body_emitter.Emit( |
| 734 ' V8Scope v8scope;\n\n') | 756 ' V8Scope v8scope;\n\n') |
| 735 | 757 |
| 736 if runtime_check: | 758 if runtime_check: |
| 737 head_emitter.Emit( | 759 body_emitter.Emit( |
| 738 '$RUNTIME_CHECK\n', | 760 '$RUNTIME_CHECK\n', |
| 739 RUNTIME_CHECK=runtime_check) | 761 RUNTIME_CHECK=runtime_check) |
| 740 | 762 |
| 741 if requires_script_execution_context: | 763 if requires_script_execution_context: |
| 742 head_emitter.Emit( | 764 body_emitter.Emit( |
| 743 ' ScriptExecutionContext* context = DartUtilities::scriptExecut
ionContext();\n' | 765 ' ScriptExecutionContext* context = DartUtilities::scriptExecut
ionContext();\n' |
| 744 ' if (!context) {\n' | 766 ' if (!context) {\n' |
| 745 ' exception = Dart_NewString("Failed to retrieve a context"
);\n' | 767 ' exception = Dart_NewString("Failed to retrieve a context"
);\n' |
| 746 ' goto fail;\n' | 768 ' goto fail;\n' |
| 747 ' }\n\n') | 769 ' }\n\n') |
| 748 | 770 |
| 749 if requires_dom_window: | 771 if requires_dom_window: |
| 750 head_emitter.Emit( | 772 body_emitter.Emit( |
| 751 ' DOMWindow* domWindow = DartUtilities::domWindowForCurrentIsol
ate();\n' | 773 ' DOMWindow* domWindow = DartUtilities::domWindowForCurrentIsol
ate();\n' |
| 752 ' if (!domWindow) {\n' | 774 ' if (!domWindow) {\n' |
| 753 ' exception = Dart_NewString("Failed to fetch domWindow");\
n' | 775 ' exception = Dart_NewString("Failed to fetch domWindow");\
n' |
| 754 ' goto fail;\n' | 776 ' goto fail;\n' |
| 755 ' }\n' | 777 ' }\n' |
| 756 ' Document* document = domWindow->document();\n') | 778 ' Document* document = domWindow->document();\n') |
| 757 | 779 |
| 758 if needs_receiver: | 780 if needs_receiver: |
| 759 head_emitter.Emit( | 781 body_emitter.Emit( |
| 760 ' $WEBCORE_CLASS_NAME* receiver = DartDOMWrapper::receiver< $WE
BCORE_CLASS_NAME >(args);\n', | 782 ' $WEBCORE_CLASS_NAME* receiver = DartDOMWrapper::receiver< $WE
BCORE_CLASS_NAME >(args);\n', |
| 761 WEBCORE_CLASS_NAME=self._interface_type_info.native_type()) | 783 WEBCORE_CLASS_NAME=self._interface_type_info.native_type()) |
| 762 | 784 |
| 763 if requires_stack_info: | 785 if requires_stack_info: |
| 764 head_emitter.Emit( | 786 body_emitter.Emit( |
| 765 '\n' | 787 '\n' |
| 766 ' Dart_Handle customArgument = Dart_GetNativeArgument(args, $IN
DEX);\n' | 788 ' Dart_Handle customArgument = Dart_GetNativeArgument(args, $IN
DEX);\n' |
| 767 ' RefPtr<ScriptArguments> scriptArguments(DartUtilities::create
ScriptArguments(customArgument, exception));\n' | 789 ' RefPtr<ScriptArguments> scriptArguments(DartUtilities::create
ScriptArguments(customArgument, exception));\n' |
| 768 ' if (!scriptArguments)\n' | 790 ' if (!scriptArguments)\n' |
| 769 ' goto fail;\n' | 791 ' goto fail;\n' |
| 770 ' RefPtr<ScriptCallStack> scriptCallStack(DartUtilities::create
ScriptCallStack());\n' | 792 ' RefPtr<ScriptCallStack> scriptCallStack(DartUtilities::create
ScriptCallStack());\n' |
| 771 ' if (!scriptCallStack->size())\n' | 793 ' if (!scriptCallStack->size())\n' |
| 772 ' return;\n', | 794 ' return;\n', |
| 773 INDEX=len(arguments)) | 795 INDEX=len(arguments)) |
| 774 | 796 |
| 775 # Process Dart cpp_arguments. | 797 # Process Dart cpp_arguments. |
| 776 start_index = 0 | 798 start_index = 0 |
| 777 if needs_receiver: | 799 if needs_receiver: |
| 778 start_index = 1 | 800 start_index = 1 |
| 779 for (i, argument) in enumerate(arguments): | 801 for (i, argument) in enumerate(arguments): |
| 780 if (i == len(arguments) - 1 and | 802 if (i == len(arguments) - 1 and |
| 781 self._interface.id == 'Console' and | 803 self._interface.id == 'Console' and |
| 782 argument.id == 'arg'): | 804 argument.id == 'arg'): |
| 783 # FIXME: we are skipping last argument here because it was added in | 805 # FIXME: we are skipping last argument here because it was added in |
| 784 # supplemental dart.idl. Cleanup dart.idl and remove this check. | 806 # supplemental dart.idl. Cleanup dart.idl and remove this check. |
| 785 break | 807 break |
| 786 argument_expression = self._GenerateToNative(head_emitter, argument, start
_index + i) | 808 argument_expression = self._GenerateToNative(body_emitter, argument, start
_index + i) |
| 787 cpp_arguments.append(argument_expression) | 809 cpp_arguments.append(argument_expression) |
| 788 | 810 |
| 789 head_emitter.Emit('\n') | 811 body_emitter.Emit('\n') |
| 790 | 812 |
| 791 # FIXME: rework in IDLs. | 813 # FIXME: rework in IDLs. |
| 792 if node.id in ['addEventListener', 'removeEventListener']: | 814 if node.id in ['addEventListener', 'removeEventListener']: |
| 793 # addEventListener's and removeEventListener's last argument is marked | 815 # addEventListener's and removeEventListener's last argument is marked |
| 794 # as optional in idl, but is not optional in webcore implementation. | 816 # as optional in idl, but is not optional in webcore implementation. |
| 795 if len(arguments) == 2: | 817 if len(arguments) == 2: |
| 796 cpp_arguments.append('false') | 818 cpp_arguments.append('false') |
| 797 | 819 |
| 798 if self._interface.id == 'CSSStyleDeclaration' and node.id == 'setProperty': | 820 if self._interface.id == 'CSSStyleDeclaration' and node.id == 'setProperty': |
| 799 # CSSStyleDeclaration.setProperty priority parameter is optional in Dart | 821 # CSSStyleDeclaration.setProperty priority parameter is optional in Dart |
| 800 # idl, but is not optional in webcore implementation. | 822 # idl, but is not optional in webcore implementation. |
| 801 if len(arguments) == 2: | 823 if len(arguments) == 2: |
| 802 cpp_arguments.append('String()') | 824 cpp_arguments.append('String()') |
| 803 | 825 |
| 804 if 'NeedsUserGestureCheck' in ext_attrs: | 826 if 'NeedsUserGestureCheck' in ext_attrs: |
| 805 cpp_arguments.append('DartUtilities::processingUserGesture'); | 827 cpp_arguments.append('DartUtilities::processingUserGesture'); |
| 806 | 828 |
| 807 invocation_emitter = head_emitter | 829 invocation_emitter = body_emitter |
| 808 if raises_dom_exception: | 830 if raises_dom_exception: |
| 809 cpp_arguments.append('ec') | 831 cpp_arguments.append('ec') |
| 810 invocation_emitter = head_emitter.Emit( | 832 invocation_emitter = body_emitter.Emit( |
| 811 ' ExceptionCode ec = 0;\n' | 833 ' ExceptionCode ec = 0;\n' |
| 812 '$!INVOCATION' | 834 '$!INVOCATION' |
| 813 ' if (UNLIKELY(ec)) {\n' | 835 ' if (UNLIKELY(ec)) {\n' |
| 814 ' exception = DartDOMWrapper::exceptionCodeToDartException(ec
);\n' | 836 ' exception = DartDOMWrapper::exceptionCodeToDartException(ec
);\n' |
| 815 ' goto fail;\n' | 837 ' goto fail;\n' |
| 816 ' }\n') | 838 ' }\n') |
| 817 | 839 |
| 818 function_call = '%s(%s)' % (function_expression, ', '.join(cpp_arguments)) | 840 function_call = '%s(%s)' % (function_expression, ', '.join(cpp_arguments)) |
| 819 if return_type == 'void': | 841 if return_type == 'void': |
| 820 invocation_emitter.Emit( | 842 invocation_emitter.Emit( |
| 821 ' $FUNCTION_CALL;\n', | 843 ' $FUNCTION_CALL;\n', |
| 822 FUNCTION_CALL=function_call) | 844 FUNCTION_CALL=function_call) |
| 823 else: | 845 else: |
| 824 return_type_info = self._TypeInfo(return_type) | 846 return_type_info = self._TypeInfo(return_type) |
| 825 self._cpp_impl_includes |= set(return_type_info.conversion_includes()) | 847 self._cpp_impl_includes |= set(return_type_info.conversion_includes()) |
| 826 | 848 |
| 827 # Generate to Dart conversion of C++ value. | 849 # Generate to Dart conversion of C++ value. |
| 828 to_dart_conversion = return_type_info.to_dart_conversion(function_call, se
lf._interface.id, ext_attrs) | 850 to_dart_conversion = return_type_info.to_dart_conversion(function_call, se
lf._interface.id, ext_attrs) |
| 829 invocation_emitter.Emit( | 851 invocation_emitter.Emit( |
| 830 ' Dart_Handle returnValue = $TO_DART_CONVERSION;\n' | 852 ' Dart_Handle returnValue = $TO_DART_CONVERSION;\n' |
| 831 ' if (returnValue)\n' | 853 ' if (returnValue)\n' |
| 832 ' Dart_SetReturnValue(args, returnValue);\n', | 854 ' Dart_SetReturnValue(args, returnValue);\n', |
| 833 TO_DART_CONVERSION=to_dart_conversion) | 855 TO_DART_CONVERSION=to_dart_conversion) |
| 834 | 856 |
| 835 body = emitter.Format( | |
| 836 ' {\n' | |
| 837 '$HEAD' | |
| 838 ' return;\n' | |
| 839 ' }\n', | |
| 840 HEAD=head_emitter.Fragments()) | |
| 841 | |
| 842 if raises_exceptions: | |
| 843 body = emitter.Format( | |
| 844 ' Dart_Handle exception = 0;\n' | |
| 845 '$BODY' | |
| 846 '\n' | |
| 847 'fail:\n' | |
| 848 ' Dart_ThrowException(exception);\n' | |
| 849 ' ASSERT_NOT_REACHED();\n', | |
| 850 BODY=body) | |
| 851 | |
| 852 self._cpp_definitions_emitter.Emit( | |
| 853 '\n' | |
| 854 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n' | |
| 855 '{\n' | |
| 856 ' DartApiScope dartApiScope;\n' | |
| 857 '$BODY' | |
| 858 '}\n', | |
| 859 CALLBACK_NAME=callback_name, | |
| 860 BODY=body) | |
| 861 | |
| 862 def _GenerateToNative(self, emitter, idl_node, index): | 857 def _GenerateToNative(self, emitter, idl_node, index): |
| 863 """idl_node is IDLArgument or IDLAttribute.""" | 858 """idl_node is IDLArgument or IDLAttribute.""" |
| 864 type_info = self._TypeInfo(idl_node.type.id) | 859 type_info = self._TypeInfo(idl_node.type.id) |
| 865 self._cpp_impl_includes |= set(type_info.to_native_includes()) | 860 self._cpp_impl_includes |= set(type_info.to_native_includes()) |
| 866 argument_name = idl_node.id | 861 argument_name = idl_node.id |
| 867 # Rename to get rid of conflicts with C++ keywords. | 862 # Rename to get rid of conflicts with C++ keywords. |
| 868 if argument_name == 'default': | 863 if argument_name == 'default': |
| 869 argument_name = 'value' | 864 argument_name = 'value' |
| 870 handle = 'Dart_GetNativeArgument(args, %i)' % index | 865 handle = 'Dart_GetNativeArgument(args, %i)' % index |
| 871 argument_expression = type_info.emit_to_native( | 866 argument_expression = type_info.emit_to_native( |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 937 | 932 |
| 938 def _IsArgumentOptionalInWebCore(argument): | 933 def _IsArgumentOptionalInWebCore(argument): |
| 939 return IsOptional(argument) and not 'Callback' in argument.ext_attrs | 934 return IsOptional(argument) and not 'Callback' in argument.ext_attrs |
| 940 | 935 |
| 941 def _ToWebKitName(name): | 936 def _ToWebKitName(name): |
| 942 name = name[0].lower() + name[1:] | 937 name = name[0].lower() + name[1:] |
| 943 name = re.sub(r'^(hTML|uRL|jS|xML|xSLT)', lambda s: s.group(1).lower(), | 938 name = re.sub(r'^(hTML|uRL|jS|xML|xSLT)', lambda s: s.group(1).lower(), |
| 944 name) | 939 name) |
| 945 return re.sub(r'^(create|exclusive)', lambda s: 'is' + s.group(1).capitalize()
, | 940 return re.sub(r'^(create|exclusive)', lambda s: 'is' + s.group(1).capitalize()
, |
| 946 name) | 941 name) |
| OLD | NEW |