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 |
| 41 import v8_attributes |
| 42 from v8_globals import includes |
| 43 import v8_methods |
| 44 import v8_types |
| 45 from v8_types import cpp_ptr_type, cpp_template_type |
| 46 import v8_utilities |
| 47 from v8_utilities import capitalize, conditional_string, cpp_name, gc_type, has_
extended_attribute_value, runtime_enabled_function_name |
| 48 |
| 49 |
| 50 INTERFACE_H_INCLUDES = frozenset([ |
| 51 'bindings/v8/V8Binding.h', |
| 52 'bindings/v8/V8DOMWrapper.h', |
| 53 'bindings/v8/WrapperTypeInfo.h', |
| 54 'platform/heap/Handle.h', |
| 55 ]) |
| 56 INTERFACE_CPP_INCLUDES = frozenset([ |
| 57 'bindings/v8/V8ExceptionState.h', |
| 58 'bindings/v8/V8DOMConfiguration.h', |
| 59 'bindings/v8/V8HiddenValue.h', |
| 60 'bindings/v8/V8ObjectConstructor.h', |
| 61 'core/dom/ContextFeatures.h', |
| 62 'core/dom/Document.h', |
| 63 'platform/RuntimeEnabledFeatures.h', |
| 64 'platform/TraceEvent.h', |
| 65 'wtf/GetPtr.h', |
| 66 'wtf/RefPtr.h', |
| 67 ]) |
| 68 |
| 69 |
| 70 def generate_interface(interface): |
| 71 includes.clear() |
| 72 includes.update(INTERFACE_CPP_INCLUDES) |
| 73 header_includes = set(INTERFACE_H_INCLUDES) |
| 74 |
| 75 parent_interface = interface.parent |
| 76 if parent_interface: |
| 77 header_includes.update(v8_types.includes_for_interface(parent_interface)
) |
| 78 extended_attributes = interface.extended_attributes |
| 79 |
| 80 is_audio_buffer = inherits_interface(interface.name, 'AudioBuffer') |
| 81 if is_audio_buffer: |
| 82 includes.add('modules/webaudio/AudioBuffer.h') |
| 83 |
| 84 is_document = inherits_interface(interface.name, 'Document') |
| 85 if is_document: |
| 86 includes.update(['bindings/v8/ScriptController.h', |
| 87 'bindings/v8/V8WindowShell.h', |
| 88 'core/frame/LocalFrame.h']) |
| 89 |
| 90 # [ActiveDOMObject] |
| 91 is_active_dom_object = 'ActiveDOMObject' in extended_attributes |
| 92 |
| 93 # [CheckSecurity] |
| 94 is_check_security = 'CheckSecurity' in extended_attributes |
| 95 if is_check_security: |
| 96 includes.add('bindings/common/BindingSecurity.h') |
| 97 |
| 98 # [DependentLifetime] |
| 99 is_dependent_lifetime = 'DependentLifetime' in extended_attributes |
| 100 |
| 101 # [MeasureAs] |
| 102 is_measure_as = 'MeasureAs' in extended_attributes |
| 103 if is_measure_as: |
| 104 includes.add('core/frame/UseCounter.h') |
| 105 |
| 106 # [SetWrapperReferenceFrom] |
| 107 reachable_node_function = extended_attributes.get('SetWrapperReferenceFrom') |
| 108 if reachable_node_function: |
| 109 includes.update(['bindings/v8/V8GCController.h', |
| 110 'core/dom/Element.h']) |
| 111 |
| 112 # [SetWrapperReferenceTo] |
| 113 set_wrapper_reference_to_list = [{ |
| 114 'name': argument.name, |
| 115 # FIXME: properly should be: |
| 116 # 'cpp_type': argument.idl_type.cpp_type_args(used_as_argument=True), |
| 117 # (if type is non-wrapper type like NodeFilter, normally RefPtr) |
| 118 # Raw pointers faster though, and NodeFilter hacky anyway. |
| 119 'cpp_type': argument.idl_type.implemented_as + '*', |
| 120 'idl_type': argument.idl_type, |
| 121 'v8_type': v8_types.v8_type(argument.idl_type.name), |
| 122 } for argument in extended_attributes.get('SetWrapperReferenceTo', [])] |
| 123 for set_wrapper_reference_to in set_wrapper_reference_to_list: |
| 124 set_wrapper_reference_to['idl_type'].add_includes_for_type() |
| 125 |
| 126 # [SpecialWrapFor] |
| 127 if 'SpecialWrapFor' in extended_attributes: |
| 128 special_wrap_for = extended_attributes['SpecialWrapFor'].split('|') |
| 129 else: |
| 130 special_wrap_for = [] |
| 131 for special_wrap_interface in special_wrap_for: |
| 132 v8_types.add_includes_for_interface(special_wrap_interface) |
| 133 |
| 134 # [Custom=Wrap], [SetWrapperReferenceFrom] |
| 135 has_visit_dom_wrapper = ( |
| 136 has_extended_attribute_value(interface, 'Custom', 'VisitDOMWrapper') or |
| 137 reachable_node_function or |
| 138 set_wrapper_reference_to_list) |
| 139 |
| 140 this_gc_type = gc_type(interface) |
| 141 |
| 142 template_contents = { |
| 143 'conditional_string': conditional_string(interface), # [Conditional] |
| 144 'cpp_class': cpp_name(interface), |
| 145 'gc_type': this_gc_type, |
| 146 'has_custom_legacy_call_as_function': has_extended_attribute_value(inter
face, 'Custom', 'LegacyCallAsFunction'), # [Custom=LegacyCallAsFunction] |
| 147 'has_custom_to_v8': has_extended_attribute_value(interface, 'Custom', 'T
oV8'), # [Custom=ToV8] |
| 148 'has_custom_wrap': has_extended_attribute_value(interface, 'Custom', 'Wr
ap'), # [Custom=Wrap] |
| 149 'has_visit_dom_wrapper': has_visit_dom_wrapper, |
| 150 'header_includes': header_includes, |
| 151 'interface_name': interface.name, |
| 152 'is_active_dom_object': is_active_dom_object, |
| 153 'is_audio_buffer': is_audio_buffer, |
| 154 'is_check_security': is_check_security, |
| 155 'is_dependent_lifetime': is_dependent_lifetime, |
| 156 'is_document': is_document, |
| 157 'is_event_target': inherits_interface(interface.name, 'EventTarget'), |
| 158 'is_exception': interface.is_exception, |
| 159 'is_node': inherits_interface(interface.name, 'Node'), |
| 160 'measure_as': v8_utilities.measure_as(interface), # [MeasureAs] |
| 161 'parent_interface': parent_interface, |
| 162 'pass_cpp_type': cpp_template_type( |
| 163 cpp_ptr_type('PassRefPtr', 'RawPtr', this_gc_type), |
| 164 cpp_name(interface)), |
| 165 'reachable_node_function': reachable_node_function, |
| 166 'runtime_enabled_function': runtime_enabled_function_name(interface), #
[RuntimeEnabled] |
| 167 'set_wrapper_reference_to_list': set_wrapper_reference_to_list, |
| 168 'special_wrap_for': special_wrap_for, |
| 169 'v8_class': v8_utilities.v8_class_name(interface), |
| 170 'wrapper_configuration': 'WrapperConfiguration::Dependent' |
| 171 if (has_visit_dom_wrapper or |
| 172 is_active_dom_object or |
| 173 is_dependent_lifetime) |
| 174 else 'WrapperConfiguration::Independent', |
| 175 } |
| 176 |
| 177 # Constructors |
| 178 constructors = [generate_constructor(interface, constructor) |
| 179 for constructor in interface.constructors |
| 180 # FIXME: shouldn't put named constructors with constructors |
| 181 # (currently needed for Perl compatibility) |
| 182 # Handle named constructors separately |
| 183 if constructor.name == 'Constructor'] |
| 184 if len(constructors) > 1: |
| 185 template_contents['constructor_overloads'] = generate_overloads(construc
tors) |
| 186 |
| 187 # [CustomConstructor] |
| 188 custom_constructors = [{ # Only needed for computing interface length |
| 189 'number_of_required_arguments': |
| 190 number_of_required_arguments(constructor), |
| 191 } for constructor in interface.custom_constructors] |
| 192 |
| 193 # [EventConstructor] |
| 194 has_event_constructor = 'EventConstructor' in extended_attributes |
| 195 any_type_attributes = [attribute for attribute in interface.attributes |
| 196 if attribute.idl_type.name == 'Any'] |
| 197 if has_event_constructor: |
| 198 includes.add('bindings/common/Dictionary.h') |
| 199 if any_type_attributes: |
| 200 includes.add('bindings/v8/SerializedScriptValue.h') |
| 201 |
| 202 # [NamedConstructor] |
| 203 named_constructor = generate_named_constructor(interface) |
| 204 |
| 205 if (constructors or custom_constructors or has_event_constructor or |
| 206 named_constructor): |
| 207 includes.add('bindings/v8/V8ObjectConstructor.h') |
| 208 includes.add('core/frame/LocalDOMWindow.h') |
| 209 |
| 210 template_contents.update({ |
| 211 'any_type_attributes': any_type_attributes, |
| 212 'constructors': constructors, |
| 213 'has_custom_constructor': bool(custom_constructors), |
| 214 'has_event_constructor': has_event_constructor, |
| 215 'interface_length': |
| 216 interface_length(interface, constructors + custom_constructors), |
| 217 'is_constructor_call_with_document': has_extended_attribute_value( |
| 218 interface, 'ConstructorCallWith', 'Document'), # [ConstructorCallWi
th=Document] |
| 219 'is_constructor_call_with_execution_context': has_extended_attribute_val
ue( |
| 220 interface, 'ConstructorCallWith', 'ExecutionContext'), # [Construct
orCallWith=ExeuctionContext] |
| 221 'is_constructor_raises_exception': extended_attributes.get('RaisesExcept
ion') == 'Constructor', # [RaisesException=Constructor] |
| 222 'named_constructor': named_constructor, |
| 223 }) |
| 224 |
| 225 # Constants |
| 226 template_contents.update({ |
| 227 'constants': [generate_constant(constant) for constant in interface.cons
tants], |
| 228 'do_not_check_constants': 'DoNotCheckConstants' in extended_attributes, |
| 229 }) |
| 230 |
| 231 # Attributes |
| 232 attributes = [v8_attributes.generate_attribute(interface, attribute) |
| 233 for attribute in interface.attributes |
| 234 if not v8_utilities.dart_custom_method(attribute.extended_attr
ibutes)] |
| 235 template_contents.update({ |
| 236 'attributes': attributes, |
| 237 'has_accessors': any(attribute['is_expose_js_accessors'] for attribute i
n attributes), |
| 238 'has_attribute_configuration': any( |
| 239 not (attribute['is_expose_js_accessors'] or |
| 240 attribute['is_static'] or |
| 241 attribute['runtime_enabled_function'] or |
| 242 attribute['per_context_enabled_function']) |
| 243 for attribute in attributes), |
| 244 'has_constructor_attributes': any(attribute['constructor_type'] for attr
ibute in attributes), |
| 245 'has_per_context_enabled_attributes': any(attribute['per_context_enabled
_function'] for attribute in attributes), |
| 246 'has_replaceable_attributes': any(attribute['is_replaceable'] for attrib
ute in attributes), |
| 247 }) |
| 248 |
| 249 # Methods |
| 250 methods = [v8_methods.generate_method(interface, method) |
| 251 for method in interface.operations |
| 252 if (method.name and # Skip anonymous special operations (methods
) |
| 253 not v8_utilities.dart_custom_method(method.extended_attribute
s))] |
| 254 generate_method_overloads(methods) |
| 255 |
| 256 per_context_enabled_methods = [] |
| 257 custom_registration_methods = [] |
| 258 method_configuration_methods = [] |
| 259 |
| 260 for method in methods: |
| 261 # Skip all but one method in each set of overloaded methods. |
| 262 if 'overload_index' in method and 'overloads' not in method: |
| 263 continue |
| 264 |
| 265 if 'overloads' in method: |
| 266 overloads = method['overloads'] |
| 267 per_context_enabled_function = overloads['per_context_enabled_functi
on_all'] |
| 268 runtime_enabled_function = overloads['runtime_enabled_function_all'] |
| 269 has_custom_registration = overloads['has_custom_registration_all'] |
| 270 else: |
| 271 per_context_enabled_function = method['per_context_enabled_function'
] |
| 272 runtime_enabled_function = method['runtime_enabled_function'] |
| 273 has_custom_registration = method['has_custom_registration'] |
| 274 |
| 275 if per_context_enabled_function: |
| 276 per_context_enabled_methods.append(method) |
| 277 continue |
| 278 if runtime_enabled_function or has_custom_registration: |
| 279 custom_registration_methods.append(method) |
| 280 continue |
| 281 method_configuration_methods.append(method) |
| 282 |
| 283 for method in methods: |
| 284 # The value of the Function object’s “length” property is a Number |
| 285 # determined as follows: |
| 286 # 1. Let S be the effective overload set for regular operations (if the |
| 287 # operation is a regular operation) or for static operations (if the |
| 288 # operation is a static operation) with identifier id on interface I and |
| 289 # with argument count 0. |
| 290 # 2. Return the length of the shortest argument list of the entries in S
. |
| 291 # FIXME: This calculation doesn't take into account whether runtime |
| 292 # enabled overloads are actually enabled, so length may be incorrect. |
| 293 # E.g., [RuntimeEnabled=Foo] void f(); void f(long x); |
| 294 # should have length 1 if Foo is not enabled, but length 0 if it is. |
| 295 method['length'] = (method['overloads']['minarg'] if 'overloads' in meth
od else |
| 296 method['number_of_required_arguments']) |
| 297 |
| 298 template_contents.update({ |
| 299 'custom_registration_methods': custom_registration_methods, |
| 300 'has_origin_safe_method_setter': any( |
| 301 method['is_check_security_for_frame'] and not method['is_read_only'] |
| 302 for method in methods), |
| 303 'method_configuration_methods': method_configuration_methods, |
| 304 'per_context_enabled_methods': per_context_enabled_methods, |
| 305 'methods': methods, |
| 306 }) |
| 307 |
| 308 template_contents.update({ |
| 309 'indexed_property_getter': indexed_property_getter(interface), |
| 310 'indexed_property_setter': indexed_property_setter(interface), |
| 311 'indexed_property_deleter': indexed_property_deleter(interface), |
| 312 'is_override_builtins': 'OverrideBuiltins' in extended_attributes, |
| 313 'named_property_getter': named_property_getter(interface), |
| 314 'named_property_setter': named_property_setter(interface), |
| 315 'named_property_deleter': named_property_deleter(interface), |
| 316 }) |
| 317 |
| 318 return template_contents |
| 319 |
| 320 |
| 321 # [DeprecateAs], [Reflect], [RuntimeEnabled] |
| 322 def generate_constant(constant): |
| 323 # (Blink-only) string literals are unquoted in tokenizer, must be re-quoted |
| 324 # in C++. |
| 325 if constant.idl_type.name == 'String': |
| 326 value = '"%s"' % constant.value |
| 327 else: |
| 328 value = constant.value |
| 329 |
| 330 extended_attributes = constant.extended_attributes |
| 331 return { |
| 332 'cpp_class': extended_attributes.get('PartialInterfaceImplementedAs'), |
| 333 'name': constant.name, |
| 334 # FIXME: use 'reflected_name' as correct 'name' |
| 335 'reflected_name': extended_attributes.get('Reflect', constant.name), |
| 336 'runtime_enabled_function': runtime_enabled_function_name(constant), |
| 337 'value': value, |
| 338 } |
| 339 |
| 340 |
| 341 ################################################################################ |
| 342 # Overloads |
| 343 ################################################################################ |
| 344 |
| 345 def generate_method_overloads(methods): |
| 346 # Regular methods |
| 347 generate_overloads_by_type([method for method in methods |
| 348 if not method['is_static']]) |
| 349 # Static methods |
| 350 generate_overloads_by_type([method for method in methods |
| 351 if method['is_static']]) |
| 352 |
| 353 |
| 354 def generate_overloads_by_type(methods): |
| 355 """Generates |method.overload*| template values. |
| 356 |
| 357 Called separately for static and non-static (regular) methods, |
| 358 as these are overloaded separately. |
| 359 Modifies |method| in place for |method| in |methods|. |
| 360 Doesn't change the |methods| list itself (only the values, i.e. individual |
| 361 methods), so ok to treat these separately. |
| 362 """ |
| 363 # Add overload information only to overloaded methods, so template code can |
| 364 # easily verify if a function is overloaded |
| 365 for name, overloads in method_overloads_by_name(methods): |
| 366 # Resolution function is generated after last overloaded function; |
| 367 # package necessary information into |method.overloads| for that method. |
| 368 overloads[-1]['overloads'] = generate_overloads(overloads) |
| 369 overloads[-1]['overloads']['name'] = name |
| 370 |
| 371 |
| 372 def method_overloads_by_name(methods): |
| 373 """Returns generator of overloaded methods by name: [name, [method]]""" |
| 374 # Filter to only methods that are actually overloaded |
| 375 method_counts = Counter(method['name'] for method in methods) |
| 376 overloaded_method_names = set(name |
| 377 for name, count in method_counts.iteritems() |
| 378 if count > 1) |
| 379 overloaded_methods = [method for method in methods |
| 380 if method['name'] in overloaded_method_names] |
| 381 |
| 382 # Group by name (generally will be defined together, but not necessarily) |
| 383 return sort_and_groupby(overloaded_methods, itemgetter('name')) |
| 384 |
| 385 |
| 386 def generate_overloads(overloads): |
| 387 """Returns |overloads| template values for a single name. |
| 388 |
| 389 Sets |method.overload_index| in place for |method| in |overloads| |
| 390 and returns dict of overall overload template values. |
| 391 """ |
| 392 assert len(overloads) > 1 # only apply to overloaded names |
| 393 for index, method in enumerate(overloads, 1): |
| 394 method['overload_index'] = index |
| 395 |
| 396 effective_overloads_by_length = effective_overload_set_by_length(overloads) |
| 397 lengths = [length for length, _ in effective_overloads_by_length] |
| 398 name = overloads[0].get('name', '<constructor>') |
| 399 |
| 400 # Check and fail if all overloads with the shortest acceptable arguments |
| 401 # list are runtime enabled, since we would otherwise set 'length' on the |
| 402 # function object to an incorrect value when none of those overloads were |
| 403 # actually enabled at runtime. The exception is if all overloads are |
| 404 # controlled by the same runtime enabled feature, in which case there would |
| 405 # be no function object at all if it is not enabled. |
| 406 shortest_overloads = effective_overloads_by_length[0][1] |
| 407 if (all(method.get('runtime_enabled_function') |
| 408 for method, _, _ in shortest_overloads) and |
| 409 not common_value(overloads, 'runtime_enabled_function')): |
| 410 raise ValueError('Function.length of %s depends on runtime enabled featu
res' % name) |
| 411 |
| 412 # Check and fail if overloads disagree on any of the extended attributes |
| 413 # that affect how the method should be registered. |
| 414 # Skip the check for overloaded constructors, since they don't support any |
| 415 # of the extended attributes in question. |
| 416 if not overloads[0].get('is_constructor'): |
| 417 overload_extended_attributes = [ |
| 418 method['custom_registration_extended_attributes'] |
| 419 for method in overloads] |
| 420 for extended_attribute in v8_methods.CUSTOM_REGISTRATION_EXTENDED_ATTRIB
UTES: |
| 421 if common_key(overload_extended_attributes, extended_attribute) is N
one: |
| 422 raise ValueError('Overloads of %s have conflicting extended attr
ibute %s' |
| 423 % (name, extended_attribute)) |
| 424 |
| 425 return { |
| 426 'deprecate_all_as': common_value(overloads, 'deprecate_as'), # [Depreca
teAs] |
| 427 'length_tests_methods': length_tests_methods(effective_overloads_by_leng
th), |
| 428 'minarg': lengths[0], |
| 429 # 1. Let maxarg be the length of the longest type list of the |
| 430 # entries in S. |
| 431 'maxarg': lengths[-1], |
| 432 'measure_all_as': common_value(overloads, 'measure_as'), # [MeasureAs] |
| 433 'has_custom_registration_all': common_value(overloads, 'has_custom_regis
tration'), |
| 434 'per_context_enabled_function_all': common_value(overloads, 'per_context
_enabled_function'), # [PerContextEnabled] |
| 435 'runtime_enabled_function_all': common_value(overloads, 'runtime_enabled
_function'), # [RuntimeEnabled] |
| 436 'valid_arities': lengths |
| 437 # Only need to report valid arities if there is a gap in the |
| 438 # sequence of possible lengths, otherwise invalid length means |
| 439 # "not enough arguments". |
| 440 if lengths[-1] - lengths[0] != len(lengths) - 1 else None, |
| 441 } |
| 442 |
| 443 |
| 444 def effective_overload_set(F): |
| 445 """Returns the effective overload set of an overloaded function. |
| 446 |
| 447 An effective overload set is the set of overloaded functions + signatures |
| 448 (type list of arguments, with optional and variadic arguments included or |
| 449 not), and is used in the overload resolution algorithm. |
| 450 |
| 451 For example, given input [f1(optional long x), f2(DOMString s)], the output |
| 452 is informally [f1(), f1(long), f2(DOMString)], and formally |
| 453 [(f1, [], []), (f1, [long], [optional]), (f2, [DOMString], [required])]. |
| 454 |
| 455 Currently the optionality list is a list of |is_optional| booleans (True |
| 456 means optional, False means required); to support variadics this needs to |
| 457 be tri-valued as required, optional, or variadic. |
| 458 |
| 459 Formally: |
| 460 An effective overload set represents the allowable invocations for a |
| 461 particular operation, constructor (specified with [Constructor] or |
| 462 [NamedConstructor]), legacy caller or callback function. |
| 463 |
| 464 An additional argument N (argument count) is needed when overloading |
| 465 variadics, but we don't use that currently. |
| 466 |
| 467 Spec: http://heycam.github.io/webidl/#dfn-effective-overload-set |
| 468 |
| 469 Formally the input and output lists are sets, but methods are stored |
| 470 internally as dicts, which can't be stored in a set because they are not |
| 471 hashable, so we use lists instead. |
| 472 |
| 473 Arguments: |
| 474 F: list of overloads for a given callable name. |
| 475 |
| 476 Returns: |
| 477 S: list of tuples of the form (callable, type list, optionality list). |
| 478 """ |
| 479 # Code closely follows the algorithm in the spec, for clarity and |
| 480 # correctness, and hence is not very Pythonic. |
| 481 |
| 482 # 1. Initialize S to ∅. |
| 483 # (We use a list because we can't use a set, as noted above.) |
| 484 S = [] |
| 485 |
| 486 # 2. Let F be a set with elements as follows, according to the kind of |
| 487 # effective overload set: |
| 488 # (Passed as argument, nothing to do.) |
| 489 |
| 490 # 3. & 4. (maxarg, m) are only needed for variadics, not used. |
| 491 |
| 492 # 5. For each operation, extended attribute or callback function X in F: |
| 493 for X in F: # X is the "callable", F is the overloads. |
| 494 arguments = X['arguments'] |
| 495 # 1. Let n be the number of arguments X is declared to take. |
| 496 n = len(arguments) |
| 497 # 2. Let t0..n−1 be a list of types, where ti is the type of X’s |
| 498 # argument at index i. |
| 499 # (“type list”) |
| 500 t = tuple(argument['idl_type_object'] for argument in arguments) |
| 501 # 3. Let o0..n−1 be a list of optionality values, where oi is “variadic” |
| 502 # if X’s argument at index i is a final, variadic argument, “optional” |
| 503 # if the argument is optional, and “required” otherwise. |
| 504 # (“optionality list”) |
| 505 # (We’re just using a boolean for optional vs. required.) |
| 506 o = tuple(argument['is_optional'] for argument in arguments) |
| 507 # 4. Add to S the tuple <X, t0..n−1, o0..n−1>. |
| 508 S.append((X, t, o)) |
| 509 # 5. If X is declared to be variadic, then: |
| 510 # (Not used, so not implemented.) |
| 511 # 6. Initialize i to n−1. |
| 512 i = n - 1 |
| 513 # 7. While i ≥ 0: |
| 514 # Spec bug (fencepost error); should be “While i > 0:” |
| 515 # https://www.w3.org/Bugs/Public/show_bug.cgi?id=25590 |
| 516 while i > 0: |
| 517 # 1. If argument i of X is not optional, then break this loop. |
| 518 if not o[i]: |
| 519 break |
| 520 # 2. Otherwise, add to S the tuple <X, t0..i−1, o0..i−1>. |
| 521 S.append((X, t[:i], o[:i])) |
| 522 # 3. Set i to i−1. |
| 523 i = i - 1 |
| 524 # 8. If n > 0 and all arguments of X are optional, then add to S the |
| 525 # tuple <X, (), ()> (where “()” represents the empty list). |
| 526 if n > 0 and all(oi for oi in o): |
| 527 S.append((X, [], [])) |
| 528 # 6. The effective overload set is S. |
| 529 return S |
| 530 |
| 531 |
| 532 def effective_overload_set_by_length(overloads): |
| 533 def type_list_length(entry): |
| 534 # Entries in the effective overload set are 3-tuples: |
| 535 # (callable, type list, optionality list) |
| 536 return len(entry[1]) |
| 537 |
| 538 effective_overloads = effective_overload_set(overloads) |
| 539 return list(sort_and_groupby(effective_overloads, type_list_length)) |
| 540 |
| 541 |
| 542 def distinguishing_argument_index(entries): |
| 543 """Returns the distinguishing argument index for a sequence of entries. |
| 544 |
| 545 Entries are elements of the effective overload set with the same number |
| 546 of arguments (formally, same type list length), each a 3-tuple of the form |
| 547 (callable, type list, optionality list). |
| 548 |
| 549 Spec: http://heycam.github.io/webidl/#dfn-distinguishing-argument-index |
| 550 |
| 551 If there is more than one entry in an effective overload set that has a |
| 552 given type list length, then for those entries there must be an index i |
| 553 such that for each pair of entries the types at index i are |
| 554 distinguishable. |
| 555 The lowest such index is termed the distinguishing argument index for the |
| 556 entries of the effective overload set with the given type list length. |
| 557 """ |
| 558 # Only applicable “If there is more than one entry” |
| 559 assert len(entries) > 1 |
| 560 type_lists = [tuple(idl_type.name for idl_type in entry[1]) |
| 561 for entry in entries] |
| 562 type_list_length = len(type_lists[0]) |
| 563 # Only applicable for entries that “[have] a given type list length” |
| 564 assert all(len(type_list) == type_list_length for type_list in type_lists) |
| 565 name = entries[0][0].get('name', 'Constructor') # for error reporting |
| 566 |
| 567 # The spec defines the distinguishing argument index by conditions it must |
| 568 # satisfy, but does not give an algorithm. |
| 569 # |
| 570 # We compute the distinguishing argument index by first computing the |
| 571 # minimum index where not all types are the same, and then checking that |
| 572 # all types in this position are distinguishable (and the optionality lists |
| 573 # up to this point are identical), since "minimum index where not all types |
| 574 # are the same" is a *necessary* condition, and more direct to check than |
| 575 # distinguishability. |
| 576 types_by_index = (set(types) for types in zip(*type_lists)) |
| 577 try: |
| 578 # “In addition, for each index j, where j is less than the |
| 579 # distinguishing argument index for a given type list length, the types |
| 580 # at index j in all of the entries’ type lists must be the same” |
| 581 index = next(i for i, types in enumerate(types_by_index) |
| 582 if len(types) > 1) |
| 583 except StopIteration: |
| 584 raise ValueError('No distinguishing index found for %s, length %s:\n' |
| 585 'All entries have the same type list:\n' |
| 586 '%s' % (name, type_list_length, type_lists[0])) |
| 587 # Check optionality |
| 588 # “and the booleans in the corresponding list indicating argument |
| 589 # optionality must be the same.” |
| 590 # FIXME: spec typo: optionality value is no longer a boolean |
| 591 # https://www.w3.org/Bugs/Public/show_bug.cgi?id=25628 |
| 592 initial_optionality_lists = set(entry[2][:index] for entry in entries) |
| 593 if len(initial_optionality_lists) > 1: |
| 594 raise ValueError( |
| 595 'Invalid optionality lists for %s, length %s:\n' |
| 596 'Optionality lists differ below distinguishing argument index %s:\n' |
| 597 '%s' |
| 598 % (name, type_list_length, index, set(initial_optionality_lists))) |
| 599 |
| 600 # Check distinguishability |
| 601 # http://heycam.github.io/webidl/#dfn-distinguishable |
| 602 # Use names to check for distinct types, since objects are distinct |
| 603 # FIXME: check distinguishability more precisely, for validation |
| 604 distinguishing_argument_type_names = [type_list[index] |
| 605 for type_list in type_lists] |
| 606 if (len(set(distinguishing_argument_type_names)) != |
| 607 len(distinguishing_argument_type_names)): |
| 608 raise ValueError('Types in distinguishing argument are not distinct:\n' |
| 609 '%s' % distinguishing_argument_type_names) |
| 610 |
| 611 return index |
| 612 |
| 613 |
| 614 def length_tests_methods(effective_overloads_by_length): |
| 615 """Returns sorted list of resolution tests and associated methods, by length
. |
| 616 |
| 617 This builds the main data structure for the overload resolution loop. |
| 618 For a given argument length, bindings test argument at distinguishing |
| 619 argument index, in order given by spec: if it is compatible with |
| 620 (optionality or) type required by an overloaded method, resolve to that |
| 621 method. |
| 622 |
| 623 Returns: |
| 624 [(length, [(test, method)])] |
| 625 """ |
| 626 return [(length, list(resolution_tests_methods(effective_overloads))) |
| 627 for length, effective_overloads in effective_overloads_by_length] |
| 628 |
| 629 |
| 630 def resolution_tests_methods(effective_overloads): |
| 631 """Yields resolution test and associated method, in resolution order, for ef
fective overloads of a given length. |
| 632 |
| 633 This is the heart of the resolution algorithm. |
| 634 http://heycam.github.io/webidl/#dfn-overload-resolution-algorithm |
| 635 |
| 636 Note that a given method can be listed multiple times, with different tests! |
| 637 This is to handle implicit type conversion. |
| 638 |
| 639 Returns: |
| 640 [(test, method)] |
| 641 """ |
| 642 methods = [effective_overload[0] |
| 643 for effective_overload in effective_overloads] |
| 644 if len(methods) == 1: |
| 645 # If only one method with a given length, no test needed |
| 646 yield 'true', methods[0] |
| 647 return |
| 648 |
| 649 # 6. If there is more than one entry in S, then set d to be the |
| 650 # distinguishing argument index for the entries of S. |
| 651 index = distinguishing_argument_index(effective_overloads) |
| 652 # (7-9 are for handling |undefined| values for optional arguments before |
| 653 # the distinguishing argument (as “missing”), so you can specify only some |
| 654 # optional arguments. We don’t support this, so we skip these steps.) |
| 655 # 10. If i = d, then: |
| 656 # (d is the distinguishing argument index) |
| 657 # 1. Let V be argi. |
| 658 # Note: This is the argument that will be used to resolve which |
| 659 # overload is selected. |
| 660 cpp_value = 'info[%s]' % index |
| 661 |
| 662 # Extract argument and IDL type to simplify accessing these in each loop. |
| 663 arguments = [method['arguments'][index] for method in methods] |
| 664 arguments_methods = zip(arguments, methods) |
| 665 idl_types = [argument['idl_type_object'] for argument in arguments] |
| 666 idl_types_methods = zip(idl_types, methods) |
| 667 |
| 668 # We can’t do a single loop through all methods or simply sort them, because |
| 669 # a method may be listed in multiple steps of the resolution algorithm, and |
| 670 # which test to apply differs depending on the step. |
| 671 # |
| 672 # Instead, we need to go through all methods at each step, either finding |
| 673 # first match (if only one test is allowed) or filtering to matches (if |
| 674 # multiple tests are allowed), and generating an appropriate tests. |
| 675 |
| 676 # 2. If V is undefined, and there is an entry in S whose list of |
| 677 # optionality values has “optional” at index i, then remove from S all |
| 678 # other entries. |
| 679 try: |
| 680 method = next(method for argument, method in arguments_methods |
| 681 if argument['is_optional']) |
| 682 test = '%s->IsUndefined()' % cpp_value |
| 683 yield test, method |
| 684 except StopIteration: |
| 685 pass |
| 686 |
| 687 # 3. Otherwise: if V is null or undefined, and there is an entry in S that |
| 688 # has one of the following types at position i of its type list, |
| 689 # • a nullable type |
| 690 try: |
| 691 method = next(method for idl_type, method in idl_types_methods |
| 692 if idl_type.is_nullable) |
| 693 test = 'isUndefinedOrNull(%s)' % cpp_value |
| 694 yield test, method |
| 695 except StopIteration: |
| 696 pass |
| 697 |
| 698 # 4. Otherwise: if V is a platform object – but not a platform array |
| 699 # object – and there is an entry in S that has one of the following |
| 700 # types at position i of its type list, |
| 701 # • an interface type that V implements |
| 702 # (Unlike most of these tests, this can return multiple methods, since we |
| 703 # test if it implements an interface. Thus we need a for loop, not a next.) |
| 704 # (We distinguish wrapper types from built-in interface types.) |
| 705 for idl_type, method in ((idl_type, method) |
| 706 for idl_type, method in idl_types_methods |
| 707 if idl_type.is_wrapper_type): |
| 708 test = 'V8{idl_type}::hasInstance({cpp_value}, isolate)'.format(idl_type
=idl_type.base_type, cpp_value=cpp_value) |
| 709 yield test, method |
| 710 |
| 711 # 8. Otherwise: if V is any kind of object except for a native Date object, |
| 712 # a native RegExp object, and there is an entry in S that has one of the |
| 713 # following types at position i of its type list, |
| 714 # • an array type |
| 715 # • a sequence type |
| 716 # ... |
| 717 # • a dictionary |
| 718 try: |
| 719 # FIXME: IDL dictionary not implemented, so use Blink Dictionary |
| 720 # http://crbug.com/321462 |
| 721 idl_type, method = next((idl_type, method) |
| 722 for idl_type, method in idl_types_methods |
| 723 if (idl_type.array_or_sequence_type or |
| 724 idl_type.name == 'Dictionary')) |
| 725 if idl_type.array_or_sequence_type: |
| 726 # (We test for Array instead of generic Object to type-check.) |
| 727 # FIXME: test for Object during resolution, then have type check for |
| 728 # Array in overloaded method: http://crbug.com/262383 |
| 729 test = '%s->IsArray()' % cpp_value |
| 730 else: |
| 731 # FIXME: should be '{1}->IsObject() && !{1}->IsDate() && !{1}->IsReg
Exp()'.format(cpp_value) |
| 732 # FIXME: the IsDate and IsRegExp checks can be skipped if we've |
| 733 # already generated tests for them. |
| 734 test = '%s->IsObject()' % cpp_value |
| 735 yield test, method |
| 736 except StopIteration: |
| 737 pass |
| 738 |
| 739 # (Check for exact type matches before performing automatic type conversion; |
| 740 # only needed if distinguishing between primitive types.) |
| 741 if len([idl_type.is_primitive_type for idl_type in idl_types]) > 1: |
| 742 # (Only needed if match in step 11, otherwise redundant.) |
| 743 if any(idl_type.name == 'String' or idl_type.is_enum |
| 744 for idl_type in idl_types): |
| 745 # 10. Otherwise: if V is a Number value, and there is an entry in S |
| 746 # that has one of the following types at position i of its type |
| 747 # list, |
| 748 # • a numeric type |
| 749 try: |
| 750 method = next(method for idl_type, method in idl_types_methods |
| 751 if idl_type.is_numeric_type) |
| 752 test = '%s->IsNumber()' % cpp_value |
| 753 yield test, method |
| 754 except StopIteration: |
| 755 pass |
| 756 |
| 757 # (Perform automatic type conversion, in order. If any of these match, |
| 758 # that’s the end, and no other tests are needed.) To keep this code simple, |
| 759 # we rely on the C++ compiler's dead code elimination to deal with the |
| 760 # redundancy if both cases below trigger. |
| 761 |
| 762 # 11. Otherwise: if there is an entry in S that has one of the following |
| 763 # types at position i of its type list, |
| 764 # • DOMString |
| 765 # • an enumeration type |
| 766 # * ByteString |
| 767 # Blink: ScalarValueString is a pending Web IDL addition |
| 768 try: |
| 769 method = next(method for idl_type, method in idl_types_methods |
| 770 if idl_type.name in ('String', |
| 771 'ByteString', |
| 772 'ScalarValueString') or |
| 773 idl_type.is_enum) |
| 774 yield 'true', method |
| 775 except StopIteration: |
| 776 pass |
| 777 |
| 778 # 12. Otherwise: if there is an entry in S that has one of the following |
| 779 # types at position i of its type list, |
| 780 # • a numeric type |
| 781 try: |
| 782 method = next(method for idl_type, method in idl_types_methods |
| 783 if idl_type.is_numeric_type) |
| 784 yield 'true', method |
| 785 except StopIteration: |
| 786 pass |
| 787 |
| 788 |
| 789 ################################################################################ |
| 790 # Utility functions |
| 791 ################################################################################ |
| 792 |
| 793 def Counter(iterable): |
| 794 # Once using Python 2.7, using collections.Counter |
| 795 counter = defaultdict(lambda: 0) |
| 796 for item in iterable: |
| 797 counter[item] += 1 |
| 798 return counter |
| 799 |
| 800 |
| 801 def common(dicts, f): |
| 802 """Returns common result of f across an iterable of dicts, or None. |
| 803 |
| 804 Call f for each dict and return its result if the same across all dicts. |
| 805 """ |
| 806 values = (f(d) for d in dicts) |
| 807 first_value = next(values) |
| 808 if all(value == first_value for value in values): |
| 809 return first_value |
| 810 return None |
| 811 |
| 812 |
| 813 def common_key(dicts, key): |
| 814 """Returns common presence of a key across an iterable of dicts, or None. |
| 815 |
| 816 True if all dicts have the key, False if none of the dicts have the key, |
| 817 and None if some but not all dicts have the key. |
| 818 """ |
| 819 return common(dicts, lambda d: key in d) |
| 820 |
| 821 |
| 822 def common_value(dicts, key): |
| 823 """Returns common value of a key across an iterable of dicts, or None. |
| 824 |
| 825 Auxiliary function for overloads, so can consolidate an extended attribute |
| 826 that appears with the same value on all items in an overload set. |
| 827 """ |
| 828 return common(dicts, lambda d: d.get(key)) |
| 829 |
| 830 |
| 831 def sort_and_groupby(l, key=None): |
| 832 """Returns a generator of (key, list), sorting and grouping list by key.""" |
| 833 l.sort(key=key) |
| 834 return ((k, list(g)) for k, g in itertools.groupby(l, key)) |
| 835 |
| 836 |
| 837 ################################################################################ |
| 838 # Constructors |
| 839 ################################################################################ |
| 840 |
| 841 # [Constructor] |
| 842 def generate_constructor(interface, constructor): |
| 843 arguments_need_try_catch = any(v8_methods.argument_needs_try_catch(argument) |
| 844 for argument in constructor.arguments) |
| 845 |
| 846 return { |
| 847 'arguments': [v8_methods.generate_argument(interface, constructor, argum
ent, index) |
| 848 for index, argument in enumerate(constructor.arguments)], |
| 849 'arguments_need_try_catch': arguments_need_try_catch, |
| 850 'cpp_type': cpp_template_type( |
| 851 cpp_ptr_type('RefPtr', 'RawPtr', gc_type(interface)), |
| 852 cpp_name(interface)), |
| 853 'cpp_value': v8_methods.cpp_value( |
| 854 interface, constructor, len(constructor.arguments)), |
| 855 'has_exception_state': |
| 856 # [RaisesException=Constructor] |
| 857 interface.extended_attributes.get('RaisesException') == 'Constructor
' or |
| 858 any(argument for argument in constructor.arguments |
| 859 if argument.idl_type.name == 'SerializedScriptValue' or |
| 860 argument.idl_type.is_integer_type), |
| 861 'is_constructor': True, |
| 862 'is_named_constructor': False, |
| 863 'number_of_required_arguments': |
| 864 number_of_required_arguments(constructor), |
| 865 } |
| 866 |
| 867 |
| 868 # [NamedConstructor] |
| 869 def generate_named_constructor(interface): |
| 870 extended_attributes = interface.extended_attributes |
| 871 if 'NamedConstructor' not in extended_attributes: |
| 872 return None |
| 873 # FIXME: parser should return named constructor separately; |
| 874 # included in constructors (and only name stored in extended attribute) |
| 875 # for Perl compatibility |
| 876 idl_constructor = interface.constructors[-1] |
| 877 assert idl_constructor.name == 'NamedConstructor' |
| 878 constructor = generate_constructor(interface, idl_constructor) |
| 879 constructor.update({ |
| 880 'name': extended_attributes['NamedConstructor'], |
| 881 'is_named_constructor': True, |
| 882 }) |
| 883 return constructor |
| 884 |
| 885 |
| 886 def number_of_required_arguments(constructor): |
| 887 return len([argument for argument in constructor.arguments |
| 888 if not argument.is_optional]) |
| 889 |
| 890 |
| 891 def interface_length(interface, constructors): |
| 892 # Docs: http://heycam.github.io/webidl/#es-interface-call |
| 893 if 'EventConstructor' in interface.extended_attributes: |
| 894 return 1 |
| 895 if not constructors: |
| 896 return 0 |
| 897 return min(constructor['number_of_required_arguments'] |
| 898 for constructor in constructors) |
| 899 |
| 900 |
| 901 ################################################################################ |
| 902 # Special operations (methods) |
| 903 # http://heycam.github.io/webidl/#idl-special-operations |
| 904 ################################################################################ |
| 905 |
| 906 def property_getter(getter, cpp_arguments): |
| 907 def is_null_expression(idl_type): |
| 908 if idl_type.is_union_type: |
| 909 return ' && '.join('!result%sEnabled' % i |
| 910 for i, _ in enumerate(idl_type.member_types)) |
| 911 if idl_type.name == 'String': |
| 912 return 'result.isNull()' |
| 913 if idl_type.is_interface_type: |
| 914 return '!result' |
| 915 return '' |
| 916 |
| 917 idl_type = getter.idl_type |
| 918 extended_attributes = getter.extended_attributes |
| 919 is_raises_exception = 'RaisesException' in extended_attributes |
| 920 |
| 921 # FIXME: make more generic, so can use v8_methods.cpp_value |
| 922 cpp_method_name = 'impl->%s' % cpp_name(getter) |
| 923 |
| 924 if is_raises_exception: |
| 925 cpp_arguments.append('exceptionState') |
| 926 union_arguments = idl_type.union_arguments |
| 927 if union_arguments: |
| 928 cpp_arguments.extend(union_arguments) |
| 929 |
| 930 cpp_value = '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments)) |
| 931 |
| 932 return { |
| 933 'cpp_type': idl_type.cpp_type, |
| 934 'cpp_value': cpp_value, |
| 935 'is_custom': |
| 936 'Custom' in extended_attributes and |
| 937 (not extended_attributes['Custom'] or |
| 938 has_extended_attribute_value(getter, 'Custom', 'PropertyGetter')), |
| 939 'is_custom_property_enumerator': has_extended_attribute_value( |
| 940 getter, 'Custom', 'PropertyEnumerator'), |
| 941 'is_custom_property_query': has_extended_attribute_value( |
| 942 getter, 'Custom', 'PropertyQuery'), |
| 943 'is_enumerable': 'NotEnumerable' not in extended_attributes, |
| 944 'is_null_expression': is_null_expression(idl_type), |
| 945 'is_raises_exception': is_raises_exception, |
| 946 'name': cpp_name(getter), |
| 947 'union_arguments': union_arguments, |
| 948 'v8_set_return_value': idl_type.v8_set_return_value('result', extended_a
ttributes=extended_attributes, script_wrappable='impl', release=idl_type.release
), |
| 949 } |
| 950 |
| 951 |
| 952 def property_setter(setter): |
| 953 idl_type = setter.arguments[1].idl_type |
| 954 extended_attributes = setter.extended_attributes |
| 955 is_raises_exception = 'RaisesException' in extended_attributes |
| 956 return { |
| 957 'has_type_checking_interface': |
| 958 has_extended_attribute_value(setter, 'TypeChecking', 'Interface') an
d |
| 959 idl_type.is_wrapper_type, |
| 960 'idl_type': idl_type.base_type, |
| 961 'is_custom': 'Custom' in extended_attributes, |
| 962 'has_exception_state': is_raises_exception or |
| 963 idl_type.is_integer_type, |
| 964 'is_raises_exception': is_raises_exception, |
| 965 'name': cpp_name(setter), |
| 966 'v8_value_to_local_cpp_value': idl_type.v8_value_to_local_cpp_value( |
| 967 extended_attributes, 'v8Value', 'propertyValue'), |
| 968 } |
| 969 |
| 970 |
| 971 def property_deleter(deleter): |
| 972 idl_type = deleter.idl_type |
| 973 if str(idl_type) != 'boolean': |
| 974 raise Exception( |
| 975 'Only deleters with boolean type are allowed, but type is "%s"' % |
| 976 idl_type) |
| 977 extended_attributes = deleter.extended_attributes |
| 978 return { |
| 979 'is_custom': 'Custom' in extended_attributes, |
| 980 'is_raises_exception': 'RaisesException' in extended_attributes, |
| 981 'name': cpp_name(deleter), |
| 982 } |
| 983 |
| 984 |
| 985 ################################################################################ |
| 986 # Indexed properties |
| 987 # http://heycam.github.io/webidl/#idl-indexed-properties |
| 988 ################################################################################ |
| 989 |
| 990 def indexed_property_getter(interface): |
| 991 try: |
| 992 # Find indexed property getter, if present; has form: |
| 993 # getter TYPE [OPTIONAL_IDENTIFIER](unsigned long ARG1) |
| 994 getter = next( |
| 995 method |
| 996 for method in interface.operations |
| 997 if ('getter' in method.specials and |
| 998 len(method.arguments) == 1 and |
| 999 str(method.arguments[0].idl_type) == 'unsigned long')) |
| 1000 except StopIteration: |
| 1001 return None |
| 1002 |
| 1003 return property_getter(getter, ['index']) |
| 1004 |
| 1005 |
| 1006 def indexed_property_setter(interface): |
| 1007 try: |
| 1008 # Find indexed property setter, if present; has form: |
| 1009 # setter RETURN_TYPE [OPTIONAL_IDENTIFIER](unsigned long ARG1, ARG_TYPE
ARG2) |
| 1010 setter = next( |
| 1011 method |
| 1012 for method in interface.operations |
| 1013 if ('setter' in method.specials and |
| 1014 len(method.arguments) == 2 and |
| 1015 str(method.arguments[0].idl_type) == 'unsigned long')) |
| 1016 except StopIteration: |
| 1017 return None |
| 1018 |
| 1019 return property_setter(setter) |
| 1020 |
| 1021 |
| 1022 def indexed_property_deleter(interface): |
| 1023 try: |
| 1024 # Find indexed property deleter, if present; has form: |
| 1025 # deleter TYPE [OPTIONAL_IDENTIFIER](unsigned long ARG) |
| 1026 deleter = next( |
| 1027 method |
| 1028 for method in interface.operations |
| 1029 if ('deleter' in method.specials and |
| 1030 len(method.arguments) == 1 and |
| 1031 str(method.arguments[0].idl_type) == 'unsigned long')) |
| 1032 except StopIteration: |
| 1033 return None |
| 1034 |
| 1035 return property_deleter(deleter) |
| 1036 |
| 1037 |
| 1038 ################################################################################ |
| 1039 # Named properties |
| 1040 # http://heycam.github.io/webidl/#idl-named-properties |
| 1041 ################################################################################ |
| 1042 |
| 1043 def named_property_getter(interface): |
| 1044 try: |
| 1045 # Find named property getter, if present; has form: |
| 1046 # getter TYPE [OPTIONAL_IDENTIFIER](DOMString ARG1) |
| 1047 getter = next( |
| 1048 method |
| 1049 for method in interface.operations |
| 1050 if ('getter' in method.specials and |
| 1051 len(method.arguments) == 1 and |
| 1052 str(method.arguments[0].idl_type) == 'DOMString')) |
| 1053 except StopIteration: |
| 1054 return None |
| 1055 |
| 1056 getter.name = getter.name or 'anonymousNamedGetter' |
| 1057 return property_getter(getter, ['propertyName']) |
| 1058 |
| 1059 |
| 1060 def named_property_setter(interface): |
| 1061 try: |
| 1062 # Find named property setter, if present; has form: |
| 1063 # setter RETURN_TYPE [OPTIONAL_IDENTIFIER](DOMString ARG1, ARG_TYPE ARG2
) |
| 1064 setter = next( |
| 1065 method |
| 1066 for method in interface.operations |
| 1067 if ('setter' in method.specials and |
| 1068 len(method.arguments) == 2 and |
| 1069 str(method.arguments[0].idl_type) == 'DOMString')) |
| 1070 except StopIteration: |
| 1071 return None |
| 1072 |
| 1073 return property_setter(setter) |
| 1074 |
| 1075 |
| 1076 def named_property_deleter(interface): |
| 1077 try: |
| 1078 # Find named property deleter, if present; has form: |
| 1079 # deleter TYPE [OPTIONAL_IDENTIFIER](DOMString ARG) |
| 1080 deleter = next( |
| 1081 method |
| 1082 for method in interface.operations |
| 1083 if ('deleter' in method.specials and |
| 1084 len(method.arguments) == 1 and |
| 1085 str(method.arguments[0].idl_type) == 'DOMString')) |
| 1086 except StopIteration: |
| 1087 return None |
| 1088 |
| 1089 return property_deleter(deleter) |
OLD | NEW |