summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/bindings/scripts/v8_attributes.py
blob: 60351a54cd00609219199697be324ec1696ba837 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# Copyright (C) 2013 Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# pylint: disable=relative-import
"""Generate template values for attributes.

Extends IdlType with property |constructor_type_name|.

Design doc: http://www.chromium.org/developers/design-documents/idl-compiler
"""

import os
import sys

sys.path.append(
    os.path.join(os.path.dirname(__file__), '..', '..', 'build', 'scripts'))
from blinkbuild.name_style_converter import NameStyleConverter
import idl_types
from idl_types import inherits_interface
from v8_globals import includes
import v8_types
import v8_utilities
from v8_utilities import capitalize
from v8_utilities import cpp_encoded_property_name
from v8_utilities import cpp_name
from v8_utilities import extended_attribute_value_as_list
from v8_utilities import has_extended_attribute_value
from v8_utilities import is_unforgeable
from v8_utilities import scoped_name
from v8_utilities import strip_suffix
from v8_utilities import uncapitalize
from blinkbuild.name_style_converter import NameStyleConverter


def attribute_context(interface, attribute, interfaces, component_info):
    """Creates a Jinja template context for an attribute of an interface.

    Args:
        interface: An interface which |attribute| belongs to
        attribute: An attribute to create the context for
        interfaces: A dict which maps an interface name to the definition
            which can be referred if needed
        component_info: A dict containing component wide information

    Returns:
        A Jinja template context for |attribute|
    """

    idl_type = attribute.idl_type
    base_idl_type = idl_type.base_type
    extended_attributes = attribute.extended_attributes

    idl_type.add_includes_for_type(extended_attributes)
    if idl_type.enum_values:
        includes.add('core/inspector/console_message.h')
        includes.add('platform/heap/heap.h')

    # [CheckSecurity]
    is_cross_origin = 'CrossOrigin' in extended_attributes
    is_check_security_for_receiver = (has_extended_attribute_value(
        interface, 'CheckSecurity', 'Receiver') and is_cross_origin)
    is_check_security_for_return_value = (has_extended_attribute_value(
        attribute, 'CheckSecurity', 'ReturnValue'))
    if is_check_security_for_receiver or is_check_security_for_return_value:
        includes.add('bindings/core/v8/binding_security.h')
    if is_check_security_for_return_value:
        includes.add('core/frame/web_feature.h')
        includes.add('platform/instrumentation/use_counter.h')
    # [CrossOrigin]
    if has_extended_attribute_value(attribute, 'CrossOrigin', 'Setter'):
        includes.add('platform/bindings/v8_cross_origin_callback_info.h')
    # [Constructor]
    # TODO(yukishiino): Constructors are much like methods although constructors
    # are not methods.  Constructors must be data-type properties, and we can
    # support them as a kind of methods.
    constructor_type = idl_type.constructor_type_name if is_constructor_attribute(
        attribute) else None
    # [CEReactions]
    is_ce_reactions = 'CEReactions' in extended_attributes
    if is_ce_reactions:
        includes.add('core/html/custom/ce_reactions_scope.h')
    # [CustomElementCallbacks], [Reflect]
    is_custom_element_callbacks = 'CustomElementCallbacks' in extended_attributes
    is_reflect = 'Reflect' in extended_attributes
    # [ReflectOnly]
    reflect_only = extended_attribute_value_as_list(attribute, 'ReflectOnly')
    if reflect_only:
        reflect_only = map(
            lambda v: cpp_content_attribute_value_name(interface, v),
            reflect_only)
    if is_custom_element_callbacks or is_reflect:
        includes.add('core/html/custom/v0_custom_element_processing_stack.h')
    # [PerWorldBindings]
    if 'PerWorldBindings' in extended_attributes:
        assert idl_type.is_wrapper_type or 'LogActivity' in \
            extended_attributes, \
            '[PerWorldBindings] should only be used with wrapper types: %s.%s' % \
            (interface.name, attribute.name)
    # [SaveSameObject]
    is_save_same_object = ('SameObject' in attribute.extended_attributes and
                           'SaveSameObject' in attribute.extended_attributes)

    # [StringContext]
    if idl_type.has_string_context:
        includes.add('bindings/core/v8/generated_code_helper.h')

    # [CachedAccessor]
    is_cached_accessor = 'CachedAccessor' in extended_attributes

    # [LenientSetter]
    is_lenient_setter = 'LenientSetter' in extended_attributes

    # [CachedAttribute]
    cached_attribute_validation_method = extended_attributes.get(
        'CachedAttribute')

    keep_alive_for_gc = is_keep_alive_for_gc(interface, attribute)

    does_generate_getter = (not has_custom_getter(attribute)
                            and not constructor_type)
    does_generate_setter = (
        has_setter(interface, attribute)
        and not (has_custom_setter(attribute) or is_lenient_setter))

    use_private_property_in_getter = (does_generate_getter
                                      and (cached_attribute_validation_method
                                           or is_save_same_object
                                           or keep_alive_for_gc))
    use_private_property_in_setter = (does_generate_setter
                                      and cached_attribute_validation_method)
    private_property_is_shared_between_getter_and_setter = (
        use_private_property_in_getter and use_private_property_in_setter)

    does_use_private_property = (use_private_property_in_getter
                                 or use_private_property_in_setter
                                 or is_cached_accessor)
    if does_use_private_property:
        includes.add('platform/bindings/v8_private_property.h')

    # [LogActivity]
    if 'LogActivity' in extended_attributes:
        includes.add('platform/bindings/v8_per_context_data.h')

    # [DeprecateAs], [MeasureAs]
    deprecate_as = v8_utilities.deprecate_as(attribute)
    measure_as = v8_utilities.measure_as(attribute, interface)

    # [HighEntropy]
    high_entropy = v8_utilities.high_entropy(attribute)

    is_lazy_data_attribute = \
        (constructor_type and not (measure_as or deprecate_as)) or \
        (str(idl_type) == 'Window' and attribute.name in ('frames', 'self', 'window'))

    runtime_features = component_info['runtime_enabled_features']

    internal_name = cpp_encoded_property_name(attribute)

    cpp_type = idl_type.cpp_type
    if idl_type.is_explicit_nullable:
        cpp_type = v8_types.cpp_template_type('base::Optional', cpp_type)

    context = {
        # [ActivityLogging]
        'activity_logging_world_list_for_getter':
        v8_utilities.activity_logging_world_list(attribute, 'Getter'),
        # [ActivityLogging]
        'activity_logging_world_list_for_setter':
        v8_utilities.activity_logging_world_list(attribute, 'Setter'),
        # [ActivityLogging]
        'activity_logging_world_check':
        v8_utilities.activity_logging_world_check(attribute),
        'cached_accessor_name':
        'k%s%s' % (interface.name, attribute.name.capitalize()),
        'cached_attribute_validation_method':
        cached_attribute_validation_method,
        'camel_case_name':
        NameStyleConverter(internal_name).to_upper_camel_case(),
        'constructor_type':
        constructor_type,
        'context_enabled_feature_name':
        v8_utilities.context_enabled_feature_name(attribute),
        'cpp_name': cpp_name(attribute),
        'cpp_type': cpp_type,
        'cpp_type_initializer': idl_type.cpp_type_initializer,
        'deprecate_as': deprecate_as,
        'does_generate_getter': does_generate_getter,
        'does_generate_setter': does_generate_setter,
        'enum_type': idl_type.enum_type,
        'enum_values': idl_type.enum_values,
        # [Exposed]
        'exposed_test':
        v8_utilities.exposed(attribute, interface),
        'getter_has_no_side_effect':
        has_extended_attribute_value(attribute, 'Affects', 'Nothing'),
        'has_cross_origin_getter':
            has_extended_attribute_value(attribute, 'CrossOrigin', None) or
            has_extended_attribute_value(attribute, 'CrossOrigin', 'Getter'),
        'has_cross_origin_setter':
        has_extended_attribute_value(attribute, 'CrossOrigin', 'Setter'),
        'has_custom_getter': has_custom_getter(attribute),
        'has_custom_setter': has_custom_setter(attribute),
        'has_promise_type': idl_type.name == 'Promise',
        'has_setter': has_setter(interface, attribute),
        'high_entropy': high_entropy,
        'idl_type': str(idl_type),
        'is_cached_accessor': is_cached_accessor,
        'is_call_with_execution_context':
        has_extended_attribute_value(attribute, 'CallWith', 'ExecutionContext'),
        'is_call_with_script_state':
        has_extended_attribute_value(attribute, 'CallWith', 'ScriptState'),
        'is_ce_reactions': is_ce_reactions,
        'is_check_security_for_receiver': is_check_security_for_receiver,
        'is_check_security_for_return_value':
        is_check_security_for_return_value,
        'is_custom_element_callbacks': is_custom_element_callbacks,
        # TODO(yukishiino): Make all DOM attributes accessor-type properties.
        'is_data_type_property': is_data_type_property(interface, attribute),
        'is_getter_raises_exception':  # [RaisesException]
            'RaisesException' in extended_attributes and
            extended_attributes['RaisesException'] in (None, 'Getter'),
        'is_keep_alive_for_gc': keep_alive_for_gc,
        'is_lazy_data_attribute': is_lazy_data_attribute,
        'is_lenient_setter': is_lenient_setter,
        'is_lenient_this': 'LenientThis' in extended_attributes,
        'is_nullable': idl_type.is_nullable,
        'is_explicit_nullable': idl_type.is_explicit_nullable,
        'is_named_constructor': is_named_constructor_attribute(attribute),
        'is_partial_interface_member':
            'PartialInterfaceImplementedAs' in extended_attributes,
        'is_per_world_bindings': 'PerWorldBindings' in extended_attributes,
        'is_put_forwards': 'PutForwards' in extended_attributes,
        'is_read_only': attribute.is_read_only,
        'is_reflect': is_reflect,
        'is_replaceable': 'Replaceable' in attribute.extended_attributes,
        'is_save_same_object': is_save_same_object,
        'is_static': attribute.is_static,
        'is_url': 'URL' in extended_attributes,
        'is_unforgeable': is_unforgeable(attribute),
        'measure_as': measure_as,
        'name': attribute.name,
        'on_instance': v8_utilities.on_instance(interface, attribute),
        'on_interface': v8_utilities.on_interface(interface, attribute),
        'on_prototype': v8_utilities.on_prototype(interface, attribute),
        # [RuntimeEnabled] for origin trial
        'origin_trial_feature_name':
            v8_utilities.origin_trial_feature_name(attribute, runtime_features),
        'private_property_is_shared_between_getter_and_setter':
        private_property_is_shared_between_getter_and_setter,
        'property_attributes': property_attributes(interface, attribute),
        'reflect_empty': cpp_content_attribute_value_name(
            interface, extended_attributes.get('ReflectEmpty')),
        'reflect_invalid': cpp_content_attribute_value_name(
            interface, extended_attributes.get('ReflectInvalid', '')),
        'reflect_missing': cpp_content_attribute_value_name(
            interface, extended_attributes.get('ReflectMissing')),
        'reflect_only': reflect_only,
        # [RuntimeEnabled] if not in origin trial
        'runtime_enabled_feature_name':
        v8_utilities.runtime_enabled_feature_name(attribute, runtime_features),
        # [SecureContext]
        'secure_context_test': v8_utilities.secure_context(attribute, interface),
        'use_output_parameter_for_result': idl_type.use_output_parameter_for_result,
        'world_suffixes': (
            ['', 'ForMainWorld']
            if 'PerWorldBindings' in extended_attributes
            else ['']),  # [PerWorldBindings]
    }

    if not has_custom_getter(attribute):
        getter_context(interface, attribute, context)
    if not has_custom_setter(attribute) and has_setter(interface, attribute):
        setter_context(interface, attribute, interfaces, context)

    # [RuntimeCallStatsCounter]
    runtime_call_stats_context(interface, attribute, context)

    # [CrossOrigin] is incompatible with a number of other attributes, so check
    # for them here.
    if is_cross_origin:
        if context['has_cross_origin_setter'] and context['has_custom_setter']:
            raise Exception(
                '[CrossOrigin] and [Custom] are incompatible on the same setter: %s.%s',
                interface.name, attribute.name)
        if context['is_per_world_bindings']:
            raise Exception(
                '[CrossOrigin] and [PerWorldBindings] are incompatible: %s.%s',
                interface.name, attribute.name)
        if context['constructor_type']:
            raise Exception(
                '[CrossOrigin] cannot be used for constructors: %s.%s',
                interface.name, attribute.name)

    return context


def runtime_call_stats_context(interface, attribute, context):
    includes.add('platform/bindings/runtime_call_stats.h')
    generic_counter_name = (
        'Blink_' + v8_utilities.cpp_name(interface) + '_' + attribute.name)
    (counter, extended_attribute_defined) = v8_utilities.rcs_counter_name(
        attribute, generic_counter_name)
    runtime_call_stats = {
        'extended_attribute_defined':
        extended_attribute_defined,
        'getter_counter':
        '%s_Getter' % counter,
        'setter_counter':
        '%s_Setter' % counter,
        'constructor_getter_callback_counter':
        '%s_ConstructorGetterCallback' % generic_counter_name,
    }
    context.update({'runtime_call_stats': runtime_call_stats})


def is_origin_trial_enabled(attribute):
    return bool(attribute['origin_trial_feature_name'])


def is_secure_context(attribute):
    return bool(attribute['secure_context_test'])


def filter_accessors(attributes):
    return [
        attribute for attribute in attributes
        if not (attribute['exposed_test'] or is_secure_context(attribute)
                or attribute['context_enabled_feature_name']
                or is_origin_trial_enabled(attribute)
                or attribute['runtime_enabled_feature_name'])
        and not attribute['is_data_type_property']
    ]


def is_data_attribute(attribute):
    return (not (attribute['exposed_test'] or is_secure_context(attribute)
                 or attribute['context_enabled_feature_name']
                 or is_origin_trial_enabled(attribute)
                 or attribute['runtime_enabled_feature_name'])
            and attribute['is_data_type_property'])


def filter_data_attributes(attributes):
    return [
        attribute for attribute in attributes if is_data_attribute(attribute)
    ]


def filter_runtime_enabled(attributes):
    return [
        attribute for attribute in attributes
        if not (attribute['exposed_test'] or is_secure_context(attribute))
        and attribute['runtime_enabled_feature_name']
    ]


def filter_conditionally_enabled(attributes):
    return [
        attribute for attribute in attributes
        if attribute['exposed_test'] or (is_secure_context(
            attribute) and not is_origin_trial_enabled(attribute))
    ]


################################################################################
# Getter
################################################################################


def getter_context(interface, attribute, context):
    idl_type = attribute.idl_type
    base_idl_type = idl_type.base_type
    extended_attributes = attribute.extended_attributes

    cpp_value = getter_expression(interface, attribute, context)
    # Normally we can inline the function call into the return statement to
    # avoid the overhead of using a Ref<> temporary, but for some cases
    # (nullable types, EventHandler, [CachedAttribute], or if there are
    # exceptions), we need to use a local variable.
    # FIXME: check if compilers are smart enough to inline this, and if so,
    # always use a local variable (for readability and CG simplicity).
    if (idl_type.is_explicit_nullable or base_idl_type == 'EventHandler'
            or 'CachedAttribute' in extended_attributes
            or 'ReflectOnly' in extended_attributes
            or context['is_keep_alive_for_gc']
            or context['is_getter_raises_exception']):
        context['cpp_value_original'] = cpp_value
        cpp_value = 'cpp_value'

    def v8_set_return_value_statement(for_main_world=False):
        if (context['is_keep_alive_for_gc']
                or 'CachedAttribute' in extended_attributes):
            return 'V8SetReturnValue(info, v8_value)'
        if idl_type.is_explicit_nullable:
            cpp_return_value = 'cpp_value.value()'
            if idl_type.is_frozen_array:
                cpp_return_value = 'FreezeV8Object(ToV8(cpp_value.value(), info.Holder(), info.GetIsolate()), info.GetIsolate())'
            return 'V8SetReturnValue(info, {})'.format(cpp_return_value)
        return idl_type.v8_set_return_value(
            cpp_value,
            extended_attributes=extended_attributes,
            script_wrappable='impl',
            for_main_world=for_main_world,
            is_static=attribute.is_static)

    cpp_value_to_script_wrappable = cpp_value
    if idl_type.is_array_buffer_view_or_typed_array:
        cpp_value_to_script_wrappable += '.View()'

    context.update({
        'cpp_value':
        cpp_value,
        'cpp_value_to_script_wrappable':
        cpp_value_to_script_wrappable,
        'cpp_value_to_v8_value':
        idl_type.cpp_value_to_v8_value(
            cpp_value=cpp_value,
            creation_context='holder',
            extended_attributes=extended_attributes),
        'v8_set_return_value_for_main_world':
        v8_set_return_value_statement(for_main_world=True),
        'v8_set_return_value':
        v8_set_return_value_statement(),
    })


def getter_expression(interface, attribute, context):
    extra_arguments = []
    this_getter_base_name = getter_base_name(interface, attribute,
                                             extra_arguments)
    getter_name = scoped_name(interface, attribute, this_getter_base_name)

    arguments = []
    arguments.extend(
        v8_utilities.call_with_arguments(
            attribute.extended_attributes.get('CallWith')))
    # Members of IDL partial interface definitions are implemented in C++ as
    # static member functions, which for instance members (non-static members)
    # take *impl as their first argument
    if ('PartialInterfaceImplementedAs' in attribute.extended_attributes
            and not attribute.is_static):
        arguments.append('*impl')
    arguments.extend(extra_arguments)
    if context['is_getter_raises_exception']:
        arguments.append('exception_state')
    if attribute.idl_type.use_output_parameter_for_result:
        arguments.append('result')

    expression = '%s(%s)' % (getter_name, ', '.join(arguments))
    # Needed to handle getter expressions returning Type& as the
    # use site for |expression| expects Type*.
    if (attribute.idl_type.is_interface_type and len(arguments) == 0
            and not attribute.idl_type.is_array_buffer_view_or_typed_array):
        return 'WTF::GetPtr(%s)' % expression
    return expression


CONTENT_ATTRIBUTE_GETTER_NAMES = {
    'boolean': 'FastHasAttribute',
    'long': 'GetIntegralAttribute',
    'unsigned long': 'GetUnsignedIntegralAttribute',
    'Element': 'GetElementAttribute',
}


def getter_base_name(interface, attribute, arguments):
    extended_attributes = attribute.extended_attributes

    if 'Reflect' not in extended_attributes:
        name = cpp_name(attribute)
        return name if 'ImplementedAs' in extended_attributes \
            else uncapitalize(name)

    content_attribute_name = (extended_attributes['Reflect']
                              or attribute.name.lower())
    if content_attribute_name in ['class', 'id', 'name']:
        # Special-case for performance optimization.
        return 'Get%sAttribute' % content_attribute_name.capitalize()

    arguments.append(scoped_content_attribute_name(interface, attribute))

    base_idl_type = attribute.idl_type.base_type
    if base_idl_type in CONTENT_ATTRIBUTE_GETTER_NAMES:
        return CONTENT_ATTRIBUTE_GETTER_NAMES[base_idl_type]
    if 'URL' in attribute.extended_attributes:
        return 'GetURLAttribute'
    idl_type = attribute.idl_type
    if idl_type.is_frozen_array:
        return 'Get%sArrayAttribute' % idl_type.element_type
    return 'FastGetAttribute'


def is_keep_alive_for_gc(interface, attribute):
    idl_type = attribute.idl_type
    base_idl_type = idl_type.base_type
    extended_attributes = attribute.extended_attributes
    if attribute.is_static:
        return False
    if idl_type.is_array_buffer_or_view:
        return False
    return (
        # For readonly attributes, for performance reasons we keep the attribute
        # wrapper alive while the owner wrapper is alive, because the attribute
        # never changes.
        (
            attribute.is_read_only and idl_type.is_wrapper_type and
            # There are some exceptions, however:
            not (
                # Node lifetime is managed by object grouping.
                inherits_interface(interface.name, 'Node')
                or inherits_interface(base_idl_type, 'Node') or
                # A self-reference is unnecessary.
                attribute.name == 'self' or
                # FIXME: Remove these hard-coded hacks.
                base_idl_type in ['EventTarget', 'Window']
                or base_idl_type.startswith(('HTML', 'SVG')))))


################################################################################
# Setter
################################################################################


def setter_context(interface, attribute, interfaces, context):
    if 'PutForwards' in attribute.extended_attributes:
        # Make sure the target interface and attribute exist.
        target_interface_name = attribute.idl_type.base_type
        target_attribute_name = attribute.extended_attributes['PutForwards']
        interface = interfaces[target_interface_name]
        try:
            next(candidate for candidate in interface.attributes
                 if candidate.name == target_attribute_name)
        except StopIteration:
            raise Exception('[PutForward] target not found:\n'
                            'Attribute "%s" is not present in interface "%s"' %
                            (target_attribute_name, target_interface_name))
        context['target_attribute_name'] = target_attribute_name
        return

    if ('Replaceable' in attribute.extended_attributes):
        # Create the property, and early-return if an exception is thrown.
        # Subsequent cleanup code may not be prepared to handle a pending
        # exception.
        context['cpp_setter'] = (
            'if (info.Holder()->CreateDataProperty(' +
            'info.GetIsolate()->GetCurrentContext(), ' +
            'property_name, v8_value).IsNothing())' + '\n  return')
        return

    extended_attributes = attribute.extended_attributes
    idl_type = attribute.idl_type

    # [RaisesException], [RaisesException=Setter]
    is_setter_raises_exception = (
        'RaisesException' in extended_attributes
        and extended_attributes['RaisesException'] in [None, 'Setter'])

    has_type_checking_interface = idl_type.is_wrapper_type

    use_common_reflection_setter = False
    # Enable use_common_reflection_setter if
    #  * extended_attributes is [CEReactions, Reflect] or
    #    [CEReactions, Reflect, RuntimeEnabled],
    #  * the type is boolean, DOMString, or DOMString?, and
    #  * the interface inherits from 'Element'.
    if ('Reflect' in extended_attributes
            and 'CEReactions' in extended_attributes
            and str(idl_type) in ('boolean', 'DOMString', 'DOMString?')
            and inherits_interface(interface.name, 'Element')):
        if (len(extended_attributes) == 2
                or (len(extended_attributes) == 3
                    and 'RuntimeEnabled' in extended_attributes)):
            use_common_reflection_setter = True

    context.update({
        'has_setter_exception_state':
        is_setter_raises_exception or has_type_checking_interface
        or idl_type.v8_conversion_needs_exception_state,
        'has_type_checking_interface':
        has_type_checking_interface,
        'is_setter_call_with_execution_context':
        has_extended_attribute_value(attribute, 'SetterCallWith',
                                     'ExecutionContext'),
        'is_setter_call_with_script_state':
        has_extended_attribute_value(attribute, 'SetterCallWith',
                                     'ScriptState'),
        'is_setter_raises_exception':
        is_setter_raises_exception,
        'use_common_reflection_setter':
        use_common_reflection_setter,
        'v8_value_to_local_cpp_value':
        idl_type.v8_value_to_local_cpp_value(
            extended_attributes,
            'v8_value',
            'cpp_value',
            code_generation_target='attribute_set'),
    })

    # setter_expression() depends on context values we set above.
    context['cpp_setter'] = setter_expression(interface, attribute, context)


def setter_expression(interface, attribute, context):
    extended_attributes = attribute.extended_attributes
    arguments = v8_utilities.call_with_arguments(
        extended_attributes.get('SetterCallWith')
        or extended_attributes.get('CallWith'))

    extra_arguments = []
    this_setter_base_name = setter_base_name(interface, attribute,
                                             extra_arguments)
    setter_name = scoped_name(interface, attribute, this_setter_base_name)

    # Members of IDL partial interface definitions are implemented in C++ as
    # static member functions, which for instance members (non-static members)
    # take *impl as their first argument
    if ('PartialInterfaceImplementedAs' in extended_attributes
            and not attribute.is_static):
        arguments.append('*impl')
    arguments.extend(extra_arguments)
    idl_type = attribute.idl_type
    if idl_type.base_type in ('EventHandler', 'OnBeforeUnloadEventHandler',
                              'OnErrorEventHandler'):
        if idl_type.base_type == 'EventHandler':
            handler_type = 'kEventHandler'
        elif idl_type.base_type == 'OnBeforeUnloadEventHandler':
            handler_type = 'kOnBeforeUnloadEventHandler'
        elif idl_type.base_type == 'OnErrorEventHandler':
            handler_type = 'kOnErrorEventHandler'
        arguments.append('JSEventHandler::CreateOrNull(' + 'v8_value, ' +
                         'JSEventHandler::HandlerType::' + handler_type + ')')
    else:
        arguments.append('cpp_value')
    if context['is_setter_raises_exception']:
        arguments.append('exception_state')
    if context['use_common_reflection_setter']:
        attr_name = scoped_content_attribute_name(interface, attribute)
        if idl_type.base_type == 'boolean':
            setter_name = 'V8SetReflectedBooleanAttribute'
            arguments = [
                'info',
                '"%s"' % interface.name,
                '"%s"' % attribute.name, attr_name
            ]
        elif idl_type.base_type == 'DOMString':
            if idl_type.is_nullable:
                setter_name = 'V8SetReflectedNullableDOMStringAttribute'
            else:
                setter_name = 'V8SetReflectedDOMStringAttribute'
            arguments = ['info', attr_name]

    return '%s(%s)' % (setter_name, ', '.join(arguments))


CONTENT_ATTRIBUTE_SETTER_NAMES = {
    'boolean': 'SetBooleanAttribute',
    'long': 'SetIntegralAttribute',
    'unsigned long': 'SetUnsignedIntegralAttribute',
    'Element': 'SetElementAttribute',
}


def setter_base_name(interface, attribute, arguments):
    if 'Reflect' not in attribute.extended_attributes:
        return 'set%s' % capitalize(cpp_name(attribute))
    arguments.append(scoped_content_attribute_name(interface, attribute))

    base_idl_type = attribute.idl_type.base_type
    if base_idl_type in CONTENT_ATTRIBUTE_SETTER_NAMES:
        return CONTENT_ATTRIBUTE_SETTER_NAMES[base_idl_type]
    idl_type = attribute.idl_type
    if idl_type.is_frozen_array:
        return 'Set%sArrayAttribute' % idl_type.element_type
    return 'setAttribute'


def scoped_content_attribute_name(interface, attribute):
    content_attribute_name = (attribute.extended_attributes['Reflect']
                              or attribute.name.lower())
    symbol_name = 'k' + NameStyleConverter(
        content_attribute_name).to_upper_camel_case()
    if interface.name.startswith('SVG'):
        namespace = 'svg_names'
        includes.add('core/svg_names.h')
    else:
        namespace = 'html_names'
        includes.add('core/html_names.h')
    return '%s::%sAttr' % (namespace, symbol_name)


def cpp_content_attribute_value_name(interface, value):
    if value == '':
        return 'g_empty_atom'
    if not value:
        return value
    includes.add('core/keywords.h')
    return 'keywords::' + NameStyleConverter(value).to_enum_value()


################################################################################
# Attribute configuration
################################################################################


# Property descriptor's {writable: boolean}
def is_writable(attribute):
    return (not attribute.is_read_only or any(
        keyword in attribute.extended_attributes
        for keyword in ['PutForwards', 'Replaceable', 'LenientSetter']))


def is_data_type_property(interface, attribute):
    return (is_constructor_attribute(attribute)
            or 'CrossOrigin' in attribute.extended_attributes)


# [PutForwards], [Replaceable], [LenientSetter]
def has_setter(interface, attribute):
    if (is_data_type_property(interface, attribute)
            and (is_constructor_attribute(attribute)
                 or 'Replaceable' in attribute.extended_attributes)):
        return False

    return is_writable(attribute)


# [NotEnumerable], [Unforgeable]
def property_attributes(interface, attribute):
    extended_attributes = attribute.extended_attributes
    property_attributes_list = []
    if ('NotEnumerable' in extended_attributes
            or is_constructor_attribute(attribute)):
        property_attributes_list.append('v8::DontEnum')
    if is_unforgeable(attribute):
        property_attributes_list.append('v8::DontDelete')
    if not is_writable(attribute):
        property_attributes_list.append('v8::ReadOnly')
    return property_attributes_list or ['v8::None']


# [Custom], [Custom=Getter]
def has_custom_getter(attribute):
    extended_attributes = attribute.extended_attributes
    return ('Custom' in extended_attributes
            and extended_attributes['Custom'] in [None, 'Getter'])


# [Custom], [Custom=Setter]
def has_custom_setter(attribute):
    extended_attributes = attribute.extended_attributes
    return (not attribute.is_read_only and 'Custom' in extended_attributes
            and extended_attributes['Custom'] in [None, 'Setter'])


################################################################################
# Constructors
################################################################################

idl_types.IdlType.constructor_type_name = property(
    lambda self: strip_suffix(self.base_type, 'Constructor'))


def is_constructor_attribute(attribute):
    return attribute.idl_type.name.endswith('Constructor')


def is_named_constructor_attribute(attribute):
    return attribute.idl_type.name.endswith('ConstructorConstructor')