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

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

Issue 9416062: Support generation of constructors in the most common case. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 10 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 | client/tests/client/client.status » ('j') | 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 generates Dart APIs from the IDL database.""" 6 """This module generates Dart APIs from the IDL database."""
7 7
8 import emitter 8 import emitter
9 import idlnode 9 import idlnode
10 import logging 10 import logging
(...skipping 2687 matching lines...) Expand 10 before | Expand all | Expand 10 after
2698 def StartInterface(self): 2698 def StartInterface(self):
2699 self._class_name = self._ImplClassName(self._interface.id) 2699 self._class_name = self._ImplClassName(self._interface.id)
2700 self._interface_type_info = GetIDLTypeInfoByName(self._interface.id) 2700 self._interface_type_info = GetIDLTypeInfoByName(self._interface.id)
2701 self._members_emitter = emitter.Emitter() 2701 self._members_emitter = emitter.Emitter()
2702 self._cpp_declarations_emitter = emitter.Emitter() 2702 self._cpp_declarations_emitter = emitter.Emitter()
2703 self._cpp_impl_includes = {} 2703 self._cpp_impl_includes = {}
2704 self._cpp_definitions_emitter = emitter.Emitter() 2704 self._cpp_definitions_emitter = emitter.Emitter()
2705 self._cpp_resolver_emitter = emitter.Emitter() 2705 self._cpp_resolver_emitter = emitter.Emitter()
2706 2706
2707 # Generate constructor. 2707 # Generate constructor.
2708 # FIXME: add proper support for non-custom constructors. 2708 # WebKit IDLs may define constructors with arguments. Currently this form i s not supported
2709 if ('CustomConstructor' in self._interface.ext_attrs or 2709 # (see b/1721). There is custom implementation for some of them, the rest a re just ignored
2710 'V8CustomConstructor' in self._interface.ext_attrs or 2710 # for now.
2711 self._interface.id in ['FileReader', 'WebKitCSSMatrix', 'XSLTProcessor'] ): 2711 SUPPORTED_CONSTRUCTORS_WITH_ARGS = [ 'WebKitCSSMatrix' ]
2712 UNSUPPORTED_CONSTRUCTORS_WITH_ARGS = [
2713 'EventSource',
2714 'MediaStream',
2715 'PeerConnection',
2716 'ShadowRoot',
2717 'SharedWorker',
2718 'TextTrackCue',
2719 'Worker' ]
2720 if self._IsConstructable() and self._interface.id not in UNSUPPORTED_CONSTRU CTORS_WITH_ARGS:
podivilov 2012/02/17 17:47:20 Please use early return.
2712 self._cpp_resolver_emitter.Emit( 2721 self._cpp_resolver_emitter.Emit(
2713 ' if (name == "$(INTERFACE_NAME)_constructor_Callback")\n' 2722 ' if (name == "$(INTERFACE_NAME)_constructor_Callback")\n'
2714 ' return Dart$(INTERFACE_NAME)Internal::constructorCallback;\n' , 2723 ' return Dart$(INTERFACE_NAME)Internal::constructorCallback;\n' ,
2715 INTERFACE_NAME=self._interface.id) 2724 INTERFACE_NAME=self._interface.id)
2716 self._cpp_declarations_emitter.Emit( 2725
2717 '\n' 2726 if self._interface.id not in SUPPORTED_CONSTRUCTORS_WITH_ARGS and 'Constru ctor' in self._interface.ext_attrs:
podivilov 2012/02/17 17:47:20 Early return as well: if custom, just emit declara
2718 'void constructorCallback(Dart_NativeArguments);\n') 2727 raises_dom_exceptions = 'ConstructorRaisesException' in self._interface. ext_attrs
2728 raises_dart_exceptions = raises_dom_exceptions
2729 type_info = GetIDLTypeInfo(self._interface)
2730 arguments = []
2731 parameter_definitions = ''
2732 if 'CallWith' in self._interface.ext_attrs:
2733 call_with = self._interface.ext_attrs['CallWith']
2734 if call_with == 'ScriptExecutionContext':
2735 raises_dart_exceptions = True
2736 parameter_definitions = (
2737 ' ScriptExecutionContext* context = DartUtilities::script ExecutionContext();\n'
2738 ' if (!context) {\n'
2739 ' exception = Dart_NewString("Failed to create an obj ect");\n'
2740 ' goto fail;\n'
2741 ' }\n')
2742 arguments = ['context']
2743 else:
2744 raise Exception('Unsupported CallWith=%s attribute' % call_with)
2745
2746 self._GenerateNativeCallbackBase(
2747 callback_name='constructorCallback',
2748 idl_node=self._interface,
2749 parameter_definitions=parameter_definitions,
2750 function_name='%s::create' % type_info.native_type(),
2751 arguments=arguments,
2752 idl_return_type=self._interface,
2753 raises_dart_exceptions=raises_dart_exceptions,
2754 raises_dom_exceptions=raises_dom_exceptions,
2755 needs_receiver=False)
2756 else:
2757 self._cpp_declarations_emitter.Emit(
2758 '\n'
2759 'void constructorCallback(Dart_NativeArguments);\n')
2760
2719 2761
2720 def _ImplClassName(self, interface_name): 2762 def _ImplClassName(self, interface_name):
2721 return interface_name + 'Implementation' 2763 return interface_name + 'Implementation'
2722 2764
2765 def _IsConstructable(self):
2766 # FIXME: support ConstructorTemplate.
2767 return set(['CustomConstructor', 'V8CustomConstructor', 'Constructor']) & se t(self._interface.ext_attrs)
2768
2723 def FinishInterface(self): 2769 def FinishInterface(self):
2724 base = self._BaseClassName(self._interface) 2770 base = self._BaseClassName(self._interface)
2725 self._dart_impl_emitter.Emit( 2771 self._dart_impl_emitter.Emit(
2726 self._templates.Load('dart_implementation.darttemplate'), 2772 self._templates.Load('dart_implementation.darttemplate'),
2727 CLASS=self._class_name, BASE=base, INTERFACE=self._interface.id, 2773 CLASS=self._class_name, BASE=base, INTERFACE=self._interface.id,
2728 MEMBERS=self._members_emitter.Fragments()) 2774 MEMBERS=self._members_emitter.Fragments())
2729 2775
2730 self._GenerateCppHeader() 2776 self._GenerateCppHeader()
2731 2777
2732 self._cpp_impl_emitter.Emit( 2778 self._cpp_impl_emitter.Emit(
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
3028 parameter_definitions = parameter_definitions_emitter.Fragments() 3074 parameter_definitions = parameter_definitions_emitter.Fragments()
3029 self._GenerateNativeCallback(cpp_callback_name, operation, 3075 self._GenerateNativeCallback(cpp_callback_name, operation,
3030 parameter_definitions, webcore_function_name, arguments, 3076 parameter_definitions, webcore_function_name, arguments,
3031 idl_return_type=operation.type, 3077 idl_return_type=operation.type,
3032 raises_dart_exceptions=raises_dart_exceptions, 3078 raises_dart_exceptions=raises_dart_exceptions,
3033 raises_dom_exceptions=operation.raises) 3079 raises_dom_exceptions=operation.raises)
3034 3080
3035 def _GenerateNativeCallback(self, callback_name, idl_node, 3081 def _GenerateNativeCallback(self, callback_name, idl_node,
3036 parameter_definitions, function_name, arguments, idl_return_type, 3082 parameter_definitions, function_name, arguments, idl_return_type,
3037 raises_dart_exceptions, raises_dom_exceptions): 3083 raises_dart_exceptions, raises_dom_exceptions):
3038 receiver = self._interface_type_info.receiver() 3084 self._GenerateNativeCallbackBase(
podivilov 2012/02/17 17:47:20 I think such trampolines make the code messy. Coul
3085 callback_name=callback_name,
3086 idl_node=idl_node,
3087 parameter_definitions=parameter_definitions,
3088 function_name=self._interface_type_info.receiver() + function_name,
3089 arguments=arguments,
3090 idl_return_type=idl_return_type,
3091 raises_dart_exceptions=raises_dart_exceptions,
3092 raises_dom_exceptions=raises_dom_exceptions,
3093 needs_receiver=True)
3094
3095 def _GenerateNativeCallbackBase(self, callback_name, idl_node,
3096 parameter_definitions, function_name, arguments, idl_return_type,
3097 raises_dart_exceptions, raises_dom_exceptions, needs_receiver):
3039 if raises_dom_exceptions: 3098 if raises_dom_exceptions:
3040 arguments.append('ec') 3099 arguments.append('ec')
3041 callback = '%s%s(%s)' % (receiver, function_name, ', '.join(arguments)) 3100 callback = '%s(%s)' % (function_name, ', '.join(arguments))
podivilov 2012/02/17 17:47:20 Could you please replace "function_name, arguments
3042 3101
3043 nested_templates = [] 3102 nested_templates = []
3044 if idl_return_type and idl_return_type.id != 'void': 3103 if idl_return_type and idl_return_type.id != 'void':
3045 return_type_info = GetIDLTypeInfo(idl_return_type) 3104 return_type_info = GetIDLTypeInfo(idl_return_type)
3046 conversion_cast = return_type_info.conversion_cast('$BODY') 3105 conversion_cast = return_type_info.conversion_cast('$BODY')
3047 if isinstance(return_type_info, SVGTearOffIDLTypeInfo): 3106 if isinstance(return_type_info, SVGTearOffIDLTypeInfo):
3048 svg_primitive_types = ['SVGAngle', 'SVGLength', 'SVGMatrix', 3107 svg_primitive_types = ['SVGAngle', 'SVGLength', 'SVGMatrix',
3049 'SVGNumber', 'SVGPoint', 'SVGRect', 'SVGTransform'] 3108 'SVGNumber', 'SVGPoint', 'SVGRect', 'SVGTransform']
3050 conversion_cast = '%s::create($BODY)' 3109 conversion_cast = '%s::create($BODY)'
3051 if self._interface.id.startswith('SVGAnimated'): 3110 if self._interface.id.startswith('SVGAnimated'):
(...skipping 25 matching lines...) Expand all
3077 nested_templates.append( 3136 nested_templates.append(
3078 ' ExceptionCode ec = 0;\n' 3137 ' ExceptionCode ec = 0;\n'
3079 '$BODY' 3138 '$BODY'
3080 ' if (UNLIKELY(ec)) {\n' 3139 ' if (UNLIKELY(ec)) {\n'
3081 ' exception = DartDOMWrapper::exceptionCodeToDartException( ec);\n' 3140 ' exception = DartDOMWrapper::exceptionCodeToDartException( ec);\n'
3082 ' goto fail;\n' 3141 ' goto fail;\n'
3083 ' }\n') 3142 ' }\n')
3084 3143
3085 nested_templates.append( 3144 nested_templates.append(
3086 ' {\n' 3145 ' {\n'
3087 ' $WEBCORE_CLASS_NAME* receiver = DartDOMWrapper::receiver< $WEBC ORE_CLASS_NAME >(args);\n'
3088 '$PARAMETER_DEFINITIONS' 3146 '$PARAMETER_DEFINITIONS'
3089 '\n'
3090 '$BODY' 3147 '$BODY'
3091 ' return;\n' 3148 ' return;\n'
3092 ' }\n') 3149 ' }\n')
3093 3150
3094 if raises_dart_exceptions: 3151 if raises_dart_exceptions:
3095 nested_templates.append( 3152 nested_templates.append(
3096 ' Dart_Handle exception;\n' 3153 ' Dart_Handle exception;\n'
3097 '$BODY' 3154 '$BODY'
3098 '\n' 3155 '\n'
3099 'fail:\n' 3156 'fail:\n'
3100 ' Dart_ThrowException(exception);\n' 3157 ' Dart_ThrowException(exception);\n'
3101 ' ASSERT_NOT_REACHED();\n') 3158 ' ASSERT_NOT_REACHED();\n')
3102 3159
3103 nested_templates.append( 3160 nested_templates.append(
3104 '\n' 3161 '\n'
3105 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n' 3162 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n'
3106 '{\n' 3163 '{\n'
3107 ' DartApiScope dartApiScope;\n' 3164 ' DartApiScope dartApiScope;\n'
3108 '$BODY' 3165 '$BODY'
3109 '}\n') 3166 '}\n')
3110 3167
3111 template_parameters = { 3168 template_parameters = {
3112 'CALLBACK_NAME': callback_name, 3169 'CALLBACK_NAME': callback_name,
3113 'WEBCORE_CLASS_NAME': self._interface_type_info.native_type(), 3170 'WEBCORE_CLASS_NAME': self._interface_type_info.native_type(),
3114 'PARAMETER_DEFINITIONS': parameter_definitions, 3171 'PARAMETER_DEFINITIONS': parameter_definitions,
3115 } 3172 }
3173 if needs_receiver:
3174 template_parameters['PARAMETER_DEFINITIONS'] = emitter.Format(
3175 ' $WEBCORE_CLASS_NAME* receiver = DartDOMWrapper::receiver< $WE BCORE_CLASS_NAME >(args);\n'
3176 ' $PARAMETER_DEFINITIONS\n',
3177 **template_parameters)
3178
3116 for template in nested_templates: 3179 for template in nested_templates:
3117 template_parameters['BODY'] = callback 3180 template_parameters['BODY'] = callback
3118 callback_emitter = emitter.Emitter() 3181 callback = emitter.Format(template, **template_parameters)
3119 callback_emitter.Emit(template, **template_parameters)
3120 callback = ''.join(callback_emitter.Fragments())
3121 3182
3122 self._cpp_definitions_emitter.Emit(callback) 3183 self._cpp_definitions_emitter.Emit(callback)
3123 3184
3124 def _GenerateParameterAdapter(self, emitter, idl_argument, index): 3185 def _GenerateParameterAdapter(self, emitter, idl_argument, index):
3125 type_info = GetIDLTypeInfo(idl_argument.type) 3186 type_info = GetIDLTypeInfo(idl_argument.type)
3126 (adapter_type, include_name) = type_info.parameter_adapter_info() 3187 (adapter_type, include_name) = type_info.parameter_adapter_info()
3127 if include_name: 3188 if include_name:
3128 self._cpp_impl_includes[include_name] = 1 3189 self._cpp_impl_includes[include_name] = 1
3129 emitter.Emit( 3190 emitter.Emit(
3130 '\n' 3191 '\n'
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
3166 namespace = 'HTMLNames' 3227 namespace = 'HTMLNames'
3167 svg_exceptions = ['class', 'id', 'onabort', 'onclick', 'onerror', 'onload', 3228 svg_exceptions = ['class', 'id', 'onabort', 'onclick', 'onerror', 'onload',
3168 'onmousedown', 'onmousemove', 'onmouseout', 'onmouseover', 3229 'onmousedown', 'onmousemove', 'onmouseout', 'onmouseover',
3169 'onmouseup', 'onresize', 'onscroll', 'onunload'] 3230 'onmouseup', 'onresize', 'onscroll', 'onunload']
3170 if self._interface.id.startswith('SVG') and not attr.id in svg_exceptions: 3231 if self._interface.id.startswith('SVG') and not attr.id in svg_exceptions:
3171 namespace = 'SVGNames' 3232 namespace = 'SVGNames'
3172 self._cpp_impl_includes[namespace] = 1 3233 self._cpp_impl_includes[namespace] = 1
3173 3234
3174 attribute_name = attr.ext_attrs['Reflect'] or attr.id.lower() 3235 attribute_name = attr.ext_attrs['Reflect'] or attr.id.lower()
3175 return 'WebCore::%s::%sAttr' % (namespace, attribute_name) 3236 return 'WebCore::%s::%sAttr' % (namespace, attribute_name)
OLDNEW
« no previous file with comments | « no previous file | client/tests/client/client.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698