| OLD | NEW |
| (Empty) | |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # coding=utf-8 |
| 3 # |
| 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are |
| 6 # met: |
| 7 # |
| 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above |
| 11 # copyright notice, this list of conditions and the following disclaimer |
| 12 # in the documentation and/or other materials provided with the |
| 13 # distribution. |
| 14 # * Neither the name of Google Inc. nor the names of its |
| 15 # contributors may be used to endorse or promote products derived from |
| 16 # this software without specific prior written permission. |
| 17 # |
| 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 """Generate template values for an interface. |
| 31 |
| 32 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler |
| 33 """ |
| 34 |
| 35 from collections import defaultdict |
| 36 import itertools |
| 37 from operator import itemgetter |
| 38 |
| 39 import idl_types |
| 40 from idl_types import IdlType, inherits_interface, IdlArrayOrSequenceType, IdlAr
rayType |
| 41 import dart_attributes |
| 42 import dart_methods |
| 43 import dart_types |
| 44 from dart_utilities import DartUtilities |
| 45 from v8_globals import includes |
| 46 import v8_attributes |
| 47 import v8_interface |
| 48 |
| 49 |
| 50 INTERFACE_H_INCLUDES = frozenset([ |
| 51 'dart/runtime/include/dart_api.h', |
| 52 ]) |
| 53 |
| 54 |
| 55 INTERFACE_CPP_INCLUDES = frozenset([ |
| 56 'sky/engine/bindings2/exception_state.h', |
| 57 'sky/engine/core/script/dom_dart_state.h', |
| 58 'sky/engine/tonic/dart_converter.h', |
| 59 'sky/engine/tonic/dart_wrappable.h', |
| 60 'sky/engine/wtf/GetPtr.h', |
| 61 'sky/engine/wtf/RefPtr.h', |
| 62 ]) |
| 63 |
| 64 # TODO(terry): Rename genenerate_interface to interface_context. |
| 65 def interface_context(interface): |
| 66 context = v8_interface.interface_context(interface) |
| 67 |
| 68 includes.clear() |
| 69 |
| 70 includes.update(INTERFACE_CPP_INCLUDES) |
| 71 header_includes = set(INTERFACE_H_INCLUDES) |
| 72 |
| 73 parent_interface = interface.parent |
| 74 if parent_interface: |
| 75 header_includes.update(dart_types.includes_for_interface(parent_interfac
e)) |
| 76 extended_attributes = interface.extended_attributes |
| 77 |
| 78 if inherits_interface(interface.name, 'EventTarget'): |
| 79 includes.update(['bindings2/dart_event_listener.h']) |
| 80 |
| 81 # [SetWrapperReferenceTo] |
| 82 set_wrapper_reference_to_list = [{ |
| 83 'name': argument.name, |
| 84 # FIXME: properly should be: |
| 85 # 'cpp_type': argument.idl_type.cpp_type_args(used_as_rvalue_type=True), |
| 86 # (if type is non-wrapper type like NodeFilter, normally RefPtr) |
| 87 # Raw pointers faster though, and NodeFilter hacky anyway. |
| 88 'cpp_type': argument.idl_type.implemented_as + '*', |
| 89 'idl_type': argument.idl_type, |
| 90 } for argument in extended_attributes.get('SetWrapperReferenceTo', [])] |
| 91 for set_wrapper_reference_to in set_wrapper_reference_to_list: |
| 92 set_wrapper_reference_to['idl_type'].add_includes_for_type() |
| 93 |
| 94 context.update({ |
| 95 'cpp_class': DartUtilities.cpp_name(interface), |
| 96 'header_includes': header_includes, |
| 97 'set_wrapper_reference_to_list': set_wrapper_reference_to_list, |
| 98 'dart_class': dart_types.dart_type(interface.name), |
| 99 }) |
| 100 |
| 101 # Constructors |
| 102 constructors = [constructor_context(interface, constructor) |
| 103 for constructor in interface.constructors |
| 104 # FIXME: shouldn't put named constructors with constructors |
| 105 # (currently needed for Perl compatibility) |
| 106 # Handle named constructors separately |
| 107 if constructor.name == 'Constructor'] |
| 108 if len(constructors) > 1: |
| 109 context.update({'constructor_overloads': overloads_context(constructors)
}) |
| 110 |
| 111 # [CustomConstructor] |
| 112 custom_constructors = [custom_constructor_context(interface, constructor) |
| 113 for constructor in interface.custom_constructors] |
| 114 |
| 115 # [NamedConstructor] |
| 116 named_constructor = generate_named_constructor(interface) |
| 117 |
| 118 generate_method_native_entries(interface, constructors, 'Constructor') |
| 119 generate_method_native_entries(interface, custom_constructors, 'Constructor'
) |
| 120 if named_constructor: |
| 121 generate_method_native_entries(interface, [named_constructor], |
| 122 'Constructor') |
| 123 event_constructor = None |
| 124 if context['has_event_constructor']: |
| 125 event_constructor = { |
| 126 'native_entries': [ |
| 127 DartUtilities.generate_native_entry( |
| 128 interface.name, None, 'Constructor', False, 2)], |
| 129 } |
| 130 |
| 131 if (context['constructors'] or custom_constructors or context['has_event_con
structor'] or |
| 132 named_constructor): |
| 133 includes.add('core/frame/LocalDOMWindow.h') |
| 134 |
| 135 context.update({ |
| 136 'constructors': constructors, |
| 137 'custom_constructors': custom_constructors, |
| 138 'event_constructor': event_constructor, |
| 139 'has_custom_constructor': bool(custom_constructors), |
| 140 'interface_length': |
| 141 v8_interface.interface_length(interface, constructors + custom_const
ructors), |
| 142 'is_constructor_call_with_document': DartUtilities.has_extended_attribut
e_value( |
| 143 interface, 'ConstructorCallWith', 'Document'), # [ConstructorCallWi
th=Document] |
| 144 'is_constructor_call_with_execution_context': DartUtilities.has_extended
_attribute_value( |
| 145 interface, 'ConstructorCallWith', 'ExecutionContext'), # [Construct
orCallWith=ExeuctionContext] |
| 146 'named_constructor': named_constructor, |
| 147 }) |
| 148 |
| 149 # Attributes |
| 150 attributes = [dart_attributes.attribute_context(interface, attribute) |
| 151 for attribute in interface.attributes |
| 152 if not v8_attributes.is_constructor_attribute(attribute)] |
| 153 context.update({ |
| 154 'attributes': attributes, |
| 155 'has_constructor_attributes': any(attribute['constructor_type'] for attr
ibute in attributes), |
| 156 'has_replaceable_attributes': any(attribute['is_replaceable'] for attrib
ute in attributes), |
| 157 }) |
| 158 |
| 159 # Methods |
| 160 methods = [dart_methods.method_context(interface, method) |
| 161 for method in interface.operations |
| 162 # Skip anonymous special operations (methods name empty). |
| 163 if (method.name and |
| 164 # detect unnamed getters from v8_interface. |
| 165 method.name != 'anonymousNamedGetter')] |
| 166 compute_method_overloads_context(methods) |
| 167 for method in methods: |
| 168 method['do_generate_method_configuration'] = ( |
| 169 # For overloaded methods, only generate one accessor |
| 170 ('overload_index' not in method or method['overload_index'] == 1)) |
| 171 |
| 172 generate_method_native_entries(interface, methods, 'Method') |
| 173 |
| 174 context.update({ |
| 175 'has_method_configuration': any(method['do_generate_method_configuration
'] for method in methods), |
| 176 'methods': methods, |
| 177 }) |
| 178 |
| 179 context.update({ |
| 180 'indexed_property_getter': indexed_property_getter(interface), |
| 181 'indexed_property_setter': indexed_property_setter(interface), |
| 182 'indexed_property_deleter': v8_interface.indexed_property_deleter(interf
ace), |
| 183 'is_override_builtins': 'OverrideBuiltins' in extended_attributes, |
| 184 'named_property_getter': named_property_getter(interface), |
| 185 'named_property_setter': named_property_setter(interface), |
| 186 'named_property_deleter': v8_interface.named_property_deleter(interface)
, |
| 187 }) |
| 188 |
| 189 generate_native_entries_for_specials(interface, context) |
| 190 |
| 191 native_entries = generate_interface_native_entries(context) |
| 192 |
| 193 context.update({ |
| 194 'native_entries': native_entries, |
| 195 }) |
| 196 |
| 197 return context |
| 198 |
| 199 |
| 200 def generate_interface_native_entries(context): |
| 201 entries = {} |
| 202 |
| 203 def add(ne): |
| 204 entries[ne['blink_entry']] = ne |
| 205 |
| 206 def addAll(nes): |
| 207 for ne in nes: |
| 208 add(ne) |
| 209 |
| 210 for constructor in context['constructors']: |
| 211 addAll(constructor['native_entries']) |
| 212 for constructor in context['custom_constructors']: |
| 213 addAll(constructor['native_entries']) |
| 214 if context['named_constructor']: |
| 215 addAll(context['named_constructor']['native_entries']) |
| 216 if context['event_constructor']: |
| 217 addAll(context['event_constructor']['native_entries']) |
| 218 for method in context['methods']: |
| 219 addAll(method['native_entries']) |
| 220 for attribute in context['attributes']: |
| 221 add(attribute['native_entry_getter']) |
| 222 if not attribute['is_read_only'] or attribute['put_forwards']: |
| 223 add(attribute['native_entry_setter']) |
| 224 if context['indexed_property_getter']: |
| 225 addAll(context['indexed_property_getter']['native_entries']) |
| 226 if context['indexed_property_setter']: |
| 227 addAll(context['indexed_property_setter']['native_entries']) |
| 228 if context['indexed_property_deleter']: |
| 229 addAll(context['indexed_property_deleter']['native_entries']) |
| 230 if context['named_property_getter']: |
| 231 addAll(context['named_property_getter']['native_entries']) |
| 232 if context['named_property_setter']: |
| 233 addAll(context['named_property_setter']['native_entries']) |
| 234 if context['named_property_deleter']: |
| 235 addAll(context['named_property_deleter']['native_entries']) |
| 236 return list(entries.values()) |
| 237 |
| 238 |
| 239 def generate_method_native_entry(interface, method, count, kind): |
| 240 name = method.get('name') |
| 241 is_static = bool(method.get('is_static')) |
| 242 native_entry = \ |
| 243 DartUtilities.generate_native_entry(interface.name, name, |
| 244 kind, is_static, count) |
| 245 return native_entry |
| 246 |
| 247 |
| 248 def generate_method_native_entries(interface, methods, kind): |
| 249 for method in methods: |
| 250 native_entries = [] |
| 251 arg_count = method['number_of_arguments'] |
| 252 native_entry = \ |
| 253 generate_method_native_entry(interface, method, arg_count, kind) |
| 254 native_entries.append(native_entry) |
| 255 |
| 256 method.update({'native_entries': native_entries}) |
| 257 |
| 258 |
| 259 ################################################################################ |
| 260 # Overloads |
| 261 ################################################################################ |
| 262 |
| 263 def compute_method_overloads_context(methods): |
| 264 # Regular methods |
| 265 compute_method_overloads_context_by_type([method for method in methods |
| 266 if not method['is_static']]) |
| 267 # Static methods |
| 268 compute_method_overloads_context_by_type([method for method in methods |
| 269 if method['is_static']]) |
| 270 |
| 271 |
| 272 def compute_method_overloads_context_by_type(methods): |
| 273 """Computes |method.overload*| template values. |
| 274 |
| 275 Called separately for static and non-static (regular) methods, |
| 276 as these are overloaded separately. |
| 277 Modifies |method| in place for |method| in |methods|. |
| 278 Doesn't change the |methods| list itself (only the values, i.e. individual |
| 279 methods), so ok to treat these separately. |
| 280 """ |
| 281 # Add overload information only to overloaded methods, so template code can |
| 282 # easily verify if a function is overloaded |
| 283 for name, overloads in v8_interface.method_overloads_by_name(methods): |
| 284 # Resolution function is generated after last overloaded function; |
| 285 # package necessary information into |method.overloads| for that method. |
| 286 overloads[-1]['overloads'] = overloads_context(overloads) |
| 287 overloads[-1]['overloads']['name'] = name |
| 288 |
| 289 |
| 290 def overloads_context(overloads): |
| 291 """Returns |overloads| template values for a single name. |
| 292 |
| 293 Sets |method.overload_index| in place for |method| in |overloads| |
| 294 and returns dict of overall overload template values. |
| 295 """ |
| 296 assert len(overloads) > 1 # only apply to overloaded names |
| 297 for index, method in enumerate(overloads, 1): |
| 298 method['overload_index'] = index |
| 299 |
| 300 effective_overloads_by_length = v8_interface.effective_overload_set_by_lengt
h(overloads) |
| 301 lengths = [length for length, _ in effective_overloads_by_length] |
| 302 name = overloads[0].get('name', '<constructor>') |
| 303 |
| 304 # Check and fail if all overloads with the shortest acceptable arguments |
| 305 # list are runtime enabled, since we would otherwise set 'length' on the |
| 306 # function object to an incorrect value when none of those overloads were |
| 307 # actually enabled at runtime. The exception is if all overloads are |
| 308 # controlled by the same runtime enabled feature, in which case there would |
| 309 # be no function object at all if it is not enabled. |
| 310 shortest_overloads = effective_overloads_by_length[0][1] |
| 311 |
| 312 return { |
| 313 'exposed_test_all': v8_interface.common_value(overloads, 'exposed_test')
, # [Exposed] |
| 314 'length_tests_methods': length_tests_methods(effective_overloads_by_leng
th), |
| 315 # 1. Let maxarg be the length of the longest type list of the |
| 316 # entries in S. |
| 317 'maxarg': lengths[-1], |
| 318 'minarg': lengths[0], |
| 319 'valid_arities': lengths |
| 320 # Only need to report valid arities if there is a gap in the |
| 321 # sequence of possible lengths, otherwise invalid length means |
| 322 # "not enough arguments". |
| 323 if lengths[-1] - lengths[0] != len(lengths) - 1 else None, |
| 324 } |
| 325 |
| 326 |
| 327 def length_tests_methods(effective_overloads_by_length): |
| 328 """Returns sorted list of resolution tests and associated methods, by length
. |
| 329 |
| 330 This builds the main data structure for the overload resolution loop. |
| 331 For a given argument length, bindings test argument at distinguishing |
| 332 argument index, in order given by spec: if it is compatible with |
| 333 (optionality or) type required by an overloaded method, resolve to that |
| 334 method. |
| 335 |
| 336 Returns: |
| 337 [(length, [(test, method)])] |
| 338 """ |
| 339 return [(length, list(resolution_tests_methods(effective_overloads))) |
| 340 for length, effective_overloads in effective_overloads_by_length] |
| 341 |
| 342 |
| 343 DART_CHECK_TYPE = { |
| 344 'ArrayBufferView': 'Dart_IsTypedData({cpp_value})', |
| 345 'ArrayBuffer': 'Dart_IsByteBuffer({cpp_value})', |
| 346 'Uint8Array': 'Dart_GetTypeOfTypedData({cpp_value}) == Dart_TypedData_kUint8
', |
| 347 'Uint8ClampedArray': 'Dart_GetTypeOfTypedData({cpp_value}) == Dart_TypedData
_kUint8Clamped', |
| 348 } |
| 349 |
| 350 |
| 351 def resolution_tests_methods(effective_overloads): |
| 352 """Yields resolution test and associated method, in resolution order, for ef
fective overloads of a given length. |
| 353 |
| 354 This is the heart of the resolution algorithm. |
| 355 http://heycam.github.io/webidl/#dfn-overload-resolution-algorithm |
| 356 |
| 357 Note that a given method can be listed multiple times, with different tests! |
| 358 This is to handle implicit type conversion. |
| 359 |
| 360 Returns: |
| 361 [(test, method)] |
| 362 """ |
| 363 methods = [effective_overload[0] |
| 364 for effective_overload in effective_overloads] |
| 365 if len(methods) == 1: |
| 366 # If only one method with a given length, no test needed |
| 367 yield 'true', methods[0] |
| 368 return |
| 369 |
| 370 # 6. If there is more than one entry in S, then set d to be the |
| 371 # distinguishing argument index for the entries of S. |
| 372 index = v8_interface.distinguishing_argument_index(effective_overloads) |
| 373 # (7-9 are for handling |undefined| values for optional arguments before |
| 374 # the distinguishing argument (as "missing"), so you can specify only some |
| 375 # optional arguments. We don't support this, so we skip these steps.) |
| 376 # 10. If i = d, then: |
| 377 # (d is the distinguishing argument index) |
| 378 # 1. Let V be argi. |
| 379 # Note: This is the argument that will be used to resolve which |
| 380 # overload is selected. |
| 381 cpp_value = 'Dart_GetNativeArgument(args, %s + argOffset)' % index |
| 382 |
| 383 # Extract argument and IDL type to simplify accessing these in each loop. |
| 384 arguments = [method['arguments'][index] for method in methods] |
| 385 arguments_methods = zip(arguments, methods) |
| 386 idl_types = [argument['idl_type_object'] for argument in arguments] |
| 387 idl_types_methods = zip(idl_types, methods) |
| 388 |
| 389 # We can't do a single loop through all methods or simply sort them, because |
| 390 # a method may be listed in multiple steps of the resolution algorithm, and |
| 391 # which test to apply differs depending on the step. |
| 392 # |
| 393 # Instead, we need to go through all methods at each step, either finding |
| 394 # first match (if only one test is allowed) or filtering to matches (if |
| 395 # multiple tests are allowed), and generating an appropriate tests. |
| 396 |
| 397 # 2. If V is undefined, and there is an entry in S whose list of |
| 398 # optionality values has "optional" at index i, then remove from S all |
| 399 # other entries. |
| 400 try: |
| 401 method = next(method for argument, method in arguments_methods |
| 402 if argument['is_optional']) |
| 403 test = 'Dart_IsNull(%s)' % cpp_value |
| 404 yield test, method |
| 405 except StopIteration: |
| 406 pass |
| 407 |
| 408 # 3. Otherwise: if V is null or undefined, and there is an entry in S that |
| 409 # has one of the following types at position i of its type list, |
| 410 # - a nullable type |
| 411 try: |
| 412 method = next(method for idl_type, method in idl_types_methods |
| 413 if idl_type.is_nullable) |
| 414 test = 'Dart_IsNull(%s)' % cpp_value |
| 415 yield test, method |
| 416 except StopIteration: |
| 417 pass |
| 418 |
| 419 # 4. Otherwise: if V is a platform object - but not a platform array |
| 420 # object - and there is an entry in S that has one of the following |
| 421 # types at position i of its type list, |
| 422 # - an interface type that V implements |
| 423 # (Unlike most of these tests, this can return multiple methods, since we |
| 424 # test if it implements an interface. Thus we need a for loop, not a next.) |
| 425 # (We distinguish wrapper types from built-in interface types.) |
| 426 for idl_type, method in ((idl_type, method) |
| 427 for idl_type, method in idl_types_methods |
| 428 if idl_type.is_wrapper_type): |
| 429 fmtstr = 'Dart{idl_type}::hasInstance({cpp_value})' |
| 430 if idl_type.base_type in DART_CHECK_TYPE: |
| 431 fmtstr = DART_CHECK_TYPE[idl_type.base_type] |
| 432 test = fmtstr.format(idl_type=idl_type.base_type, cpp_value=cpp_value) |
| 433 yield test, method |
| 434 |
| 435 # 8. Otherwise: if V is any kind of object except for a native Date object, |
| 436 # a native RegExp object, and there is an entry in S that has one of the |
| 437 # following types at position i of its type list, |
| 438 # - an array type |
| 439 # - a sequence type |
| 440 # ... |
| 441 # - a dictionary |
| 442 try: |
| 443 idl_type, method = next((idl_type, method) |
| 444 for idl_type, method in idl_types_methods |
| 445 if (idl_type.native_array_element_type)) |
| 446 if idl_type.native_array_element_type: |
| 447 # (We test for Array instead of generic Object to type-check.) |
| 448 # FIXME: test for Object during resolution, then have type check for |
| 449 # Array in overloaded method: http://crbug.com/262383 |
| 450 test = 'Dart_IsList(%s)' % cpp_value |
| 451 else: |
| 452 # FIXME: should be '{1}->IsObject() && !{1}->IsDate() && !{1}->IsReg
Exp()'.format(cpp_value) |
| 453 # FIXME: the IsDate and IsRegExp checks can be skipped if we've |
| 454 # already generated tests for them. |
| 455 test = 'Dart_IsInstance(%s)' % cpp_value |
| 456 yield test, method |
| 457 except StopIteration: |
| 458 pass |
| 459 |
| 460 # (Check for exact type matches before performing automatic type conversion; |
| 461 # only needed if distinguishing between primitive types.) |
| 462 if len([idl_type.is_primitive_type for idl_type in idl_types]) > 1: |
| 463 # (Only needed if match in step 11, otherwise redundant.) |
| 464 if any(idl_type.is_string_type or idl_type.is_enum |
| 465 for idl_type in idl_types): |
| 466 # 10. Otherwise: if V is a Number value, and there is an entry in S |
| 467 # that has one of the following types at position i of its type |
| 468 # list, |
| 469 # - a numeric type |
| 470 try: |
| 471 method = next(method for idl_type, method in idl_types_methods |
| 472 if idl_type.is_numeric_type) |
| 473 test = 'Dart_IsNumber(%s)' % cpp_value |
| 474 yield test, method |
| 475 except StopIteration: |
| 476 pass |
| 477 |
| 478 # (Perform automatic type conversion, in order. If any of these match, |
| 479 # that's the end, and no other tests are needed.) To keep this code simple, |
| 480 # we rely on the C++ compiler's dead code elimination to deal with the |
| 481 # redundancy if both cases below trigger. |
| 482 |
| 483 # 11. Otherwise: if there is an entry in S that has one of the following |
| 484 # types at position i of its type list, |
| 485 # - DOMString |
| 486 # - ByteString |
| 487 # - ScalarValueString [a DOMString typedef, per definition.] |
| 488 # - an enumeration type |
| 489 try: |
| 490 method = next(method for idl_type, method in idl_types_methods |
| 491 if idl_type.is_string_type or idl_type.is_enum) |
| 492 yield 'true', method |
| 493 except StopIteration: |
| 494 pass |
| 495 |
| 496 # 12. Otherwise: if there is an entry in S that has one of the following |
| 497 # types at position i of its type list, |
| 498 # - a numeric type |
| 499 try: |
| 500 method = next(method for idl_type, method in idl_types_methods |
| 501 if idl_type.is_numeric_type) |
| 502 yield 'true', method |
| 503 except StopIteration: |
| 504 pass |
| 505 |
| 506 |
| 507 ################################################################################ |
| 508 # Constructors |
| 509 ################################################################################ |
| 510 |
| 511 # [Constructor] |
| 512 def custom_constructor_context(interface, constructor): |
| 513 return { |
| 514 'arguments': [custom_constructor_argument(argument, index) |
| 515 for index, argument in enumerate(constructor.arguments)], |
| 516 'auto_scope': 'true', |
| 517 'is_auto_scope': True, |
| 518 'is_call_with_script_arguments': False, |
| 519 'is_custom': True, |
| 520 'number_of_arguments': len(constructor.arguments), |
| 521 'number_of_required_arguments': |
| 522 v8_interface.number_of_required_arguments(constructor), |
| 523 } |
| 524 |
| 525 |
| 526 # We don't need much from this - just the idl_type_objects and preproceed_type |
| 527 # to use in generating the resolver strings. |
| 528 def custom_constructor_argument(argument, index): |
| 529 return { |
| 530 'idl_type_object': argument.idl_type, |
| 531 'name': argument.name, |
| 532 'preprocessed_type': str(argument.idl_type.preprocessed_type), |
| 533 } |
| 534 |
| 535 |
| 536 # [Constructor] |
| 537 def constructor_context(interface, constructor): |
| 538 return { |
| 539 'arguments': [dart_methods.argument_context(interface, constructor, argu
ment, index) |
| 540 for index, argument in enumerate(constructor.arguments)], |
| 541 'auto_scope': 'true', |
| 542 'cpp_value': dart_methods.cpp_value( |
| 543 interface, constructor, len(constructor.arguments)), |
| 544 'has_exception_state': |
| 545 # [RaisesException=Constructor] |
| 546 interface.extended_attributes.get('RaisesException') == 'Constructor
' or |
| 547 any(argument for argument in constructor.arguments |
| 548 if argument.idl_type.name == 'SerializedScriptValue' or |
| 549 argument.idl_type.is_integer_type), |
| 550 'is_auto_scope': True, |
| 551 'is_call_with_script_arguments': False, |
| 552 'is_constructor': True, |
| 553 'is_custom': False, |
| 554 'is_variadic': False, # Required for overload resolution |
| 555 'number_of_required_arguments': |
| 556 v8_interface.number_of_required_arguments(constructor), |
| 557 'number_of_arguments': len(constructor.arguments), |
| 558 } |
| 559 |
| 560 |
| 561 # [NamedConstructor] |
| 562 def generate_named_constructor(interface): |
| 563 extended_attributes = interface.extended_attributes |
| 564 if 'NamedConstructor' not in extended_attributes: |
| 565 return None |
| 566 # FIXME: parser should return named constructor separately; |
| 567 # included in constructors (and only name stored in extended attribute) |
| 568 # for Perl compatibility |
| 569 idl_constructor = interface.constructors[0] |
| 570 constructor = constructor_context(interface, idl_constructor) |
| 571 # FIXME(vsm): We drop the name. We don't use this in Dart APIs right now. |
| 572 # We probably need to encode this somehow to deal with conflicts. |
| 573 # constructor['name'] = extended_attributes['NamedConstructor'] |
| 574 return constructor |
| 575 |
| 576 |
| 577 ################################################################################ |
| 578 # Special operations (methods) |
| 579 # http://heycam.github.io/webidl/#idl-special-operations |
| 580 ################################################################################ |
| 581 |
| 582 def property_getter(getter, cpp_arguments): |
| 583 def is_null_expression(idl_type): |
| 584 if idl_type.is_union_type: |
| 585 return ' && '.join('!result%sEnabled' % i |
| 586 for i, _ in enumerate(idl_type.member_types)) |
| 587 if idl_type.name == 'String': |
| 588 # FIXME(vsm): This looks V8 specific. |
| 589 return 'result.isNull()' |
| 590 if idl_type.is_interface_type: |
| 591 return '!result' |
| 592 return '' |
| 593 |
| 594 context = v8_interface.property_getter(getter, []) |
| 595 |
| 596 idl_type = getter.idl_type |
| 597 extended_attributes = getter.extended_attributes |
| 598 is_raises_exception = 'RaisesException' in extended_attributes |
| 599 |
| 600 # FIXME: make more generic, so can use dart_methods.cpp_value |
| 601 cpp_method_name = 'receiver->%s' % DartUtilities.cpp_name(getter) |
| 602 |
| 603 if is_raises_exception: |
| 604 cpp_arguments.append('es') |
| 605 union_arguments = idl_type.union_arguments |
| 606 if union_arguments: |
| 607 cpp_arguments.extend([member_argument['cpp_value'] |
| 608 for member_argument in union_arguments]) |
| 609 |
| 610 cpp_value = '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments)) |
| 611 |
| 612 context.update({ |
| 613 'cpp_type': idl_type.cpp_type, |
| 614 'cpp_value': cpp_value, |
| 615 'is_null_expression': is_null_expression(idl_type), |
| 616 'is_raises_exception': is_raises_exception, |
| 617 'name': DartUtilities.cpp_name(getter), |
| 618 'union_arguments': union_arguments, |
| 619 'dart_set_return_value': idl_type.dart_set_return_value('result', |
| 620 extended_attribu
tes=extended_attributes, |
| 621 script_wrappable
='receiver', |
| 622 release=idl_type
.release)}) |
| 623 return context |
| 624 |
| 625 |
| 626 def property_setter(setter): |
| 627 context = v8_interface.property_setter(setter) |
| 628 |
| 629 idl_type = setter.arguments[1].idl_type |
| 630 extended_attributes = setter.extended_attributes |
| 631 |
| 632 context.update({ |
| 633 'dart_value_to_local_cpp_value': idl_type.dart_value_to_local_cpp_value( |
| 634 extended_attributes, 'propertyValue', False, |
| 635 context['has_type_checking_interface']), |
| 636 }) |
| 637 |
| 638 return context |
| 639 |
| 640 |
| 641 ################################################################################ |
| 642 # Indexed properties |
| 643 # http://heycam.github.io/webidl/#idl-indexed-properties |
| 644 ################################################################################ |
| 645 |
| 646 def indexed_property_getter(interface): |
| 647 try: |
| 648 # Find indexed property getter, if present; has form: |
| 649 # getter TYPE [OPTIONAL_IDENTIFIER](unsigned long ARG1) |
| 650 getter = next( |
| 651 method |
| 652 for method in interface.operations |
| 653 if ('getter' in method.specials and |
| 654 len(method.arguments) == 1 and |
| 655 str(method.arguments[0].idl_type) == 'unsigned long')) |
| 656 except StopIteration: |
| 657 return None |
| 658 |
| 659 getter.name = getter.name or 'anonymousIndexedGetter' |
| 660 |
| 661 return property_getter(getter, ['index']) |
| 662 |
| 663 |
| 664 def indexed_property_setter(interface): |
| 665 try: |
| 666 # Find indexed property setter, if present; has form: |
| 667 # setter RETURN_TYPE [OPTIONAL_IDENTIFIER](unsigned long ARG1, ARG_TYPE
ARG2) |
| 668 setter = next( |
| 669 method |
| 670 for method in interface.operations |
| 671 if ('setter' in method.specials and |
| 672 len(method.arguments) == 2 and |
| 673 str(method.arguments[0].idl_type) == 'unsigned long')) |
| 674 except StopIteration: |
| 675 return None |
| 676 |
| 677 return property_setter(setter) |
| 678 |
| 679 |
| 680 ################################################################################ |
| 681 # Named properties |
| 682 # http://heycam.github.io/webidl/#idl-named-properties |
| 683 ################################################################################ |
| 684 |
| 685 def named_property_getter(interface): |
| 686 try: |
| 687 # Find named property getter, if present; has form: |
| 688 # getter TYPE [OPTIONAL_IDENTIFIER](DOMString ARG1) |
| 689 getter = next( |
| 690 method |
| 691 for method in interface.operations |
| 692 if ('getter' in method.specials and |
| 693 len(method.arguments) == 1 and |
| 694 str(method.arguments[0].idl_type) == 'DOMString')) |
| 695 except StopIteration: |
| 696 return None |
| 697 |
| 698 getter.name = getter.name or 'anonymousNamedGetter' |
| 699 |
| 700 return property_getter(getter, ['propertyName']) |
| 701 |
| 702 |
| 703 def named_property_setter(interface): |
| 704 try: |
| 705 # Find named property setter, if present; has form: |
| 706 # setter RETURN_TYPE [OPTIONAL_IDENTIFIER](DOMString ARG1, ARG_TYPE ARG2
) |
| 707 setter = next( |
| 708 method |
| 709 for method in interface.operations |
| 710 if ('setter' in method.specials and |
| 711 len(method.arguments) == 2 and |
| 712 str(method.arguments[0].idl_type) == 'DOMString')) |
| 713 except StopIteration: |
| 714 return None |
| 715 |
| 716 return property_setter(setter) |
| 717 |
| 718 |
| 719 def generate_native_entries_for_specials(interface, context): |
| 720 def add(prop, name, arity): |
| 721 if context[prop]: |
| 722 if 'native_entries' not in context[prop]: |
| 723 context[prop].update({'native_entries': []}) |
| 724 context[prop]['native_entries'].append( |
| 725 DartUtilities.generate_native_entry( |
| 726 interface.name, name, 'Method', False, arity)) |
| 727 |
| 728 pre = ['indexed_property', 'named_property'] |
| 729 post = [('setter', '__setter__', 2), |
| 730 ('getter', '__getter__', 1), |
| 731 ('deleter', '__delete__', 1), |
| 732 ] |
| 733 props = [(p1 + "_" + p2, name, arity) |
| 734 for (p1, (p2, name, arity)) in itertools.product(pre, post)] |
| 735 for t in props: |
| 736 add(*t) |
| 737 |
| 738 for (p, name, arity) in props: |
| 739 if context[p]: |
| 740 if context[p].get('is_custom_property_query'): |
| 741 add(p, '__propertyQuery__', 1) |
| OLD | NEW |