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 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 2686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2697 | 2697 |
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 self._GenerateConstructors() |
2708 # FIXME: add proper support for non-custom constructors. | 2708 |
2709 if ('CustomConstructor' in self._interface.ext_attrs or | 2709 def _GenerateConstructors(self): |
2710 'V8CustomConstructor' in self._interface.ext_attrs or | 2710 # WebKit IDLs may define constructors with arguments. Currently this form i
s not supported |
2711 self._interface.id in ['FileReader', 'WebKitCSSMatrix', 'XSLTProcessor']
): | 2711 # (see b/1721). There is custom implementation for some of them, the rest a
re just ignored |
2712 self._cpp_resolver_emitter.Emit( | 2712 # for now. |
2713 ' if (name == "$(INTERFACE_NAME)_constructor_Callback")\n' | 2713 SUPPORTED_CONSTRUCTORS_WITH_ARGS = [ 'WebKitCSSMatrix' ] |
2714 ' return Dart$(INTERFACE_NAME)Internal::constructorCallback;\n'
, | 2714 UNSUPPORTED_CONSTRUCTORS_WITH_ARGS = [ |
2715 INTERFACE_NAME=self._interface.id) | 2715 'EventSource', |
| 2716 'MediaStream', |
| 2717 'PeerConnection', |
| 2718 'ShadowRoot', |
| 2719 'SharedWorker', |
| 2720 'TextTrackCue', |
| 2721 'Worker' ] |
| 2722 if not self._IsConstructable() or self._interface.id in UNSUPPORTED_CONSTRUC
TORS_WITH_ARGS: |
| 2723 return |
| 2724 |
| 2725 # TODO(antonm): currently we don't have information about number of argument
s expected by |
| 2726 # the constructor, so name only dispatch. |
| 2727 self._cpp_resolver_emitter.Emit( |
| 2728 ' if (name == "$(INTERFACE_NAME)_constructor_Callback")\n' |
| 2729 ' return Dart$(INTERFACE_NAME)Internal::constructorCallback;\n', |
| 2730 INTERFACE_NAME=self._interface.id) |
| 2731 |
| 2732 |
| 2733 if self._interface.id in SUPPORTED_CONSTRUCTORS_WITH_ARGS or 'Constructor' n
ot in self._interface.ext_attrs: |
| 2734 # We have a custom implementation for it. |
2716 self._cpp_declarations_emitter.Emit( | 2735 self._cpp_declarations_emitter.Emit( |
2717 '\n' | 2736 '\n' |
2718 'void constructorCallback(Dart_NativeArguments);\n') | 2737 'void constructorCallback(Dart_NativeArguments);\n') |
| 2738 return |
| 2739 |
| 2740 raises_dom_exceptions = 'ConstructorRaisesException' in self._interface.ext_
attrs |
| 2741 raises_dart_exceptions = raises_dom_exceptions |
| 2742 type_info = GetIDLTypeInfo(self._interface) |
| 2743 arguments = [] |
| 2744 parameter_definitions = '' |
| 2745 if 'CallWith' in self._interface.ext_attrs: |
| 2746 call_with = self._interface.ext_attrs['CallWith'] |
| 2747 if call_with == 'ScriptExecutionContext': |
| 2748 raises_dart_exceptions = True |
| 2749 parameter_definitions = ( |
| 2750 ' ScriptExecutionContext* context = DartUtilities::scriptExec
utionContext();\n' |
| 2751 ' if (!context) {\n' |
| 2752 ' exception = Dart_NewString("Failed to create an object"
);\n' |
| 2753 ' goto fail;\n' |
| 2754 ' }\n') |
| 2755 arguments = ['context'] |
| 2756 else: |
| 2757 raise Exception('Unsupported CallWith=%s attribute' % call_with) |
| 2758 |
| 2759 self._GenerateNativeCallback( |
| 2760 callback_name='constructorCallback', |
| 2761 idl_node=self._interface, |
| 2762 parameter_definitions=parameter_definitions, |
| 2763 needs_receiver=False, function_name='%s::create' % type_info.native_type
(), |
| 2764 arguments=arguments, |
| 2765 idl_return_type=self._interface, |
| 2766 raises_dart_exceptions=raises_dart_exceptions, |
| 2767 raises_dom_exceptions=raises_dom_exceptions) |
| 2768 |
2719 | 2769 |
2720 def _ImplClassName(self, interface_name): | 2770 def _ImplClassName(self, interface_name): |
2721 return interface_name + 'Implementation' | 2771 return interface_name + 'Implementation' |
2722 | 2772 |
| 2773 def _IsConstructable(self): |
| 2774 # FIXME: support ConstructorTemplate. |
| 2775 return set(['CustomConstructor', 'V8CustomConstructor', 'Constructor']) & se
t(self._interface.ext_attrs) |
| 2776 |
2723 def FinishInterface(self): | 2777 def FinishInterface(self): |
2724 base = self._BaseClassName(self._interface) | 2778 base = self._BaseClassName(self._interface) |
2725 self._dart_impl_emitter.Emit( | 2779 self._dart_impl_emitter.Emit( |
2726 self._templates.Load('dart_implementation.darttemplate'), | 2780 self._templates.Load('dart_implementation.darttemplate'), |
2727 CLASS=self._class_name, BASE=base, INTERFACE=self._interface.id, | 2781 CLASS=self._class_name, BASE=base, INTERFACE=self._interface.id, |
2728 MEMBERS=self._members_emitter.Fragments()) | 2782 MEMBERS=self._members_emitter.Fragments()) |
2729 | 2783 |
2730 self._GenerateCppHeader() | 2784 self._GenerateCppHeader() |
2731 | 2785 |
2732 self._cpp_impl_emitter.Emit( | 2786 self._cpp_impl_emitter.Emit( |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2825 webcore_function_name = re.sub(r'^(HTML|URL|JS|XML|XSLT|\w)', | 2879 webcore_function_name = re.sub(r'^(HTML|URL|JS|XML|XSLT|\w)', |
2826 lambda s: s.group(1).lower(), | 2880 lambda s: s.group(1).lower(), |
2827 attr.id) | 2881 attr.id) |
2828 webcore_function_name = re.sub(r'^(create|exclusive)', | 2882 webcore_function_name = re.sub(r'^(create|exclusive)', |
2829 lambda s: 'is' + s.group(1).capitalize(), | 2883 lambda s: 'is' + s.group(1).capitalize(), |
2830 webcore_function_name) | 2884 webcore_function_name) |
2831 if attr.type.id.startswith('SVGAnimated'): | 2885 if attr.type.id.startswith('SVGAnimated'): |
2832 webcore_function_name += 'Animated' | 2886 webcore_function_name += 'Animated' |
2833 | 2887 |
2834 self._GenerateNativeCallback(cpp_callback_name, attr, '', | 2888 self._GenerateNativeCallback(cpp_callback_name, attr, '', |
2835 webcore_function_name, arguments, idl_return_type=attr.type, | 2889 True, webcore_function_name, arguments, idl_return_type=attr.type, |
2836 raises_dart_exceptions=attr.get_raises, | 2890 raises_dart_exceptions=attr.get_raises, |
2837 raises_dom_exceptions=attr.get_raises) | 2891 raises_dom_exceptions=attr.get_raises) |
2838 | 2892 |
2839 def _AddSetter(self, attr): | 2893 def _AddSetter(self, attr): |
2840 dart_declaration = 'void set %s(%s)' % (attr.id, attr.type.id) | 2894 dart_declaration = 'void set %s(%s)' % (attr.id, attr.type.id) |
2841 is_custom = set(['Custom', 'CustomSetter', 'V8CustomSetter']) & set(attr.ext
_attrs) | 2895 is_custom = set(['Custom', 'CustomSetter', 'V8CustomSetter']) & set(attr.ext
_attrs) |
2842 cpp_callback_name = self._GenerateNativeBinding(attr.id, 2, | 2896 cpp_callback_name = self._GenerateNativeBinding(attr.id, 2, |
2843 dart_declaration, 'Setter', is_custom) | 2897 dart_declaration, 'Setter', is_custom) |
2844 if is_custom: | 2898 if is_custom: |
2845 return | 2899 return |
2846 | 2900 |
2847 arguments = [] | 2901 arguments = [] |
2848 if 'Reflect' in attr.ext_attrs: | 2902 if 'Reflect' in attr.ext_attrs: |
2849 webcore_function_name = GetIDLTypeInfo(attr.type).webcore_setter_name() | 2903 webcore_function_name = GetIDLTypeInfo(attr.type).webcore_setter_name() |
2850 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr)) | 2904 arguments.append(self._GenerateWebCoreReflectionAttributeName(attr)) |
2851 else: | 2905 else: |
2852 webcore_function_name = re.sub(r'^(xml(?=[A-Z])|\w)', | 2906 webcore_function_name = re.sub(r'^(xml(?=[A-Z])|\w)', |
2853 lambda s: s.group(1).upper(), | 2907 lambda s: s.group(1).upper(), |
2854 attr.id) | 2908 attr.id) |
2855 webcore_function_name = 'set%s' % webcore_function_name | 2909 webcore_function_name = 'set%s' % webcore_function_name |
2856 if attr.type.id.startswith('SVGAnimated'): | 2910 if attr.type.id.startswith('SVGAnimated'): |
2857 webcore_function_name += 'Animated' | 2911 webcore_function_name += 'Animated' |
2858 | 2912 |
2859 arguments.append(attr.id) | 2913 arguments.append(attr.id) |
2860 parameter_definitions_emitter = emitter.Emitter() | 2914 parameter_definitions_emitter = emitter.Emitter() |
2861 self._GenerateParameterAdapter(parameter_definitions_emitter, attr, 0) | 2915 self._GenerateParameterAdapter(parameter_definitions_emitter, attr, 0) |
2862 parameter_definitions = parameter_definitions_emitter.Fragments() | 2916 parameter_definitions = parameter_definitions_emitter.Fragments() |
2863 self._GenerateNativeCallback(cpp_callback_name, attr, parameter_definitions, | 2917 self._GenerateNativeCallback(cpp_callback_name, attr, parameter_definitions, |
2864 webcore_function_name, arguments, idl_return_type=None, | 2918 True, webcore_function_name, arguments, idl_return_type=None, |
2865 raises_dart_exceptions=True, | 2919 raises_dart_exceptions=True, |
2866 raises_dom_exceptions=attr.set_raises) | 2920 raises_dom_exceptions=attr.set_raises) |
2867 | 2921 |
2868 def _HasNativeIndexGetter(self, interface): | 2922 def _HasNativeIndexGetter(self, interface): |
2869 return ('CustomIndexedGetter' in interface.ext_attrs or | 2923 return ('CustomIndexedGetter' in interface.ext_attrs or |
2870 'NumericIndexedGetter' in interface.ext_attrs) | 2924 'NumericIndexedGetter' in interface.ext_attrs) |
2871 | 2925 |
2872 def _EmitNativeIndexGetter(self, interface, element_type): | 2926 def _EmitNativeIndexGetter(self, interface, element_type): |
2873 dart_declaration = '%s operator[](int index)' % element_type | 2927 dart_declaration = '%s operator[](int index)' % element_type |
2874 self._GenerateNativeBinding('numericIndexGetter', 2, dart_declaration, | 2928 self._GenerateNativeBinding('numericIndexGetter', 2, dart_declaration, |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3019 if self._interface.id == 'CSSStyleDeclaration' and operation.id == 'setPrope
rty': | 3073 if self._interface.id == 'CSSStyleDeclaration' and operation.id == 'setPrope
rty': |
3020 # CSSStyleDeclaration.setProperty priority parameter is optional in Dart | 3074 # CSSStyleDeclaration.setProperty priority parameter is optional in Dart |
3021 # idl, but is not optional in webcore implementation. | 3075 # idl, but is not optional in webcore implementation. |
3022 if len(operation.arguments) == 2: | 3076 if len(operation.arguments) == 2: |
3023 arguments.append('String()') | 3077 arguments.append('String()') |
3024 | 3078 |
3025 if 'NeedsUserGestureCheck' in operation.ext_attrs: | 3079 if 'NeedsUserGestureCheck' in operation.ext_attrs: |
3026 arguments.extend('DartUtilities::processingUserGesture') | 3080 arguments.extend('DartUtilities::processingUserGesture') |
3027 | 3081 |
3028 parameter_definitions = parameter_definitions_emitter.Fragments() | 3082 parameter_definitions = parameter_definitions_emitter.Fragments() |
3029 self._GenerateNativeCallback(cpp_callback_name, operation, | 3083 self._GenerateNativeCallback(cpp_callback_name, operation, parameter_definit
ions, |
3030 parameter_definitions, webcore_function_name, arguments, | 3084 True, webcore_function_name, arguments, idl_return_type=operation.type, |
3031 idl_return_type=operation.type, | |
3032 raises_dart_exceptions=raises_dart_exceptions, | 3085 raises_dart_exceptions=raises_dart_exceptions, |
3033 raises_dom_exceptions=operation.raises) | 3086 raises_dom_exceptions=operation.raises) |
3034 | 3087 |
3035 def _GenerateNativeCallback(self, callback_name, idl_node, | 3088 def _GenerateNativeCallback(self, callback_name, idl_node, |
3036 parameter_definitions, function_name, arguments, idl_return_type, | 3089 parameter_definitions, needs_receiver, function_name, arguments, idl_retur
n_type, |
3037 raises_dart_exceptions, raises_dom_exceptions): | 3090 raises_dart_exceptions, raises_dom_exceptions): |
3038 receiver = self._interface_type_info.receiver() | |
3039 if raises_dom_exceptions: | 3091 if raises_dom_exceptions: |
3040 arguments.append('ec') | 3092 arguments.append('ec') |
3041 callback = '%s%s(%s)' % (receiver, function_name, ', '.join(arguments)) | 3093 prefix = '' |
| 3094 if needs_receiver: prefix = self._interface_type_info.receiver() |
| 3095 callback = '%s%s(%s)' % (prefix, function_name, ', '.join(arguments)) |
3042 | 3096 |
3043 nested_templates = [] | 3097 nested_templates = [] |
3044 if idl_return_type and idl_return_type.id != 'void': | 3098 if idl_return_type and idl_return_type.id != 'void': |
3045 return_type_info = GetIDLTypeInfo(idl_return_type) | 3099 return_type_info = GetIDLTypeInfo(idl_return_type) |
3046 conversion_cast = return_type_info.conversion_cast('$BODY') | 3100 conversion_cast = return_type_info.conversion_cast('$BODY') |
3047 if isinstance(return_type_info, SVGTearOffIDLTypeInfo): | 3101 if isinstance(return_type_info, SVGTearOffIDLTypeInfo): |
3048 svg_primitive_types = ['SVGAngle', 'SVGLength', 'SVGMatrix', | 3102 svg_primitive_types = ['SVGAngle', 'SVGLength', 'SVGMatrix', |
3049 'SVGNumber', 'SVGPoint', 'SVGRect', 'SVGTransform'] | 3103 'SVGNumber', 'SVGPoint', 'SVGRect', 'SVGTransform'] |
3050 conversion_cast = '%s::create($BODY)' | 3104 conversion_cast = '%s::create($BODY)' |
3051 if self._interface.id.startswith('SVGAnimated'): | 3105 if self._interface.id.startswith('SVGAnimated'): |
(...skipping 25 matching lines...) Expand all Loading... |
3077 nested_templates.append( | 3131 nested_templates.append( |
3078 ' ExceptionCode ec = 0;\n' | 3132 ' ExceptionCode ec = 0;\n' |
3079 '$BODY' | 3133 '$BODY' |
3080 ' if (UNLIKELY(ec)) {\n' | 3134 ' if (UNLIKELY(ec)) {\n' |
3081 ' exception = DartDOMWrapper::exceptionCodeToDartException(
ec);\n' | 3135 ' exception = DartDOMWrapper::exceptionCodeToDartException(
ec);\n' |
3082 ' goto fail;\n' | 3136 ' goto fail;\n' |
3083 ' }\n') | 3137 ' }\n') |
3084 | 3138 |
3085 nested_templates.append( | 3139 nested_templates.append( |
3086 ' {\n' | 3140 ' {\n' |
3087 ' $WEBCORE_CLASS_NAME* receiver = DartDOMWrapper::receiver< $WEBC
ORE_CLASS_NAME >(args);\n' | |
3088 '$PARAMETER_DEFINITIONS' | 3141 '$PARAMETER_DEFINITIONS' |
3089 '\n' | |
3090 '$BODY' | 3142 '$BODY' |
3091 ' return;\n' | 3143 ' return;\n' |
3092 ' }\n') | 3144 ' }\n') |
3093 | 3145 |
3094 if raises_dart_exceptions: | 3146 if raises_dart_exceptions: |
3095 nested_templates.append( | 3147 nested_templates.append( |
3096 ' Dart_Handle exception;\n' | 3148 ' Dart_Handle exception;\n' |
3097 '$BODY' | 3149 '$BODY' |
3098 '\n' | 3150 '\n' |
3099 'fail:\n' | 3151 'fail:\n' |
3100 ' Dart_ThrowException(exception);\n' | 3152 ' Dart_ThrowException(exception);\n' |
3101 ' ASSERT_NOT_REACHED();\n') | 3153 ' ASSERT_NOT_REACHED();\n') |
3102 | 3154 |
3103 nested_templates.append( | 3155 nested_templates.append( |
3104 '\n' | 3156 '\n' |
3105 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n' | 3157 'static void $CALLBACK_NAME(Dart_NativeArguments args)\n' |
3106 '{\n' | 3158 '{\n' |
3107 ' DartApiScope dartApiScope;\n' | 3159 ' DartApiScope dartApiScope;\n' |
3108 '$BODY' | 3160 '$BODY' |
3109 '}\n') | 3161 '}\n') |
3110 | 3162 |
3111 template_parameters = { | 3163 template_parameters = { |
3112 'CALLBACK_NAME': callback_name, | 3164 'CALLBACK_NAME': callback_name, |
3113 'WEBCORE_CLASS_NAME': self._interface_type_info.native_type(), | 3165 'WEBCORE_CLASS_NAME': self._interface_type_info.native_type(), |
3114 'PARAMETER_DEFINITIONS': parameter_definitions, | 3166 'PARAMETER_DEFINITIONS': parameter_definitions, |
3115 } | 3167 } |
| 3168 if needs_receiver: |
| 3169 template_parameters['PARAMETER_DEFINITIONS'] = emitter.Format( |
| 3170 ' $WEBCORE_CLASS_NAME* receiver = DartDOMWrapper::receiver< $WE
BCORE_CLASS_NAME >(args);\n' |
| 3171 ' $PARAMETER_DEFINITIONS\n', |
| 3172 **template_parameters) |
| 3173 |
3116 for template in nested_templates: | 3174 for template in nested_templates: |
3117 template_parameters['BODY'] = callback | 3175 template_parameters['BODY'] = callback |
3118 callback_emitter = emitter.Emitter() | 3176 callback = emitter.Format(template, **template_parameters) |
3119 callback_emitter.Emit(template, **template_parameters) | |
3120 callback = ''.join(callback_emitter.Fragments()) | |
3121 | 3177 |
3122 self._cpp_definitions_emitter.Emit(callback) | 3178 self._cpp_definitions_emitter.Emit(callback) |
3123 | 3179 |
3124 def _GenerateParameterAdapter(self, emitter, idl_argument, index): | 3180 def _GenerateParameterAdapter(self, emitter, idl_argument, index): |
3125 type_info = GetIDLTypeInfo(idl_argument.type) | 3181 type_info = GetIDLTypeInfo(idl_argument.type) |
3126 (adapter_type, include_name) = type_info.parameter_adapter_info() | 3182 (adapter_type, include_name) = type_info.parameter_adapter_info() |
3127 if include_name: | 3183 if include_name: |
3128 self._cpp_impl_includes[include_name] = 1 | 3184 self._cpp_impl_includes[include_name] = 1 |
3129 emitter.Emit( | 3185 emitter.Emit( |
3130 '\n' | 3186 '\n' |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3166 namespace = 'HTMLNames' | 3222 namespace = 'HTMLNames' |
3167 svg_exceptions = ['class', 'id', 'onabort', 'onclick', 'onerror', 'onload', | 3223 svg_exceptions = ['class', 'id', 'onabort', 'onclick', 'onerror', 'onload', |
3168 'onmousedown', 'onmousemove', 'onmouseout', 'onmouseover', | 3224 'onmousedown', 'onmousemove', 'onmouseout', 'onmouseover', |
3169 'onmouseup', 'onresize', 'onscroll', 'onunload'] | 3225 'onmouseup', 'onresize', 'onscroll', 'onunload'] |
3170 if self._interface.id.startswith('SVG') and not attr.id in svg_exceptions: | 3226 if self._interface.id.startswith('SVG') and not attr.id in svg_exceptions: |
3171 namespace = 'SVGNames' | 3227 namespace = 'SVGNames' |
3172 self._cpp_impl_includes[namespace] = 1 | 3228 self._cpp_impl_includes[namespace] = 1 |
3173 | 3229 |
3174 attribute_name = attr.ext_attrs['Reflect'] or attr.id.lower() | 3230 attribute_name = attr.ext_attrs['Reflect'] or attr.id.lower() |
3175 return 'WebCore::%s::%sAttr' % (namespace, attribute_name) | 3231 return 'WebCore::%s::%sAttr' % (namespace, attribute_name) |
OLD | NEW |