18abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# Copyright (C) 2013 Google Inc. All rights reserved.
28abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#
38abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# Redistribution and use in source and binary forms, with or without
48abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# modification, are permitted provided that the following conditions are
58abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# met:
68abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#
78abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#     * Redistributions of source code must retain the above copyright
88abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# notice, this list of conditions and the following disclaimer.
98abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#     * Redistributions in binary form must reproduce the above
108abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# copyright notice, this list of conditions and the following disclaimer
118abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# in the documentation and/or other materials provided with the
128abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# distribution.
138abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#     * Neither the name of Google Inc. nor the names of its
148abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# contributors may be used to endorse or promote products derived from
158abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# this software without specific prior written permission.
168abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#
178abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
188abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
198abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
208abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
218abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
228abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
238abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
278abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
299bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)"""Generate template values for attributes.
309bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
319bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)FIXME: Not currently used in build.
329bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)This is a rewrite of the Perl IDL compiler in Python, but is not complete.
339bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)Once it is complete, we will switch all IDL files over to Python at once.
349bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)Until then, please work on the Perl IDL compiler.
359bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)For details, see bug http://crbug.com/239771
369bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)"""
378abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
38f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)from v8_globals import includes
398abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)import v8_types
408abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)import v8_utilities
41bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)from v8_utilities import capitalize, cpp_name, has_extended_attribute, uncapitalize
428abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
438abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
44f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)def generate_attribute(interface, attribute):
45f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    idl_type = attribute.idl_type
461e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    extended_attributes = attribute.extended_attributes
47bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
4851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    v8_types.add_includes_for_type(idl_type)
4951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
5051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    # [CheckSecurity]
5151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    is_check_security_for_node = 'CheckSecurity' in extended_attributes
5251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if is_check_security_for_node:
5351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        includes.add('bindings/v8/BindingSecurity.h')
5451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    # [Custom]
5551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    has_custom_getter = ('Custom' in extended_attributes and
5651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)                         extended_attributes['Custom'] in [None, 'Getter'])
5751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    has_custom_setter = (not attribute.is_read_only and
5851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)                         'Custom' in extended_attributes and
5951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)                         extended_attributes['Custom'] in [None, 'Setter'])
6051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    # [Reflect]
6151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    is_reflect = 'Reflect' in extended_attributes
6251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if is_reflect:
6351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        includes.add('core/dom/custom/CustomElementCallbackDispatcher.h')
6451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
6551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (idl_type == 'EventHandler' and
6651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        interface.name in ['Window', 'WorkerGlobalScope'] and
6751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        attribute.name == 'onerror'):
6851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        includes.add('bindings/v8/V8ErrorHandler.h')
6951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
708abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    contents = {
711e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        'access_control_list': access_control_list(attribute),
72f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        'activity_logging_world_list_for_getter': v8_utilities.activity_logging_world_list(attribute, 'Getter'),  # [ActivityLogging]
73f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        'activity_logging_world_list_for_setter': v8_utilities.activity_logging_world_list(attribute, 'Setter'),  # [ActivityLogging]
741e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        'cached_attribute_validation_method': extended_attributes.get('CachedAttribute'),
7551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'conditional_string': v8_utilities.conditional_string(attribute),
76e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)        'constructor_type': v8_types.constructor_type(idl_type)
77e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)                            if is_constructor_attribute(attribute) else None,
78a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        'cpp_name': cpp_name(attribute),
7906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)        'cpp_type': v8_types.cpp_type(idl_type),
8051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'deprecate_as': v8_utilities.deprecate_as(attribute),  # [DeprecateAs]
8151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'enum_validation_expression':
8251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            v8_utilities.enum_validation_expression(idl_type),
83bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        'has_custom_getter': has_custom_getter,
84bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        'has_custom_setter': has_custom_setter,
8551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'has_strict_type_checking': (
8651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            'StrictTypeChecking' in extended_attributes and
8751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            v8_types.is_interface_type(idl_type)),
881e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        'idl_type': idl_type,
8951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_call_with_execution_context': v8_utilities.has_extended_attribute_value(attribute, 'CallWith', 'ExecutionContext'),
9051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_check_security_for_node': is_check_security_for_node,
9151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_expose_js_accessors': 'ExposeJSAccessors' in extended_attributes,
92e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)        'is_getter_raises_exception': (  # [RaisesException]
9351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            'RaisesException' in extended_attributes and
9451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            extended_attributes['RaisesException'] in [None, 'Getter']),
95a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        'is_initialized_by_event_constructor':
96a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            'InitializedByEventConstructor' in extended_attributes,
97bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        'is_keep_alive_for_gc': is_keep_alive_for_gc(attribute),
981e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        'is_nullable': attribute.is_nullable,
9951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_per_world_bindings': 'PerWorldBindings' in extended_attributes,
100f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        'is_read_only': attribute.is_read_only,
10151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_reflect': is_reflect,
102bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        'is_replaceable': 'Replaceable' in attribute.extended_attributes,
10351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_setter_raises_exception': (
10451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            'RaisesException' in extended_attributes and
10551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            extended_attributes['RaisesException'] in [None, 'Setter']),
1061e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        'is_static': attribute.is_static,
10751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'is_unforgeable': 'Unforgeable' in extended_attributes,
10851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'measure_as': v8_utilities.measure_as(attribute),  # [MeasureAs]
1091e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        'name': attribute.name,
110a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        'per_context_enabled_function': v8_utilities.per_context_enabled_function_name(attribute),  # [PerContextEnabled]
1111e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        'property_attributes': property_attributes(attribute),
11251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'setter_callback': setter_callback_name(interface, attribute),
1138abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        'v8_type': v8_types.v8_type(idl_type),
114a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        'runtime_enabled_function': v8_utilities.runtime_enabled_function_name(attribute),  # [RuntimeEnabled]
11551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'world_suffixes': ['', 'ForMainWorld']
11651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)                          if 'PerWorldBindings' in extended_attributes
11751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)                          else [''],  # [PerWorldBindings]
1188abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
11951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
120f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if is_constructor_attribute(attribute):
121f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return contents
122bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if not has_custom_getter:
123f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        generate_getter(interface, attribute, contents)
12451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if not(attribute.is_read_only or has_custom_setter):
12551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        contents.update({
12651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            'cpp_setter': setter_expression(interface, attribute, contents),
12751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            'v8_value_to_local_cpp_value': v8_types.v8_value_to_local_cpp_value(
12851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)                idl_type, extended_attributes, 'jsValue', 'cppValue'),
12951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        })
130bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
131f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return contents
132bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
133bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
134f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# Getter
135f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
136f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)def generate_getter(interface, attribute, contents):
137f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    idl_type = attribute.idl_type
138bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    extended_attributes = attribute.extended_attributes
1391e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
140f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    cpp_value = getter_expression(interface, attribute, contents)
1411e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    # Normally we can inline the function call into the return statement to
1421e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    # avoid the overhead of using a Ref<> temporary, but for some cases
1431e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    # (nullable types, EventHandler, [CachedAttribute], or if there are
1441e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    # exceptions), we need to use a local variable.
1451e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    # FIXME: check if compilers are smart enough to inline this, and if so,
1461e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    # always use a local variable (for readability and CG simplicity).
1471e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (attribute.is_nullable or
1481e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        idl_type == 'EventHandler' or
1491e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        'CachedAttribute' in extended_attributes or
150bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        contents['is_getter_raises_exception']):
1511e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        contents['cpp_value_original'] = cpp_value
152bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        cpp_value = 'jsValue'
1531e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
154bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if contents['is_keep_alive_for_gc']:
155bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        v8_set_return_value_statement = 'v8SetReturnValue(info, wrapper)'
1561e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        includes.add('bindings/v8/V8HiddenPropertyName.h')
1571e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    else:
15819cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)        v8_set_return_value_statement = v8_types.v8_set_return_value(idl_type, cpp_value, extended_attributes=extended_attributes, script_wrappable='imp')
1591e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1601e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    contents.update({
16151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'cpp_value': cpp_value,
16251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        'v8_set_return_value': v8_set_return_value_statement,
1631e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    })
1641e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1651e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
166f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)def getter_expression(interface, attribute, contents):
167bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    arguments = []
168f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    this_getter_base_name = getter_base_name(attribute, arguments)
16951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    getter_name = v8_utilities.scoped_name(interface, attribute, this_getter_base_name)
1701e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
17151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    arguments.extend(v8_utilities.call_with_arguments(attribute))
1721e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if attribute.is_nullable:
1731e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        arguments.append('isNull')
174bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if contents['is_getter_raises_exception']:
17551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        arguments.append('exceptionState')
176f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if attribute.idl_type == 'EventHandler':
1771e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        arguments.append('isolatedWorldForIsolate(info.GetIsolate())')
178bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    return '%s(%s)' % (getter_name, ', '.join(arguments))
1791e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
1801e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
181bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)CONTENT_ATTRIBUTE_GETTER_NAMES = {
182bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    'boolean': 'fastHasAttribute',
183bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    'long': 'getIntegralAttribute',
184bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    'unsigned long': 'getUnsignedIntegralAttribute',
185bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)}
186bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
187bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
188f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)def getter_base_name(attribute, arguments):
189bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    extended_attributes = attribute.extended_attributes
190bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if 'Reflect' not in extended_attributes:
191bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        return uncapitalize(cpp_name(attribute))
192bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
193bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    content_attribute_name = extended_attributes['Reflect'] or attribute.name.lower()
194bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if content_attribute_name in ['class', 'id', 'name']:
195bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        # Special-case for performance optimization.
196bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        return 'get%sAttribute' % content_attribute_name.capitalize()
197bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
198f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    arguments.append(scoped_content_attribute_name(attribute))
199bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
200f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    idl_type = attribute.idl_type
201bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if idl_type in CONTENT_ATTRIBUTE_GETTER_NAMES:
202bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        return CONTENT_ATTRIBUTE_GETTER_NAMES[idl_type]
203bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if 'URL' in attribute.extended_attributes:
204bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        return 'getURLAttribute'
205bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    return 'fastGetAttribute'
2061e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
2071e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
2081e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)def is_keep_alive_for_gc(attribute):
209f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    idl_type = attribute.idl_type
2101e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    extended_attributes = attribute.extended_attributes
2111e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    return (
2121e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        # For readonly attributes, for performance reasons we keep the attribute
2131e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        # wrapper alive while the owner wrapper is alive, because the attribute
2141e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        # never changes.
2151e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        (attribute.is_read_only and
216bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)         v8_types.is_wrapper_type(idl_type) and
2171e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)         # There are some exceptions, however:
2181e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)         not(
2191e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)             # Node lifetime is managed by object grouping.
220bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)             v8_types.is_dom_node_type(idl_type) or
2211e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)             # A self-reference is unnecessary.
2221e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)             attribute.name == 'self' or
2231e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)             # FIXME: Remove these hard-coded hacks.
2241e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)             idl_type in ['EventHandler', 'Promise', 'Window'] or
2251e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)             idl_type.startswith('HTML'))))
2261e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
2271e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
228f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# Setter
229f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
230f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)def setter_expression(interface, attribute, contents):
23151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    arguments = v8_utilities.call_with_arguments(attribute, attribute.extended_attributes.get('SetterCallWith'))
232bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
233f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    this_setter_base_name = setter_base_name(attribute, arguments)
23451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    setter_name = v8_utilities.scoped_name(interface, attribute, this_setter_base_name)
235bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
236f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    idl_type = attribute.idl_type
237bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if idl_type == 'EventHandler':
238bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        # FIXME: pass the isolate instead of the isolated world
239bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        isolated_world = 'isolatedWorldForIsolate(info.GetIsolate())'
240bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        arguments.extend(['V8EventListenerList::getEventListener(jsValue, true, ListenerFindOrCreate)', isolated_world])
241bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        contents['event_handler_getter_expression'] = 'imp->%s(%s)' % (cpp_name(attribute), isolated_world)
242bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    elif v8_types.is_interface_type(idl_type) and not v8_types.array_type(idl_type):
243bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        # FIXME: should be able to eliminate WTF::getPtr in most or all cases
244bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        arguments.append('WTF::getPtr(cppValue)')
245bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    else:
246bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        arguments.append('cppValue')
247bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if contents['is_setter_raises_exception']:
24851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        arguments.append('exceptionState')
249bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
250bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    return '%s(%s)' % (setter_name, ', '.join(arguments))
251bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
252bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
253bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)CONTENT_ATTRIBUTE_SETTER_NAMES = {
254bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    'boolean': 'setBooleanAttribute',
255bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    'long': 'setIntegralAttribute',
256bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    'unsigned long': 'setUnsignedIntegralAttribute',
257bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)}
258bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
259bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
260f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)def setter_base_name(attribute, arguments):
261bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if 'Reflect' not in attribute.extended_attributes:
262bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        return 'set%s' % capitalize(cpp_name(attribute))
263f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    arguments.append(scoped_content_attribute_name(attribute))
264bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
265f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    idl_type = attribute.idl_type
266bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if idl_type in CONTENT_ATTRIBUTE_SETTER_NAMES:
267bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        return CONTENT_ATTRIBUTE_SETTER_NAMES[idl_type]
268bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    return 'setAttribute'
269bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
270bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
271f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)def scoped_content_attribute_name(attribute):
272bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    content_attribute_name = attribute.extended_attributes['Reflect'] or attribute.name.lower()
273bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    namespace = 'HTMLNames'  # FIXME: can be SVG too
274bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    includes.add('%s.h' % namespace)
275bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    return '%s::%sAttr' % (namespace, content_attribute_name)
276bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
277bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
278bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)def scoped_name(interface, attribute, base_name):
279bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if attribute.is_static:
280bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        return '%s::%s' % (interface.name, base_name)
281bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    return 'imp->%s' % base_name
282bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
283bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
284f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# Attribute configuration
285f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
286f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)# [Replaceable]
287bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)def setter_callback_name(interface, attribute):
288bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    cpp_class_name = cpp_name(interface)
289f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if ('Replaceable' in attribute.extended_attributes or
29051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        is_constructor_attribute(attribute)):
291f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        # FIXME: rename to ForceSetAttributeOnThisCallback, since also used for Constructors
292bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        return '{0}V8Internal::{0}ReplaceableAttributeSetterCallback'.format(cpp_class_name)
293f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    # FIXME: support [PutForwards]
294f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if attribute.is_read_only:
295f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return '0'
296bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    return '%sV8Internal::%sAttributeSetterCallback' % (cpp_class_name, attribute.name)
297bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
298bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
29951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)# [DoNotCheckSecurity], [Unforgeable]
3001e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)def access_control_list(attribute):
3011e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    extended_attributes = attribute.extended_attributes
302bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    access_control = []
3031e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if 'DoNotCheckSecurity' in extended_attributes:
30451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        do_not_check_security = extended_attributes['DoNotCheckSecurity']
30551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        if do_not_check_security == 'Setter':
3061e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)            access_control.append('v8::ALL_CAN_WRITE')
30751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        else:
30851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            access_control.append('v8::ALL_CAN_READ')
30951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            if not attribute.is_read_only:
31051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)                access_control.append('v8::ALL_CAN_WRITE')
311bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if 'Unforgeable' in extended_attributes:
312bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        access_control.append('v8::PROHIBITS_OVERWRITING')
313bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    return access_control or ['v8::DEFAULT']
3141e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
3151e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)
316bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)# [NotEnumerable], [Unforgeable]
3171e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)def property_attributes(attribute):
318bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    extended_attributes = attribute.extended_attributes
3191e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    property_attributes_list = []
320f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if ('NotEnumerable' in extended_attributes or
321f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        is_constructor_attribute(attribute)):
3221e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)        property_attributes_list.append('v8::DontEnum')
323bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    if 'Unforgeable' in extended_attributes:
324bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)        property_attributes_list.append('v8::DontDelete')
3251e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    return property_attributes_list or ['v8::None']
326bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
327bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)
328e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)# Constructors
329e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)
330f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)def is_constructor_attribute(attribute):
331f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return attribute.idl_type.endswith('Constructor')
332