| 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 740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 html_name = self._html_system.RenameInHtmlLibrary( | 751 html_name = self._html_system.RenameInHtmlLibrary( |
| 752 html_interface_name, info.name, implementation_class=True) | 752 html_interface_name, info.name, implementation_class=True) |
| 753 | 753 |
| 754 if not html_name and info.name == 'item': | 754 if not html_name and info.name == 'item': |
| 755 # FIXME: item should be renamed to operator[], not removed. | 755 # FIXME: item should be renamed to operator[], not removed. |
| 756 html_name = '_item' | 756 html_name = '_item' |
| 757 | 757 |
| 758 if not html_name: | 758 if not html_name: |
| 759 return | 759 return |
| 760 | 760 |
| 761 is_custom = 'Custom' in operation.ext_attrs |
| 762 has_optional_arguments = any(_IsArgumentOptionalInWebCore(argument) for argu
ment in operation.arguments) |
| 763 needs_dispatcher = not is_custom and (len(info.operations) > 1 or has_option
al_arguments) |
| 764 |
| 765 if not needs_dispatcher: |
| 766 type_renamer = self._DartType |
| 767 default_value = 'null' |
| 768 else: |
| 769 type_renamer = lambda x: 'Dynamic' |
| 770 default_value = '_null' |
| 771 |
| 761 dart_declaration = '%s%s %s(%s)' % ( | 772 dart_declaration = '%s%s %s(%s)' % ( |
| 762 'static ' if info.IsStatic() else '', | 773 'static ' if info.IsStatic() else '', |
| 763 self._DartType(info.type_name), | 774 self._DartType(info.type_name), |
| 764 html_name, | 775 html_name, |
| 765 info.ParametersImplementationDeclaration(self._DartType)) | 776 info.ParametersImplementationDeclaration(type_renamer, default_value)) |
| 766 | 777 |
| 767 is_custom = 'Custom' in operation.ext_attrs | |
| 768 has_optional_arguments = any(IsOptional(argument) for argument in operation.
arguments) | |
| 769 needs_dispatcher = not is_custom and (len(info.operations) > 1 or has_option
al_arguments) | |
| 770 if not needs_dispatcher: | 778 if not needs_dispatcher: |
| 771 # Bind directly to native implementation | 779 # Bind directly to native implementation |
| 772 argument_count = (0 if info.IsStatic() else 1) + len(info.param_infos) | 780 argument_count = (0 if info.IsStatic() else 1) + len(info.param_infos) |
| 773 cpp_callback_name = self._GenerateNativeBinding( | 781 cpp_callback_name = self._GenerateNativeBinding( |
| 774 info.name, argument_count, dart_declaration, 'Callback', is_custom) | 782 info.name, argument_count, dart_declaration, 'Callback', is_custom) |
| 775 if not is_custom: | 783 if not is_custom: |
| 776 self._GenerateOperationNativeCallback(operation, operation.arguments, cp
p_callback_name) | 784 self._GenerateOperationNativeCallback(operation, operation.arguments, cp
p_callback_name) |
| 777 else: | 785 else: |
| 778 self._GenerateDispatcher(info.operations, dart_declaration, [info.name for
info in info.param_infos]) | 786 self._GenerateDispatcher(info.operations, dart_declaration, [info.name for
info in info.param_infos]) |
| 779 | 787 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 807 | 815 |
| 808 dart_declaration = '%s%s _%s(%s)' % ( | 816 dart_declaration = '%s%s _%s(%s)' % ( |
| 809 'static ' if operation.is_static else '', | 817 'static ' if operation.is_static else '', |
| 810 self._DartType(operation.type.id), overload_name, argument_list) | 818 self._DartType(operation.type.id), overload_name, argument_list) |
| 811 cpp_callback_name = self._GenerateNativeBinding( | 819 cpp_callback_name = self._GenerateNativeBinding( |
| 812 overload_name, (0 if operation.is_static else 1) + argument_count, | 820 overload_name, (0 if operation.is_static else 1) + argument_count, |
| 813 dart_declaration, 'Callback', False) | 821 dart_declaration, 'Callback', False) |
| 814 self._GenerateOperationNativeCallback(operation, operation.arguments[:argu
ment_count], cpp_callback_name) | 822 self._GenerateOperationNativeCallback(operation, operation.arguments[:argu
ment_count], cpp_callback_name) |
| 815 | 823 |
| 816 def GenerateChecksAndCall(operation, argument_count): | 824 def GenerateChecksAndCall(operation, argument_count): |
| 817 checks = ['%s === null' % name for name in argument_names] | 825 checks = ['%s === _null' % name for name in argument_names] |
| 818 for i in range(0, argument_count): | 826 for i in range(0, argument_count): |
| 819 argument = operation.arguments[i] | 827 argument = operation.arguments[i] |
| 820 checks[i] = '%s is %s' % (argument_names[i], self._DartType(argument.typ
e.id)) | 828 argument_name = argument_names[i] |
| 821 if IsOptional(argument) and 'Callback' in argument.ext_attrs: | 829 checks[i] = '(%s is %s || %s === null)' % ( |
| 822 checks[i] = '(%s or %s === null)' % (checks[position], argument_names[
i]) | 830 argument_name, self._DartType(argument.type.id), argument_name) |
| 823 GenerateCall(operation, argument_count, checks) | 831 GenerateCall(operation, argument_count, checks) |
| 824 | 832 |
| 825 def IsOptionalInWebCore(argument): | |
| 826 return IsOptional(argument) and not 'Callback' in argument.ext_attrs | |
| 827 | |
| 828 # TODO: Optimize the dispatch to avoid repeated checks. | 833 # TODO: Optimize the dispatch to avoid repeated checks. |
| 829 if len(operations) > 1: | 834 if len(operations) > 1: |
| 830 for operation in operations: | 835 for operation in operations: |
| 831 for position, argument in enumerate(operation.arguments): | 836 for position, argument in enumerate(operation.arguments): |
| 832 if IsOptionalInWebCore(argument): | 837 if _IsArgumentOptionalInWebCore(argument): |
| 833 GenerateChecksAndCall(operation, position) | 838 GenerateChecksAndCall(operation, position) |
| 834 GenerateChecksAndCall(operation, len(operation.arguments)) | 839 GenerateChecksAndCall(operation, len(operation.arguments)) |
| 835 body.Emit(' throw "Incorrect number or type of arguments";\n'); | 840 body.Emit(' throw "Incorrect number or type of arguments";\n'); |
| 836 else: | 841 else: |
| 837 operation = operations[0] | 842 operation = operations[0] |
| 838 for position, argument in list(enumerate(operation.arguments))[::-1]: | 843 for position, argument in list(enumerate(operation.arguments))[::-1]: |
| 839 if IsOptionalInWebCore(argument): | 844 if _IsArgumentOptionalInWebCore(argument): |
| 840 check = '%s === null' % argument_names[position] | 845 check = '%s === _null' % argument_names[position] |
| 841 GenerateCall(operation, position, [check]) | 846 GenerateCall(operation, position, [check]) |
| 842 GenerateCall(operation, len(operation.arguments), []) | 847 GenerateCall(operation, len(operation.arguments), []) |
| 843 | 848 |
| 844 def AddOperation(self, info): | 849 def AddOperation(self, info): |
| 845 self._AddOperation(info) | 850 self._AddOperation(info) |
| 846 | 851 |
| 847 def AddStaticOperation(self, info): | 852 def AddStaticOperation(self, info): |
| 848 self._AddOperation(info) | 853 self._AddOperation(info) |
| 849 | 854 |
| 850 def AddSecondaryOperation(self, interface, info): | 855 def AddSecondaryOperation(self, interface, info): |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1045 for parent in interface.parents: | 1050 for parent in interface.parents: |
| 1046 parent_name = parent.type.id | 1051 parent_name = parent.type.id |
| 1047 if not database.HasInterface(parent.type.id): | 1052 if not database.HasInterface(parent.type.id): |
| 1048 continue | 1053 continue |
| 1049 parent_interface = database.GetInterface(parent.type.id) | 1054 parent_interface = database.GetInterface(parent.type.id) |
| 1050 if callback(parent_interface): | 1055 if callback(parent_interface): |
| 1051 return parent_interface | 1056 return parent_interface |
| 1052 parent_interface = _FindParent(parent_interface, database, callback) | 1057 parent_interface = _FindParent(parent_interface, database, callback) |
| 1053 if parent_interface: | 1058 if parent_interface: |
| 1054 return parent_interface | 1059 return parent_interface |
| 1060 |
| 1061 def _IsArgumentOptionalInWebCore(argument): |
| 1062 return IsOptional(argument) and not 'Callback' in argument.ext_attrs |
| OLD | NEW |