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 """Functions for type handling and type conversion (Blink/C++ <-> V8/JS). |
| 30 |
| 31 Extends IdlType and IdlUnionType with V8-specific properties, methods, and |
| 32 class methods. |
| 33 |
| 34 Spec: |
| 35 http://www.w3.org/TR/WebIDL/#es-type-mapping |
| 36 |
| 37 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler |
| 38 """ |
| 39 |
| 40 import posixpath |
| 41 |
| 42 from idl_types import IdlType, IdlUnionType |
| 43 import v8_attributes # for IdlType.constructor_type_name |
| 44 from v8_globals import includes |
| 45 |
| 46 |
| 47 ################################################################################ |
| 48 # V8-specific handling of IDL types |
| 49 ################################################################################ |
| 50 |
| 51 NON_WRAPPER_TYPES = frozenset([ |
| 52 'CompareHow', |
| 53 'Dictionary', |
| 54 'EventHandler', |
| 55 'EventListener', |
| 56 'MediaQueryListListener', |
| 57 'NodeFilter', |
| 58 'SerializedScriptValue', |
| 59 ]) |
| 60 TYPED_ARRAYS = { |
| 61 # (cpp_type, v8_type), used by constructor templates |
| 62 'ArrayBuffer': None, |
| 63 'ArrayBufferView': None, |
| 64 'Float32Array': ('float', 'v8::kExternalFloatArray'), |
| 65 'Float64Array': ('double', 'v8::kExternalDoubleArray'), |
| 66 'Int8Array': ('signed char', 'v8::kExternalByteArray'), |
| 67 'Int16Array': ('short', 'v8::kExternalShortArray'), |
| 68 'Int32Array': ('int', 'v8::kExternalIntArray'), |
| 69 'Uint8Array': ('unsigned char', 'v8::kExternalUnsignedByteArray'), |
| 70 'Uint8ClampedArray': ('unsigned char', 'v8::kExternalPixelArray'), |
| 71 'Uint16Array': ('unsigned short', 'v8::kExternalUnsignedShortArray'), |
| 72 'Uint32Array': ('unsigned int', 'v8::kExternalUnsignedIntArray'), |
| 73 } |
| 74 |
| 75 IdlType.is_typed_array_type = property( |
| 76 lambda self: self.base_type in TYPED_ARRAYS) |
| 77 |
| 78 |
| 79 IdlType.is_wrapper_type = property( |
| 80 lambda self: (self.is_interface_type and |
| 81 self.base_type not in NON_WRAPPER_TYPES)) |
| 82 |
| 83 |
| 84 ################################################################################ |
| 85 # C++ types |
| 86 ################################################################################ |
| 87 |
| 88 CPP_TYPE_SAME_AS_IDL_TYPE = set([ |
| 89 'double', |
| 90 'float', |
| 91 'long long', |
| 92 'unsigned long long', |
| 93 ]) |
| 94 CPP_INT_TYPES = set([ |
| 95 'byte', |
| 96 'long', |
| 97 'short', |
| 98 ]) |
| 99 CPP_UNSIGNED_TYPES = set([ |
| 100 'octet', |
| 101 'unsigned int', |
| 102 'unsigned long', |
| 103 'unsigned short', |
| 104 ]) |
| 105 CPP_SPECIAL_CONVERSION_RULES = { |
| 106 'CompareHow': 'Range::CompareHow', |
| 107 'Date': 'double', |
| 108 'Dictionary': 'Dictionary', |
| 109 'EventHandler': 'EventListener*', |
| 110 'MediaQueryListListener': 'RefPtrWillBeRawPtr<MediaQueryListListener>', |
| 111 'NodeFilter': 'RefPtrWillBeRawPtr<NodeFilter>', |
| 112 'Promise': 'ScriptPromise', |
| 113 'ScriptValue': 'ScriptValue', |
| 114 # FIXME: Eliminate custom bindings for XPathNSResolver http://crbug.com/345
529 |
| 115 'XPathNSResolver': 'RefPtrWillBeRawPtr<XPathNSResolver>', |
| 116 'boolean': 'bool', |
| 117 'unrestricted double': 'double', |
| 118 'unrestricted float': 'float', |
| 119 } |
| 120 |
| 121 |
| 122 def cpp_type(idl_type, extended_attributes=None, used_as_argument=False, used_as
_variadic_argument=False, used_in_cpp_sequence=False): |
| 123 """Returns C++ type corresponding to IDL type. |
| 124 |
| 125 |idl_type| argument is of type IdlType, while return value is a string |
| 126 |
| 127 Args: |
| 128 idl_type: |
| 129 IdlType |
| 130 used_as_argument: |
| 131 bool, True if idl_type's raw/primitive C++ type should be returned. |
| 132 used_in_cpp_sequence: |
| 133 bool, True if the C++ type is used as an element of an array or sequ
ence. |
| 134 """ |
| 135 def string_mode(): |
| 136 # FIXME: the Web IDL spec requires 'EmptyString', not 'NullString', |
| 137 # but we use NullString for performance. |
| 138 if extended_attributes.get('TreatNullAs') != 'NullString': |
| 139 return '' |
| 140 if extended_attributes.get('TreatUndefinedAs') != 'NullString': |
| 141 return 'WithNullCheck' |
| 142 return 'WithUndefinedOrNullCheck' |
| 143 |
| 144 extended_attributes = extended_attributes or {} |
| 145 idl_type = idl_type.preprocessed_type |
| 146 |
| 147 # Composite types |
| 148 if used_as_variadic_argument: |
| 149 array_or_sequence_type = idl_type |
| 150 else: |
| 151 array_or_sequence_type = idl_type.array_or_sequence_type |
| 152 if array_or_sequence_type: |
| 153 vector_type = cpp_ptr_type('Vector', 'HeapVector', array_or_sequence_typ
e.gc_type) |
| 154 return cpp_template_type(vector_type, array_or_sequence_type.cpp_type_ar
gs(used_in_cpp_sequence=True)) |
| 155 |
| 156 # Simple types |
| 157 base_idl_type = idl_type.base_type |
| 158 |
| 159 if base_idl_type in CPP_TYPE_SAME_AS_IDL_TYPE: |
| 160 return base_idl_type |
| 161 if base_idl_type in CPP_INT_TYPES: |
| 162 return 'int' |
| 163 if base_idl_type in CPP_UNSIGNED_TYPES: |
| 164 return 'unsigned' |
| 165 if base_idl_type in CPP_SPECIAL_CONVERSION_RULES: |
| 166 return CPP_SPECIAL_CONVERSION_RULES[base_idl_type] |
| 167 |
| 168 if base_idl_type in NON_WRAPPER_TYPES: |
| 169 return 'RefPtr<%s>' % base_idl_type |
| 170 if base_idl_type in ('DOMString', 'ByteString', 'ScalarValueString'): |
| 171 if not used_as_argument: |
| 172 return 'String' |
| 173 return 'V8StringResource<%s>' % string_mode() |
| 174 |
| 175 if idl_type.is_typed_array_type and used_as_argument: |
| 176 return base_idl_type + '*' |
| 177 if idl_type.is_interface_type: |
| 178 implemented_as_class = idl_type.implemented_as |
| 179 if used_as_argument: |
| 180 return implemented_as_class + '*' |
| 181 new_type = 'Member' if used_in_cpp_sequence else 'RawPtr' |
| 182 ptr_type = cpp_ptr_type('RefPtr', new_type, idl_type.gc_type) |
| 183 return cpp_template_type(ptr_type, implemented_as_class) |
| 184 # Default, assume native type is a pointer with same type name as idl type |
| 185 return base_idl_type + '*' |
| 186 |
| 187 |
| 188 def cpp_type_union(idl_type, extended_attributes=None, used_as_argument=False): |
| 189 return (member_type.cpp_type for member_type in idl_type.member_types) |
| 190 |
| 191 |
| 192 # Allow access as idl_type.cpp_type if no arguments |
| 193 IdlType.cpp_type = property(cpp_type) |
| 194 IdlUnionType.cpp_type = property(cpp_type_union) |
| 195 IdlType.cpp_type_args = cpp_type |
| 196 IdlUnionType.cpp_type_args = cpp_type_union |
| 197 |
| 198 |
| 199 def cpp_template_type(template, inner_type): |
| 200 """Returns C++ template specialized to type, with space added if needed.""" |
| 201 if inner_type.endswith('>'): |
| 202 format_string = '{template}<{inner_type} >' |
| 203 else: |
| 204 format_string = '{template}<{inner_type}>' |
| 205 return format_string.format(template=template, inner_type=inner_type) |
| 206 |
| 207 |
| 208 def cpp_ptr_type(old_type, new_type, gc_type): |
| 209 if gc_type == 'GarbageCollectedObject': |
| 210 return new_type |
| 211 if gc_type == 'WillBeGarbageCollectedObject': |
| 212 if old_type == 'Vector': |
| 213 return 'WillBe' + new_type |
| 214 return old_type + 'WillBe' + new_type |
| 215 return old_type |
| 216 |
| 217 |
| 218 def v8_type(interface_name): |
| 219 return 'V8' + interface_name |
| 220 |
| 221 |
| 222 # [ImplementedAs] |
| 223 # This handles [ImplementedAs] on interface types, not [ImplementedAs] in the |
| 224 # interface being generated. e.g., given: |
| 225 # Foo.idl: interface Foo {attribute Bar bar}; |
| 226 # Bar.idl: [ImplementedAs=Zork] interface Bar {}; |
| 227 # when generating bindings for Foo, the [ImplementedAs] on Bar is needed. |
| 228 # This data is external to Foo.idl, and hence computed as global information in |
| 229 # compute_interfaces_info.py to avoid having to parse IDLs of all used interface
s. |
| 230 IdlType.implemented_as_interfaces = {} |
| 231 |
| 232 |
| 233 def implemented_as(idl_type): |
| 234 base_idl_type = idl_type.base_type |
| 235 if base_idl_type in IdlType.implemented_as_interfaces: |
| 236 return IdlType.implemented_as_interfaces[base_idl_type] |
| 237 return base_idl_type |
| 238 |
| 239 |
| 240 IdlType.implemented_as = property(implemented_as) |
| 241 |
| 242 IdlType.set_implemented_as_interfaces = classmethod( |
| 243 lambda cls, new_implemented_as_interfaces: |
| 244 cls.implemented_as_interfaces.update(new_implemented_as_interfaces)) |
| 245 |
| 246 |
| 247 # [GarbageCollected] |
| 248 IdlType.garbage_collected_types = set() |
| 249 |
| 250 IdlType.is_garbage_collected = property( |
| 251 lambda self: self.base_type in IdlType.garbage_collected_types) |
| 252 |
| 253 IdlType.set_garbage_collected_types = classmethod( |
| 254 lambda cls, new_garbage_collected_types: |
| 255 cls.garbage_collected_types.update(new_garbage_collected_types)) |
| 256 |
| 257 |
| 258 # [WillBeGarbageCollected] |
| 259 IdlType.will_be_garbage_collected_types = set() |
| 260 |
| 261 IdlType.is_will_be_garbage_collected = property( |
| 262 lambda self: self.base_type in IdlType.will_be_garbage_collected_types) |
| 263 |
| 264 IdlType.set_will_be_garbage_collected_types = classmethod( |
| 265 lambda cls, new_will_be_garbage_collected_types: |
| 266 cls.will_be_garbage_collected_types.update(new_will_be_garbage_collected
_types)) |
| 267 |
| 268 |
| 269 def gc_type(idl_type): |
| 270 if idl_type.is_garbage_collected: |
| 271 return 'GarbageCollectedObject' |
| 272 if idl_type.is_will_be_garbage_collected: |
| 273 return 'WillBeGarbageCollectedObject' |
| 274 return 'RefCountedObject' |
| 275 |
| 276 IdlType.gc_type = property(gc_type) |
| 277 |
| 278 |
| 279 ################################################################################ |
| 280 # Includes |
| 281 ################################################################################ |
| 282 |
| 283 def includes_for_cpp_class(class_name, relative_dir_posix): |
| 284 return set([posixpath.join('bindings', relative_dir_posix, class_name + '.h'
)]) |
| 285 |
| 286 |
| 287 INCLUDES_FOR_TYPE = { |
| 288 'object': set(), |
| 289 'CompareHow': set(), |
| 290 'Dictionary': set(['bindings/common/Dictionary.h']), |
| 291 'EventHandler': set(['bindings/v8/V8AbstractEventListener.h', |
| 292 'bindings/v8/V8EventListenerList.h']), |
| 293 'EventListener': set(['bindings/common/BindingSecurity.h', |
| 294 'bindings/v8/V8EventListenerList.h', |
| 295 'core/frame/LocalDOMWindow.h']), |
| 296 'HTMLCollection': set(['bindings/core/v8/V8HTMLCollection.h', |
| 297 'core/dom/ClassCollection.h', |
| 298 'core/dom/TagCollection.h', |
| 299 'core/html/HTMLCollection.h', |
| 300 'core/html/HTMLFormControlsCollection.h', |
| 301 'core/html/HTMLTableRowsCollection.h']), |
| 302 'MediaQueryListListener': set(['core/css/MediaQueryListListener.h']), |
| 303 'NodeList': set(['bindings/core/v8/V8NodeList.h', |
| 304 'core/dom/NameNodeList.h', |
| 305 'core/dom/NodeList.h', |
| 306 'core/dom/StaticNodeList.h', |
| 307 'core/html/LabelsNodeList.h']), |
| 308 'Promise': set(['bindings/v8/ScriptPromise.h']), |
| 309 'SerializedScriptValue': set(['bindings/v8/SerializedScriptValue.h']), |
| 310 'ScriptValue': set(['bindings/common/ScriptValue.h']), |
| 311 } |
| 312 |
| 313 |
| 314 def includes_for_type(idl_type): |
| 315 idl_type = idl_type.preprocessed_type |
| 316 |
| 317 # Composite types |
| 318 array_or_sequence_type = idl_type.array_or_sequence_type |
| 319 if array_or_sequence_type: |
| 320 return includes_for_type(array_or_sequence_type) |
| 321 |
| 322 # Simple types |
| 323 base_idl_type = idl_type.base_type |
| 324 if base_idl_type in INCLUDES_FOR_TYPE: |
| 325 return INCLUDES_FOR_TYPE[base_idl_type] |
| 326 if idl_type.is_basic_type: |
| 327 return set() |
| 328 if idl_type.is_typed_array_type: |
| 329 return set(['bindings/v8/custom/V8%sCustom.h' % base_idl_type]) |
| 330 if base_idl_type.endswith('ConstructorConstructor'): |
| 331 # FIXME: rename to NamedConstructor |
| 332 # FIXME: replace with a [NamedConstructorAttribute] extended attribute |
| 333 # Ending with 'ConstructorConstructor' indicates a named constructor, |
| 334 # and these do not have header files, as they are part of the generated |
| 335 # bindings for the interface |
| 336 return set() |
| 337 if base_idl_type.endswith('Constructor'): |
| 338 # FIXME: replace with a [ConstructorAttribute] extended attribute |
| 339 base_idl_type = idl_type.constructor_type_name |
| 340 return set(['bindings/%s/v8/V8%s.h' % (component_dir[base_idl_type], |
| 341 base_idl_type)]) |
| 342 |
| 343 IdlType.includes_for_type = property(includes_for_type) |
| 344 IdlUnionType.includes_for_type = property( |
| 345 lambda self: set.union(*[includes_for_type(member_type) |
| 346 for member_type in self.member_types])) |
| 347 |
| 348 |
| 349 def add_includes_for_type(idl_type): |
| 350 includes.update(idl_type.includes_for_type) |
| 351 |
| 352 IdlType.add_includes_for_type = add_includes_for_type |
| 353 IdlUnionType.add_includes_for_type = add_includes_for_type |
| 354 |
| 355 |
| 356 def includes_for_interface(interface_name): |
| 357 return IdlType(interface_name).includes_for_type |
| 358 |
| 359 |
| 360 def add_includes_for_interface(interface_name): |
| 361 includes.update(includes_for_interface(interface_name)) |
| 362 |
| 363 component_dir = {} |
| 364 |
| 365 |
| 366 def set_component_dirs(new_component_dirs): |
| 367 component_dir.update(new_component_dirs) |
| 368 |
| 369 |
| 370 ################################################################################ |
| 371 # V8 -> C++ |
| 372 ################################################################################ |
| 373 |
| 374 V8_VALUE_TO_CPP_VALUE = { |
| 375 # Basic |
| 376 'Date': 'toCoreDate({v8_value})', |
| 377 'DOMString': '{v8_value}', |
| 378 'ByteString': 'toByteString({arguments})', |
| 379 'ScalarValueString': 'toScalarValueString({arguments})', |
| 380 'boolean': '{v8_value}->BooleanValue()', |
| 381 'float': 'static_cast<float>({v8_value}->NumberValue())', |
| 382 'unrestricted float': 'static_cast<float>({v8_value}->NumberValue())', |
| 383 'double': 'static_cast<double>({v8_value}->NumberValue())', |
| 384 'unrestricted double': 'static_cast<double>({v8_value}->NumberValue())', |
| 385 'byte': 'toInt8({arguments})', |
| 386 'octet': 'toUInt8({arguments})', |
| 387 'short': 'toInt16({arguments})', |
| 388 'unsigned short': 'toUInt16({arguments})', |
| 389 'long': 'toInt32({arguments})', |
| 390 'unsigned long': 'toUInt32({arguments})', |
| 391 'long long': 'toInt64({arguments})', |
| 392 'unsigned long long': 'toUInt64({arguments})', |
| 393 # Interface types |
| 394 'CompareHow': 'static_cast<Range::CompareHow>({v8_value}->Int32Value())', |
| 395 'Dictionary': 'Dictionary({v8_value}, info.GetIsolate())', |
| 396 'EventTarget': 'V8DOMWrapper::isDOMWrapper({v8_value}) ? toWrapperTypeInfo(v
8::Handle<v8::Object>::Cast({v8_value}))->toEventTarget(v8::Handle<v8::Object>::
Cast({v8_value})) : 0', |
| 397 'MediaQueryListListener': 'MediaQueryListListener::create(V8ScriptState::cur
rent(info.GetIsolate()), ScriptValue(V8ScriptState::current(info.GetIsolate()),
{v8_value}))', |
| 398 'NodeFilter': 'toNodeFilter({v8_value}, info.Holder(), V8ScriptState::curren
t(info.GetIsolate()))', |
| 399 'Promise': 'ScriptPromise::cast(V8ScriptState::current(info.GetIsolate()), {
v8_value})', |
| 400 'SerializedScriptValue': 'SerializedScriptValue::create({v8_value}, info.Get
Isolate())', |
| 401 'ScriptValue': 'ScriptValue(V8ScriptState::current(info.GetIsolate()), {v8_v
alue})', |
| 402 'Window': 'toDOMWindow({v8_value}, info.GetIsolate())', |
| 403 'XPathNSResolver': 'toXPathNSResolver({v8_value}, info.GetIsolate())', |
| 404 } |
| 405 |
| 406 |
| 407 def v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, index): |
| 408 # Composite types |
| 409 array_or_sequence_type = idl_type.array_or_sequence_type |
| 410 if array_or_sequence_type: |
| 411 return v8_value_to_cpp_value_array_or_sequence(array_or_sequence_type, v
8_value, index) |
| 412 |
| 413 # Simple types |
| 414 idl_type = idl_type.preprocessed_type |
| 415 add_includes_for_type(idl_type) |
| 416 base_idl_type = idl_type.base_type |
| 417 |
| 418 if 'EnforceRange' in extended_attributes: |
| 419 arguments = ', '.join([v8_value, 'EnforceRange', 'exceptionState']) |
| 420 elif (idl_type.is_integer_type or # NormalConversion |
| 421 idl_type.name in ('ByteString', 'ScalarValueString')): |
| 422 arguments = ', '.join([v8_value, 'exceptionState']) |
| 423 else: |
| 424 arguments = v8_value |
| 425 |
| 426 if base_idl_type in V8_VALUE_TO_CPP_VALUE: |
| 427 cpp_expression_format = V8_VALUE_TO_CPP_VALUE[base_idl_type] |
| 428 elif idl_type.is_typed_array_type: |
| 429 cpp_expression_format = ( |
| 430 '{v8_value}->Is{idl_type}() ? ' |
| 431 'V8{idl_type}::toNative(v8::Handle<v8::{idl_type}>::Cast({v8_value})
) : 0') |
| 432 else: |
| 433 cpp_expression_format = ( |
| 434 'V8{idl_type}::toNativeWithTypeCheck(info.GetIsolate(), {v8_value})'
) |
| 435 |
| 436 return cpp_expression_format.format(arguments=arguments, idl_type=base_idl_t
ype, v8_value=v8_value) |
| 437 |
| 438 |
| 439 def v8_value_to_cpp_value_array_or_sequence(array_or_sequence_type, v8_value, in
dex): |
| 440 # Index is None for setters, index (starting at 0) for method arguments, |
| 441 # and is used to provide a human-readable exception message |
| 442 if index is None: |
| 443 index = 0 # special case, meaning "setter" |
| 444 else: |
| 445 index += 1 # human-readable index |
| 446 if (array_or_sequence_type.is_interface_type and |
| 447 array_or_sequence_type.name != 'Dictionary'): |
| 448 this_cpp_type = None |
| 449 ref_ptr_type = cpp_ptr_type('RefPtr', 'Member', array_or_sequence_type.g
c_type) |
| 450 expression_format = '(to{ref_ptr_type}NativeArray<{array_or_sequence_typ
e}, V8{array_or_sequence_type}>({v8_value}, {index}, info.GetIsolate()))' |
| 451 add_includes_for_type(array_or_sequence_type) |
| 452 else: |
| 453 ref_ptr_type = None |
| 454 this_cpp_type = array_or_sequence_type.cpp_type |
| 455 expression_format = 'toNativeArray<{cpp_type}>({v8_value}, {index}, info
.GetIsolate())' |
| 456 expression = expression_format.format(array_or_sequence_type=array_or_sequen
ce_type.name, cpp_type=this_cpp_type, index=index, ref_ptr_type=ref_ptr_type, v8
_value=v8_value) |
| 457 return expression |
| 458 |
| 459 |
| 460 def v8_value_to_local_cpp_value(idl_type, extended_attributes, v8_value, variabl
e_name, index=None, declare_variable=True): |
| 461 """Returns an expression that converts a V8 value to a C++ value and stores
it as a local value.""" |
| 462 this_cpp_type = idl_type.cpp_type_args(extended_attributes=extended_attribut
es, used_as_argument=True) |
| 463 |
| 464 idl_type = idl_type.preprocessed_type |
| 465 cpp_value = v8_value_to_cpp_value(idl_type, extended_attributes, v8_value, i
ndex) |
| 466 args = [variable_name, cpp_value] |
| 467 if idl_type.base_type == 'DOMString' and not idl_type.array_or_sequence_type
: |
| 468 macro = 'TOSTRING_VOID' |
| 469 elif (idl_type.is_integer_type or |
| 470 idl_type.name in ('ByteString', 'ScalarValueString')): |
| 471 macro = 'TONATIVE_VOID_EXCEPTIONSTATE' |
| 472 args.append('exceptionState') |
| 473 else: |
| 474 macro = 'TONATIVE_VOID' |
| 475 |
| 476 # Macros come in several variants, to minimize expensive creation of |
| 477 # v8::TryCatch. |
| 478 suffix = '' |
| 479 |
| 480 if declare_variable: |
| 481 args.insert(0, this_cpp_type) |
| 482 else: |
| 483 suffix += '_INTERNAL' |
| 484 |
| 485 return '%s(%s)' % (macro + suffix, ', '.join(args)) |
| 486 |
| 487 |
| 488 IdlType.v8_value_to_local_cpp_value = v8_value_to_local_cpp_value |
| 489 IdlUnionType.v8_value_to_local_cpp_value = v8_value_to_local_cpp_value |
| 490 |
| 491 |
| 492 ################################################################################ |
| 493 # C++ -> V8 |
| 494 ################################################################################ |
| 495 |
| 496 def preprocess_idl_type(idl_type): |
| 497 if idl_type.is_enum: |
| 498 # Enumerations are internally DOMStrings |
| 499 return IdlType('DOMString') |
| 500 if (idl_type.name == 'Any' or idl_type.is_callback_function): |
| 501 return IdlType('ScriptValue') |
| 502 return idl_type |
| 503 |
| 504 IdlType.preprocessed_type = property(preprocess_idl_type) |
| 505 IdlUnionType.preprocessed_type = property(preprocess_idl_type) |
| 506 |
| 507 |
| 508 def preprocess_idl_type_and_value(idl_type, cpp_value, extended_attributes): |
| 509 """Returns IDL type and value, with preliminary type conversions applied.""" |
| 510 idl_type = idl_type.preprocessed_type |
| 511 if idl_type.name == 'Promise': |
| 512 idl_type = IdlType('ScriptValue') |
| 513 if idl_type.base_type in ['long long', 'unsigned long long']: |
| 514 # long long and unsigned long long are not representable in ECMAScript; |
| 515 # we represent them as doubles. |
| 516 idl_type = IdlType('double', is_nullable=idl_type.is_nullable) |
| 517 cpp_value = 'static_cast<double>(%s)' % cpp_value |
| 518 # HTML5 says that unsigned reflected attributes should be in the range |
| 519 # [0, 2^31). When a value isn't in this range, a default value (or 0) |
| 520 # should be returned instead. |
| 521 extended_attributes = extended_attributes or {} |
| 522 if ('Reflect' in extended_attributes and |
| 523 idl_type.base_type in ['unsigned long', 'unsigned short']): |
| 524 cpp_value = cpp_value.replace('getUnsignedIntegralAttribute', |
| 525 'getIntegralAttribute') |
| 526 cpp_value = 'std::max(0, %s)' % cpp_value |
| 527 return idl_type, cpp_value |
| 528 |
| 529 |
| 530 def v8_conversion_type(idl_type, extended_attributes): |
| 531 """Returns V8 conversion type, adding any additional includes. |
| 532 |
| 533 The V8 conversion type is used to select the C++ -> V8 conversion function |
| 534 or v8SetReturnValue* function; it can be an idl_type, a cpp_type, or a |
| 535 separate name for the type of conversion (e.g., 'DOMWrapper'). |
| 536 """ |
| 537 extended_attributes = extended_attributes or {} |
| 538 |
| 539 # Composite types |
| 540 array_or_sequence_type = idl_type.array_or_sequence_type |
| 541 if array_or_sequence_type: |
| 542 if array_or_sequence_type.is_interface_type: |
| 543 add_includes_for_type(array_or_sequence_type) |
| 544 return 'array' |
| 545 |
| 546 # Simple types |
| 547 base_idl_type = idl_type.base_type |
| 548 # Basic types, without additional includes |
| 549 if base_idl_type in CPP_INT_TYPES: |
| 550 return 'int' |
| 551 if base_idl_type in CPP_UNSIGNED_TYPES: |
| 552 return 'unsigned' |
| 553 if base_idl_type in ('DOMString', 'ByteString', 'ScalarValueString'): |
| 554 if 'TreatReturnedNullStringAs' not in extended_attributes: |
| 555 return base_idl_type |
| 556 treat_returned_null_string_as = extended_attributes['TreatReturnedNullSt
ringAs'] |
| 557 if treat_returned_null_string_as == 'Null': |
| 558 return 'StringOrNull' |
| 559 if treat_returned_null_string_as == 'Undefined': |
| 560 return 'StringOrUndefined' |
| 561 raise 'Unrecognized TreatReturnNullStringAs value: "%s"' % treat_returne
d_null_string_as |
| 562 if idl_type.is_basic_type or base_idl_type == 'ScriptValue': |
| 563 return base_idl_type |
| 564 |
| 565 # Data type with potential additional includes |
| 566 add_includes_for_type(idl_type) |
| 567 if base_idl_type in V8_SET_RETURN_VALUE: # Special v8SetReturnValue treatme
nt |
| 568 return base_idl_type |
| 569 |
| 570 # Pointer type |
| 571 return 'DOMWrapper' |
| 572 |
| 573 IdlType.v8_conversion_type = v8_conversion_type |
| 574 |
| 575 |
| 576 V8_SET_RETURN_VALUE = { |
| 577 'boolean': 'v8SetReturnValueBool(info, {cpp_value})', |
| 578 'int': 'v8SetReturnValueInt(info, {cpp_value})', |
| 579 'unsigned': 'v8SetReturnValueUnsigned(info, {cpp_value})', |
| 580 'DOMString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsolate())', |
| 581 'ByteString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsolate())'
, |
| 582 'ScalarValueString': 'v8SetReturnValueString(info, {cpp_value}, info.GetIsol
ate())', |
| 583 # [TreatNullReturnValueAs] |
| 584 'StringOrNull': 'v8SetReturnValueStringOrNull(info, {cpp_value}, info.GetIso
late())', |
| 585 'StringOrUndefined': 'v8SetReturnValueStringOrUndefined(info, {cpp_value}, i
nfo.GetIsolate())', |
| 586 'void': '', |
| 587 # No special v8SetReturnValue* function (set value directly) |
| 588 'float': 'v8SetReturnValue(info, {cpp_value})', |
| 589 'unrestricted float': 'v8SetReturnValue(info, {cpp_value})', |
| 590 'double': 'v8SetReturnValue(info, {cpp_value})', |
| 591 'unrestricted double': 'v8SetReturnValue(info, {cpp_value})', |
| 592 # No special v8SetReturnValue* function, but instead convert value to V8 |
| 593 # and then use general v8SetReturnValue. |
| 594 'array': 'v8SetReturnValue(info, {cpp_value})', |
| 595 'Date': 'v8SetReturnValue(info, {cpp_value})', |
| 596 'EventHandler': 'v8SetReturnValue(info, {cpp_value})', |
| 597 'ScriptValue': 'v8SetReturnValue(info, {cpp_value})', |
| 598 'SerializedScriptValue': 'v8SetReturnValue(info, {cpp_value})', |
| 599 # DOMWrapper |
| 600 'DOMWrapperForMainWorld': 'v8SetReturnValueForMainWorld(info, WTF::getPtr({c
pp_value}))', |
| 601 'DOMWrapperFast': 'v8SetReturnValueFast(info, WTF::getPtr({cpp_value}), {scr
ipt_wrappable})', |
| 602 'DOMWrapperDefault': 'v8SetReturnValue(info, {cpp_value})', |
| 603 } |
| 604 |
| 605 |
| 606 def v8_set_return_value(idl_type, cpp_value, extended_attributes=None, script_wr
appable='', release=False, for_main_world=False): |
| 607 """Returns a statement that converts a C++ value to a V8 value and sets it a
s a return value. |
| 608 |
| 609 """ |
| 610 def dom_wrapper_conversion_type(): |
| 611 if not script_wrappable: |
| 612 return 'DOMWrapperDefault' |
| 613 if for_main_world: |
| 614 return 'DOMWrapperForMainWorld' |
| 615 return 'DOMWrapperFast' |
| 616 |
| 617 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext
ended_attributes) |
| 618 this_v8_conversion_type = idl_type.v8_conversion_type(extended_attributes) |
| 619 # SetReturn-specific overrides |
| 620 if this_v8_conversion_type in ['Date', 'EventHandler', 'ScriptValue', 'Seria
lizedScriptValue', 'array']: |
| 621 # Convert value to V8 and then use general v8SetReturnValue |
| 622 cpp_value = idl_type.cpp_value_to_v8_value(cpp_value, extended_attribute
s=extended_attributes) |
| 623 if this_v8_conversion_type == 'DOMWrapper': |
| 624 this_v8_conversion_type = dom_wrapper_conversion_type() |
| 625 |
| 626 format_string = V8_SET_RETURN_VALUE[this_v8_conversion_type] |
| 627 # FIXME: oilpan: Remove .release() once we remove all RefPtrs from generated
code. |
| 628 if release: |
| 629 cpp_value = '%s.release()' % cpp_value |
| 630 statement = format_string.format(cpp_value=cpp_value, script_wrappable=scrip
t_wrappable) |
| 631 return statement |
| 632 |
| 633 |
| 634 def v8_set_return_value_union(idl_type, cpp_value, extended_attributes=None, scr
ipt_wrappable='', release=False, for_main_world=False): |
| 635 """ |
| 636 release: can be either False (False for all member types) or |
| 637 a sequence (list or tuple) of booleans (if specified individually). |
| 638 """ |
| 639 |
| 640 return [ |
| 641 member_type.v8_set_return_value(cpp_value + str(i), |
| 642 extended_attributes, |
| 643 script_wrappable, |
| 644 release and release[i], |
| 645 for_main_world) |
| 646 for i, member_type in |
| 647 enumerate(idl_type.member_types)] |
| 648 |
| 649 IdlType.v8_set_return_value = v8_set_return_value |
| 650 IdlUnionType.v8_set_return_value = v8_set_return_value_union |
| 651 |
| 652 IdlType.release = property(lambda self: self.is_interface_type) |
| 653 IdlUnionType.release = property( |
| 654 lambda self: [member_type.is_interface_type |
| 655 for member_type in self.member_types]) |
| 656 |
| 657 |
| 658 CPP_VALUE_TO_V8_VALUE = { |
| 659 # Built-in types |
| 660 'Date': 'v8DateOrNaN({cpp_value}, {isolate})', |
| 661 'DOMString': 'v8String({isolate}, {cpp_value})', |
| 662 'ByteString': 'v8String({isolate}, {cpp_value})', |
| 663 'ScalarValueString': 'v8String({isolate}, {cpp_value})', |
| 664 'boolean': 'v8Boolean({cpp_value}, {isolate})', |
| 665 'int': 'v8::Integer::New({isolate}, {cpp_value})', |
| 666 'unsigned': 'v8::Integer::NewFromUnsigned({isolate}, {cpp_value})', |
| 667 'float': 'v8::Number::New({isolate}, {cpp_value})', |
| 668 'unrestricted float': 'v8::Number::New({isolate}, {cpp_value})', |
| 669 'double': 'v8::Number::New({isolate}, {cpp_value})', |
| 670 'unrestricted double': 'v8::Number::New({isolate}, {cpp_value})', |
| 671 'void': 'v8Undefined()', |
| 672 # Special cases |
| 673 'EventHandler': '{cpp_value} ? v8::Handle<v8::Value>(V8AbstractEventListener
::cast({cpp_value})->getListenerObject(impl->executionContext())) : v8::Handle<v
8::Value>(v8::Null({isolate}))', |
| 674 'ScriptValue': '{cpp_value}.v8Value()', |
| 675 'SerializedScriptValue': '{cpp_value} ? {cpp_value}->deserialize() : v8::Han
dle<v8::Value>(v8::Null({isolate}))', |
| 676 # General |
| 677 'array': 'v8Array({cpp_value}, {creation_context}, {isolate})', |
| 678 'DOMWrapper': 'toV8({cpp_value}, {creation_context}, {isolate})', |
| 679 } |
| 680 |
| 681 |
| 682 def cpp_value_to_v8_value(idl_type, cpp_value, isolate='info.GetIsolate()', crea
tion_context='info.Holder()', extended_attributes=None): |
| 683 """Returns an expression that converts a C++ value to a V8 value.""" |
| 684 # the isolate parameter is needed for callback interfaces |
| 685 idl_type, cpp_value = preprocess_idl_type_and_value(idl_type, cpp_value, ext
ended_attributes) |
| 686 this_v8_conversion_type = idl_type.v8_conversion_type(extended_attributes) |
| 687 format_string = CPP_VALUE_TO_V8_VALUE[this_v8_conversion_type] |
| 688 statement = format_string.format(cpp_value=cpp_value, isolate=isolate, creat
ion_context=creation_context) |
| 689 return statement |
| 690 |
| 691 IdlType.cpp_value_to_v8_value = cpp_value_to_v8_value |
OLD | NEW |