| 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 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 operation = info.operations[0] | 575 operation = info.operations[0] |
| 576 | 576 |
| 577 if 'CheckSecurityForNode' in operation.ext_attrs: | 577 if 'CheckSecurityForNode' in operation.ext_attrs: |
| 578 # FIXME: exclude from interface as well. | 578 # FIXME: exclude from interface as well. |
| 579 return | 579 return |
| 580 | 580 |
| 581 is_custom = 'Custom' in operation.ext_attrs | 581 is_custom = 'Custom' in operation.ext_attrs |
| 582 has_optional_arguments = any(self._IsArgumentOptionalInWebCore(operation, ar
gument) for argument in operation.arguments) | 582 has_optional_arguments = any(self._IsArgumentOptionalInWebCore(operation, ar
gument) for argument in operation.arguments) |
| 583 needs_dispatcher = not is_custom and (len(info.operations) > 1 or has_option
al_arguments) | 583 needs_dispatcher = not is_custom and (len(info.operations) > 1 or has_option
al_arguments) |
| 584 | 584 |
| 585 if not needs_dispatcher: | |
| 586 type_renamer = self._DartType | |
| 587 default_value = 'null' | |
| 588 else: | |
| 589 type_renamer = lambda x: 'Dynamic' | |
| 590 default_value = '_null' | |
| 591 | |
| 592 dart_declaration = '%s%s %s(%s)' % ( | 585 dart_declaration = '%s%s %s(%s)' % ( |
| 593 'static ' if info.IsStatic() else '', | 586 'static ' if info.IsStatic() else '', |
| 594 self._DartType(info.type_name), | 587 self._DartType(info.type_name), |
| 595 html_name, | 588 html_name, |
| 596 info.ParametersImplementationDeclaration(type_renamer, default_value)) | 589 info.ParametersImplementationDeclaration( |
| 590 (lambda x: 'Dynamic') if needs_dispatcher else self._DartType)) |
| 597 | 591 |
| 598 if not needs_dispatcher: | 592 if not needs_dispatcher: |
| 599 # Bind directly to native implementation | 593 # Bind directly to native implementation |
| 600 argument_count = (0 if info.IsStatic() else 1) + len(info.param_infos) | 594 argument_count = (0 if info.IsStatic() else 1) + len(info.param_infos) |
| 601 cpp_callback_name = self._GenerateNativeBinding( | 595 cpp_callback_name = self._GenerateNativeBinding( |
| 602 info.name, argument_count, dart_declaration, 'Callback', is_custom) | 596 info.name, argument_count, dart_declaration, 'Callback', is_custom) |
| 603 if not is_custom: | 597 if not is_custom: |
| 604 self._GenerateOperationNativeCallback(operation, operation.arguments, cp
p_callback_name) | 598 self._GenerateOperationNativeCallback(operation, operation.arguments, cp
p_callback_name) |
| 605 else: | 599 else: |
| 606 self._GenerateDispatcher(info.operations, dart_declaration, [info.name for
info in info.param_infos]) | 600 self._GenerateDispatcher(info.operations, dart_declaration, [info.name for
info in info.param_infos]) |
| (...skipping 28 matching lines...) Expand all Loading... |
| 635 | 629 |
| 636 dart_declaration = '%s%s _%s(%s)' % ( | 630 dart_declaration = '%s%s _%s(%s)' % ( |
| 637 'static ' if operation.is_static else '', | 631 'static ' if operation.is_static else '', |
| 638 self._DartType(operation.type.id), overload_name, argument_list) | 632 self._DartType(operation.type.id), overload_name, argument_list) |
| 639 cpp_callback_name = self._GenerateNativeBinding( | 633 cpp_callback_name = self._GenerateNativeBinding( |
| 640 overload_name, (0 if operation.is_static else 1) + argument_count, | 634 overload_name, (0 if operation.is_static else 1) + argument_count, |
| 641 dart_declaration, 'Callback', False) | 635 dart_declaration, 'Callback', False) |
| 642 self._GenerateOperationNativeCallback(operation, operation.arguments[:argu
ment_count], cpp_callback_name) | 636 self._GenerateOperationNativeCallback(operation, operation.arguments[:argu
ment_count], cpp_callback_name) |
| 643 | 637 |
| 644 def GenerateChecksAndCall(operation, argument_count): | 638 def GenerateChecksAndCall(operation, argument_count): |
| 645 checks = ['%s === _null' % name for name in argument_names] | 639 checks = ['!?%s' % name for name in argument_names] |
| 646 for i in range(0, argument_count): | 640 for i in range(0, argument_count): |
| 647 argument = operation.arguments[i] | 641 argument = operation.arguments[i] |
| 648 argument_name = argument_names[i] | 642 argument_name = argument_names[i] |
| 649 checks[i] = '(%s is %s || %s === null)' % ( | 643 checks[i] = '(%s is %s || %s === null)' % ( |
| 650 argument_name, self._DartType(argument.type.id), argument_name) | 644 argument_name, self._DartType(argument.type.id), argument_name) |
| 651 GenerateCall(operation, argument_count, checks) | 645 GenerateCall(operation, argument_count, checks) |
| 652 | 646 |
| 653 # TODO: Optimize the dispatch to avoid repeated checks. | 647 # TODO: Optimize the dispatch to avoid repeated checks. |
| 654 if len(operations) > 1: | 648 if len(operations) > 1: |
| 655 for operation in operations: | 649 for operation in operations: |
| 656 for position, argument in enumerate(operation.arguments): | 650 for position, argument in enumerate(operation.arguments): |
| 657 if self._IsArgumentOptionalInWebCore(operation, argument): | 651 if self._IsArgumentOptionalInWebCore(operation, argument): |
| 658 GenerateChecksAndCall(operation, position) | 652 GenerateChecksAndCall(operation, position) |
| 659 GenerateChecksAndCall(operation, len(operation.arguments)) | 653 GenerateChecksAndCall(operation, len(operation.arguments)) |
| 660 body.Emit(' throw "Incorrect number or type of arguments";\n'); | 654 body.Emit(' throw "Incorrect number or type of arguments";\n'); |
| 661 else: | 655 else: |
| 662 operation = operations[0] | 656 operation = operations[0] |
| 663 argument_count = len(operation.arguments) | 657 argument_count = len(operation.arguments) |
| 664 for position, argument in list(enumerate(operation.arguments))[::-1]: | 658 for position, argument in list(enumerate(operation.arguments))[::-1]: |
| 665 if self._IsArgumentOptionalInWebCore(operation, argument): | 659 if self._IsArgumentOptionalInWebCore(operation, argument): |
| 666 check = '%s !== _null' % argument_names[position] | 660 check = '?%s' % argument_names[position] |
| 667 # argument_count instead of position + 1 is used here to cover one | 661 # argument_count instead of position + 1 is used here to cover one |
| 668 # complicated case with the effectively optional argument in the middl
e. | 662 # complicated case with the effectively optional argument in the middl
e. |
| 669 # Consider foo(x, [Optional] y, [Optional=DefaultIsNullString] z) | 663 # Consider foo(x, [Optional] y, [Optional=DefaultIsNullString] z) |
| 670 # (as of now it's modelled after HTMLMediaElement.webkitAddKey). | 664 # (as of now it's modelled after HTMLMediaElement.webkitAddKey). |
| 671 # y is optional in WebCore, while z is not. | 665 # y is optional in WebCore, while z is not. |
| 672 # In this case, if y !== _null, we'd like to emit foo(x, y, z) invocat
ion, not | 666 # In this case, if y was actually passed, we'd like to emit foo(x, y,
z) invocation, |
| 673 # foo(x, y). | 667 # not foo(x, y). |
| 674 GenerateCall(operation, argument_count, [check]) | 668 GenerateCall(operation, argument_count, [check]) |
| 675 argument_count = position | 669 argument_count = position |
| 676 GenerateCall(operation, argument_count, []) | 670 GenerateCall(operation, argument_count, []) |
| 677 | 671 |
| 678 def SecondaryContext(self, interface): | 672 def SecondaryContext(self, interface): |
| 679 pass | 673 pass |
| 680 | 674 |
| 681 def _GenerateOperationNativeCallback(self, operation, arguments, cpp_callback_
name): | 675 def _GenerateOperationNativeCallback(self, operation, arguments, cpp_callback_
name): |
| 682 webcore_function_name = operation.ext_attrs.get('ImplementedAs', operation.i
d) | 676 webcore_function_name = operation.ext_attrs.get('ImplementedAs', operation.i
d) |
| 683 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi
on_name, operation) | 677 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi
on_name, operation) |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 960 parent_interface = _FindInHierarchy(database, parent_interface, test) | 954 parent_interface = _FindInHierarchy(database, parent_interface, test) |
| 961 if parent_interface: | 955 if parent_interface: |
| 962 return parent_interface | 956 return parent_interface |
| 963 | 957 |
| 964 def _ToWebKitName(name): | 958 def _ToWebKitName(name): |
| 965 name = name[0].lower() + name[1:] | 959 name = name[0].lower() + name[1:] |
| 966 name = re.sub(r'^(hTML|uRL|jS|xML|xSLT)', lambda s: s.group(1).lower(), | 960 name = re.sub(r'^(hTML|uRL|jS|xML|xSLT)', lambda s: s.group(1).lower(), |
| 967 name) | 961 name) |
| 968 return re.sub(r'^(create|exclusive)', lambda s: 'is' + s.group(1).capitalize()
, | 962 return re.sub(r'^(create|exclusive)', lambda s: 'is' + s.group(1).capitalize()
, |
| 969 name) | 963 name) |
| OLD | NEW |