| 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 this_cpp_type.append("RefPtr<%s>" % cpp_type) | |
| 61 else: | |
| 62 this_cpp_type = idl_type.cpp_type | |
| 63 | |
| 64 is_auto_scope = not 'DartNoAutoScope' in extended_attributes | |
| 65 | |
| 66 arguments_data = [argument_context(interface, method, argument, index) | |
| 67 for index, argument in enumerate(arguments)] | |
| 68 | |
| 69 union_arguments = [] | |
| 70 if idl_type.union_arguments: | |
| 71 union_arguments.extend([union_arg['cpp_value'] | |
| 72 for union_arg in idl_type.union_arguments]) | |
| 73 | |
| 74 is_custom = 'Custom' in extended_attributes or 'DartCustom' in extended_attr
ibutes | |
| 75 | |
| 76 context.update({ | |
| 77 'arguments': arguments_data, | |
| 78 'cpp_type': this_cpp_type, | |
| 79 'cpp_value': this_cpp_value, | |
| 80 'dart_type': dart_types.idl_type_to_dart_type(idl_type), | |
| 81 'dart_name': extended_attributes.get('DartName'), | |
| 82 'has_exception_state': | |
| 83 context['is_raises_exception'] or | |
| 84 any(argument for argument in arguments | |
| 85 if argument.idl_type.name == 'SerializedScriptValue' or | |
| 86 argument.idl_type.is_integer_type), | |
| 87 'is_auto_scope': is_auto_scope, | |
| 88 'auto_scope': DartUtilities.bool_to_cpp(is_auto_scope), | |
| 89 'is_custom': is_custom, | |
| 90 'is_custom_dart': 'DartCustom' in extended_attributes, | |
| 91 'is_custom_dart_new': DartUtilities.has_extended_attribute_value(method,
'DartCustom', 'New'), | |
| 92 # FIXME(terry): DartStrictTypeChecking no longer supported; TypeChecking
is | |
| 93 # new extended attribute. | |
| 94 'is_strict_type_checking': | |
| 95 'DartStrictTypeChecking' in extended_attributes or | |
| 96 'DartStrictTypeChecking' in interface.extended_attributes, | |
| 97 'union_arguments': union_arguments, | |
| 98 'dart_set_return_value': dart_set_return_value(interface.name, method, t
his_cpp_value), | |
| 99 }) | |
| 100 return context | |
| 101 | |
| 102 def argument_context(interface, method, argument, index): | |
| 103 context = v8_methods.argument_context(interface, method, argument, index) | |
| 104 | |
| 105 extended_attributes = argument.extended_attributes | |
| 106 idl_type = argument.idl_type | |
| 107 this_cpp_value = cpp_value(interface, method, index) | |
| 108 auto_scope = not 'DartNoAutoScope' in extended_attributes | |
| 109 arg_index = index + 1 if not (method.is_static or method.is_constructor) els
e index | |
| 110 preprocessed_type = str(idl_type.preprocessed_type) | |
| 111 local_cpp_type = idl_type.cpp_type_args(argument.extended_attributes, raw_ty
pe=True) | |
| 112 default_value = argument.default_cpp_value | |
| 113 if context['has_default']: | |
| 114 default_value = (argument.default_cpp_value or | |
| 115 dart_types.default_cpp_value_for_cpp_type(idl_type)) | |
| 116 dart_type = dart_types.idl_type_to_dart_type(idl_type) | |
| 117 dart_default_value = dart_types.dart_default_value(dart_type, argument) | |
| 118 context.update({ | |
| 119 'cpp_type': idl_type.cpp_type_args(extended_attributes=extended_attribut
es, | |
| 120 raw_type=True, | |
| 121 used_in_cpp_sequence=False), | |
| 122 'dart_type': dart_type, | |
| 123 'implemented_as': idl_type.implemented_as, | |
| 124 'cpp_value': this_cpp_value, | |
| 125 'local_cpp_type': local_cpp_type, | |
| 126 # FIXME: check that the default value's type is compatible with the argu
ment's | |
| 127 'default_value': default_value, | |
| 128 'dart_default_value': dart_default_value, | |
| 129 'enum_validation_expression': idl_type.enum_validation_expression, | |
| 130 'preprocessed_type': preprocessed_type, | |
| 131 'is_array_or_sequence_type': not not idl_type.native_array_element_type, | |
| 132 'is_strict_type_checking': 'DartStrictTypeChecking' in extended_attribut
es, | |
| 133 'dart_set_return_value_for_main_world': dart_set_return_value(interface.
name, method, | |
| 134 this_cpp_v
alue, for_main_world=True), | |
| 135 'dart_set_return_value': dart_set_return_value(interface.name, method, t
his_cpp_value), | |
| 136 'arg_index': arg_index, | |
| 137 'dart_value_to_local_cpp_value': dart_value_to_local_cpp_value(interface
, | |
| 138 context['
has_type_checking_interface'], | |
| 139 argument,
arg_index, auto_scope), | |
| 140 }) | |
| 141 return context | |
| 142 | |
| 143 | |
| 144 ################################################################################ | |
| 145 # Value handling | |
| 146 ################################################################################ | |
| 147 | |
| 148 def cpp_value(interface, method, number_of_arguments): | |
| 149 def cpp_argument(argument): | |
| 150 argument_name = dart_types.check_reserved_name(argument.name) | |
| 151 idl_type = argument.idl_type | |
| 152 | |
| 153 if idl_type.is_typed_array_type: | |
| 154 return '%s.get()' % argument_name | |
| 155 | |
| 156 if idl_type.name == 'EventListener': | |
| 157 if (interface.name == 'EventTarget' and | |
| 158 method.name == 'removeEventListener'): | |
| 159 # FIXME: remove this special case by moving get() into | |
| 160 # EventTarget::removeEventListener | |
| 161 return '%s.get()' % argument_name | |
| 162 return argument.name | |
| 163 if (idl_type.is_callback_interface or | |
| 164 idl_type.name in ['NodeFilter', 'XPathNSResolver']): | |
| 165 # FIXME: remove this special case | |
| 166 return '%s.release()' % argument_name | |
| 167 return argument_name | |
| 168 | |
| 169 # Truncate omitted optional arguments | |
| 170 arguments = method.arguments[:number_of_arguments] | |
| 171 if method.is_constructor: | |
| 172 call_with_values = interface.extended_attributes.get('ConstructorCallWit
h') | |
| 173 else: | |
| 174 call_with_values = method.extended_attributes.get('CallWith') | |
| 175 cpp_arguments = DartUtilities.call_with_arguments(call_with_values) | |
| 176 if ('PartialInterfaceImplementedAs' in method.extended_attributes and not me
thod.is_static): | |
| 177 cpp_arguments.append('*receiver') | |
| 178 | |
| 179 cpp_arguments.extend(cpp_argument(argument) for argument in arguments) | |
| 180 this_union_arguments = method.idl_type and method.idl_type.union_arguments | |
| 181 if this_union_arguments: | |
| 182 cpp_arguments.extend([member_argument['cpp_value'] | |
| 183 for member_argument in this_union_arguments]) | |
| 184 | |
| 185 if ('RaisesException' in method.extended_attributes or | |
| 186 (method.is_constructor and | |
| 187 DartUtilities.has_extended_attribute_value(interface, 'RaisesException'
, 'Constructor'))): | |
| 188 cpp_arguments.append('es') | |
| 189 | |
| 190 if method.name == 'Constructor': | |
| 191 base_name = 'create' | |
| 192 elif method.name == 'NamedConstructor': | |
| 193 base_name = 'createForJSConstructor' | |
| 194 else: | |
| 195 base_name = DartUtilities.cpp_name(method) | |
| 196 cpp_method_name = DartUtilities.scoped_name(interface, method, base_name) | |
| 197 return '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments)) | |
| 198 | |
| 199 | |
| 200 def dart_set_return_value(interface_name, method, cpp_value, for_main_world=Fals
e): | |
| 201 idl_type = method.idl_type | |
| 202 extended_attributes = method.extended_attributes | |
| 203 if not idl_type or idl_type.name == 'void': | |
| 204 # Constructors and void methods don't have a return type | |
| 205 return None | |
| 206 | |
| 207 release = False | |
| 208 | |
| 209 if idl_type.is_union_type: | |
| 210 release = idl_type.release | |
| 211 | |
| 212 # [CallWith=ScriptState], [RaisesException] | |
| 213 # TODO(terry): Disable ScriptState temporarily need to handle. | |
| 214 # if (has_extended_attribute_value(method, 'CallWith', 'ScriptState') or | |
| 215 # 'RaisesException' in extended_attributes or | |
| 216 # idl_type.is_union_type): | |
| 217 # cpp_value = 'result' # use local variable for value | |
| 218 # release = idl_type.release | |
| 219 | |
| 220 auto_scope = not 'DartNoAutoScope' in extended_attributes | |
| 221 script_wrappable = 'impl' if inherits_interface(interface_name, 'Node') else
'' | |
| 222 return idl_type.dart_set_return_value(cpp_value, extended_attributes, | |
| 223 script_wrappable=script_wrappable, | |
| 224 release=release, | |
| 225 for_main_world=for_main_world, | |
| 226 auto_scope=auto_scope) | |
| 227 | |
| 228 | |
| 229 def dart_value_to_local_cpp_value(interface, has_type_checking_interface, | |
| 230 argument, index, auto_scope=True): | |
| 231 extended_attributes = argument.extended_attributes | |
| 232 idl_type = argument.idl_type | |
| 233 name = argument.name | |
| 234 | |
| 235 # FIXME: V8 has some special logic around the addEventListener and | |
| 236 # removeEventListener methods that should be added in somewhere. | |
| 237 # There is also some logic in systemnative.py to force a null check | |
| 238 # for the useCapture argument of those same methods that we may need to | |
| 239 # pull over. | |
| 240 null_check = ((argument.is_optional and idl_type.is_callback_interface) or | |
| 241 (argument.default_value and argument.default_value.is_null)) | |
| 242 | |
| 243 return idl_type.dart_value_to_local_cpp_value( | |
| 244 extended_attributes, name, null_check, has_type_checking_interface, | |
| 245 index=index, auto_scope=auto_scope) | |
| OLD | NEW |