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

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

Issue 9695015: Provide DartDomNameOfAttribute (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fix native Created 8 years, 9 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
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
11 import systemwrapping 11 import systemwrapping
12 from generator import * 12 from generator import *
13 from systembase import * 13 from systembase import *
14 14
15
16 _cpp_keywords = set(['default', 'operator'])
17
18 def CppSafeName(name):
19 """Returns version of name safe for use in cpp code, i.e. not a keyword."""
20 if name in _cpp_keywords:
21 return '_' + name
22 return name
23
15 class NativeImplementationSystem(System): 24 class NativeImplementationSystem(System):
16 25
17 def __init__(self, templates, database, emitters, auxiliary_dir, output_dir): 26 def __init__(self, templates, database, emitters, auxiliary_dir, output_dir):
18 super(NativeImplementationSystem, self).__init__( 27 super(NativeImplementationSystem, self).__init__(
19 templates, database, emitters, output_dir) 28 templates, database, emitters, output_dir)
20 29
21 self._auxiliary_dir = auxiliary_dir 30 self._auxiliary_dir = auxiliary_dir
22 self._dom_public_files = [] 31 self._dom_public_files = []
23 self._dom_impl_files = [] 32 self._dom_impl_files = []
24 self._cpp_header_files = [] 33 self._cpp_header_files = []
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 ' if (!context) {\n' 282 ' if (!context) {\n'
274 ' exception = Dart_NewString("Failed to create an object" );\n' 283 ' exception = Dart_NewString("Failed to create an object" );\n'
275 ' goto fail;\n' 284 ' goto fail;\n'
276 ' }\n') 285 ' }\n')
277 arguments.append('context') 286 arguments.append('context')
278 else: 287 else:
279 raise Exception('Unsupported CallWith=%s attribute' % call_with) 288 raise Exception('Unsupported CallWith=%s attribute' % call_with)
280 289
281 # Process constructor arguments. 290 # Process constructor arguments.
282 for (i, arg) in enumerate(constructor_info.idl_args): 291 for (i, arg) in enumerate(constructor_info.idl_args):
283 self._GenerateParameterAdapter(parameter_definitions_emitter, arg, i - 1) 292 cpp_arg_name = CppSafeName(arg.id)
284 arguments.append(arg.id) 293 self._GenerateParameterAdapter(
294 parameter_definitions_emitter, arg, cpp_arg_name, i - 1)
295 arguments.append(cpp_arg_name)
285 296
286 function_expression = '%s::%s' % (self._interface_type_info.native_type(), c reate_function) 297 function_expression = '%s::%s' % (self._interface_type_info.native_type(), c reate_function)
287 invocation = self._GenerateWebCoreInvocation(function_expression, arguments, 298 invocation = self._GenerateWebCoreInvocation(function_expression, arguments,
288 self._interface.id, self._interface.ext_attrs, raises_dom_exceptions) 299 self._interface.id, self._interface.ext_attrs, raises_dom_exceptions)
289 self._GenerateNativeCallback(callback_name='constructorCallback', 300 self._GenerateNativeCallback(callback_name='constructorCallback',
290 parameter_definitions=parameter_definitions_emitter.Fragments(), 301 parameter_definitions=parameter_definitions_emitter.Fragments(),
291 needs_receiver=False, invocation=invocation, 302 needs_receiver=False, invocation=invocation,
292 raises_exceptions=raises_dart_exceptions) 303 raises_exceptions=raises_dart_exceptions)
293 304
294 def _ImplClassName(self, interface_name): 305 def _ImplClassName(self, interface_name):
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 getter and set(['Custom', 'CustomGetter']) & set(getter.ext_attrs)): 377 getter and set(['Custom', 'CustomGetter']) & set(getter.ext_attrs)):
367 return 378 return
368 379
369 if getter: 380 if getter:
370 self._AddGetter(getter) 381 self._AddGetter(getter)
371 if setter: 382 if setter:
372 self._AddSetter(setter) 383 self._AddSetter(setter)
373 384
374 def _AddGetter(self, attr): 385 def _AddGetter(self, attr):
375 type_info = GetIDLTypeInfo(attr.type.id) 386 type_info = GetIDLTypeInfo(attr.type.id)
376 dart_declaration = '%s get %s()' % (type_info.dart_type(), attr.id) 387 dart_declaration = '%s get %s()' % (
388 type_info.dart_type(), DartDomNameOfAttribute(attr))
377 is_custom = 'Custom' in attr.ext_attrs or 'CustomGetter' in attr.ext_attrs 389 is_custom = 'Custom' in attr.ext_attrs or 'CustomGetter' in attr.ext_attrs
378 cpp_callback_name = self._GenerateNativeBinding(attr.id, 1, 390 cpp_callback_name = self._GenerateNativeBinding(attr.id, 1,
379 dart_declaration, 'Getter', is_custom) 391 dart_declaration, 'Getter', is_custom)
380 if is_custom: 392 if is_custom:
381 return 393 return
382 394
383 arguments = [] 395 arguments = []
384 if 'Reflect' in attr.ext_attrs: 396 if 'Reflect' in attr.ext_attrs:
385 webcore_function_name = GetIDLTypeInfo(attr.type.id).webcore_getter_name() 397 webcore_function_name = GetIDLTypeInfo(attr.type.id).webcore_getter_name()
386 if 'URL' in attr.ext_attrs: 398 if 'URL' in attr.ext_attrs:
387 if 'NonEmpty' in attr.ext_attrs: 399 if 'NonEmpty' in attr.ext_attrs:
388 webcore_function_name = 'getNonEmptyURLAttribute' 400 webcore_function_name = 'getNonEmptyURLAttribute'
389 else: 401 else:
390 webcore_function_name = 'getURLAttribute' 402 webcore_function_name = 'getURLAttribute'
391 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr)) 403 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr))
392 else: 404 else:
393 if attr.id == 'operator': 405 webcore_function_name = CppSafeName(attr.id)
394 webcore_function_name = '_operator' 406 if attr.id == 'target' and attr.type.id == 'SVGAnimatedString':
395 elif attr.id == 'target' and attr.type.id == 'SVGAnimatedString':
396 webcore_function_name = 'svgTarget' 407 webcore_function_name = 'svgTarget'
397 else: 408 else:
398 webcore_function_name = re.sub(r'^(HTML|URL|JS|XML|XSLT|\w)', 409 webcore_function_name = re.sub(r'^(HTML|URL|JS|XML|XSLT|\w)',
399 lambda s: s.group(1).lower(), 410 lambda s: s.group(1).lower(),
400 attr.id) 411 webcore_function_name)
401 webcore_function_name = re.sub(r'^(create|exclusive)', 412 webcore_function_name = re.sub(r'^(create|exclusive)',
402 lambda s: 'is' + s.group(1).capitalize(), 413 lambda s: 'is' + s.group(1).capitalize(),
403 webcore_function_name) 414 webcore_function_name)
404 if attr.type.id.startswith('SVGAnimated'): 415 if attr.type.id.startswith('SVGAnimated'):
405 webcore_function_name += 'Animated' 416 webcore_function_name += 'Animated'
406 417
407 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr) 418 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr)
408 invocation = self._GenerateWebCoreInvocation(function_expression, 419 invocation = self._GenerateWebCoreInvocation(function_expression,
409 arguments, attr.type.id, attr.ext_attrs, attr.get_raises) 420 arguments, attr.type.id, attr.ext_attrs, attr.get_raises)
410 self._GenerateNativeCallback(cpp_callback_name, '', True, invocation, 421 self._GenerateNativeCallback(cpp_callback_name, '', True, invocation,
411 raises_exceptions=attr.get_raises) 422 raises_exceptions=attr.get_raises)
412 423
413 def _AddSetter(self, attr): 424 def _AddSetter(self, attr):
414 type_info = GetIDLTypeInfo(attr.type.id) 425 type_info = GetIDLTypeInfo(attr.type.id)
415 dart_declaration = 'void set %s(%s)' % (attr.id, type_info.dart_type()) 426 dart_declaration = 'void set %s(%s)' % (
427 DartDomNameOfAttribute(attr), type_info.dart_type())
416 is_custom = set(['Custom', 'CustomSetter', 'V8CustomSetter']) & set(attr.ext _attrs) 428 is_custom = set(['Custom', 'CustomSetter', 'V8CustomSetter']) & set(attr.ext _attrs)
417 cpp_callback_name = self._GenerateNativeBinding(attr.id, 2, 429 cpp_callback_name = self._GenerateNativeBinding(attr.id, 2,
418 dart_declaration, 'Setter', is_custom) 430 dart_declaration, 'Setter', is_custom)
419 if is_custom: 431 if is_custom:
420 return 432 return
421 433
422 arguments = [] 434 arguments = []
423 if 'Reflect' in attr.ext_attrs: 435 if 'Reflect' in attr.ext_attrs:
424 webcore_function_name = GetIDLTypeInfo(attr.type.id).webcore_setter_name() 436 webcore_function_name = GetIDLTypeInfo(attr.type.id).webcore_setter_name()
425 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr)) 437 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr))
426 else: 438 else:
427 webcore_function_name = re.sub(r'^(xml(?=[A-Z])|\w)', 439 webcore_function_name = re.sub(r'^(xml(?=[A-Z])|\w)',
428 lambda s: s.group(1).upper(), 440 lambda s: s.group(1).upper(),
429 attr.id) 441 attr.id)
430 webcore_function_name = 'set%s' % webcore_function_name 442 webcore_function_name = 'set%s' % webcore_function_name
431 if attr.type.id.startswith('SVGAnimated'): 443 if attr.type.id.startswith('SVGAnimated'):
432 webcore_function_name += 'Animated' 444 webcore_function_name += 'Animated'
433 445
434 arguments.append(attr.id) 446 cpp_arg_name = CppSafeName(attr.id)
sra1 2012/03/13 02:41:27 This is how the attribute called default gets conv
447 arguments.append(cpp_arg_name)
435 448
436 parameter_definitions_emitter = emitter.Emitter() 449 parameter_definitions_emitter = emitter.Emitter()
437 self._GenerateParameterAdapter(parameter_definitions_emitter, attr, 0) 450 self._GenerateParameterAdapter(
451 parameter_definitions_emitter, attr, cpp_arg_name, 0)
438 parameter_definitions = parameter_definitions_emitter.Fragments() 452 parameter_definitions = parameter_definitions_emitter.Fragments()
439 453
440 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr) 454 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi on_name, attr)
441 invocation = self._GenerateWebCoreInvocation(function_expression, 455 invocation = self._GenerateWebCoreInvocation(function_expression,
442 arguments, 'void', attr.ext_attrs, attr.set_raises) 456 arguments, 'void', attr.ext_attrs, attr.set_raises)
443 457
444 self._GenerateNativeCallback(cpp_callback_name, parameter_definitions, 458 self._GenerateNativeCallback(cpp_callback_name, parameter_definitions,
445 True, invocation, raises_exceptions=True) 459 True, invocation, raises_exceptions=True)
446 460
447 def _HasNativeIndexGetter(self, interface): 461 def _HasNativeIndexGetter(self, interface):
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 ' if (!scriptArguments)\n' 578 ' if (!scriptArguments)\n'
565 ' goto fail;\n' 579 ' goto fail;\n'
566 ' RefPtr<ScriptCallStack> scriptCallStack(DartUtilities::crea teScriptCallStack());\n' 580 ' RefPtr<ScriptCallStack> scriptCallStack(DartUtilities::crea teScriptCallStack());\n'
567 ' if (!scriptCallStack->size())\n' 581 ' if (!scriptCallStack->size())\n'
568 ' return;\n', 582 ' return;\n',
569 INDEX=len(operation.arguments)) 583 INDEX=len(operation.arguments))
570 arguments.extend(['scriptArguments', 'scriptCallStack']) 584 arguments.extend(['scriptArguments', 'scriptCallStack'])
571 585
572 # Process Dart arguments. 586 # Process Dart arguments.
573 for (i, argument) in enumerate(operation.arguments): 587 for (i, argument) in enumerate(operation.arguments):
574 if i == len(operation.arguments) - 1 and self._interface.id == 'Console' a nd argument.id == 'arg': 588 if (i == len(operation.arguments) - 1 and
589 self._interface.id == 'Console' and
590 argument.id == 'arg'):
575 # FIXME: we are skipping last argument here because it was added in 591 # FIXME: we are skipping last argument here because it was added in
576 # supplemental dart.idl. Cleanup dart.idl and remove this check. 592 # supplemental dart.idl. Cleanup dart.idl and remove this check.
577 break 593 break
578 self._GenerateParameterAdapter(parameter_definitions_emitter, argument, i) 594 cpp_arg_name = CppSafeName(argument.id)
579 arguments.append(argument.id) 595 self._GenerateParameterAdapter(
596 parameter_definitions_emitter, argument, cpp_arg_name, i)
597 arguments.append(cpp_arg_name)
580 598
581 if operation.id in ['addEventListener', 'removeEventListener']: 599 if operation.id in ['addEventListener', 'removeEventListener']:
582 # addEventListener's and removeEventListener's last argument is marked 600 # addEventListener's and removeEventListener's last argument is marked
583 # as optional in idl, but is not optional in webcore implementation. 601 # as optional in idl, but is not optional in webcore implementation.
584 if len(operation.arguments) == 2: 602 if len(operation.arguments) == 2:
585 arguments.append('false') 603 arguments.append('false')
586 604
587 if self._interface.id == 'CSSStyleDeclaration' and operation.id == 'setPrope rty': 605 if self._interface.id == 'CSSStyleDeclaration' and operation.id == 'setPrope rty':
588 # CSSStyleDeclaration.setProperty priority parameter is optional in Dart 606 # CSSStyleDeclaration.setProperty priority parameter is optional in Dart
589 # idl, but is not optional in webcore implementation. 607 # idl, but is not optional in webcore implementation.
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 self._cpp_definitions_emitter.Emit( 651 self._cpp_definitions_emitter.Emit(
634 '\n' 652 '\n'
635 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n' 653 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n'
636 '{\n' 654 '{\n'
637 ' DartApiScope dartApiScope;\n' 655 ' DartApiScope dartApiScope;\n'
638 '$BODY' 656 '$BODY'
639 '}\n', 657 '}\n',
640 CALLBACK_NAME=callback_name, 658 CALLBACK_NAME=callback_name,
641 BODY=body) 659 BODY=body)
642 660
643 def _GenerateParameterAdapter(self, emitter, idl_argument, index): 661 def _GenerateParameterAdapter(self, emitter, idl_node, name, index):
644 type_info = GetIDLTypeInfo(idl_argument.type.id) 662 """idl_node is IDLArgument or IDLAttribute."""
663 type_info = GetIDLTypeInfo(idl_node.type.id)
645 (adapter_type, include_name) = type_info.parameter_adapter_info() 664 (adapter_type, include_name) = type_info.parameter_adapter_info()
646 if include_name: 665 if include_name:
647 self._cpp_impl_includes.add(include_name) 666 self._cpp_impl_includes.add(include_name)
648 flags = '' 667 flags = ''
649 if (idl_argument.ext_attrs.get('Optional') == 'DefaultIsNullString' or 668 if (idl_node.ext_attrs.get('Optional') == 'DefaultIsNullString' or
650 'RequiredCppParameter' in idl_argument.ext_attrs): 669 'RequiredCppParameter' in idl_node.ext_attrs):
651 flags = ', DartUtilities::ConvertNullToDefaultValue' 670 flags = ', DartUtilities::ConvertNullToDefaultValue'
652 emitter.Emit( 671 emitter.Emit(
653 '\n' 672 '\n'
654 ' const $ADAPTER_TYPE $NAME(Dart_GetNativeArgument(args, $INDEX)$ FLAGS);\n' 673 ' const $ADAPTER_TYPE $NAME(Dart_GetNativeArgument(args, $INDEX)$ FLAGS);\n'
655 ' if (!$NAME.conversionSuccessful()) {\n' 674 ' if (!$NAME.conversionSuccessful()) {\n'
656 ' exception = $NAME.exception();\n' 675 ' exception = $NAME.exception();\n'
657 ' goto fail;\n' 676 ' goto fail;\n'
658 ' }\n', 677 ' }\n',
659 ADAPTER_TYPE=adapter_type, 678 ADAPTER_TYPE=adapter_type,
660 NAME=idl_argument.id, 679 NAME=name,
661 INDEX=index + 1, 680 INDEX=index + 1,
662 FLAGS=flags) 681 FLAGS=flags)
663 682
664 def _GenerateNativeBinding(self, idl_name, argument_count, dart_declaration, 683 def _GenerateNativeBinding(self, idl_name, argument_count, dart_declaration,
665 native_suffix, is_custom): 684 native_suffix, is_custom):
666 native_binding = '%s_%s_%s' % (self._interface.id, idl_name, native_suffix) 685 native_binding = '%s_%s_%s' % (self._interface.id, idl_name, native_suffix)
667 self._members_emitter.Emit( 686 self._members_emitter.Emit(
668 '\n' 687 '\n'
669 ' $DART_DECLARATION native "$NATIVE_BINDING";\n', 688 ' $DART_DECLARATION native "$NATIVE_BINDING";\n',
670 DART_DECLARATION=dart_declaration, NATIVE_BINDING=native_binding) 689 DART_DECLARATION=dart_declaration, NATIVE_BINDING=native_binding)
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 773
755 if 'ImplementedBy' in attributes: 774 if 'ImplementedBy' in attributes:
756 arguments.insert(0, 'receiver') 775 arguments.insert(0, 'receiver')
757 self._cpp_impl_includes.add(attributes['ImplementedBy']) 776 self._cpp_impl_includes.add(attributes['ImplementedBy'])
758 777
759 return emitter.Format(invocation_template, 778 return emitter.Format(invocation_template,
760 FUNCTION_CALL='%s(%s)' % (function_expression, ', '.join(arguments))) 779 FUNCTION_CALL='%s(%s)' % (function_expression, ', '.join(arguments)))
761 780
762 def _GenerateCPPIncludes(includes): 781 def _GenerateCPPIncludes(includes):
763 return ''.join(['#include "%s.h"\n' % include for include in includes]) 782 return ''.join(['#include "%s.h"\n' % include for include in includes])
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698