| 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 IdlArgument with property |default_cpp_value|. | |
| 32 Extends IdlTypeBase and IdlUnionType with property |union_arguments|. | |
| 33 | |
| 34 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler | |
| 35 """ | |
| 36 | |
| 37 from idl_definitions import IdlArgument | |
| 38 from idl_types import IdlTypeBase, IdlUnionType, inherits_interface | |
| 39 from v8_globals import includes | |
| 40 import v8_types | |
| 41 import v8_utilities | |
| 42 from v8_utilities import has_extended_attribute_value | |
| 43 | |
| 44 | |
| 45 # Methods with any of these require custom method registration code in the | |
| 46 # interface's configure*Template() function. | |
| 47 CUSTOM_REGISTRATION_EXTENDED_ATTRIBUTES = frozenset([ | |
| 48 'NotEnumerable', | |
| 49 ]) | |
| 50 | |
| 51 | |
| 52 def argument_needs_try_catch(method, argument): | |
| 53 return_promise = method.idl_type and method.idl_type.name == 'Promise' | |
| 54 idl_type = argument.idl_type | |
| 55 base_type = idl_type.base_type | |
| 56 | |
| 57 return not( | |
| 58 # These cases are handled by separate code paths in the | |
| 59 # generate_argument() macro in engine/bindings/templates/methods.cpp. | |
| 60 idl_type.is_callback_interface or | |
| 61 base_type == 'SerializedScriptValue' or | |
| 62 (argument.is_variadic and idl_type.is_wrapper_type) or | |
| 63 # String and enumeration arguments converted using one of the | |
| 64 # TOSTRING_* macros except for _PROMISE variants in | |
| 65 # engine/bindings/core/v8/V8BindingMacros.h don't use a v8::TryCatch. | |
| 66 ((base_type == 'DOMString' or idl_type.is_enum) and | |
| 67 not argument.is_variadic and | |
| 68 not return_promise)) | |
| 69 | |
| 70 | |
| 71 def use_local_result(method): | |
| 72 extended_attributes = method.extended_attributes | |
| 73 idl_type = method.idl_type | |
| 74 return (has_extended_attribute_value(method, 'CallWith', 'ScriptState') or | |
| 75 'RaisesException' in extended_attributes or | |
| 76 idl_type.is_union_type or | |
| 77 idl_type.is_explicit_nullable) | |
| 78 | |
| 79 | |
| 80 def method_context(interface, method): | |
| 81 arguments = method.arguments | |
| 82 extended_attributes = method.extended_attributes | |
| 83 idl_type = method.idl_type | |
| 84 is_static = method.is_static | |
| 85 name = method.name | |
| 86 | |
| 87 idl_type.add_includes_for_type() | |
| 88 this_cpp_value = cpp_value(interface, method, len(arguments)) | |
| 89 | |
| 90 def function_template(): | |
| 91 if is_static: | |
| 92 return 'functionTemplate' | |
| 93 return 'prototypeTemplate' | |
| 94 | |
| 95 is_call_with_script_arguments = has_extended_attribute_value(method, 'CallWi
th', 'ScriptArguments') | |
| 96 if is_call_with_script_arguments: | |
| 97 includes.update(['bindings/core/v8/ScriptCallStackFactory.h', | |
| 98 'core/inspector/ScriptArguments.h']) | |
| 99 is_call_with_script_state = has_extended_attribute_value(method, 'CallWith',
'ScriptState') | |
| 100 if is_call_with_script_state: | |
| 101 includes.add('bindings/core/v8/ScriptState.h') | |
| 102 is_custom_element_callbacks = 'CustomElementCallbacks' in extended_attribute
s | |
| 103 if is_custom_element_callbacks: | |
| 104 includes.add('core/dom/custom/CustomElementProcessingStack.h') | |
| 105 | |
| 106 is_raises_exception = 'RaisesException' in extended_attributes | |
| 107 | |
| 108 arguments_need_try_catch = ( | |
| 109 any(argument_needs_try_catch(method, argument) | |
| 110 for argument in arguments)) | |
| 111 | |
| 112 return { | |
| 113 'arguments': [argument_context(interface, method, argument, index) | |
| 114 for index, argument in enumerate(arguments)], | |
| 115 'arguments_need_try_catch': arguments_need_try_catch, | |
| 116 'cpp_type': (v8_types.cpp_template_type('Nullable', idl_type.cpp_type) | |
| 117 if idl_type.is_explicit_nullable else idl_type.cpp_type), | |
| 118 'cpp_value': this_cpp_value, | |
| 119 'cpp_type_initializer': idl_type.cpp_type_initializer, | |
| 120 'custom_registration_extended_attributes': | |
| 121 CUSTOM_REGISTRATION_EXTENDED_ATTRIBUTES.intersection( | |
| 122 extended_attributes.iterkeys()), | |
| 123 'exposed_test': v8_utilities.exposed(method, interface), # [Exposed] | |
| 124 'function_template': function_template(), | |
| 125 'has_custom_registration': is_static or | |
| 126 v8_utilities.has_extended_attribute( | |
| 127 method, CUSTOM_REGISTRATION_EXTENDED_ATTRIBUTES), | |
| 128 'has_exception_state': | |
| 129 is_raises_exception or | |
| 130 any(argument for argument in arguments | |
| 131 if argument.idl_type.name == 'SerializedScriptValue' or | |
| 132 argument.idl_type.may_raise_exception_on_conversion), | |
| 133 'idl_type': idl_type.base_type, | |
| 134 'is_call_with_execution_context': has_extended_attribute_value(method, '
CallWith', 'ExecutionContext'), | |
| 135 'is_call_with_script_arguments': is_call_with_script_arguments, | |
| 136 'is_call_with_script_state': is_call_with_script_state, | |
| 137 'is_custom': 'Custom' in extended_attributes, | |
| 138 'is_custom_element_callbacks': is_custom_element_callbacks, | |
| 139 'is_explicit_nullable': idl_type.is_explicit_nullable, | |
| 140 'is_partial_interface_member': | |
| 141 'PartialInterfaceImplementedAs' in extended_attributes, | |
| 142 'is_raises_exception': is_raises_exception, | |
| 143 'is_static': is_static, | |
| 144 'is_variadic': arguments and arguments[-1].is_variadic, | |
| 145 'name': name, | |
| 146 'number_of_arguments': len(arguments), | |
| 147 'number_of_required_arguments': len([ | |
| 148 argument for argument in arguments | |
| 149 if not (argument.is_optional or argument.is_variadic)]), | |
| 150 'number_of_required_or_variadic_arguments': len([ | |
| 151 argument for argument in arguments | |
| 152 if not argument.is_optional]), | |
| 153 'union_arguments': idl_type.union_arguments, | |
| 154 'use_local_result': use_local_result(method), | |
| 155 } | |
| 156 | |
| 157 | |
| 158 def argument_context(interface, method, argument, index): | |
| 159 extended_attributes = argument.extended_attributes | |
| 160 idl_type = argument.idl_type | |
| 161 this_cpp_value = cpp_value(interface, method, index) | |
| 162 return_promise = (method.idl_type.name == 'Promise' if method.idl_type | |
| 163 else False) | |
| 164 | |
| 165 default_cpp_value = argument.default_cpp_value | |
| 166 return { | |
| 167 'cpp_type': idl_type.cpp_type_args(extended_attributes=extended_attribut
es, | |
| 168 raw_type=True, | |
| 169 used_as_variadic_argument=argument.is
_variadic), | |
| 170 'cpp_value': this_cpp_value, | |
| 171 # FIXME: check that the default value's type is compatible with the argu
ment's | |
| 172 'default_value': default_cpp_value, | |
| 173 'enum_validation_expression': idl_type.enum_validation_expression, | |
| 174 'handle': '%sHandle' % argument.name, | |
| 175 # FIXME: remove once [Default] removed and just use argument.default_val
ue | |
| 176 'has_default': 'Default' in extended_attributes or default_cpp_value, | |
| 177 'has_type_checking_interface': | |
| 178 (has_extended_attribute_value(interface, 'TypeChecking', 'Interface'
) or | |
| 179 has_extended_attribute_value(method, 'TypeChecking', 'Interface'))
and | |
| 180 idl_type.is_wrapper_type, | |
| 181 'has_type_checking_unrestricted': | |
| 182 (has_extended_attribute_value(interface, 'TypeChecking', 'Unrestrict
ed') or | |
| 183 has_extended_attribute_value(method, 'TypeChecking', 'Unrestricted'
)) and | |
| 184 idl_type.name in ('Float', 'Double'), | |
| 185 'idl_type': idl_type.base_type, | |
| 186 'idl_type_object': idl_type, | |
| 187 'index': index, | |
| 188 'is_callback_interface': idl_type.is_callback_interface, | |
| 189 'is_nullable': idl_type.is_nullable, | |
| 190 'is_optional': argument.is_optional, | |
| 191 'is_wrapper_type': idl_type.is_wrapper_type, | |
| 192 'name': argument.name, | |
| 193 } | |
| 194 | |
| 195 | |
| 196 ################################################################################ | |
| 197 # Value handling | |
| 198 ################################################################################ | |
| 199 | |
| 200 def cpp_value(interface, method, number_of_arguments): | |
| 201 def cpp_argument(argument): | |
| 202 idl_type = argument.idl_type | |
| 203 if idl_type.name == 'EventListener': | |
| 204 return argument.name | |
| 205 if (idl_type.is_callback_interface or | |
| 206 idl_type.name in ['NodeFilter', 'NodeFilterOrNull']): | |
| 207 # FIXME: remove this special case | |
| 208 return '%s.release()' % argument.name | |
| 209 return argument.name | |
| 210 | |
| 211 # Truncate omitted optional arguments | |
| 212 arguments = method.arguments[:number_of_arguments] | |
| 213 cpp_arguments = [] | |
| 214 | |
| 215 if method.is_constructor: | |
| 216 call_with_values = interface.extended_attributes.get('ConstructorCallWit
h') | |
| 217 else: | |
| 218 call_with_values = method.extended_attributes.get('CallWith') | |
| 219 cpp_arguments.extend(v8_utilities.call_with_arguments(call_with_values)) | |
| 220 | |
| 221 # Members of IDL partial interface definitions are implemented in C++ as | |
| 222 # static member functions, which for instance members (non-static members) | |
| 223 # take *impl as their first argument | |
| 224 if ('PartialInterfaceImplementedAs' in method.extended_attributes and | |
| 225 not method.is_static): | |
| 226 cpp_arguments.append('*impl') | |
| 227 cpp_arguments.extend(cpp_argument(argument) for argument in arguments) | |
| 228 | |
| 229 this_union_arguments = method.idl_type and method.idl_type.union_arguments | |
| 230 if this_union_arguments: | |
| 231 cpp_arguments.extend([member_argument['cpp_value'] | |
| 232 for member_argument in this_union_arguments]) | |
| 233 | |
| 234 if ('RaisesException' in method.extended_attributes or | |
| 235 (method.is_constructor and | |
| 236 has_extended_attribute_value(interface, 'RaisesException', 'Constructor
'))): | |
| 237 cpp_arguments.append('exceptionState') | |
| 238 | |
| 239 if method.name == 'Constructor': | |
| 240 base_name = 'create' | |
| 241 elif method.name == 'NamedConstructor': | |
| 242 base_name = 'createForJSConstructor' | |
| 243 else: | |
| 244 base_name = v8_utilities.cpp_name(method) | |
| 245 | |
| 246 cpp_method_name = v8_utilities.scoped_name(interface, method, base_name) | |
| 247 return '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments)) | |
| 248 | |
| 249 | |
| 250 def v8_value_to_local_cpp_variadic_value(argument, index, return_promise): | |
| 251 assert argument.is_variadic | |
| 252 idl_type = argument.idl_type | |
| 253 | |
| 254 suffix = '' | |
| 255 | |
| 256 macro = 'TONATIVE_VOID' | |
| 257 macro_args = [ | |
| 258 argument.name, | |
| 259 'toNativeArguments<%s>(info, %s)' % (idl_type.cpp_type, index), | |
| 260 ] | |
| 261 | |
| 262 if return_promise: | |
| 263 suffix += '_PROMISE' | |
| 264 macro_args.append('info') | |
| 265 | |
| 266 suffix += '_INTERNAL' | |
| 267 | |
| 268 return '%s%s(%s)' % (macro, suffix, ', '.join(macro_args)) | |
| 269 | |
| 270 | |
| 271 def v8_value_to_local_cpp_value(argument, index, return_promise=False): | |
| 272 extended_attributes = argument.extended_attributes | |
| 273 idl_type = argument.idl_type | |
| 274 name = argument.name | |
| 275 if argument.is_variadic: | |
| 276 return v8_value_to_local_cpp_variadic_value(argument, index, return_prom
ise) | |
| 277 return idl_type.v8_value_to_local_cpp_value(extended_attributes, 'info[%s]'
% index, | |
| 278 name, index=index, declare_varia
ble=False, return_promise=return_promise) | |
| 279 | |
| 280 | |
| 281 ################################################################################ | |
| 282 # Auxiliary functions | |
| 283 ################################################################################ | |
| 284 | |
| 285 def union_member_argument_context(idl_type, index): | |
| 286 """Returns a context of union member for argument.""" | |
| 287 this_cpp_value = 'result%d' % index | |
| 288 this_cpp_type = idl_type.cpp_type | |
| 289 this_cpp_type_initializer = idl_type.cpp_type_initializer | |
| 290 cpp_return_value = this_cpp_value | |
| 291 | |
| 292 if not idl_type.cpp_type_has_null_value: | |
| 293 this_cpp_type = v8_types.cpp_template_type('Nullable', this_cpp_type) | |
| 294 this_cpp_type_initializer = '' | |
| 295 cpp_return_value = '%s.get()' % this_cpp_value | |
| 296 | |
| 297 if idl_type.is_string_type: | |
| 298 null_check_value = '!%s.isNull()' % this_cpp_value | |
| 299 else: | |
| 300 null_check_value = this_cpp_value | |
| 301 | |
| 302 return { | |
| 303 'cpp_type': this_cpp_type, | |
| 304 'cpp_type_initializer': this_cpp_type_initializer, | |
| 305 'cpp_value': this_cpp_value, | |
| 306 'null_check_value': null_check_value, | |
| 307 } | |
| 308 | |
| 309 | |
| 310 def union_arguments(idl_type): | |
| 311 return [union_member_argument_context(member_idl_type, index) | |
| 312 for index, member_idl_type | |
| 313 in enumerate(idl_type.member_types)] | |
| 314 | |
| 315 | |
| 316 def argument_default_cpp_value(argument): | |
| 317 if not argument.default_value: | |
| 318 return None | |
| 319 return argument.idl_type.literal_cpp_value(argument.default_value) | |
| 320 | |
| 321 IdlTypeBase.union_arguments = None | |
| 322 IdlUnionType.union_arguments = property(union_arguments) | |
| 323 IdlArgument.default_cpp_value = property(argument_default_cpp_value) | |
| OLD | NEW |