OLD | NEW |
| (Empty) |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | |
2 # | |
3 # Redistribution and use in source and binary forms, with or without | |
4 # modification, are permitted provided that the following conditions are | |
5 # met: | |
6 # | |
7 # * Redistributions of source code must retain the above copyright | |
8 # notice, this list of conditions and the following disclaimer. | |
9 # * Redistributions in binary form must reproduce the above | |
10 # copyright notice, this list of conditions and the following disclaimer | |
11 # in the documentation and/or other materials provided with the | |
12 # distribution. | |
13 # * Neither the name of Google Inc. nor the names of its | |
14 # contributors may be used to endorse or promote products derived from | |
15 # this software without specific prior written permission. | |
16 # | |
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | |
29 """Generate template values for methods. | |
30 | |
31 FIXME: Not currently used in build. | |
32 This is a rewrite of the Perl IDL compiler in Python, but is not complete. | |
33 Once it is complete, we will switch all IDL files over to Python at once. | |
34 Until then, please work on the Perl IDL compiler. | |
35 For details, see bug http://crbug.com/239771 | |
36 """ | |
37 | |
38 from v8_globals import includes | |
39 import v8_types | |
40 import v8_utilities | |
41 from v8_utilities import has_extended_attribute_value | |
42 | |
43 | |
44 def generate_method(interface, method): | |
45 arguments = method.arguments | |
46 extended_attributes = method.extended_attributes | |
47 idl_type = method.idl_type | |
48 is_static = method.is_static | |
49 name = method.name | |
50 | |
51 v8_types.add_includes_for_type(idl_type) | |
52 this_cpp_value = cpp_value(interface, method, len(arguments)) | |
53 | |
54 def function_template(): | |
55 if is_static: | |
56 return 'functionTemplate' | |
57 if 'Unforgeable' in extended_attributes: | |
58 return 'instanceTemplate' | |
59 return 'prototypeTemplate' | |
60 | |
61 is_call_with_script_arguments = has_extended_attribute_value(method, 'CallWi
th', 'ScriptArguments') | |
62 if is_call_with_script_arguments: | |
63 includes.update(['bindings/v8/ScriptCallStackFactory.h', | |
64 'core/inspector/ScriptArguments.h']) | |
65 is_call_with_script_state = has_extended_attribute_value(method, 'CallWith',
'ScriptState') | |
66 if is_call_with_script_state: | |
67 includes.add('bindings/v8/ScriptState.h') | |
68 is_check_security_for_node = 'CheckSecurity' in extended_attributes | |
69 if is_check_security_for_node: | |
70 includes.add('bindings/v8/BindingSecurity.h') | |
71 is_custom_element_callbacks = 'CustomElementCallbacks' in extended_attribute
s | |
72 if is_custom_element_callbacks: | |
73 includes.add('core/dom/custom/CustomElementCallbackDispatcher.h') | |
74 | |
75 is_check_security_for_frame = ( | |
76 'CheckSecurity' in interface.extended_attributes and | |
77 'DoNotCheckSecurity' not in extended_attributes) | |
78 is_raises_exception = 'RaisesException' in extended_attributes | |
79 | |
80 return { | |
81 'activity_logging_world_list': v8_utilities.activity_logging_world_list(
method), # [ActivityLogging] | |
82 'arguments': [generate_argument(interface, method, argument, index) | |
83 for index, argument in enumerate(arguments)], | |
84 'conditional_string': v8_utilities.conditional_string(method), | |
85 'cpp_type': v8_types.cpp_type(idl_type), | |
86 'cpp_value': this_cpp_value, | |
87 'deprecate_as': v8_utilities.deprecate_as(method), # [DeprecateAs] | |
88 'do_not_check_signature': not(is_static or | |
89 v8_utilities.has_extended_attribute(method, | |
90 ['DoNotCheckSecurity', 'DoNotCheckSignature', 'NotEnumerable', | |
91 'ReadOnly', 'RuntimeEnabled', 'Unforgeable'])), | |
92 'function_template': function_template(), | |
93 'idl_type': idl_type, | |
94 'has_exception_state': | |
95 is_raises_exception or | |
96 is_check_security_for_frame or | |
97 any(argument for argument in arguments | |
98 if argument.idl_type == 'SerializedScriptValue' or | |
99 v8_types.is_integer_type(argument.idl_type)) or | |
100 name in ['addEventListener', 'removeEventListener', 'dispatchEvent']
, | |
101 'is_call_with_execution_context': has_extended_attribute_value(method, '
CallWith', 'ExecutionContext'), | |
102 'is_call_with_script_arguments': is_call_with_script_arguments, | |
103 'is_call_with_script_state': is_call_with_script_state, | |
104 'is_check_security_for_frame': is_check_security_for_frame, | |
105 'is_check_security_for_node': is_check_security_for_node, | |
106 'is_custom': 'Custom' in extended_attributes, | |
107 'is_custom_element_callbacks': is_custom_element_callbacks, | |
108 'is_do_not_check_security': 'DoNotCheckSecurity' in extended_attributes, | |
109 'is_do_not_check_signature': 'DoNotCheckSignature' in extended_attribute
s, | |
110 'is_implemented_by': 'ImplementedBy' in extended_attributes, | |
111 'is_per_world_bindings': 'PerWorldBindings' in extended_attributes, | |
112 'is_raises_exception': is_raises_exception, | |
113 'is_read_only': 'ReadOnly' in extended_attributes, | |
114 'is_static': is_static, | |
115 'is_strict_type_checking': | |
116 'StrictTypeChecking' in extended_attributes or | |
117 'StrictTypeChecking' in interface.extended_attributes, | |
118 'is_variadic': arguments and arguments[-1].is_variadic, | |
119 'measure_as': v8_utilities.measure_as(method), # [MeasureAs] | |
120 'name': name, | |
121 'number_of_arguments': len(arguments), | |
122 'number_of_required_arguments': len([ | |
123 argument for argument in arguments | |
124 if not (argument.is_optional or argument.is_variadic)]), | |
125 'number_of_required_or_variadic_arguments': len([ | |
126 argument for argument in arguments | |
127 if not argument.is_optional]), | |
128 'per_context_enabled_function': v8_utilities.per_context_enabled_functio
n_name(method), # [PerContextEnabled] | |
129 'property_attributes': property_attributes(method), | |
130 'runtime_enabled_function': v8_utilities.runtime_enabled_function_name(m
ethod), # [RuntimeEnabled] | |
131 'signature': 'v8::Local<v8::Signature>()' if is_static or 'DoNotCheckSig
nature' in extended_attributes else 'defaultSignature', | |
132 'union_arguments': union_arguments(idl_type), | |
133 'v8_set_return_value_for_main_world': v8_set_return_value(interface.name
, method, this_cpp_value, for_main_world=True), | |
134 'v8_set_return_value': v8_set_return_value(interface.name, method, this_
cpp_value), | |
135 'world_suffixes': ['', 'ForMainWorld'] if 'PerWorldBindings' in extended
_attributes else [''], # [PerWorldBindings] | |
136 } | |
137 | |
138 | |
139 def generate_argument(interface, method, argument, index): | |
140 extended_attributes = argument.extended_attributes | |
141 idl_type = argument.idl_type | |
142 this_cpp_value = cpp_value(interface, method, index) | |
143 return { | |
144 'cpp_type': v8_types.cpp_type(idl_type), | |
145 'cpp_value': this_cpp_value, | |
146 'enum_validation_expression': v8_utilities.enum_validation_expression(id
l_type), | |
147 'has_default': 'Default' in extended_attributes, | |
148 'idl_type': idl_type, | |
149 'index': index, | |
150 'is_clamp': 'Clamp' in extended_attributes, | |
151 'is_callback_interface': v8_types.is_callback_interface(idl_type), | |
152 'is_nullable': argument.is_nullable, | |
153 'is_optional': argument.is_optional, | |
154 'is_strict_type_checking': 'StrictTypeChecking' in extended_attributes, | |
155 'is_variadic_wrapper_type': argument.is_variadic and v8_types.is_wrapper
_type(idl_type), | |
156 'is_wrapper_type': v8_types.is_wrapper_type(idl_type), | |
157 'name': argument.name, | |
158 'v8_set_return_value_for_main_world': v8_set_return_value(interface.name
, method, this_cpp_value, for_main_world=True), | |
159 'v8_set_return_value': v8_set_return_value(interface.name, method, this_
cpp_value), | |
160 'v8_value_to_local_cpp_value': v8_value_to_local_cpp_value(argument, ind
ex), | |
161 } | |
162 | |
163 | |
164 ################################################################################ | |
165 # Value handling | |
166 ################################################################################ | |
167 | |
168 def cpp_value(interface, method, number_of_arguments): | |
169 def cpp_argument(argument): | |
170 idl_type = argument.idl_type | |
171 if (v8_types.is_callback_interface(idl_type) or | |
172 idl_type in ['NodeFilter', 'XPathNSResolver']): | |
173 # FIXME: remove this special case | |
174 return '%s.release()' % argument.name | |
175 return argument.name | |
176 | |
177 # Truncate omitted optional arguments | |
178 arguments = method.arguments[:number_of_arguments] | |
179 cpp_arguments = v8_utilities.call_with_arguments(method) | |
180 if ('ImplementedBy' in method.extended_attributes and | |
181 not method.is_static): | |
182 cpp_arguments.append('*imp') | |
183 cpp_arguments.extend(cpp_argument(argument) for argument in arguments) | |
184 this_union_arguments = union_arguments(method.idl_type) | |
185 if this_union_arguments: | |
186 cpp_arguments.extend(this_union_arguments) | |
187 | |
188 if 'RaisesException' in method.extended_attributes: | |
189 cpp_arguments.append('exceptionState') | |
190 | |
191 cpp_method_name = v8_utilities.scoped_name(interface, method, v8_utilities.c
pp_name(method)) | |
192 return '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments)) | |
193 | |
194 | |
195 def v8_set_return_value(interface_name, method, cpp_value, for_main_world=False)
: | |
196 idl_type = method.idl_type | |
197 extended_attributes = method.extended_attributes | |
198 if idl_type == 'void': | |
199 return None | |
200 is_union_type = v8_types.is_union_type(idl_type) | |
201 | |
202 release = False | |
203 # [CallWith=ScriptState], [RaisesException] | |
204 if (has_extended_attribute_value(method, 'CallWith', 'ScriptState') or | |
205 'RaisesException' in extended_attributes or | |
206 is_union_type): | |
207 # use local variable for value | |
208 cpp_value = 'result' | |
209 | |
210 if is_union_type: | |
211 release = [v8_types.is_interface_type(union_member_type) | |
212 for union_member_type in idl_type.union_member_types] | |
213 else: | |
214 release = v8_types.is_interface_type(idl_type) | |
215 | |
216 script_wrappable = 'imp' if v8_types.inherits_interface(interface_name, 'Nod
e') else '' | |
217 return v8_types.v8_set_return_value(idl_type, cpp_value, extended_attributes
, script_wrappable=script_wrappable, release=release, for_main_world=for_main_wo
rld) | |
218 | |
219 | |
220 def v8_value_to_local_cpp_value(argument, index): | |
221 extended_attributes = argument.extended_attributes | |
222 idl_type = argument.idl_type | |
223 name = argument.name | |
224 if argument.is_variadic: | |
225 return 'V8TRYCATCH_VOID(Vector<{cpp_type}>, {name}, toNativeArguments<{c
pp_type}>(info, {index}))'.format( | |
226 cpp_type=v8_types.cpp_type(idl_type), name=name, index=index) | |
227 # [Default=NullString] | |
228 if (argument.is_optional and idl_type == 'DOMString' and | |
229 extended_attributes.get('Default') == 'NullString'): | |
230 v8_value = 'argumentOrNull(info, %s)' % index | |
231 else: | |
232 v8_value = 'info[%s]' % index | |
233 return v8_types.v8_value_to_local_cpp_value( | |
234 idl_type, argument.extended_attributes, v8_value, name, index=index) | |
235 | |
236 | |
237 ################################################################################ | |
238 # Auxiliary functions | |
239 ################################################################################ | |
240 | |
241 # [NotEnumerable] | |
242 def property_attributes(method): | |
243 extended_attributes = method.extended_attributes | |
244 property_attributes_list = [] | |
245 if 'NotEnumerable' in extended_attributes: | |
246 property_attributes_list.append('v8::DontEnum') | |
247 if 'ReadOnly' in extended_attributes: | |
248 property_attributes_list.append('v8::ReadOnly') | |
249 if property_attributes_list: | |
250 property_attributes_list.insert(0, 'v8::DontDelete') | |
251 return property_attributes_list | |
252 | |
253 | |
254 def union_arguments(idl_type): | |
255 """Return list of ['result0Enabled', 'result0', 'result1Enabled', ...] for u
nion types, for use in setting return value""" | |
256 if not v8_types.is_union_type(idl_type): | |
257 return None | |
258 return [arg | |
259 for i in range(len(idl_type.union_member_types)) | |
260 for arg in ['result%sEnabled' % i, 'result%s' % i]] | |
OLD | NEW |