| 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 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 ' if (!context) {\n' | 296 ' if (!context) {\n' |
| 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._GenerateToNative( |
| 307 parameter_definitions_emitter, argument, i) | 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) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 CALLBACKS=self._cpp_definitions_emitter.Fragments(), | 380 CALLBACKS=self._cpp_definitions_emitter.Fragments(), |
| 381 RESOLVER=self._cpp_resolver_emitter.Fragments()) | 381 RESOLVER=self._cpp_resolver_emitter.Fragments()) |
| 382 | 382 |
| 383 def _GenerateCppHeader(self): | 383 def _GenerateCppHeader(self): |
| 384 to_native_emitter = emitter.Emitter() | 384 to_native_emitter = emitter.Emitter() |
| 385 if self._interface_type_info.custom_to_native(): | 385 if self._interface_type_info.custom_to_native(): |
| 386 to_native_emitter.Emit( | 386 to_native_emitter.Emit( |
| 387 ' static PassRefPtr<NativeType> toNative(Dart_Handle handle, Dart_H
andle& exception);\n') | 387 ' static PassRefPtr<NativeType> toNative(Dart_Handle handle, Dart_H
andle& exception);\n') |
| 388 else: | 388 else: |
| 389 to_native_emitter.Emit( | 389 to_native_emitter.Emit( |
| 390 ' static PassRefPtr<NativeType> toNative(Dart_Handle handle, Dart_H
andle& exception)\n' | 390 ' static NativeType* toNative(Dart_Handle handle, Dart_Handle& exce
ption)\n' |
| 391 ' {\n' | 391 ' {\n' |
| 392 ' return DartDOMWrapper::unwrapDartWrapper<Dart$INTERFACE>(hand
le, exception);\n' | 392 ' return DartDOMWrapper::unwrapDartWrapper<Dart$INTERFACE>(hand
le, exception);\n' |
| 393 ' }\n', | 393 ' }\n', |
| 394 INTERFACE=self._interface.id) | 394 INTERFACE=self._interface.id) |
| 395 | 395 |
| 396 to_dart_emitter = emitter.Emitter() | 396 to_dart_emitter = emitter.Emitter() |
| 397 if ('CustomToJS' in self._interface.ext_attrs or | 397 if ('CustomToJS' in self._interface.ext_attrs or |
| 398 'CustomToJSObject' in self._interface.ext_attrs or | 398 'CustomToJSObject' in self._interface.ext_attrs or |
| 399 'PureInterface' in self._interface.ext_attrs or | 399 'PureInterface' in self._interface.ext_attrs or |
| 400 'CPPPureInterface' in self._interface.ext_attrs or | 400 'CPPPureInterface' in self._interface.ext_attrs or |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 webcore_function_name = GetIDLTypeInfo(attr.type.id).webcore_setter_name() | 530 webcore_function_name = GetIDLTypeInfo(attr.type.id).webcore_setter_name() |
| 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._GenerateToNative( |
| 541 parameter_definitions_emitter, attr, 1, adapter_name='value') | 541 parameter_definitions_emitter, attr, 1, argument_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 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 start_index = 1 | 664 start_index = 1 |
| 665 if operation.is_static: | 665 if operation.is_static: |
| 666 start_index = 0 | 666 start_index = 0 |
| 667 for (i, argument) in enumerate(operation.arguments): | 667 for (i, argument) in enumerate(operation.arguments): |
| 668 if (i == len(operation.arguments) - 1 and | 668 if (i == len(operation.arguments) - 1 and |
| 669 self._interface.id == 'Console' and | 669 self._interface.id == 'Console' and |
| 670 argument.id == 'arg'): | 670 argument.id == 'arg'): |
| 671 # 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 |
| 672 # supplemental dart.idl. Cleanup dart.idl and remove this check. | 672 # supplemental dart.idl. Cleanup dart.idl and remove this check. |
| 673 break | 673 break |
| 674 argument_expression = self._GenerateParameterAdapter( | 674 argument_expression = self._GenerateToNative( |
| 675 parameter_definitions_emitter, argument, start_index + i) | 675 parameter_definitions_emitter, argument, start_index + i) |
| 676 arguments.append(argument_expression) | 676 arguments.append(argument_expression) |
| 677 | 677 |
| 678 if operation.id in ['addEventListener', 'removeEventListener']: | 678 if operation.id in ['addEventListener', 'removeEventListener']: |
| 679 # addEventListener's and removeEventListener's last argument is marked | 679 # addEventListener's and removeEventListener's last argument is marked |
| 680 # as optional in idl, but is not optional in webcore implementation. | 680 # as optional in idl, but is not optional in webcore implementation. |
| 681 if len(operation.arguments) == 2: | 681 if len(operation.arguments) == 2: |
| 682 arguments.append('false') | 682 arguments.append('false') |
| 683 | 683 |
| 684 if self._interface.id == 'CSSStyleDeclaration' and operation.id == 'setPrope
rty': | 684 if self._interface.id == 'CSSStyleDeclaration' and operation.id == 'setPrope
rty': |
| (...skipping 27 matching lines...) Expand all Loading... |
| 712 ' {\n' | 712 ' {\n' |
| 713 '$PARAMETER_DEFINITIONS' | 713 '$PARAMETER_DEFINITIONS' |
| 714 '$INVOCATION' | 714 '$INVOCATION' |
| 715 ' return;\n' | 715 ' return;\n' |
| 716 ' }\n', | 716 ' }\n', |
| 717 PARAMETER_DEFINITIONS=parameter_definitions, | 717 PARAMETER_DEFINITIONS=parameter_definitions, |
| 718 INVOCATION=invocation) | 718 INVOCATION=invocation) |
| 719 | 719 |
| 720 if raises_exceptions: | 720 if raises_exceptions: |
| 721 body = emitter.Format( | 721 body = emitter.Format( |
| 722 ' Dart_Handle exception;\n' | 722 ' Dart_Handle exception = 0;\n' |
| 723 '$BODY' | 723 '$BODY' |
| 724 '\n' | 724 '\n' |
| 725 'fail:\n' | 725 'fail:\n' |
| 726 ' Dart_ThrowException(exception);\n' | 726 ' Dart_ThrowException(exception);\n' |
| 727 ' ASSERT_NOT_REACHED();\n', | 727 ' ASSERT_NOT_REACHED();\n', |
| 728 BODY=body) | 728 BODY=body) |
| 729 | 729 |
| 730 self._cpp_definitions_emitter.Emit( | 730 self._cpp_definitions_emitter.Emit( |
| 731 '\n' | 731 '\n' |
| 732 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n' | 732 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n' |
| 733 '{\n' | 733 '{\n' |
| 734 ' DartApiScope dartApiScope;\n' | 734 ' DartApiScope dartApiScope;\n' |
| 735 '$BODY' | 735 '$BODY' |
| 736 '}\n', | 736 '}\n', |
| 737 CALLBACK_NAME=callback_name, | 737 CALLBACK_NAME=callback_name, |
| 738 BODY=body) | 738 BODY=body) |
| 739 | 739 |
| 740 def _GenerateParameterAdapter(self, emitter, idl_node, index, | 740 def _GenerateToNative(self, emitter, idl_node, index, |
| 741 adapter_name=None): | 741 argument_name=None): |
| 742 """idl_node is IDLArgument or IDLAttribute.""" | 742 """idl_node is IDLArgument or IDLAttribute.""" |
| 743 type_info = GetIDLTypeInfo(idl_node.type.id) | 743 type_info = GetIDLTypeInfo(idl_node.type.id) |
| 744 (adapter_type, include_name) = type_info.parameter_adapter_info() | 744 if not IsPrimitiveType(idl_node.type.id): |
| 745 if include_name: | 745 self._cpp_impl_includes.add('"Dart%s.h"' % type_info.idl_type()) |
| 746 self._cpp_impl_includes.add(include_name) | 746 argument_name = argument_name or idl_node.id |
| 747 adapter_name = adapter_name or idl_node.id | 747 handle = 'Dart_GetNativeArgument(args, %i)' % index |
| 748 flags = '' | 748 return type_info.emit_to_native(emitter, idl_node, argument_name, handle, se
lf._interface.id) |
| 749 if (idl_node.ext_attrs.get('Optional') == 'DefaultIsNullString' or | |
| 750 'RequiredCppParameter' in idl_node.ext_attrs): | |
| 751 flags = ', DartUtilities::ConvertNullToDefaultValue' | |
| 752 emitter.Emit( | |
| 753 '\n' | |
| 754 ' const $ADAPTER_TYPE $NAME(Dart_GetNativeArgument(args, $INDEX)$
FLAGS);\n' | |
| 755 ' if (!$NAME.conversionSuccessful()) {\n' | |
| 756 ' exception = $NAME.exception();\n' | |
| 757 ' goto fail;\n' | |
| 758 ' }\n', | |
| 759 ADAPTER_TYPE=adapter_type, | |
| 760 NAME=adapter_name, | |
| 761 INDEX=index, | |
| 762 FLAGS=flags) | |
| 763 | |
| 764 conversion = '%s' | |
| 765 if isinstance(type_info, SVGTearOffIDLTypeInfo) and not self._interface.id.e
ndswith('List'): | |
| 766 conversion = '%s.get()->propertyReference()' | |
| 767 elif type_info.idl_type() == 'SVGMatrix' and self._interface.id == 'SVGTrans
formList': | |
| 768 conversion = '%s.get()' | |
| 769 return conversion % adapter_name | |
| 770 | 749 |
| 771 def _GenerateNativeBinding(self, idl_name, argument_count, dart_declaration, | 750 def _GenerateNativeBinding(self, idl_name, argument_count, dart_declaration, |
| 772 native_suffix, is_custom): | 751 native_suffix, is_custom): |
| 773 native_binding = '%s_%s_%s' % (self._interface.id, idl_name, native_suffix) | 752 native_binding = '%s_%s_%s' % (self._interface.id, idl_name, native_suffix) |
| 774 self._members_emitter.Emit( | 753 self._members_emitter.Emit( |
| 775 '\n' | 754 '\n' |
| 776 ' $DART_DECLARATION native "$NATIVE_BINDING";\n', | 755 ' $DART_DECLARATION native "$NATIVE_BINDING";\n', |
| 777 DART_DECLARATION=dart_declaration, NATIVE_BINDING=native_binding) | 756 DART_DECLARATION=dart_declaration, NATIVE_BINDING=native_binding) |
| 778 | 757 |
| 779 cpp_callback_name = '%s%s' % (idl_name, native_suffix) | 758 cpp_callback_name = '%s%s' % (idl_name, native_suffix) |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 def _InstanceOfNode(database, interface): | 842 def _InstanceOfNode(database, interface): |
| 864 if interface.id == 'Node': | 843 if interface.id == 'Node': |
| 865 return True | 844 return True |
| 866 for parent in interface.parents: | 845 for parent in interface.parents: |
| 867 if not database.HasInterface(parent.type.id): | 846 if not database.HasInterface(parent.type.id): |
| 868 continue | 847 continue |
| 869 parent_interface = database.GetInterface(parent.type.id) | 848 parent_interface = database.GetInterface(parent.type.id) |
| 870 if _InstanceOfNode(database, parent_interface): | 849 if _InstanceOfNode(database, parent_interface): |
| 871 return True | 850 return True |
| 872 return False | 851 return False |
| OLD | NEW |