Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: lib/dom/scripts/systemnative.py

Issue 10818037: Refactor callback emission. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 238
239 ext_attrs = self._interface.ext_attrs 239 ext_attrs = self._interface.ext_attrs
240 240
241 if 'CustomConstructor' in ext_attrs: 241 if 'CustomConstructor' in ext_attrs:
242 # We have a custom implementation for it. 242 # We have a custom implementation for it.
243 self._cpp_declarations_emitter.Emit( 243 self._cpp_declarations_emitter.Emit(
244 '\n' 244 '\n'
245 'void constructorCallback(Dart_NativeArguments);\n') 245 'void constructorCallback(Dart_NativeArguments);\n')
246 return 246 return
247 247
248 raises_dom_exceptions = 'ConstructorRaisesException' in ext_attrs
249 raises_exceptions = raises_dom_exceptions or len(constructor_info.idl_args) > 0
250 arguments = []
251 parameter_definitions_emitter = emitter.Emitter()
252 create_function = 'create' 248 create_function = 'create'
253 if 'NamedConstructor' in ext_attrs: 249 if 'NamedConstructor' in ext_attrs:
254 raises_exceptions = True
255 self._cpp_impl_includes.add('"DOMWindow.h"')
256 parameter_definitions_emitter.Emit(
257 ' DOMWindow* domWindow = DartUtilities::domWindowForCurrentIs olate();\n'
258 ' if (!domWindow) {\n'
259 ' exception = Dart_NewString("Failed to fetch domWindow") ;\n'
260 ' goto fail;\n'
261 ' }\n'
262 ' Document* document = domWindow->document();\n')
263 arguments.append('document')
264 create_function = 'createForJSConstructor' 250 create_function = 'createForJSConstructor'
265 if 'CallWith' in ext_attrs:
266 call_with = ext_attrs['CallWith']
267 if call_with == 'ScriptExecutionContext':
268 raises_exceptions = True
269 parameter_definitions_emitter.Emit(
270 ' ScriptExecutionContext* context = DartUtilities::scriptExec utionContext();\n'
271 ' if (!context) {\n'
272 ' exception = Dart_NewString("Failed to create an object" );\n'
273 ' goto fail;\n'
274 ' }\n')
275 arguments.append('context')
276 else:
277 raise Exception('Unsupported CallWith=%s attribute' % call_with)
278
279 # Process constructor arguments.
280 for (i, argument) in enumerate(constructor_info.idl_args):
281 argument_expression = self._GenerateToNative(
282 parameter_definitions_emitter, argument, i)
283 arguments.append(argument_expression)
284
285 function_expression = '%s::%s' % (self._interface_type_info.native_type(), c reate_function) 251 function_expression = '%s::%s' % (self._interface_type_info.native_type(), c reate_function)
286 invocation = self._GenerateWebCoreInvocation(function_expression, arguments, 252 self._GenerateNativeCallback(
287 self._interface.id, ext_attrs, raises_dom_exceptions) 253 'constructorCallback',
288 254 False,
289 runtime_check = None 255 function_expression,
290 database = self._database 256 self._interface,
291 assert (not ( 257 constructor_info.idl_args,
292 'synthesizedV8EnabledPerContext' in ext_attrs and 258 self._interface.id,
293 'synthesizedV8EnabledAtRuntime' in ext_attrs)) 259 'ConstructorRaisesException' in ext_attrs)
294 if 'synthesizedV8EnabledPerContext' in ext_attrs:
295 raises_exceptions = True
296 self._cpp_impl_includes.add('"ContextFeatures.h"')
297 self._cpp_impl_includes.add('"DOMWindow.h"')
298 runtime_check = emitter.Format(
299 ' if (!ContextFeatures::$(FEATURE)Enabled(DartUtilities::domWin dowForCurrentIsolate()->document())) {\n'
300 ' exception = Dart_NewString("Feature $FEATURE is not enabl ed");\n'
301 ' goto fail;\n'
302 ' }',
303 FEATURE=ext_attrs['synthesizedV8EnabledPerContext'])
304
305 if 'synthesizedV8EnabledAtRuntime' in ext_attrs:
306 raises_exceptions = True
307 self._cpp_impl_includes.add('"RuntimeEnabledFeatures.h"')
308 runtime_check = emitter.Format(
309 ' if (!RuntimeEnabledFeatures::$(FEATURE)Enabled()) {\n'
310 ' exception = Dart_NewString("Feature $FEATURE is not enabl ed");\n'
311 ' goto fail;\n'
312 ' }',
313 FEATURE=_ToWebKitName(ext_attrs['synthesizedV8EnabledAtRuntime']))
314
315 self._GenerateNativeCallback(callback_name='constructorCallback',
316 parameter_definitions=parameter_definitions_emitter.Fragments(),
317 needs_receiver=False, invocation=invocation,
318 raises_exceptions=raises_exceptions,
319 runtime_check=runtime_check,
320 requires_v8_scope=self._RequiresV8Scope(ext_attrs, constructor_info.idl_ args))
321
322 260
323 def _ImplClassName(self, interface_name): 261 def _ImplClassName(self, interface_name):
324 return '_%sImpl' % interface_name 262 return '_%sImpl' % interface_name
325 263
326 def _BaseClassName(self): 264 def _BaseClassName(self):
327 root_class = 'NativeFieldWrapperClass1' 265 root_class = 'NativeFieldWrapperClass1'
328 266
329 if not self._interface.parents: 267 if not self._interface.parents:
330 return root_class 268 return root_class
331 269
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 INTERFACE=self._interface.id, 370 INTERFACE=self._interface.id,
433 WEBCORE_INCLUDES=webcore_includes, 371 WEBCORE_INCLUDES=webcore_includes,
434 WEBCORE_CLASS_NAME=self._interface_type_info.native_type(), 372 WEBCORE_CLASS_NAME=self._interface_type_info.native_type(),
435 DECLARATIONS=self._cpp_declarations_emitter.Fragments(), 373 DECLARATIONS=self._cpp_declarations_emitter.Fragments(),
436 IS_NODE=TypeCheckHelper(is_node_test), 374 IS_NODE=TypeCheckHelper(is_node_test),
437 IS_ACTIVE=TypeCheckHelper(is_active_test), 375 IS_ACTIVE=TypeCheckHelper(is_active_test),
438 IS_EVENT_TARGET=TypeCheckHelper(is_event_target_test), 376 IS_EVENT_TARGET=TypeCheckHelper(is_event_target_test),
439 TO_NATIVE=to_native_emitter.Fragments(), 377 TO_NATIVE=to_native_emitter.Fragments(),
440 TO_DART=to_dart_emitter.Fragments()) 378 TO_DART=to_dart_emitter.Fragments())
441 379
442 def _GenerateCallWithHandling(self, node, parameter_definitions_emitter, argum ents):
443 if 'CallWith' not in node.ext_attrs:
444 return False
445
446 call_with = node.ext_attrs['CallWith']
447 if call_with == 'ScriptExecutionContext':
448 parameter_definitions_emitter.Emit(
449 '\n'
450 ' ScriptExecutionContext* context = DartUtilities::scriptExecut ionContext();\n'
451 ' if (!context)\n'
452 ' return;\n')
453 arguments.append('context')
454 return False
455
456 if call_with == 'ScriptArguments|CallStack':
457 self._cpp_impl_includes.add('"ScriptArguments.h"')
458 self._cpp_impl_includes.add('"ScriptCallStack.h"')
459 parameter_definitions_emitter.Emit(
460 '\n'
461 ' Dart_Handle customArgument = Dart_GetNativeArgument(args, $IN DEX);\n'
462 ' RefPtr<ScriptArguments> scriptArguments(DartUtilities::create ScriptArguments(customArgument, exception));\n'
463 ' if (!scriptArguments)\n'
464 ' goto fail;\n'
465 ' RefPtr<ScriptCallStack> scriptCallStack(DartUtilities::create ScriptCallStack());\n'
466 ' if (!scriptCallStack->size())\n'
467 ' return;\n',
468 INDEX=len(node.arguments))
469 arguments.extend(['scriptArguments', 'scriptCallStack'])
470 return True
471
472 return False
473
474 def AddAttribute(self, attribute, html_name, read_only): 380 def AddAttribute(self, attribute, html_name, read_only):
475 if 'CheckSecurityForNode' in attribute.ext_attrs: 381 if 'CheckSecurityForNode' in attribute.ext_attrs:
476 # FIXME: exclude from interface as well. 382 # FIXME: exclude from interface as well.
477 return 383 return
478 384
479 self._AddGetter(attribute, html_name) 385 self._AddGetter(attribute, html_name)
480 if not read_only: 386 if not read_only:
481 self._AddSetter(attribute, html_name) 387 self._AddSetter(attribute, html_name)
482 388
483 def _AddGetter(self, attr, html_name): 389 def _AddGetter(self, attr, html_name):
484 type_info = self._TypeInfo(attr.type.id) 390 type_info = self._TypeInfo(attr.type.id)
485 dart_declaration = '%s get %s()' % (self._DartType(attr.type.id), html_name) 391 dart_declaration = '%s get %s()' % (self._DartType(attr.type.id), html_name)
486 is_custom = 'Custom' in attr.ext_attrs or 'CustomGetter' in attr.ext_attrs 392 is_custom = 'Custom' in attr.ext_attrs or 'CustomGetter' in attr.ext_attrs
487 cpp_callback_name = self._GenerateNativeBinding(attr.id, 1, 393 cpp_callback_name = self._GenerateNativeBinding(attr.id, 1,
488 dart_declaration, 'Getter', is_custom) 394 dart_declaration, 'Getter', is_custom)
489 if is_custom: 395 if is_custom:
490 return 396 return
491 397
492 arguments = []
493 parameter_definitions_emitter = emitter.Emitter()
494 raises_exceptions = self._GenerateCallWithHandling(
495 attr, parameter_definitions_emitter, arguments)
496 raises_exceptions = raises_exceptions or attr.get_raises
497
498 if 'Reflect' in attr.ext_attrs: 398 if 'Reflect' in attr.ext_attrs:
499 webcore_function_name = self._TypeInfo(attr.type.id).webcore_getter_name() 399 webcore_function_name = self._TypeInfo(attr.type.id).webcore_getter_name()
500 if 'URL' in attr.ext_attrs: 400 if 'URL' in attr.ext_attrs:
501 if 'NonEmpty' in attr.ext_attrs: 401 if 'NonEmpty' in attr.ext_attrs:
502 webcore_function_name = 'getNonEmptyURLAttribute' 402 webcore_function_name = 'getNonEmptyURLAttribute'
503 else: 403 else:
504 webcore_function_name = 'getURLAttribute' 404 webcore_function_name = 'getURLAttribute'
505 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr))
506 else: 405 else:
507 if attr.id == 'operator': 406 if attr.id == 'operator':
508 webcore_function_name = '_operator' 407 webcore_function_name = '_operator'
509 elif attr.id == 'target' and attr.type.id == 'SVGAnimatedString': 408 elif attr.id == 'target' and attr.type.id == 'SVGAnimatedString':
510 webcore_function_name = 'svgTarget' 409 webcore_function_name = 'svgTarget'
511 else: 410 else:
512 webcore_function_name = _ToWebKitName(attr.id) 411 webcore_function_name = _ToWebKitName(attr.id)
513 if attr.type.id.startswith('SVGAnimated'): 412 if attr.type.id.startswith('SVGAnimated'):
514 webcore_function_name += 'Animated' 413 webcore_function_name += 'Animated'
515 414
516 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr) 415 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr)
517 invocation = self._GenerateWebCoreInvocation(function_expression, 416 self._GenerateNativeCallback(
518 arguments, attr.type.id, attr.ext_attrs, attr.get_raises) 417 cpp_callback_name,
519 self._GenerateNativeCallback(cpp_callback_name, parameter_definitions_emitte r.Fragments(), 418 True,
520 True, invocation, raises_exceptions=raises_exceptions, 419 function_expression,
521 runtime_check=None, 420 attr,
522 requires_v8_scope=self._RequiresV8Scope(attr.ext_attrs, [])) 421 [],
422 attr.type.id,
423 attr.get_raises)
523 424
524 def _AddSetter(self, attr, html_name): 425 def _AddSetter(self, attr, html_name):
525 type_info = self._TypeInfo(attr.type.id) 426 type_info = self._TypeInfo(attr.type.id)
526 dart_declaration = 'void set %s(%s)' % (html_name, self._DartType(attr.type. id)) 427 dart_declaration = 'void set %s(%s)' % (html_name, self._DartType(attr.type. id))
527 is_custom = set(['Custom', 'CustomSetter', 'V8CustomSetter']) & set(attr.ext _attrs) 428 is_custom = set(['Custom', 'CustomSetter', 'V8CustomSetter']) & set(attr.ext _attrs)
528 cpp_callback_name = self._GenerateNativeBinding(attr.id, 2, 429 cpp_callback_name = self._GenerateNativeBinding(attr.id, 2,
529 dart_declaration, 'Setter', is_custom) 430 dart_declaration, 'Setter', is_custom)
530 if is_custom: 431 if is_custom:
531 return 432 return
532 433
533 arguments = []
534 parameter_definitions_emitter = emitter.Emitter()
535 self._GenerateCallWithHandling(attr, parameter_definitions_emitter, argument s)
536
537 if 'Reflect' in attr.ext_attrs: 434 if 'Reflect' in attr.ext_attrs:
538 webcore_function_name = self._TypeInfo(attr.type.id).webcore_setter_name() 435 webcore_function_name = self._TypeInfo(attr.type.id).webcore_setter_name()
539 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr))
540 else: 436 else:
541 webcore_function_name = re.sub(r'^(xml(?=[A-Z])|\w)', 437 webcore_function_name = re.sub(r'^(xml(?=[A-Z])|\w)',
542 lambda s: s.group(1).upper(), 438 lambda s: s.group(1).upper(),
543 attr.id) 439 attr.id)
544 webcore_function_name = 'set%s' % webcore_function_name 440 webcore_function_name = 'set%s' % webcore_function_name
545 if attr.type.id.startswith('SVGAnimated'): 441 if attr.type.id.startswith('SVGAnimated'):
546 webcore_function_name += 'Animated' 442 webcore_function_name += 'Animated'
547 443
548 argument_expression = self._GenerateToNative(
549 parameter_definitions_emitter, attr, 1, argument_name='value')
550 arguments.append(argument_expression)
551
552 parameter_definitions = parameter_definitions_emitter.Fragments()
553 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr) 444 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr)
554 invocation = self._GenerateWebCoreInvocation(function_expression, 445 self._GenerateNativeCallback(
555 arguments, 'void', attr.ext_attrs, attr.set_raises) 446 cpp_callback_name,
556 447 True,
557 self._GenerateNativeCallback(cpp_callback_name, parameter_definitions_emitte r.Fragments(), 448 function_expression,
558 True, invocation, raises_exceptions=True, 449 attr,
559 runtime_check=None, 450 [attr],
560 requires_v8_scope=self._RequiresV8Scope(attr.ext_attrs, [attr])) 451 'void',
452 attr.set_raises)
561 453
562 def AddIndexer(self, element_type): 454 def AddIndexer(self, element_type):
563 """Adds all the methods required to complete implementation of List.""" 455 """Adds all the methods required to complete implementation of List."""
564 # We would like to simply inherit the implementation of everything except 456 # We would like to simply inherit the implementation of everything except
565 # get length(), [], and maybe []=. It is possible to extend from a base 457 # get length(), [], and maybe []=. It is possible to extend from a base
566 # array implementation class only when there is no other implementation 458 # array implementation class only when there is no other implementation
567 # inheritance. There might be no implementation inheritance other than 459 # inheritance. There might be no implementation inheritance other than
568 # DOMBaseWrapper for many classes, but there might be some where the 460 # DOMBaseWrapper for many classes, but there might be some where the
569 # array-ness is introduced by a non-root interface: 461 # array-ness is introduced by a non-root interface:
570 # 462 #
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 if _IsArgumentOptionalInWebCore(argument): 634 if _IsArgumentOptionalInWebCore(argument):
743 check = '%s === _null' % argument_names[position] 635 check = '%s === _null' % argument_names[position]
744 GenerateCall(operation, position, [check]) 636 GenerateCall(operation, position, [check])
745 GenerateCall(operation, len(operation.arguments), []) 637 GenerateCall(operation, len(operation.arguments), [])
746 638
747 def SecondaryContext(self, interface): 639 def SecondaryContext(self, interface):
748 pass 640 pass
749 641
750 def _GenerateOperationNativeCallback(self, operation, arguments, cpp_callback_ name): 642 def _GenerateOperationNativeCallback(self, operation, arguments, cpp_callback_ name):
751 webcore_function_name = operation.ext_attrs.get('ImplementedAs', operation.i d) 643 webcore_function_name = operation.ext_attrs.get('ImplementedAs', operation.i d)
644 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, operation)
645 self._GenerateNativeCallback(
646 cpp_callback_name,
647 not operation.is_static,
648 function_expression,
649 operation,
650 arguments,
651 operation.type.id,
652 operation.raises)
653
654 def _GenerateNativeCallback(self,
655 callback_name,
656 needs_receiver,
657 function_expression,
658 node,
659 arguments,
660 return_type,
661 raises_dom_exception):
662 ext_attrs = node.ext_attrs
663 cpp_arguments = []
664
665 requires_v8_scope = False
666 runtime_check = None
667 raises_exceptions = False
668 requires_script_execution_context = False
669 requires_dom_window = False
670 requires_stack_info = False
podivilov1 2012/08/21 10:28:53 There is too many flags coming from different plac
Anton Muhin 2012/08/21 12:09:49 Agree. BTW, please, have a look at the latest ver
671
672 if raises_dom_exception or arguments:
673 raises_exceptions = True
674
675 if ext_attrs.get('CallWith') == 'ScriptArguments|CallStack':
676 raises_exceptions = True
677 requires_v8_scope = True
678 requires_stack_info = True
679 self._cpp_impl_includes.add('"ScriptArguments.h"')
680 self._cpp_impl_includes.add('"ScriptCallStack.h"')
681 cpp_arguments = ['scriptArguments', 'scriptCallStack']
682
683 if ext_attrs.get('CallWith') == 'ScriptExecutionContext':
684 raises_exceptions = True
685 requires_script_execution_context = True
686
687 if 'NamedConstructor' in ext_attrs:
688 raises_exceptions = True
689 requires_dom_window = True
690 self._cpp_impl_includes.add('"DOMWindow.h"')
691 cpp_arguments = ['document']
692
693 if 'Reflect' in ext_attrs:
694 cpp_arguments = [self._GenerateWebCoreReflectionAttributeName(node)]
695
696 for argument in arguments:
697 if self._TypeInfo(argument.type.id).requires_v8_scope():
698 requires_v8_scope = True
752 699
753 parameter_definitions_emitter = emitter.Emitter() 700 parameter_definitions_emitter = emitter.Emitter()
754 cpp_arguments = []
755 raises_exceptions = self._GenerateCallWithHandling(
756 operation, parameter_definitions_emitter, cpp_arguments)
757 raises_exceptions = raises_exceptions or len(arguments) > 0 or operation.rai ses
758
759 # Process Dart cpp_arguments. 701 # Process Dart cpp_arguments.
760 start_index = 1 702 start_index = 0
761 if operation.is_static: 703 if needs_receiver:
762 start_index = 0 704 start_index = 1
763 for (i, argument) in enumerate(arguments): 705 for (i, argument) in enumerate(arguments):
764 if (i == len(arguments) - 1 and 706 if (i == len(arguments) - 1 and
765 self._interface.id == 'Console' and 707 self._interface.id == 'Console' and
766 argument.id == 'arg'): 708 argument.id == 'arg'):
767 # FIXME: we are skipping last argument here because it was added in 709 # FIXME: we are skipping last argument here because it was added in
768 # supplemental dart.idl. Cleanup dart.idl and remove this check. 710 # supplemental dart.idl. Cleanup dart.idl and remove this check.
769 break 711 break
770 argument_expression = self._GenerateToNative( 712 argument_expression = self._GenerateToNative(
771 parameter_definitions_emitter, argument, start_index + i) 713 parameter_definitions_emitter, argument, start_index + i)
772 cpp_arguments.append(argument_expression) 714 cpp_arguments.append(argument_expression)
773 715
774 if operation.id in ['addEventListener', 'removeEventListener']: 716 # FIXME: rework in IDLs.
717 if node.id in ['addEventListener', 'removeEventListener']:
775 # addEventListener's and removeEventListener's last argument is marked 718 # addEventListener's and removeEventListener's last argument is marked
776 # as optional in idl, but is not optional in webcore implementation. 719 # as optional in idl, but is not optional in webcore implementation.
777 if len(arguments) == 2: 720 if len(arguments) == 2:
778 cpp_arguments.append('false') 721 cpp_arguments.append('false')
779 722
780 if self._interface.id == 'CSSStyleDeclaration' and operation.id == 'setPrope rty': 723 if self._interface.id == 'CSSStyleDeclaration' and node.id == 'setProperty':
781 # CSSStyleDeclaration.setProperty priority parameter is optional in Dart 724 # CSSStyleDeclaration.setProperty priority parameter is optional in Dart
782 # idl, but is not optional in webcore implementation. 725 # idl, but is not optional in webcore implementation.
783 if len(arguments) == 2: 726 if len(arguments) == 2:
784 cpp_arguments.append('String()') 727 cpp_arguments.append('String()')
785 728
786 if 'NeedsUserGestureCheck' in operation.ext_attrs: 729 if 'NeedsUserGestureCheck' in ext_attrs:
787 cpp_arguments.append('DartUtilities::processingUserGesture') 730 cpp_arguments.append('DartUtilities::processingUserGesture');
podivilov1 2012/08/21 10:28:53 semicolon :)
Anton Muhin 2012/08/21 12:09:49 Oops. On 2012/08/21 10:28:53, podivilov1 wrote:
788 731
789 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, operation) 732 assert (not (
790 invocation = self._GenerateWebCoreInvocation(function_expression, cpp_argume nts, 733 'synthesizedV8EnabledPerContext' in ext_attrs and
791 operation.type.id, operation.ext_attrs, operation.raises) 734 'synthesizedV8EnabledAtRuntime' in ext_attrs))
792 self._GenerateNativeCallback(cpp_callback_name, 735 if 'synthesizedV8EnabledPerContext' in ext_attrs:
793 parameter_definitions=parameter_definitions_emitter.Fragments(), 736 raises_exceptions = True
794 needs_receiver=not operation.is_static, invocation=invocation, 737 self._cpp_impl_includes.add('"ContextFeatures.h"')
795 raises_exceptions=raises_exceptions, 738 self._cpp_impl_includes.add('"DOMWindow.h"')
796 runtime_check=None, 739 runtime_check = emitter.Format(
797 requires_v8_scope=self._RequiresV8Scope(operation.ext_attrs, arguments)) 740 ' if (!ContextFeatures::$(FEATURE)Enabled(DartUtilities::domWin dowForCurrentIsolate()->document())) {\n'
741 ' exception = Dart_NewString("Feature $FEATURE is not enabl ed");\n'
742 ' goto fail;\n'
743 ' }',
744 FEATURE=ext_attrs['synthesizedV8EnabledPerContext'])
798 745
799 def _GenerateNativeCallback(self, callback_name, parameter_definitions, 746 if 'synthesizedV8EnabledAtRuntime' in ext_attrs:
800 needs_receiver, invocation, raises_exceptions, runtime_check, 747 raises_exceptions = True
801 requires_v8_scope): 748 self._cpp_impl_includes.add('"RuntimeEnabledFeatures.h"')
749 runtime_check = emitter.Format(
750 ' if (!RuntimeEnabledFeatures::$(FEATURE)Enabled()) {\n'
751 ' exception = Dart_NewString("Feature $FEATURE is not enabl ed");\n'
752 ' goto fail;\n'
753 ' }',
754 FEATURE=_ToWebKitName(ext_attrs['synthesizedV8EnabledAtRuntime']))
755
756 invocation = self._GenerateWebCoreInvocation(
757 function_expression, cpp_arguments, return_type, ext_attrs, raises_dom_e xception)
802 758
803 head_emitter = emitter.Emitter() 759 head_emitter = emitter.Emitter()
804 760
805 if requires_v8_scope: 761 if requires_v8_scope:
806 head_emitter.Emit( 762 head_emitter.Emit(
807 ' V8Scope v8scope;\n\n') 763 ' V8Scope v8scope;\n\n')
808 764
809 if runtime_check: 765 if runtime_check:
810 head_emitter.Emit( 766 head_emitter.Emit(
811 '$RUNTIME_CHECK\n', 767 '$RUNTIME_CHECK\n',
812 RUNTIME_CHECK=runtime_check) 768 RUNTIME_CHECK=runtime_check)
813 769
770 if requires_script_execution_context:
771 head_emitter.Emit(
772 ' ScriptExecutionContext* context = DartUtilities::scriptExecut ionContext();\n'
773 ' if (!context) {\n'
774 ' exception = Dart_NewString("Failed to retrieve a context" );\n'
775 ' goto fail;\n'
776 ' }\n\n')
777
778 if requires_dom_window:
779 head_emitter.Emit(
780 ' DOMWindow* domWindow = DartUtilities::domWindowForCurrentIsol ate();\n'
781 ' if (!domWindow) {\n'
782 ' exception = Dart_NewString("Failed to fetch domWindow");\ n'
783 ' goto fail;\n'
784 ' }\n'
785 ' Document* document = domWindow->document();\n')
podivilov1 2012/08/21 10:28:53 Personally, I don't like splitting c++ parameter d
Anton Muhin 2012/08/21 12:09:49 I overall agree, but there were some unpleasant ch
786
814 if needs_receiver: 787 if needs_receiver:
815 head_emitter.Emit( 788 head_emitter.Emit(
816 ' $WEBCORE_CLASS_NAME* receiver = DartDOMWrapper::receiver< $WE BCORE_CLASS_NAME >(args);\n', 789 ' $WEBCORE_CLASS_NAME* receiver = DartDOMWrapper::receiver< $WE BCORE_CLASS_NAME >(args);\n',
817 WEBCORE_CLASS_NAME=self._interface_type_info.native_type()) 790 WEBCORE_CLASS_NAME=self._interface_type_info.native_type())
818 791
792 if requires_stack_info:
793 head_emitter.Emit(
794 '\n'
795 ' Dart_Handle customArgument = Dart_GetNativeArgument(args, $IN DEX);\n'
796 ' RefPtr<ScriptArguments> scriptArguments(DartUtilities::create ScriptArguments(customArgument, exception));\n'
797 ' if (!scriptArguments)\n'
798 ' goto fail;\n'
799 ' RefPtr<ScriptCallStack> scriptCallStack(DartUtilities::create ScriptCallStack());\n'
800 ' if (!scriptCallStack->size())\n'
801 ' return;\n',
802 INDEX=len(arguments))
803
819 head_emitter.Emit( 804 head_emitter.Emit(
820 '$PARAMETE_DEFINITIONS\n', 805 '$PARAMETE_DEFINITIONS\n',
821 PARAMETE_DEFINITIONS=parameter_definitions) 806 PARAMETE_DEFINITIONS=parameter_definitions_emitter.Fragments())
822 807
823 body = emitter.Format( 808 body = emitter.Format(
824 ' {\n' 809 ' {\n'
825 '$HEAD' 810 '$HEAD'
826 '$INVOCATION' 811 '$INVOCATION'
827 ' return;\n' 812 ' return;\n'
828 ' }\n', 813 ' }\n',
829 HEAD=head_emitter.Fragments(), 814 HEAD=head_emitter.Fragments(),
830 INVOCATION=invocation) 815 INVOCATION=invocation)
831 816
(...skipping 10 matching lines...) Expand all
842 self._cpp_definitions_emitter.Emit( 827 self._cpp_definitions_emitter.Emit(
843 '\n' 828 '\n'
844 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n' 829 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n'
845 '{\n' 830 '{\n'
846 ' DartApiScope dartApiScope;\n' 831 ' DartApiScope dartApiScope;\n'
847 '$BODY' 832 '$BODY'
848 '}\n', 833 '}\n',
849 CALLBACK_NAME=callback_name, 834 CALLBACK_NAME=callback_name,
850 BODY=body) 835 BODY=body)
851 836
852 def _GenerateToNative(self, emitter, idl_node, index, argument_name=None): 837 def _GenerateToNative(self, emitter, idl_node, index):
853 """idl_node is IDLArgument or IDLAttribute.""" 838 """idl_node is IDLArgument or IDLAttribute."""
854 type_info = self._TypeInfo(idl_node.type.id) 839 type_info = self._TypeInfo(idl_node.type.id)
855 self._cpp_impl_includes |= set(type_info.to_native_includes()) 840 self._cpp_impl_includes |= set(type_info.to_native_includes())
856 argument_name = argument_name or idl_node.id 841 argument_name = idl_node.id
842 # Rename to get rid of conflicts with C++ keywords.
843 if argument_name == 'default':
podivilov1 2012/08/21 10:28:53 This is fragile because it differs from what v8 ge
Anton Muhin 2012/08/21 12:09:49 I don't think it's a big problem to be different f
844 argument_name = 'value'
857 handle = 'Dart_GetNativeArgument(args, %i)' % index 845 handle = 'Dart_GetNativeArgument(args, %i)' % index
858 argument_expression = type_info.emit_to_native( 846 argument_expression = type_info.emit_to_native(
859 emitter, idl_node, argument_name, handle, self._interface.id) 847 emitter, idl_node, argument_name, handle, self._interface.id)
860 return argument_expression 848 return argument_expression
861 849
862 def _RequiresV8Scope(self, ext_attrs, arguments):
863 if 'CallWith' in ext_attrs and ext_attrs['CallWith'] == 'ScriptArguments|Cal lStack':
864 return True
865 for argument in arguments:
866 if self._TypeInfo(argument.type.id).requires_v8_scope():
867 return True
868 return False
869
870 def _GenerateNativeBinding(self, idl_name, argument_count, dart_declaration, 850 def _GenerateNativeBinding(self, idl_name, argument_count, dart_declaration,
871 native_suffix, is_custom): 851 native_suffix, is_custom):
872 native_binding = '%s_%s_%s' % (self._interface.id, idl_name, native_suffix) 852 native_binding = '%s_%s_%s' % (self._interface.id, idl_name, native_suffix)
873 self._members_emitter.Emit( 853 self._members_emitter.Emit(
874 '\n' 854 '\n'
875 ' $DART_DECLARATION native "$NATIVE_BINDING";\n', 855 ' $DART_DECLARATION native "$NATIVE_BINDING";\n',
876 DART_DECLARATION=dart_declaration, NATIVE_BINDING=native_binding) 856 DART_DECLARATION=dart_declaration, NATIVE_BINDING=native_binding)
877 857
878 cpp_callback_name = '%s%s' % (idl_name, native_suffix) 858 cpp_callback_name = '%s%s' % (idl_name, native_suffix)
879 self._cpp_resolver_emitter.Emit( 859 self._cpp_resolver_emitter.Emit(
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
932 invocation_template = emitter.Format( 912 invocation_template = emitter.Format(
933 ' ExceptionCode ec = 0;\n' 913 ' ExceptionCode ec = 0;\n'
934 '$INVOCATION' 914 '$INVOCATION'
935 ' if (UNLIKELY(ec)) {\n' 915 ' if (UNLIKELY(ec)) {\n'
936 ' exception = DartDOMWrapper::exceptionCodeToDartException( ec);\n' 916 ' exception = DartDOMWrapper::exceptionCodeToDartException( ec);\n'
937 ' goto fail;\n' 917 ' goto fail;\n'
938 ' }\n', 918 ' }\n',
939 INVOCATION=invocation_template) 919 INVOCATION=invocation_template)
940 920
941 if 'ImplementedBy' in attributes: 921 if 'ImplementedBy' in attributes:
942 # FIXME: rather ugly way to solve the problem. 922 arguments.insert(0, 'receiver')
943 index = 1 if 'ScriptExecutionContext' == attributes.get('CallWith') else 0
944 arguments.insert(index, 'receiver')
945 self._cpp_impl_includes.add('"%s.h"' % attributes['ImplementedBy']) 923 self._cpp_impl_includes.add('"%s.h"' % attributes['ImplementedBy'])
946 924
925 if attributes.get('CallWith') == 'ScriptExecutionContext':
926 arguments.insert(0, 'context')
927
947 return emitter.Format(invocation_template, 928 return emitter.Format(invocation_template,
948 FUNCTION_CALL='%s(%s)' % (function_expression, ', '.join(arguments))) 929 FUNCTION_CALL='%s(%s)' % (function_expression, ', '.join(arguments)))
949 930
950 def _TypeInfo(self, type_name): 931 def _TypeInfo(self, type_name):
951 return self._system._type_registry.TypeInfo(type_name) 932 return self._system._type_registry.TypeInfo(type_name)
952 933
953 934
954 def _GenerateCPPIncludes(includes): 935 def _GenerateCPPIncludes(includes):
955 return ''.join(['#include %s\n' % include for include in sorted(includes)]) 936 return ''.join(['#include %s\n' % include for include in sorted(includes)])
956 937
(...skipping 11 matching lines...) Expand all
968 949
969 def _IsArgumentOptionalInWebCore(argument): 950 def _IsArgumentOptionalInWebCore(argument):
970 return IsOptional(argument) and not 'Callback' in argument.ext_attrs 951 return IsOptional(argument) and not 'Callback' in argument.ext_attrs
971 952
972 def _ToWebKitName(name): 953 def _ToWebKitName(name):
973 name = name[0].lower() + name[1:] 954 name = name[0].lower() + name[1:]
974 name = re.sub(r'^(hTML|uRL|jS|xML|xSLT)', lambda s: s.group(1).lower(), 955 name = re.sub(r'^(hTML|uRL|jS|xML|xSLT)', lambda s: s.group(1).lower(),
975 name) 956 name)
976 return re.sub(r'^(create|exclusive)', lambda s: 'is' + s.group(1).capitalize() , 957 return re.sub(r'^(create|exclusive)', lambda s: 'is' + s.group(1).capitalize() ,
977 name) 958 name)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698