| 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 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 if not needs_dispatcher: | 435 if not needs_dispatcher: |
| 436 # Bind directly to native implementation | 436 # Bind directly to native implementation |
| 437 argument_count = (0 if info.IsStatic() else 1) + len(info.param_infos) | 437 argument_count = (0 if info.IsStatic() else 1) + len(info.param_infos) |
| 438 cpp_callback_name = self._GenerateNativeBinding( | 438 cpp_callback_name = self._GenerateNativeBinding( |
| 439 info.name, argument_count, dart_declaration, 'Callback', is_custom) | 439 info.name, argument_count, dart_declaration, 'Callback', is_custom) |
| 440 if not is_custom: | 440 if not is_custom: |
| 441 self._GenerateOperationNativeCallback(operation, operation.arguments, cp
p_callback_name) | 441 self._GenerateOperationNativeCallback(operation, operation.arguments, cp
p_callback_name) |
| 442 else: | 442 else: |
| 443 self._GenerateDispatcher(info.operations, dart_declaration, [info.name for
info in info.param_infos]) | 443 self._GenerateDispatcher(info.operations, dart_declaration, [info.name for
info in info.param_infos]) |
| 444 | 444 |
| 445 def _GenerateDispatcher(self, operations, dart_declaration, argument_names): | 445 def _GenerateDispatcher(self, operations, dart_declaration, parameter_names): |
| 446 | 446 |
| 447 body = self._members_emitter.Emit( | 447 body = self._members_emitter.Emit( |
| 448 '\n' | 448 '\n' |
| 449 ' $DECLARATION {\n' | 449 ' $DECLARATION {\n' |
| 450 '$!BODY' | 450 '$!BODY' |
| 451 ' }\n', | 451 ' }\n', |
| 452 DECLARATION=dart_declaration) | 452 DECLARATION=dart_declaration) |
| 453 | 453 |
| 454 version = [1] | 454 version = [1] |
| 455 def GenerateCall(operation, argument_count, checks): | 455 def GenerateCall(operation, argument_count, checks): |
| 456 if checks: | 456 if checks: |
| 457 if operation.type.id != 'void': | 457 if operation.type.id != 'void': |
| 458 template = ' if ($CHECKS) {\n return $CALL;\n }\n' | 458 template = ' if ($CHECKS) {\n return $CALL;\n }\n' |
| 459 else: | 459 else: |
| 460 template = ' if ($CHECKS) {\n $CALL;\n return;\n }\n' | 460 template = ' if ($CHECKS) {\n $CALL;\n return;\n }\n' |
| 461 else: | 461 else: |
| 462 if operation.type.id != 'void': | 462 if operation.type.id != 'void': |
| 463 template = ' return $CALL;\n' | 463 template = ' return $CALL;\n' |
| 464 else: | 464 else: |
| 465 template = ' $CALL;\n' | 465 template = ' $CALL;\n' |
| 466 | 466 |
| 467 overload_name = '%s_%s' % (operation.id, version[0]) | 467 overload_name = '%s_%s' % (operation.id, version[0]) |
| 468 version[0] += 1 | 468 version[0] += 1 |
| 469 argument_list = ', '.join(argument_names[:argument_count]) | 469 argument_list = ', '.join(parameter_names[:argument_count]) |
| 470 call = '_%s(%s)' % (overload_name, argument_list) | 470 call = '_%s(%s)' % (overload_name, argument_list) |
| 471 body.Emit(template, CHECKS=' && '.join(checks), CALL=call) | 471 body.Emit(template, CHECKS=' && '.join(checks), CALL=call) |
| 472 | 472 |
| 473 dart_declaration = '%s%s _%s(%s)' % ( | 473 dart_declaration = '%s%s _%s(%s)' % ( |
| 474 'static ' if operation.is_static else '', | 474 'static ' if operation.is_static else '', |
| 475 self.SecureOutputType(operation.type.id), | 475 self.SecureOutputType(operation.type.id), |
| 476 overload_name, argument_list) | 476 overload_name, argument_list) |
| 477 cpp_callback_name = self._GenerateNativeBinding( | 477 cpp_callback_name = self._GenerateNativeBinding( |
| 478 overload_name, (0 if operation.is_static else 1) + argument_count, | 478 overload_name, (0 if operation.is_static else 1) + argument_count, |
| 479 dart_declaration, 'Callback', False) | 479 dart_declaration, 'Callback', False) |
| 480 self._GenerateOperationNativeCallback(operation, operation.arguments[:argu
ment_count], cpp_callback_name) | 480 self._GenerateOperationNativeCallback(operation, operation.arguments[:argu
ment_count], cpp_callback_name) |
| 481 | 481 |
| 482 def GenerateChecksAndCall(operation, argument_count): | 482 def GenerateChecksAndCall(operation, argument_count): |
| 483 checks = [] | 483 GenerateCall(operation, argument_count, |
| 484 for i in range(0, argument_count): | 484 self._OverloadChecks(operation, parameter_names, argument_count)) |
| 485 argument = operation.arguments[i] | |
| 486 argument_name = argument_names[i] | |
| 487 type = self._DartType(argument.type.id) | |
| 488 if type not in ['dynamic', 'Object']: | |
| 489 checks.append('(%s is %s || %s == null)' % (argument_name, type, argum
ent_name)) | |
| 490 checks.extend(['!?%s' % name for name in argument_names[argument_count:]]) | |
| 491 GenerateCall(operation, argument_count, checks) | |
| 492 | 485 |
| 493 # TODO: Optimize the dispatch to avoid repeated checks. | 486 # TODO: Optimize the dispatch to avoid repeated checks. |
| 494 if len(operations) > 1: | 487 if len(operations) > 1: |
| 495 for operation in operations: | 488 for operation in operations: |
| 496 for position, argument in enumerate(operation.arguments): | 489 for position, argument in enumerate(operation.arguments): |
| 497 if self._IsArgumentOptionalInWebCore(operation, argument): | 490 if self._IsArgumentOptionalInWebCore(operation, argument): |
| 498 GenerateChecksAndCall(operation, position) | 491 GenerateChecksAndCall(operation, position) |
| 499 GenerateChecksAndCall(operation, len(operation.arguments)) | 492 GenerateChecksAndCall(operation, len(operation.arguments)) |
| 500 body.Emit(' throw "Incorrect number or type of arguments";\n'); | 493 body.Emit(' throw "Incorrect number or type of arguments";\n'); |
| 501 else: | 494 else: |
| 502 operation = operations[0] | 495 operation = operations[0] |
| 503 argument_count = len(operation.arguments) | 496 argument_count = len(operation.arguments) |
| 504 for position, argument in list(enumerate(operation.arguments))[::-1]: | 497 for position, argument in list(enumerate(operation.arguments))[::-1]: |
| 505 if self._IsArgumentOptionalInWebCore(operation, argument): | 498 if self._IsArgumentOptionalInWebCore(operation, argument): |
| 506 check = '?%s' % argument_names[position] | 499 check = '?%s' % parameter_names[position] |
| 507 # argument_count instead of position + 1 is used here to cover one | 500 # argument_count instead of position + 1 is used here to cover one |
| 508 # complicated case with the effectively optional argument in the middl
e. | 501 # complicated case with the effectively optional argument in the middl
e. |
| 509 # Consider foo(x, [Optional] y, [Optional=DefaultIsNullString] z) | 502 # Consider foo(x, [Optional] y, [Optional=DefaultIsNullString] z) |
| 510 # (as of now it's modelled after HTMLMediaElement.webkitAddKey). | 503 # (as of now it's modelled after HTMLMediaElement.webkitAddKey). |
| 511 # y is optional in WebCore, while z is not. | 504 # y is optional in WebCore, while z is not. |
| 512 # In this case, if y was actually passed, we'd like to emit foo(x, y,
z) invocation, | 505 # In this case, if y was actually passed, we'd like to emit foo(x, y,
z) invocation, |
| 513 # not foo(x, y). | 506 # not foo(x, y). |
| 514 GenerateCall(operation, argument_count, [check]) | 507 GenerateCall(operation, argument_count, [check]) |
| 515 argument_count = position | 508 argument_count = position |
| 516 GenerateCall(operation, argument_count, []) | 509 GenerateCall(operation, argument_count, []) |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 897 LIBRARY_NAME=library_name) | 890 LIBRARY_NAME=library_name) |
| 898 | 891 |
| 899 headers = self._library_headers[library_name] | 892 headers = self._library_headers[library_name] |
| 900 for header_file in headers: | 893 for header_file in headers: |
| 901 path = os.path.relpath(header_file, output_dir) | 894 path = os.path.relpath(header_file, output_dir) |
| 902 includes_emitter.Emit('#include "$PATH"\n', PATH=path) | 895 includes_emitter.Emit('#include "$PATH"\n', PATH=path) |
| 903 body_emitter.Emit( | 896 body_emitter.Emit( |
| 904 ' if (Dart_NativeFunction func = $CLASS_NAME::resolver(name, argu
mentCount))\n' | 897 ' if (Dart_NativeFunction func = $CLASS_NAME::resolver(name, argu
mentCount))\n' |
| 905 ' return func;\n', | 898 ' return func;\n', |
| 906 CLASS_NAME=os.path.splitext(os.path.basename(path))[0]) | 899 CLASS_NAME=os.path.splitext(os.path.basename(path))[0]) |
| OLD | NEW |