1f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# Copyright (C) 2013 Google Inc. All rights reserved.
2f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#
3f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# Redistribution and use in source and binary forms, with or without
4f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# modification, are permitted provided that the following conditions are
5f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# met:
6f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#
7f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#     * Redistributions of source code must retain the above copyright
8f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# notice, this list of conditions and the following disclaimer.
9f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#     * Redistributions in binary form must reproduce the above
10f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# copyright notice, this list of conditions and the following disclaimer
11f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# in the documentation and/or other materials provided with the
12f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# distribution.
13f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#     * Neither the name of Google Inc. nor the names of its
14f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# contributors may be used to endorse or promote products derived from
15f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# this software without specific prior written permission.
16f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#
17f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
29f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)"""Generate template values for methods.
30f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
31e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)Extends IdlArgument with property |default_cpp_value|.
32e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)Extends IdlTypeBase and IdlUnionType with property |union_arguments|.
33d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
34d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)Design doc: http://www.chromium.org/developers/design-documents/idl-compiler
35f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)"""
36f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
377242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccifrom idl_definitions import IdlArgument, IdlOperation
38e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)from idl_types import IdlTypeBase, IdlUnionType, inherits_interface
3951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)from v8_globals import includes
40f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)import v8_types
41f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)import v8_utilities
4251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)from v8_utilities import has_extended_attribute_value
43f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
44f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
455d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)# Methods with any of these require custom method registration code in the
465d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)# interface's configure*Template() function.
475d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)CUSTOM_REGISTRATION_EXTENDED_ATTRIBUTES = frozenset([
485d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    'DoNotCheckSecurity',
495d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    'DoNotCheckSignature',
505d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    'NotEnumerable',
515d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    'Unforgeable',
525d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)])
535d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)
545d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)
55197021e6b966cfb06891637935ef33fff06433d1Ben Murdochdef use_local_result(method):
56197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    extended_attributes = method.extended_attributes
57197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    idl_type = method.idl_type
58197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    return (has_extended_attribute_value(method, 'CallWith', 'ScriptState') or
59197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            'ImplementedInPrivateScript' in extended_attributes or
60197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            'RaisesException' in extended_attributes or
61197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            idl_type.is_union_type or
62197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            idl_type.is_explicit_nullable)
63197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
64197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
65197021e6b966cfb06891637935ef33fff06433d1Ben Murdochdef method_context(interface, method):
66f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    arguments = method.arguments
6751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    extended_attributes = method.extended_attributes
6851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    idl_type = method.idl_type
6951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    is_static = method.is_static
7051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    name = method.name
7151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
72d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    idl_type.add_includes_for_type()
7351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    this_cpp_value = cpp_value(interface, method, len(arguments))
7451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
7551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    def function_template():
7651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        if is_static:
7751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            return 'functionTemplate'
7851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        if 'Unforgeable' in extended_attributes:
7951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            return 'instanceTemplate'
8051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return 'prototypeTemplate'
8151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
82197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    is_implemented_in_private_script = 'ImplementedInPrivateScript' in extended_attributes
83197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if is_implemented_in_private_script:
84197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        includes.add('bindings/core/v8/PrivateScriptRunner.h')
85197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        includes.add('core/frame/LocalFrame.h')
86c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        includes.add('platform/ScriptForbiddenScope.h')
87c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
88c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    # [OnlyExposedToPrivateScript]
89c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    is_only_exposed_to_private_script = 'OnlyExposedToPrivateScript' in extended_attributes
90c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
9151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    is_call_with_script_arguments = has_extended_attribute_value(method, 'CallWith', 'ScriptArguments')
9251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if is_call_with_script_arguments:
93197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        includes.update(['bindings/core/v8/ScriptCallStackFactory.h',
9451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)                         'core/inspector/ScriptArguments.h'])
9551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    is_call_with_script_state = has_extended_attribute_value(method, 'CallWith', 'ScriptState')
9651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if is_call_with_script_state:
97197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        includes.add('bindings/core/v8/ScriptState.h')
9851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    is_check_security_for_node = 'CheckSecurity' in extended_attributes
9951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if is_check_security_for_node:
100197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        includes.add('bindings/core/v8/BindingSecurity.h')
10151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    is_custom_element_callbacks = 'CustomElementCallbacks' in extended_attributes
10251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if is_custom_element_callbacks:
1037242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        includes.add('core/dom/custom/CustomElementProcessingStack.h')
10409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
1059e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    is_do_not_check_security = 'DoNotCheckSecurity' in extended_attributes
1069e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)
10709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    is_check_security_for_frame = (
1089e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        has_extended_attribute_value(interface, 'CheckSecurity', 'Frame') and
1099e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        not is_do_not_check_security)
1109e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)
1119e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    is_check_security_for_window = (
1129e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        has_extended_attribute_value(interface, 'CheckSecurity', 'Window') and
1139e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        not is_do_not_check_security)
1149e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)
11551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    is_raises_exception = 'RaisesException' in extended_attributes
11651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
11709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return {
11851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'activity_logging_world_list': v8_utilities.activity_logging_world_list(method),  # [ActivityLogging]
119197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'arguments': [argument_context(interface, method, argument, index)
120f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                      for index, argument in enumerate(arguments)],
121197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'argument_declarations_for_private_script':
122197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            argument_declarations_for_private_script(interface, method),
12351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'conditional_string': v8_utilities.conditional_string(method),
124197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'cpp_type': (v8_types.cpp_template_type('Nullable', idl_type.cpp_type)
125197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                     if idl_type.is_explicit_nullable else idl_type.cpp_type),
12651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'cpp_value': this_cpp_value,
127197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'cpp_type_initializer': idl_type.cpp_type_initializer,
1285d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        'custom_registration_extended_attributes':
1295d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)            CUSTOM_REGISTRATION_EXTENDED_ATTRIBUTES.intersection(
1305d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)                extended_attributes.iterkeys()),
13151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'deprecate_as': v8_utilities.deprecate_as(method),  # [DeprecateAs]
132e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        'exposed_test': v8_utilities.exposed(method, interface),  # [Exposed]
13351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'function_template': function_template(),
1345d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        'has_custom_registration': is_static or
1355d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)            v8_utilities.has_extended_attribute(
1365d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)                method, CUSTOM_REGISTRATION_EXTENDED_ATTRIBUTES),
13709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        'has_exception_state':
13809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            is_raises_exception or
13909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            is_check_security_for_frame or
1409e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)            is_check_security_for_window or
14109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            any(argument for argument in arguments
1427242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci                if (argument.idl_type.name == 'SerializedScriptValue' or
1437242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci                    argument_conversion_needs_exception_state(method, argument))),
1445d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        'idl_type': idl_type.base_type,
14551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_call_with_execution_context': has_extended_attribute_value(method, 'CallWith', 'ExecutionContext'),
14651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_call_with_script_arguments': is_call_with_script_arguments,
14751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_call_with_script_state': is_call_with_script_state,
14809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        'is_check_security_for_frame': is_check_security_for_frame,
14951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_check_security_for_node': is_check_security_for_node,
1509e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        'is_check_security_for_window': is_check_security_for_window,
15151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_custom': 'Custom' in extended_attributes,
15251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_custom_element_callbacks': is_custom_element_callbacks,
1539e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        'is_do_not_check_security': is_do_not_check_security,
15409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        'is_do_not_check_signature': 'DoNotCheckSignature' in extended_attributes,
155197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'is_explicit_nullable': idl_type.is_explicit_nullable,
156197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'is_implemented_in_private_script': is_implemented_in_private_script,
157a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        'is_partial_interface_member':
158a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch            'PartialInterfaceImplementedAs' in extended_attributes,
15951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_per_world_bindings': 'PerWorldBindings' in extended_attributes,
16051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_raises_exception': is_raises_exception,
161197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'is_read_only': 'Unforgeable' in extended_attributes,
16251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_static': is_static,
16351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_variadic': arguments and arguments[-1].is_variadic,
16451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'measure_as': v8_utilities.measure_as(method),  # [MeasureAs]
16551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'name': name,
16651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'number_of_arguments': len(arguments),
167f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        'number_of_required_arguments': len([
168f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            argument for argument in arguments
169f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if not (argument.is_optional or argument.is_variadic)]),
170f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        'number_of_required_or_variadic_arguments': len([
171f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            argument for argument in arguments
172f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if not argument.is_optional]),
173c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        'only_exposed_to_private_script': is_only_exposed_to_private_script,
174a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        'per_context_enabled_function': v8_utilities.per_context_enabled_function_name(method),  # [PerContextEnabled]
175197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'private_script_v8_value_to_local_cpp_value': idl_type.v8_value_to_local_cpp_value(
176197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            extended_attributes, 'v8Value', 'cppValue', isolate='scriptState->isolate()', used_in_private_script=True),
17751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'property_attributes': property_attributes(method),
178a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        'runtime_enabled_function': v8_utilities.runtime_enabled_function_name(method),  # [RuntimeEnabled]
179c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        'should_be_exposed_to_script': not (is_implemented_in_private_script and is_only_exposed_to_private_script),
18009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        'signature': 'v8::Local<v8::Signature>()' if is_static or 'DoNotCheckSignature' in extended_attributes else 'defaultSignature',
181d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        'union_arguments': idl_type.union_arguments,
182c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        'use_local_result': use_local_result(method),
18309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        'v8_set_return_value': v8_set_return_value(interface.name, method, this_cpp_value),
184197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'v8_set_return_value_for_main_world': v8_set_return_value(interface.name, method, this_cpp_value, for_main_world=True),
185197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'world_suffixes': ['', 'ForMainWorld'] if 'PerWorldBindings' in extended_attributes else [''],  # [PerWorldBindings],
186f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
187f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
188f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
189197021e6b966cfb06891637935ef33fff06433d1Ben Murdochdef argument_context(interface, method, argument, index):
19019cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)    extended_attributes = argument.extended_attributes
191f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    idl_type = argument.idl_type
19251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    this_cpp_value = cpp_value(interface, method, index)
193d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    is_variadic_wrapper_type = argument.is_variadic and idl_type.is_wrapper_type
194f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
195197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if ('ImplementedInPrivateScript' in extended_attributes and
196197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        not idl_type.is_wrapper_type and
197197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        not idl_type.is_basic_type):
198197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        raise Exception('Private scripts supports only primitive types and DOM wrappers.')
199197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
2007242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    default_cpp_value = argument.default_cpp_value
201f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return {
202d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        'cpp_type': idl_type.cpp_type_args(extended_attributes=extended_attributes,
203197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                                           raw_type=True,
204d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                                           used_as_variadic_argument=argument.is_variadic),
20551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'cpp_value': this_cpp_value,
2065d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        # FIXME: check that the default value's type is compatible with the argument's
2077242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        'default_value': default_cpp_value,
208d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        'enum_validation_expression': idl_type.enum_validation_expression,
209197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'handle': '%sHandle' % argument.name,
2105d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        # FIXME: remove once [Default] removed and just use argument.default_value
2117242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        'has_default': 'Default' in extended_attributes or default_cpp_value,
212f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        'has_type_checking_interface':
213f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu            (has_extended_attribute_value(interface, 'TypeChecking', 'Interface') or
214f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu             has_extended_attribute_value(method, 'TypeChecking', 'Interface')) and
215f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu            idl_type.is_wrapper_type,
216f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        'has_type_checking_unrestricted':
217f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu            (has_extended_attribute_value(interface, 'TypeChecking', 'Unrestricted') or
218f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu             has_extended_attribute_value(method, 'TypeChecking', 'Unrestricted')) and
219f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu            idl_type.name in ('Float', 'Double'),
220d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        # Dictionary is special-cased, but arrays and sequences shouldn't be
221e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        'idl_type': idl_type.base_type,
2226f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        'idl_type_object': idl_type,
223f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        'index': index,
224d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        'is_callback_interface': idl_type.is_callback_interface,
2257242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        # FIXME: Remove generic 'Dictionary' special-casing
2267242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        'is_dictionary': idl_type.is_dictionary or idl_type.base_type == 'Dictionary',
227d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        'is_nullable': idl_type.is_nullable,
228f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        'is_optional': argument.is_optional,
229d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        'is_variadic_wrapper_type': is_variadic_wrapper_type,
230d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        'is_wrapper_type': idl_type.is_wrapper_type,
231f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        'name': argument.name,
232197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'private_script_cpp_value_to_v8_value': idl_type.cpp_value_to_v8_value(
233197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            argument.name, isolate='scriptState->isolate()',
234197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            creation_context='scriptState->context()->Global()'),
23509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        'v8_set_return_value': v8_set_return_value(interface.name, method, this_cpp_value),
236197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'v8_set_return_value_for_main_world': v8_set_return_value(interface.name, method, this_cpp_value, for_main_world=True),
2377242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        'v8_value_to_local_cpp_value': v8_value_to_local_cpp_value(argument, index, return_promise=method.returns_promise),
238197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        'vector_type': v8_types.cpp_ptr_type('Vector', 'HeapVector', idl_type.gc_type),
239f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
240f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
241f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
242197021e6b966cfb06891637935ef33fff06433d1Ben Murdochdef argument_declarations_for_private_script(interface, method):
243197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    argument_declarations = ['LocalFrame* frame']
244197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    argument_declarations.append('%s* holderImpl' % interface.name)
245197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    argument_declarations.extend(['%s %s' % (argument.idl_type.cpp_type_args(
246c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        used_as_rvalue_type=True), argument.name) for argument in method.arguments])
247197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if method.idl_type.name != 'void':
248197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        argument_declarations.append('%s* %s' % (method.idl_type.cpp_type, 'result'))
249197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    return argument_declarations
250197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
251197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
25209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)################################################################################
25309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# Value handling
25409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)################################################################################
25509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
25651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)def cpp_value(interface, method, number_of_arguments):
257f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    def cpp_argument(argument):
25809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        idl_type = argument.idl_type
25943e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)        if idl_type.name == 'EventListener':
26043e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)            return argument.name
2617242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        if idl_type.is_dictionary:
2627242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci            return '*%s' % argument.name
2637242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        if (idl_type.name in ['NodeFilter', 'NodeFilterOrNull',
264197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                              'XPathNSResolver', 'XPathNSResolverOrNull']):
265f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            # FIXME: remove this special case
266a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            return '%s.release()' % argument.name
267f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return argument.name
268f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
269f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    # Truncate omitted optional arguments
270f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    arguments = method.arguments[:number_of_arguments]
2715d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    cpp_arguments = []
272197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if 'ImplementedInPrivateScript' in method.extended_attributes:
273197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        cpp_arguments.append('toFrameIfNotDetached(info.GetIsolate()->GetCurrentContext())')
274197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        cpp_arguments.append('impl')
275197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
2765d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    if method.is_constructor:
2775d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        call_with_values = interface.extended_attributes.get('ConstructorCallWith')
2785d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    else:
2795d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        call_with_values = method.extended_attributes.get('CallWith')
2805d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    cpp_arguments.extend(v8_utilities.call_with_arguments(call_with_values))
281197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
282a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    # Members of IDL partial interface definitions are implemented in C++ as
283a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    # static member functions, which for instance members (non-static members)
284a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    # take *impl as their first argument
285a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    if ('PartialInterfaceImplementedAs' in method.extended_attributes and
286e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        not 'ImplementedInPrivateScript' in method.extended_attributes and
28709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        not method.is_static):
28843e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)        cpp_arguments.append('*impl')
28951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    cpp_arguments.extend(cpp_argument(argument) for argument in arguments)
290197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
2916f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    this_union_arguments = method.idl_type and method.idl_type.union_arguments
29209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if this_union_arguments:
293e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        cpp_arguments.extend([member_argument['cpp_value']
294e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)                              for member_argument in this_union_arguments])
29509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
296197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if 'ImplementedInPrivateScript' in method.extended_attributes:
297197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        if method.idl_type.name != 'void':
298197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            cpp_arguments.append('&result')
299197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    elif ('RaisesException' in method.extended_attributes or
3005d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        (method.is_constructor and
3015d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)         has_extended_attribute_value(interface, 'RaisesException', 'Constructor'))):
30251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        cpp_arguments.append('exceptionState')
30351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
3046f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    if method.name == 'Constructor':
3056f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        base_name = 'create'
3066f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    elif method.name == 'NamedConstructor':
3076f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        base_name = 'createForJSConstructor'
308197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    elif 'ImplementedInPrivateScript' in method.extended_attributes:
309e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        base_name = '%sMethod' % method.name
3106f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    else:
3116f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        base_name = v8_utilities.cpp_name(method)
3126f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch
3136f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    cpp_method_name = v8_utilities.scoped_name(interface, method, base_name)
31451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    return '%s(%s)' % (cpp_method_name, ', '.join(cpp_arguments))
31551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
316f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
31709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)def v8_set_return_value(interface_name, method, cpp_value, for_main_world=False):
318f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    idl_type = method.idl_type
31909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    extended_attributes = method.extended_attributes
320323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    if not idl_type or idl_type.name == 'void':
321323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        # Constructors and void methods don't have a return type
32251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return None
323f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
324197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if ('ImplementedInPrivateScript' in extended_attributes and
325197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        not idl_type.is_wrapper_type and
326197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        not idl_type.is_basic_type):
327197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        raise Exception('Private scripts supports only primitive types and DOM wrappers.')
328197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
32909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    release = False
330f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    # [CallWith=ScriptState], [RaisesException]
331197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if use_local_result(method):
332197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        if idl_type.is_explicit_nullable:
333197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            # result is of type Nullable<T>
334197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            cpp_value = 'result.get()'
335197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        else:
336197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            cpp_value = 'result'
337d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        release = idl_type.release
338f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
33943e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)    script_wrappable = 'impl' if inherits_interface(interface_name, 'Node') else ''
340d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return idl_type.v8_set_return_value(cpp_value, extended_attributes, script_wrappable=script_wrappable, release=release, for_main_world=for_main_world)
34151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
34251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
343e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)def v8_value_to_local_cpp_variadic_value(argument, index, return_promise):
344d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    assert argument.is_variadic
345d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    idl_type = argument.idl_type
346d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
347e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    suffix = ''
348e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
3497242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    macro = 'TONATIVE_VOID_EXCEPTIONSTATE'
350d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    macro_args = [
3517242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        argument.name,
3527242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        'toImplArguments<%s>(info, %s, exceptionState)' % (idl_type.cpp_type, index),
3537242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        'exceptionState',
354d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    ]
355d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
356e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    if return_promise:
357e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        suffix += '_PROMISE'
3587242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        macro_args.extend(['info', 'ScriptState::current(info.GetIsolate())'])
359e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
360e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    suffix += '_INTERNAL'
361e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
362e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    return '%s%s(%s)' % (macro, suffix, ', '.join(macro_args))
363d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
364d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
365e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)def v8_value_to_local_cpp_value(argument, index, return_promise=False):
36619cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)    extended_attributes = argument.extended_attributes
367f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    idl_type = argument.idl_type
368f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    name = argument.name
369f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if argument.is_variadic:
370e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        return v8_value_to_local_cpp_variadic_value(argument, index, return_promise)
371197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    return idl_type.v8_value_to_local_cpp_value(extended_attributes, 'info[%s]' % index,
372e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)                                                name, index=index, declare_variable=False, return_promise=return_promise)
37309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
37409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
37509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)################################################################################
37609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# Auxiliary functions
37709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)################################################################################
37809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
37909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# [NotEnumerable]
38009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)def property_attributes(method):
38109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    extended_attributes = method.extended_attributes
38209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    property_attributes_list = []
38309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if 'NotEnumerable' in extended_attributes:
38409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        property_attributes_list.append('v8::DontEnum')
385197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if 'Unforgeable' in extended_attributes:
38609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        property_attributes_list.append('v8::ReadOnly')
38709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if property_attributes_list:
38809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        property_attributes_list.insert(0, 'v8::DontDelete')
38909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return property_attributes_list
39009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
39109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
392e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)def union_member_argument_context(idl_type, index):
393e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    """Returns a context of union member for argument."""
394e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    this_cpp_value = 'result%d' % index
395e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    this_cpp_type = idl_type.cpp_type
3969e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    this_cpp_type_initializer = idl_type.cpp_type_initializer
397e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    cpp_return_value = this_cpp_value
398e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
399e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    if not idl_type.cpp_type_has_null_value:
400e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        this_cpp_type = v8_types.cpp_template_type('Nullable', this_cpp_type)
4019e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        this_cpp_type_initializer = ''
402e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        cpp_return_value = '%s.get()' % this_cpp_value
403e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
404e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    if idl_type.is_string_type:
405e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        null_check_value = '!%s.isNull()' % this_cpp_value
406e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    else:
407e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        null_check_value = this_cpp_value
408e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
409e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    return {
410e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        'cpp_type': this_cpp_type,
4119e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        'cpp_type_initializer': this_cpp_type_initializer,
412e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        'cpp_value': this_cpp_value,
413e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        'null_check_value': null_check_value,
414e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        'v8_set_return_value': idl_type.v8_set_return_value(
415e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            cpp_value=cpp_return_value,
416e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            release=idl_type.release),
417e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    }
418e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
419e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
42009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)def union_arguments(idl_type):
421e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    return [union_member_argument_context(member_idl_type, index)
422e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            for index, member_idl_type
423e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            in enumerate(idl_type.member_types)]
424d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
425197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
426197021e6b966cfb06891637935ef33fff06433d1Ben Murdochdef argument_default_cpp_value(argument):
4277242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if argument.idl_type.is_dictionary:
4287242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        # We always create impl objects for IDL dictionaries.
4297242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        return '%s::create()' % argument.idl_type.base_type
430197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if not argument.default_value:
431197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        return None
432197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    return argument.idl_type.literal_cpp_value(argument.default_value)
433197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
434e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)IdlTypeBase.union_arguments = None
435d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)IdlUnionType.union_arguments = property(union_arguments)
436197021e6b966cfb06891637935ef33fff06433d1Ben MurdochIdlArgument.default_cpp_value = property(argument_default_cpp_value)
4377242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
4387242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
4397242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccidef method_returns_promise(method):
4407242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    return method.idl_type and method.idl_type.name == 'Promise'
4417242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
4427242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano TucciIdlOperation.returns_promise = property(method_returns_promise)
4437242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
4447242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
4457242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccidef argument_conversion_needs_exception_state(method, argument):
4467242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    idl_type = argument.idl_type
4477242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    return (idl_type.v8_conversion_needs_exception_state or
4487242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci            argument.is_variadic or
4497242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci            (method.returns_promise and (idl_type.is_string_type or
4507242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci                                         idl_type.is_enum)))
451