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 Extends IdlType and IdlUnionType with property |union_arguments|. | |
32 | |
33 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler | |
34 """ | |
35 | |
36 from idl_types import inherits_interface | |
37 import dart_types | |
38 from dart_utilities import DartUtilities | |
39 from v8_globals import includes | |
40 | |
41 import v8_methods | |
42 | |
43 | |
44 def method_context(interface, method): | |
45 context = v8_methods.method_context(interface, method) | |
46 | |
47 arguments = method.arguments | |
48 extended_attributes = method.extended_attributes | |
49 idl_type = method.idl_type | |
50 | |
51 # idl_type.add_includes_for_type() | |
52 this_cpp_value = cpp_value(interface, method, len(arguments)) | |
53 | |
54 if context['is_call_with_script_state']: | |
55 includes.add('bindings/core/dart/DartScriptState.h') | |
56 | |
57 if idl_type.union_arguments and len(idl_type.union_arguments) > 0: | |
58 this_cpp_type = [] | |
59 for cpp_type in idl_type.member_types: | |
60 # FIXMEDART: we shouldn't just assume RefPtr. We should append | |
61 # WillBeGC as appropriate. | |
62 this_cpp_type.append("RefPtr<%s>" % cpp_type) | |
63 else: | |
64 this_cpp_type = idl_type.cpp_type | |
65 | |
66 is_auto_scope = not 'DartNoAutoScope' in extended_attributes | |
67 | |
68 arguments_data = [argument_context(interface, method, argument, index) | |
69 for index, argument in enumerate(arguments)] | |
70 | |
71 union_arguments = [] | |
72 if idl_type.union_arguments: | |
73 union_arguments.extend([union_arg['cpp_value'] | |
74 for union_arg in idl_type.union_arguments]) | |
75 | |
76 is_custom = 'Custom' in extended_attributes or 'DartCustom' in extended_attr
ibutes | |
77 | |
78 context.update({ | |
79 'activity_logging_world_list': DartUtilities.activity_logging_world_list
(method), # [ActivityLogging] | |
80 'arguments': arguments_data, | |
81 'cpp_type': this_cpp_type, | |
82 'cpp_value': this_cpp_value, | |
83 'dart_name': extended_attributes.get('DartName'), | |
84 'deprecate_as': DartUtilities.deprecate_as(method), # [DeprecateAs] | |
85 'do_not_check_signature': not(context['is_static'] or | |
86 DartUtilities.has_extended_attribute(method, | |
87 ['DoNotCheckSecurity', 'DoNotCheckSignature', 'NotEnumerable', | |
88 'ReadOnly', 'RuntimeEnabled', 'Unforgeable'])), | |
89 'has_exception_state': | |
90 context['is_raises_exception'] or | |
91 context['is_check_security_for_frame'] or | |
92 any(argument for argument in arguments | |
93 if argument.idl_type.name == 'SerializedScriptValue' or | |
94 argument.idl_type.is_integer_type), | |
95 'is_auto_scope': is_auto_scope, | |
96 'auto_scope': DartUtilities.bool_to_cpp(is_auto_scope), | |
97 'is_custom': is_custom, | |
98 'is_custom_dart': 'DartCustom' in extended_attributes, | |
99 'is_custom_dart_new': DartUtilities.has_extended_attribute_value(method,
'DartCustom', 'New'), | |
100 # FIXME(terry): DartStrictTypeChecking no longer supported; TypeChecking
is | |
101 # new extended attribute. | |
102 'is_strict_type_checking': | |
103 'DartStrictTypeChecking' in extended_attributes or | |
104 'DartStrictTypeChecking' in interface.extended_attributes, | |
105 'measure_as': DartUtilities.measure_as(method), # [MeasureAs] | |
106 'suppressed': (arguments and arguments[-1].is_variadic), # FIXME: imple
ment variadic | |
107 'union_arguments': union_arguments, | |
108 'dart_set_return_value': dart_set_return_value(interface.name, method, t
his_cpp_value), | |
109 }) | |
110 return context | |
111 | |
112 def argument_context(interface, method, argument, index): | |
113 context = v8_methods.argument_context(interface, method, argument, index) | |
114 | |
115 extended_attributes = argument.extended_attributes | |
116 idl_type = argument.idl_type | |
117 this_cpp_value = cpp_value(interface, method, index) | |
118 use_heap_vector_type = context['is_variadic_wrapper_type'] and idl_type.is_w
ill_be_garbage_collected | |
119 auto_scope = not 'DartNoAutoScope' in extended_attributes | |
120 arg_index = index + 1 if not (method.is_static or method.is_constructor) els
e index | |
121 preprocessed_type = str(idl_type.preprocessed_type) | |
122 local_cpp_type = idl_type.cpp_type_args(argument.extended_attributes, raw_ty
pe=True) | |
123 default_value = argument.default_cpp_value | |
124 if context['has_default']: | |
125 default_value = (argument.default_cpp_value or | |
126 dart_types.default_cpp_value_for_cpp_type(idl_type)) | |
127 # FIXMEDART: handle the drift between preprocessed type names in 1847 and | |
128 # 1985 dartium builds in a more generic way. | |
129 if preprocessed_type == 'unrestricted float': | |
130 preprocessed_type = 'float' | |
131 if preprocessed_type == 'unrestricted double': | |
132 preprocessed_type = 'double' | |
133 | |
134 dart_enum_expression = idl_type.enum_validation_expression | |
135 if dart_enum_expression: | |
136 dart_enum_expression = dart_enum_expression.format(param_name=argument.n
ame) | |
137 context.update({ | |
138 'cpp_type': idl_type.cpp_type_args(extended_attributes=extended_attribut
es, | |
139 raw_type=True, | |
140 used_in_cpp_sequence=use_heap_vector_
type), | |
141 'cpp_value': this_cpp_value, | |
142 'local_cpp_type': local_cpp_type, | |
143 # FIXME: check that the default value's type is compatible with the argu
ment's | |
144 'default_value': default_value, | |
145 'enum_validation_expression': dart_enum_expression, | |
146 'preprocessed_type': preprocessed_type, | |
147 'is_array_or_sequence_type': not not idl_type.native_array_element_type, | |
148 'is_strict_type_checking': 'DartStrictTypeChecking' in extended_attribut
es, | |
149 'is_dictionary': idl_type.is_dictionary or idl_type.base_type == 'Dictio
nary', | |
150 'vector_type': 'WillBeHeapVector' if use_heap_vector_type else 'Vector', | |
151 'dart_set_return_value_for_main_world': dart_set_return_value(interface.
name, method, | |
152 this_cpp_v
alue, for_main_world=True), | |
153 'dart_set_return_value': dart_set_return_value(interface.name, method, t
his_cpp_value), | |
154 'arg_index': arg_index, | |
155 'dart_value_dictionary_cpp_value': dart_dictionary_value_argument(argume
nt, arg_index), | |
156 'dart_value_to_local_cpp_value': dart_value_to_local_cpp_value(interface
, | |
157 context['
has_type_checking_interface'], | |
158 argument,
arg_index, auto_scope), | |
159 }) | |
160 return context | |
161 | |
162 | |
163 ################################################################################ | |
164 # Value handling | |
165 ################################################################################ | |
166 | |
167 def cpp_value(interface, method, number_of_arguments): | |
168 def cpp_argument(argument): | |
169 argument_name = dart_types.check_reserved_name(argument.name) | |
170 idl_type = argument.idl_type | |
171 | |
172 if idl_type.is_typed_array_type: | |
173 return '%s.get()' % argument_name | |
174 | |
175 if idl_type.name == 'EventListener': | |
176 if (interface.name == 'EventTarget' and | |
177 method.name == 'removeEventListener'): | |
178 # FIXME: remove this special case by moving get() into | |
179 # EventTarget::removeEventListener | |
180 return '%s.get()' % argument_name | |
181 return argument.name | |
182 if (idl_type.name in ['NodeFilter', 'NodeFilterOrNull', | |
183 'XPathNSResolver', 'XPathNSResolverOrNull']): | |
184 # FIXME: remove this special case | |
185 return '%s.release()' % argument_name | |
186 # Need to de-ref the generated dictionary class for create call. | |
187 if (idl_type.is_dictionary): | |
188 return '*%s' % argument_name | |
189 return argument_name | |
190 | |
191 # Truncate omitted optional arguments | |
192 arguments = method.arguments[:number_of_arguments] | |
193 if method.is_constructor: | |
194 call_with_values = interface.extended_attributes.get('ConstructorCallWit
h') | |
195 else: | |
196 call_with_values = method.extended_attributes.get('CallWith') | |
197 cpp_arguments = DartUtilities.call_with_arguments(call_with_values) | |
198 if ('PartialInterfaceImplementedAs' in method.extended_attributes and not me
thod.is_static): | |
199 cpp_arguments.append('*receiver') | |
200 | |
201 cpp_arguments.extend(cpp_argument(argument) for argument in arguments) | |
202 this_union_arguments = method.idl_type and method.idl_type.union_arguments | |
203 if this_union_arguments: | |
204 cpp_arguments.extend([member_argument['cpp_value'] | |
205 for member_argument in this_union_arguments]) | |
206 | |
207 if ('RaisesException' in method.extended_attributes or | |
208 (method.is_constructor and | |
209 DartUtilities.has_extended_attribute_value(interface, 'RaisesException'
, 'Constructor'))): | |
210 cpp_arguments.append('es') | |
211 | |
212 if method.name == 'Constructor': | |
213 base_name = 'create' | |
214 elif method.name == 'NamedConstructor': | |
215 base_name = 'createForJSConstructor' | |
216 else: | |
217 base_name = DartUtilities.cpp_name(method) | |
218 cpp_method_name = DartUtilities.scoped_name(interface, method, base_name) | |
219 return '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments)) | |
220 | |
221 | |
222 # Mapping of IDL type to DartUtilities helper types. | |
223 def dart_arg_type(argument_type): | |
224 if (argument_type.cpp_type == 'String'): | |
225 return 'DartStringAdapter' | |
226 | |
227 return argument_type.cpp_type | |
228 | |
229 | |
230 def dart_set_return_value(interface_name, method, cpp_value, for_main_world=Fals
e): | |
231 idl_type = method.idl_type | |
232 extended_attributes = method.extended_attributes | |
233 if not idl_type or idl_type.name == 'void': | |
234 # Constructors and void methods don't have a return type | |
235 return None | |
236 | |
237 release = False | |
238 | |
239 if idl_type.is_union_type: | |
240 release = idl_type.release | |
241 | |
242 # [CallWith=ScriptState], [RaisesException] | |
243 # TODO(terry): Disable ScriptState temporarily need to handle. | |
244 # if (has_extended_attribute_value(method, 'CallWith', 'ScriptState') or | |
245 # 'RaisesException' in extended_attributes or | |
246 # idl_type.is_union_type): | |
247 # cpp_value = 'result' # use local variable for value | |
248 # release = idl_type.release | |
249 | |
250 auto_scope = not 'DartNoAutoScope' in extended_attributes | |
251 script_wrappable = 'impl' if inherits_interface(interface_name, 'Node') else
'' | |
252 return idl_type.dart_set_return_value(cpp_value, extended_attributes, | |
253 script_wrappable=script_wrappable, | |
254 release=release, | |
255 for_main_world=for_main_world, | |
256 auto_scope=auto_scope) | |
257 | |
258 | |
259 def dart_dictionary_value_argument(argument, index): | |
260 idl_type = argument.idl_type | |
261 return idl_type.dart_dictionary_to_local_cpp_value(index=index) | |
262 | |
263 | |
264 def dart_value_to_local_cpp_value(interface, has_type_checking_interface, | |
265 argument, index, auto_scope=True): | |
266 extended_attributes = argument.extended_attributes | |
267 idl_type = argument.idl_type | |
268 name = argument.name | |
269 # TODO(terry): Variadic arguments are not handled but treated as one argumen
t. | |
270 # if argument.is_variadic: | |
271 # vector_type = 'WillBeHeapVector' if idl_type.is_will_be_garbage_col
lected else 'Vector' | |
272 # return 'V8TRYCATCH_VOID({vector_type}<{cpp_type}>, {name}, toNative
Arguments<{cpp_type}>(info, {index}))'.format( | |
273 # cpp_type=idl_type.cpp_type, name=name, index=index, vector_
type=vector_type) | |
274 | |
275 # FIXME: V8 has some special logic around the addEventListener and | |
276 # removeEventListener methods that should be added in somewhere. | |
277 # There is also some logic in systemnative.py to force a null check | |
278 # for the useCapture argument of those same methods that we may need to | |
279 # pull over. | |
280 null_check = ((argument.is_optional and idl_type.is_callback_interface) or | |
281 (idl_type.name == 'Dictionary') or | |
282 (argument.default_value and argument.default_value.is_null)) | |
283 | |
284 return idl_type.dart_value_to_local_cpp_value( | |
285 extended_attributes, name, null_check, has_type_checking_interface, | |
286 index=index, auto_scope=auto_scope) | |
OLD | NEW |