Chromium Code Reviews| 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 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 WEBCORE_CLASS_NAME=self._interface_type_info.native_type()) | 348 WEBCORE_CLASS_NAME=self._interface_type_info.native_type()) | 
| 349 | 349 | 
| 350 self._cpp_header_emitter.Emit( | 350 self._cpp_header_emitter.Emit( | 
| 351 self._templates.Load('cpp_header.template'), | 351 self._templates.Load('cpp_header.template'), | 
| 352 INTERFACE=self._interface.id, | 352 INTERFACE=self._interface.id, | 
| 353 WEBCORE_INCLUDES=webcore_includes, | 353 WEBCORE_INCLUDES=webcore_includes, | 
| 354 WEBCORE_CLASS_NAME=self._interface_type_info.native_type(), | 354 WEBCORE_CLASS_NAME=self._interface_type_info.native_type(), | 
| 355 TO_DART_VALUE=to_dart_value_emitter.Fragments(), | 355 TO_DART_VALUE=to_dart_value_emitter.Fragments(), | 
| 356 DECLARATIONS=self._cpp_declarations_emitter.Fragments()) | 356 DECLARATIONS=self._cpp_declarations_emitter.Fragments()) | 
| 357 | 357 | 
| 358 def _GenerateCallWithHandling(self, node, parameter_definitions_emitter, argum ents): | |
| 359 if 'CallWith' not in node.ext_attrs: | |
| 360 return False | |
| 361 | |
| 362 call_with = node.ext_attrs['CallWith'] | |
| 363 if call_with == 'ScriptExecutionContext': | |
| 364 parameter_definitions_emitter.Emit( | |
| 365 '\n' | |
| 366 ' ScriptExecutionContext* context = DartUtilities::scriptExecut ionContext();\n' | |
| 367 ' if (!context)\n' | |
| 368 ' return;\n') | |
| 369 arguments.append('context') | |
| 370 return False | |
| 371 | |
| 372 if call_with == 'ScriptArguments|CallStack': | |
| 373 self._cpp_impl_includes.add('DOMWindow') | |
| 374 self._cpp_impl_includes.add('ScriptArguments') | |
| 375 self._cpp_impl_includes.add('ScriptCallStack') | |
| 376 self._cpp_impl_includes.add('V8Proxy') | |
| 377 self._cpp_impl_includes.add('v8') | |
| 378 parameter_definitions_emitter.Emit( | |
| 379 '\n' | |
| 380 ' v8::HandleScope handleScope;\n' | |
| 381 ' v8::Context::Scope scope(V8Proxy::mainWorldContext(DartUtilit ies::domWindowForCurrentIsolate()->frame()));\n' | |
| 382 ' Dart_Handle customArgument = Dart_GetNativeArgument(args, $IN DEX);\n' | |
| 383 ' RefPtr<ScriptArguments> scriptArguments(DartUtilities::create ScriptArguments(customArgument, exception));\n' | |
| 384 ' if (!scriptArguments)\n' | |
| 385 ' goto fail;\n' | |
| 386 ' RefPtr<ScriptCallStack> scriptCallStack(DartUtilities::create ScriptCallStack());\n' | |
| 387 ' if (!scriptCallStack->size())\n' | |
| 388 ' return;\n', | |
| 389 INDEX=len(node.arguments)) | |
| 390 arguments.extend(['scriptArguments', 'scriptCallStack']) | |
| 391 return True | |
| 392 | |
| 393 return False | |
| 394 | |
| 358 def AddAttribute(self, getter, setter): | 395 def AddAttribute(self, getter, setter): | 
| 359 # FIXME: Dartium does not support attribute event listeners. However, JS | 396 # FIXME: Dartium does not support attribute event listeners. However, JS | 
| 360 # implementation falls back to them when addEventListener is not available. | 397 # implementation falls back to them when addEventListener is not available. | 
| 361 # Make sure addEventListener is available in all EventTargets and remove | 398 # Make sure addEventListener is available in all EventTargets and remove | 
| 362 # this check. | 399 # this check. | 
| 363 if (getter or setter).type.id == 'EventListener': | 400 if (getter or setter).type.id == 'EventListener': | 
| 364 return | 401 return | 
| 365 | 402 | 
| 366 if 'CheckSecurityForNode' in (getter or setter).ext_attrs: | 403 if 'CheckSecurityForNode' in (getter or setter).ext_attrs: | 
| 367 # FIXME: exclude from interface as well. | 404 # FIXME: exclude from interface as well. | 
| (...skipping 18 matching lines...) Expand all Loading... | |
| 386 type_info = GetIDLTypeInfo(attr.type.id) | 423 type_info = GetIDLTypeInfo(attr.type.id) | 
| 387 dart_declaration = '%s get %s()' % ( | 424 dart_declaration = '%s get %s()' % ( | 
| 388 type_info.dart_type(), DartDomNameOfAttribute(attr)) | 425 type_info.dart_type(), DartDomNameOfAttribute(attr)) | 
| 389 is_custom = 'Custom' in attr.ext_attrs or 'CustomGetter' in attr.ext_attrs | 426 is_custom = 'Custom' in attr.ext_attrs or 'CustomGetter' in attr.ext_attrs | 
| 390 cpp_callback_name = self._GenerateNativeBinding(attr.id, 1, | 427 cpp_callback_name = self._GenerateNativeBinding(attr.id, 1, | 
| 391 dart_declaration, 'Getter', is_custom) | 428 dart_declaration, 'Getter', is_custom) | 
| 392 if is_custom: | 429 if is_custom: | 
| 393 return | 430 return | 
| 394 | 431 | 
| 395 arguments = [] | 432 arguments = [] | 
| 433 parameter_definitions_emitter = emitter.Emitter() | |
| 434 raises_exceptions = self._GenerateCallWithHandling(attr, parameter_definitio ns_emitter, arguments) | |
| 435 raises_exceptions = raises_exceptions or attr.get_raises | |
| 436 | |
| 396 if 'Reflect' in attr.ext_attrs: | 437 if 'Reflect' in attr.ext_attrs: | 
| 397 webcore_function_name = GetIDLTypeInfo(attr.type.id).webcore_getter_name() | 438 webcore_function_name = GetIDLTypeInfo(attr.type.id).webcore_getter_name() | 
| 398 if 'URL' in attr.ext_attrs: | 439 if 'URL' in attr.ext_attrs: | 
| 399 if 'NonEmpty' in attr.ext_attrs: | 440 if 'NonEmpty' in attr.ext_attrs: | 
| 400 webcore_function_name = 'getNonEmptyURLAttribute' | 441 webcore_function_name = 'getNonEmptyURLAttribute' | 
| 401 else: | 442 else: | 
| 402 webcore_function_name = 'getURLAttribute' | 443 webcore_function_name = 'getURLAttribute' | 
| 403 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr)) | 444 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr)) | 
| 404 else: | 445 else: | 
| 405 webcore_function_name = CppSafeName(attr.id) | 446 webcore_function_name = CppSafeName(attr.id) | 
| 406 if attr.id == 'target' and attr.type.id == 'SVGAnimatedString': | 447 if attr.id == 'target' and attr.type.id == 'SVGAnimatedString': | 
| 407 webcore_function_name = 'svgTarget' | 448 webcore_function_name = 'svgTarget' | 
| 408 else: | 449 else: | 
| 409 webcore_function_name = re.sub(r'^(HTML|URL|JS|XML|XSLT|\w)', | 450 webcore_function_name = re.sub(r'^(HTML|URL|JS|XML|XSLT|\w)', | 
| 410 lambda s: s.group(1).lower(), | 451 lambda s: s.group(1).lower(), | 
| 411 webcore_function_name) | 452 webcore_function_name) | 
| 412 webcore_function_name = re.sub(r'^(create|exclusive)', | 453 webcore_function_name = re.sub(r'^(create|exclusive)', | 
| 413 lambda s: 'is' + s.group(1).capitalize(), | 454 lambda s: 'is' + s.group(1).capitalize(), | 
| 414 webcore_function_name) | 455 webcore_function_name) | 
| 415 if attr.type.id.startswith('SVGAnimated'): | 456 if attr.type.id.startswith('SVGAnimated'): | 
| 416 webcore_function_name += 'Animated' | 457 webcore_function_name += 'Animated' | 
| 417 | 458 | 
| 418 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr) | 459 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr) | 
| 419 invocation = self._GenerateWebCoreInvocation(function_expression, | 460 invocation = self._GenerateWebCoreInvocation(function_expression, | 
| 420 arguments, attr.type.id, attr.ext_attrs, attr.get_raises) | 461 arguments, attr.type.id, attr.ext_attrs, attr.get_raises) | 
| 421 self._GenerateNativeCallback(cpp_callback_name, '', True, invocation, | 462 self._GenerateNativeCallback(cpp_callback_name, parameter_definitions_emitte r.Fragments(), | 
| 422 raises_exceptions=attr.get_raises) | 463 True, invocation, raises_exceptions=raises_exceptions) | 
| 423 | 464 | 
| 424 def _AddSetter(self, attr): | 465 def _AddSetter(self, attr): | 
| 425 type_info = GetIDLTypeInfo(attr.type.id) | 466 type_info = GetIDLTypeInfo(attr.type.id) | 
| 426 dart_declaration = 'void set %s(%s)' % ( | 467 dart_declaration = 'void set %s(%s)' % ( | 
| 427 DartDomNameOfAttribute(attr), type_info.dart_type()) | 468 DartDomNameOfAttribute(attr), type_info.dart_type()) | 
| 428 is_custom = set(['Custom', 'CustomSetter', 'V8CustomSetter']) & set(attr.ext _attrs) | 469 is_custom = set(['Custom', 'CustomSetter', 'V8CustomSetter']) & set(attr.ext _attrs) | 
| 429 cpp_callback_name = self._GenerateNativeBinding(attr.id, 2, | 470 cpp_callback_name = self._GenerateNativeBinding(attr.id, 2, | 
| 430 dart_declaration, 'Setter', is_custom) | 471 dart_declaration, 'Setter', is_custom) | 
| 431 if is_custom: | 472 if is_custom: | 
| 432 return | 473 return | 
| 433 | 474 | 
| 434 arguments = [] | 475 arguments = [] | 
| 476 parameter_definitions_emitter = emitter.Emitter() | |
| 477 self._GenerateCallWithHandling(attr, parameter_definitions_emitter, argument s) | |
| 478 | |
| 435 if 'Reflect' in attr.ext_attrs: | 479 if 'Reflect' in attr.ext_attrs: | 
| 436 webcore_function_name = GetIDLTypeInfo(attr.type.id).webcore_setter_name() | 480 webcore_function_name = GetIDLTypeInfo(attr.type.id).webcore_setter_name() | 
| 437 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr)) | 481 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr)) | 
| 438 else: | 482 else: | 
| 439 webcore_function_name = re.sub(r'^(xml(?=[A-Z])|\w)', | 483 webcore_function_name = re.sub(r'^(xml(?=[A-Z])|\w)', | 
| 440 lambda s: s.group(1).upper(), | 484 lambda s: s.group(1).upper(), | 
| 441 attr.id) | 485 attr.id) | 
| 442 webcore_function_name = 'set%s' % webcore_function_name | 486 webcore_function_name = 'set%s' % webcore_function_name | 
| 443 if attr.type.id.startswith('SVGAnimated'): | 487 if attr.type.id.startswith('SVGAnimated'): | 
| 444 webcore_function_name += 'Animated' | 488 webcore_function_name += 'Animated' | 
| 445 | 489 | 
| 446 cpp_arg_name = CppSafeName(attr.id) | 490 cpp_arg_name = CppSafeName(attr.id) | 
| 447 arguments.append(cpp_arg_name) | 491 arguments.append(cpp_arg_name) | 
| 448 | 492 | 
| 449 parameter_definitions_emitter = emitter.Emitter() | |
| 450 self._GenerateParameterAdapter( | 493 self._GenerateParameterAdapter( | 
| 451 parameter_definitions_emitter, attr, cpp_arg_name, 0) | 494 parameter_definitions_emitter, attr, cpp_arg_name, 0) | 
| 452 parameter_definitions = parameter_definitions_emitter.Fragments() | |
| 453 | 495 | 
| 454 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr) | 496 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr) | 
| 455 invocation = self._GenerateWebCoreInvocation(function_expression, | 497 invocation = self._GenerateWebCoreInvocation(function_expression, | 
| 456 arguments, 'void', attr.ext_attrs, attr.set_raises) | 498 arguments, 'void', attr.ext_attrs, attr.set_raises) | 
| 457 | 499 | 
| 458 self._GenerateNativeCallback(cpp_callback_name, parameter_definitions, | 500 self._GenerateNativeCallback(cpp_callback_name, parameter_definitions_emitte r.Fragments(), | 
| 459 True, invocation, raises_exceptions=True) | 501 True, invocation, raises_exceptions=True) | 
| 460 | 502 | 
| 461 def _HasNativeIndexGetter(self, interface): | 503 def _HasNativeIndexGetter(self, interface): | 
| 462 return ('CustomIndexedGetter' in interface.ext_attrs or | 504 return ('CustomIndexedGetter' in interface.ext_attrs or | 
| 463 'NumericIndexedGetter' in interface.ext_attrs) | 505 'NumericIndexedGetter' in interface.ext_attrs) | 
| 464 | 506 | 
| 465 def _EmitNativeIndexGetter(self, interface, element_type): | 507 def _EmitNativeIndexGetter(self, interface, element_type): | 
| 466 dart_declaration = '%s operator[](int index)' % element_type | 508 dart_declaration = '%s operator[](int index)' % element_type | 
| 467 self._GenerateNativeBinding('numericIndexGetter', 2, dart_declaration, | 509 self._GenerateNativeBinding('numericIndexGetter', 2, dart_declaration, | 
| 468 'Callback', True) | 510 'Callback', True) | 
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 544 cpp_callback_name = self._GenerateNativeBinding( | 586 cpp_callback_name = self._GenerateNativeBinding( | 
| 545 native_name, 1 + len(operation.arguments), dart_declaration, 'Callback', | 587 native_name, 1 + len(operation.arguments), dart_declaration, 'Callback', | 
| 546 is_custom) | 588 is_custom) | 
| 547 if is_custom: | 589 if is_custom: | 
| 548 return | 590 return | 
| 549 | 591 | 
| 550 # Generate callback. | 592 # Generate callback. | 
| 551 webcore_function_name = operation.ext_attrs.get('ImplementedAs', operation.i d) | 593 webcore_function_name = operation.ext_attrs.get('ImplementedAs', operation.i d) | 
| 552 | 594 | 
| 553 parameter_definitions_emitter = emitter.Emitter() | 595 parameter_definitions_emitter = emitter.Emitter() | 
| 554 raises_dart_exceptions = len(operation.arguments) > 0 or operation.raises | |
| 555 arguments = [] | 596 arguments = [] | 
| 556 | 597 raises_dart_exceptions = self._GenerateCallWithHandling( | 
| 
 
podivilov
2012/03/14 12:28:58
Please rename to raises_exceptions.
 
Anton Muhin
2012/03/14 17:49:22
Sorry, it was the original name.  I'll leave it fo
 
 | |
| 557 # Process 'CallWith' argument. | 598 operation, parameter_definitions_emitter, arguments) | 
| 558 if 'CallWith' in operation.ext_attrs: | 599 raises_dart_exceptions = raises_dart_exceptions or len(operation.arguments) > 0 or operation.raises | 
| 559 call_with = operation.ext_attrs['CallWith'] | |
| 560 if call_with == 'ScriptExecutionContext': | |
| 561 parameter_definitions_emitter.Emit( | |
| 562 ' ScriptExecutionContext* context = DartUtilities::scriptExec utionContext();\n' | |
| 563 ' if (!context)\n' | |
| 564 ' return;\n') | |
| 565 arguments.append('context') | |
| 566 elif call_with == 'ScriptArguments|CallStack': | |
| 567 raises_dart_exceptions = True | |
| 568 self._cpp_impl_includes.add('DOMWindow') | |
| 569 self._cpp_impl_includes.add('ScriptArguments') | |
| 570 self._cpp_impl_includes.add('ScriptCallStack') | |
| 571 self._cpp_impl_includes.add('V8Proxy') | |
| 572 self._cpp_impl_includes.add('v8') | |
| 573 parameter_definitions_emitter.Emit( | |
| 574 ' v8::HandleScope handleScope;\n' | |
| 575 ' v8::Context::Scope scope(V8Proxy::mainWorldContext(DartUtil ities::domWindowForCurrentIsolate()->frame()));\n' | |
| 576 ' Dart_Handle customArgument = Dart_GetNativeArgument(args, $ INDEX);\n' | |
| 577 ' RefPtr<ScriptArguments> scriptArguments(DartUtilities::crea teScriptArguments(customArgument, exception));\n' | |
| 578 ' if (!scriptArguments)\n' | |
| 579 ' goto fail;\n' | |
| 580 ' RefPtr<ScriptCallStack> scriptCallStack(DartUtilities::crea teScriptCallStack());\n' | |
| 581 ' if (!scriptCallStack->size())\n' | |
| 582 ' return;\n', | |
| 583 INDEX=len(operation.arguments)) | |
| 584 arguments.extend(['scriptArguments', 'scriptCallStack']) | |
| 585 | 600 | 
| 586 # Process Dart arguments. | 601 # Process Dart arguments. | 
| 587 for (i, argument) in enumerate(operation.arguments): | 602 for (i, argument) in enumerate(operation.arguments): | 
| 588 if (i == len(operation.arguments) - 1 and | 603 if (i == len(operation.arguments) - 1 and | 
| 589 self._interface.id == 'Console' and | 604 self._interface.id == 'Console' and | 
| 590 argument.id == 'arg'): | 605 argument.id == 'arg'): | 
| 591 # FIXME: we are skipping last argument here because it was added in | 606 # FIXME: we are skipping last argument here because it was added in | 
| 592 # supplemental dart.idl. Cleanup dart.idl and remove this check. | 607 # supplemental dart.idl. Cleanup dart.idl and remove this check. | 
| 593 break | 608 break | 
| 594 cpp_arg_name = CppSafeName(argument.id) | 609 cpp_arg_name = CppSafeName(argument.id) | 
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 773 | 788 | 
| 774 if 'ImplementedBy' in attributes: | 789 if 'ImplementedBy' in attributes: | 
| 775 arguments.insert(0, 'receiver') | 790 arguments.insert(0, 'receiver') | 
| 776 self._cpp_impl_includes.add(attributes['ImplementedBy']) | 791 self._cpp_impl_includes.add(attributes['ImplementedBy']) | 
| 777 | 792 | 
| 778 return emitter.Format(invocation_template, | 793 return emitter.Format(invocation_template, | 
| 779 FUNCTION_CALL='%s(%s)' % (function_expression, ', '.join(arguments))) | 794 FUNCTION_CALL='%s(%s)' % (function_expression, ', '.join(arguments))) | 
| 780 | 795 | 
| 781 def _GenerateCPPIncludes(includes): | 796 def _GenerateCPPIncludes(includes): | 
| 782 return ''.join(['#include "%s.h"\n' % include for include in includes]) | 797 return ''.join(['#include "%s.h"\n' % include for include in includes]) | 
| OLD | NEW |