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