summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/bindings/templates/attributes.cc.tmpl
blob: d616eaab473c4fba708da803146d631bfc9fe305 (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
{% from 'utilities.cc.tmpl' import declare_enum_validation_variable, v8_value_to_local_cpp_value %}
{% from 'methods.cc.tmpl' import runtime_timer_scope, runtime_timer_scope_disabled_by_default %}

{##############################################################################}
{% macro attribute_getter(attribute, world_suffix) %}
static void {{attribute.camel_case_name}}AttributeGetter{{world_suffix}}(
{%- if attribute.is_data_type_property %}
const v8::PropertyCallbackInfo<v8::Value>& info
{%- else %}
const v8::FunctionCallbackInfo<v8::Value>& info
{%- endif %}) {
  {% filter format_remove_duplicates(['ExceptionState exception_state']) %}
  {% set define_exception_state -%}
  ExceptionState exception_state(info.GetIsolate(), ExceptionState::kGetterContext, "{{interface_name}}", "{{attribute.name}}");
  {%- endset %}

  {% if attribute.is_lenient_this %}
  // [LenientThis]
  // Make sure that info.Holder() really points to an instance if [LenientThis].
  if (!{{v8_class}}::HasInstance(info.Holder(), info.GetIsolate()))
    return; // Return silently because of [LenientThis].
  {% elif attribute.has_promise_type %}
  // This attribute returns a Promise.
  // Per https://heycam.github.io/webidl/#dfn-attribute-getter, all exceptions
  // must be turned into a Promise rejection.
  {{define_exception_state}}
  ExceptionToRejectPromiseScope reject_promise_scope(info, exception_state);

  // Returning a Promise type requires us to disable some of V8's type checks,
  // so we have to manually check that info.Holder() really points to an
  // instance of the type.
  if (!{{v8_class}}::HasInstance(info.Holder(), info.GetIsolate())) {
    exception_state.ThrowTypeError("Illegal invocation");
    return;
  }
  {% endif %}

  {% if not attribute.is_static or attribute.is_save_same_object %}
  v8::Local<v8::Object> holder = info.Holder();
  {% endif %}

  {% if attribute.is_save_same_object %}
  // [SaveSameObject]
  static const V8PrivateProperty::SymbolKey kSaveSameObjectKey;
  auto private_same_object =
      V8PrivateProperty::GetSymbol(info.GetIsolate(), kSaveSameObjectKey);
  {
    v8::Local<v8::Value> v8_value;
    if (private_same_object.GetOrUndefined(holder).ToLocal(&v8_value) && !v8_value->IsUndefined()) {
      V8SetReturnValue(info, v8_value);
      return;
    }
  }
  {% endif %}

  {% if not attribute.is_static %}
  {% set local_dom_window_only = interface_name == 'Window' and not attribute.has_cross_origin_getter %}
  {% if local_dom_window_only %}
  {% if attribute.is_check_security_for_receiver %}
  {{cpp_class}}* unchecked_impl = {{v8_class}}::ToImpl(holder);
  {% else %}
  // Same-origin attribute getters are never exposed via the cross-origin
  // interceptors. Since same-origin access requires a LocalDOMWindow, it is
  // safe to downcast here.
  LocalDOMWindow* impl = To<LocalDOMWindow>({{v8_class}}::ToImpl(holder));
  {% endif %}{# attribute.is_check_security_for_receiver #}
  {% else %}
  {{cpp_class}}* impl = {{v8_class}}::ToImpl(holder);
  {% endif %}{# local_dom_window_only #}
  {% endif %}{# not attribute.is_static #}

  {% if attribute.cached_attribute_validation_method %}
  // [CachedAttribute]
  {% if cpp_class == 'History' and attribute.camel_case_name == 'State' %}
  V8PrivateProperty::Symbol property_symbol =
      V8PrivateProperty::GetHistoryStateSymbol(info.GetIsolate());
  {% else %}
  {% if not attribute.private_property_is_shared_between_getter_and_setter %}
  static const V8PrivateProperty::SymbolKey kPrivateProperty{{attribute.camel_case_name}};

  {% endif %}
  V8PrivateProperty::Symbol property_symbol =
      V8PrivateProperty::GetSymbol(info.GetIsolate(),
          kPrivateProperty{{attribute.camel_case_name}});
  {% endif %}
  if (!static_cast<const {{cpp_class}}*>(impl)->{{attribute.cached_attribute_validation_method}}()) {
    v8::Local<v8::Value> v8_value;
    if (property_symbol.GetOrUndefined(holder).ToLocal(&v8_value) && !v8_value->IsUndefined()) {
      V8SetReturnValue(info, v8_value);
      return;
    }
  }
  {% endif %}

  {% if attribute.is_check_security_for_receiver and not attribute.is_data_type_property %}
  // Perform a security check for the receiver object.
  {{define_exception_state}}
  {% if local_dom_window_only %}
  if (!BindingSecurity::ShouldAllowAccessTo(CurrentDOMWindow(info.GetIsolate()), unchecked_impl, exception_state)) {
  {% else %}
  if (!BindingSecurity::ShouldAllowAccessTo(CurrentDOMWindow(info.GetIsolate()), impl, exception_state)) {
  {% endif %}{# local_dom_window_only #}
    V8SetReturnValueNull(info);
    return;
  }
  {% if local_dom_window_only %}
  LocalDOMWindow* impl = To<LocalDOMWindow>(unchecked_impl);
  {% endif %}{# local_dom_window_only #}
  {% endif %}

  {% if attribute.is_check_security_for_return_value %}
  // Perform a security check for the returned object.
  {{define_exception_state}}
  if (!BindingSecurity::ShouldAllowAccessTo(CurrentDOMWindow(info.GetIsolate()), {{attribute.cpp_value}}, BindingSecurity::ErrorReportOption::kDoNotReport)) {
    UseCounter::Count(CurrentExecutionContext(info.GetIsolate()),
                      WebFeature::kCrossOrigin{{interface_name}}{{attribute.camel_case_name}});
    V8SetReturnValueNull(info);
    return;
  }
  {% endif %}

  {% if attribute.is_call_with_execution_context %}
  {% if attribute.is_static %}
  ExecutionContext* execution_context = ExecutionContext::ForCurrentRealm(info);
  {% else %}
  ExecutionContext* execution_context = ExecutionContext::ForRelevantRealm(info);
  {% endif %}
  {% endif %}

  {% if attribute.is_call_with_script_state %}
  {% if attribute.is_static %}
  ScriptState* script_state = ScriptState::ForCurrentRealm(info);
  {% else %}
  ScriptState* script_state = ScriptState::ForRelevantRealm(info);
  {% endif %}
  {% endif %}

  {% if attribute.is_getter_raises_exception %}
  {{define_exception_state}}
  {% endif %}
  {% if attribute.is_explicit_nullable %}
  bool is_null = false;
  {% endif %}

  {% if attribute.cpp_value_original %}
  {{attribute.cpp_type}} {{attribute.cpp_value}}({{attribute.cpp_value_original}});
  {% endif %}

  {% if attribute.use_output_parameter_for_result %}
  {{attribute.cpp_type}} result;
  {{attribute.cpp_value}};
  {% endif %}

  {% if attribute.is_getter_raises_exception %}
  if (UNLIKELY(exception_state.HadException()))
    return;
  {% endif %}

  {% if attribute.reflect_only %}
  {{release_only_check(attribute.reflect_only, attribute.reflect_missing,
                       attribute.reflect_invalid, attribute.reflect_empty,
                       attribute.cpp_value)
      | trim | indent(2)}}
  {% endif %}

  {% if attribute.cached_attribute_validation_method %}
  // [CachedAttribute]
  v8::Local<v8::Value> v8_value({{attribute.cpp_value_to_v8_value}});
  property_symbol.Set(holder, v8_value);
  {% endif %}

  {% if attribute.is_explicit_nullable %}
  if (is_null) {
    V8SetReturnValueNull(info);
    return;
  }
  {% endif %}

  {% if attribute.is_keep_alive_for_gc %}
  // Keep the wrapper object for the return value alive as long as |this|
  // object is alive in order to save creation time of the wrapper object.
  if ({{attribute.cpp_value}} && DOMDataStore::SetReturnValue{{world_suffix}}(info.GetReturnValue(), {{attribute.cpp_value_to_script_wrappable}}))
    return;
  v8::Local<v8::Value> v8_value(ToV8({{attribute.cpp_value_to_script_wrappable}}, holder, info.GetIsolate()));
  static const V8PrivateProperty::SymbolKey kKeepAliveKey;
  V8PrivateProperty::GetSymbol(info.GetIsolate(), kKeepAliveKey)
      .Set(holder, v8_value);
  {% endif %}

  {% if world_suffix %}
  {{attribute.v8_set_return_value_for_main_world}};
  {% else %}
  {{attribute.v8_set_return_value}};
  {% endif %}

  {% if attribute.is_save_same_object %}
  // [SaveSameObject]
  private_same_object.Set(holder, info.GetReturnValue().Get());
  {% endif %}
  {% endfilter %}{# format_remove_duplicates #}
}
{% endmacro %}


{######################################}
{% macro release_only_check(reflect_only_values, reflect_missing,
                            reflect_invalid, reflect_empty, cpp_value) %}
{# Attribute is limited to only known values: check that the attribute value is
   one of those. If not, set it to the empty string.
   http://www.whatwg.org/specs/web-apps/current-work/#limited-to-only-known-values #}
{% if reflect_only_values %}
AtomicString atomic_{{cpp_value}}({{cpp_value}}.LowerASCII());
{% endif %}
{% if reflect_empty %}
if ({{cpp_value}}.IsNull()) {
{% if reflect_missing %}
  {{cpp_value}} = {{reflect_missing}};
{% else %}
  ;
{% endif %}
} else if ({{cpp_value}}.IsEmpty()) {
  {{cpp_value}} = {{reflect_empty}};
{% else %}
if ({{cpp_value}}.IsEmpty()) {
{# FIXME: should use [ReflectEmpty] instead; need to change IDL files #}
{% if reflect_missing %}
  {{cpp_value}} = {{reflect_missing}};
{% else %}
  ;
{% endif %}
{% endif %}
{% for value in reflect_only_values %}
} else if (atomic_{{cpp_value}} == {{value}}) {
  {{cpp_value}} = {{value}};
{% endfor %}
} else {
  {{cpp_value}} = {{reflect_invalid}};
}
{% endmacro %}


{##############################################################################}
{% macro attribute_getter_callback(attribute, world_suffix) %}
void {{v8_class_or_partial}}::{{attribute.camel_case_name}}AttributeGetterCallback{{world_suffix}}(
{%- if attribute.is_data_type_property %}
v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value>& info
{%- else %}
const v8::FunctionCallbackInfo<v8::Value>& info
{%- endif %}) {
  {% if attribute.runtime_call_stats.extended_attribute_defined %}
  {{ runtime_timer_scope(attribute.runtime_call_stats.getter_counter) | trim | indent(2) }}
  {% else %}
  {{ runtime_timer_scope_disabled_by_default(attribute.runtime_call_stats.getter_counter) }}
  {% endif %}

  {% if attribute.deprecate_as %}
  Deprecation::CountDeprecation(CurrentExecutionContext(info.GetIsolate()), WebFeature::k{{attribute.deprecate_as}});
  {% endif %}

  {% if attribute.measure_as %}
  ExecutionContext* execution_context_for_measurement = CurrentExecutionContext(info.GetIsolate());
  UseCounter::Count(execution_context_for_measurement, WebFeature::k{{attribute.measure_as('AttributeGetter')}});
  {% if attribute.high_entropy %}
  Dactyloscoper::Record(execution_context_for_measurement, WebFeature::k{{attribute.measure_as('AttributeGetter')}});
  {% endif %}
  {% endif %}

  {% if world_suffix in attribute.activity_logging_world_list_for_getter %}
  {% if attribute.is_static %}
  ScriptState* script_state = ScriptState::ForCurrentRealm(info);
  {% else %}
  ScriptState* script_state = ScriptState::ForRelevantRealm(info);
  {% endif %}
  V8PerContextData* context_data = script_state->PerContextData();
  if (
  {%- if attribute.activity_logging_world_check -%}
      script_state->World().IsIsolatedWorld() && {# one space at the end #}
  {%- endif -%}
      context_data && context_data->ActivityLogger()) {
    context_data->ActivityLogger()->LogGetter("{{interface_name}}.{{attribute.name}}");
  }
  {% endif %}

  {% if attribute.has_custom_getter %}
  {{v8_class}}::{{attribute.camel_case_name}}AttributeGetterCustom(info);
  {% else %}
  {{internal_namespace}}::{{attribute.camel_case_name}}AttributeGetter{{world_suffix}}(info);
  {% endif %}
}
{% endmacro %}


{##############################################################################}
{% macro constructor_getter_callback(attribute, world_suffix) %}
void {{v8_class_or_partial}}::{{attribute.camel_case_name}}ConstructorGetterCallback{{world_suffix}}(
    v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& info) {
  {{ runtime_timer_scope_disabled_by_default(attribute.runtime_call_stats.constructor_getter_callback_counter) }}
  {% if attribute.deprecate_as %}
  Deprecation::CountDeprecation(CurrentExecutionContext(info.GetIsolate()), WebFeature::k{{attribute.deprecate_as}});
  {% endif %}

  {% if attribute.measure_as %}
  ExecutionContext* execution_context_for_measurement = CurrentExecutionContext(info.GetIsolate());
  UseCounter::Count(execution_context_for_measurement, WebFeature::k{{attribute.measure_as('ConstructorGetter')}});
  {% if attribute.high_entropy %}
  Dactyloscoper::Record(execution_context_for_measurement, WebFeature::k{{attribute.measure_as('ConstructorGetter')}});
  {% endif %}
  {% endif %}

  {% if attribute.is_named_constructor %}
  V8{{attribute.constructor_type}}::NamedConstructorAttributeGetter(property, info);
  {% else %}
  V8ConstructorAttributeGetter(property, info, V8{{attribute.constructor_type}}::GetWrapperTypeInfo());
  {% endif %}
}
{% endmacro %}


{##############################################################################}
{% macro attribute_setter(attribute, world_suffix) %}
{% if not attribute.use_common_reflection_setter %}
static void {{attribute.camel_case_name}}AttributeSetter{{world_suffix}}(
    {% if attribute.has_cross_origin_setter %}
    v8::Local<v8::Value> v8_value, const V8CrossOriginCallbackInfo& info
    {% elif attribute.is_data_type_property %}
    v8::Local<v8::Value> v8_value, const v8::PropertyCallbackInfo<void>& info
    {% else %}
    v8::Local<v8::Value> v8_value, const v8::FunctionCallbackInfo<v8::Value>& info
    {%- endif %}) {
  {% filter format_remove_duplicates(['ExceptionState exception_state']) %}
  v8::Isolate* isolate = info.GetIsolate();
  ALLOW_UNUSED_LOCAL(isolate);

  v8::Local<v8::Object> holder = info.Holder();
  ALLOW_UNUSED_LOCAL(holder);

  {% set define_exception_state -%}
  ExceptionState exception_state(isolate, ExceptionState::kSetterContext, "{{interface_name}}", "{{attribute.name}}");
  {%- endset %}

  {% if attribute.is_lenient_this %}
  // [LenientThis]
  // Make sure that info.Holder() really points to an instance if [LenientThis].
  if (!{{v8_class}}::HasInstance(holder, isolate))
    return; // Return silently because of [LenientThis].
  {% endif %}

  {% if not attribute.is_static and not attribute.is_replaceable and not attribute.is_put_forwards %}
  {% set local_dom_window_only = interface_name == 'Window' and not attribute.has_cross_origin_setter %}
  {% if local_dom_window_only %}
  {% if attribute.is_check_security_for_receiver %}
  {{cpp_class}}* unchecked_impl = {{v8_class}}::ToImpl(holder);
  {% else %}
  // Same-origin attributes setters are never exposed via the cross-origin
  // interceptors. Since same-origin access requires a LocalDOMWindow, it is
  // safe to downcast here.
  LocalDOMWindow* impl = To<LocalDOMWindow>({{v8_class}}::ToImpl(holder));
  {% endif %}{# attribute.is_check_security_for_receiver #}
  {% else %}
  {{cpp_class}}* impl = {{v8_class}}::ToImpl(holder);
  {% endif %}{# local_dom_window_only #}
  {% endif %}

  {% if attribute.is_check_security_for_receiver and not attribute.is_data_type_property %}
  // Perform a security check for the receiver object.
  {{define_exception_state}}
  {% if local_dom_window_only %}
  if (!BindingSecurity::ShouldAllowAccessTo(CurrentDOMWindow(isolate), unchecked_impl, exception_state)) {
  {% else %}
  if (!BindingSecurity::ShouldAllowAccessTo(CurrentDOMWindow(isolate), impl, exception_state)) {
  {% endif %}{# local_dom_window_only #}
    V8SetReturnValue(info, v8_value);
    return;
  }
  {% if local_dom_window_only %}
  LocalDOMWindow* impl = To<LocalDOMWindow>(unchecked_impl);
  {% endif %}{# local_dom_window_only #}
  {% endif %}

  {% if attribute.is_check_security_for_return_value %}
#error Attribute setter with the security check for the return value is not supported.  Since the return value is the given value to be set, it\'s meaningless to perform the security check for the return value.
  {% endif %}

  {% if attribute.is_put_forwards %}
  // [PutForwards] => {{attribute.name}}.{{attribute.target_attribute_name}}
  {{define_exception_state}}
  v8::Local<v8::Value> target;
  if (!holder->Get(isolate->GetCurrentContext(), V8AtomicString(isolate, "{{attribute.name}}"))
      .ToLocal(&target)) {
    return;
  }
  if (!target->IsObject()) {
    exception_state.ThrowTypeError("The attribute value is not an object");
    return;
  }
  bool result;
  if (!target.As<v8::Object>()->Set(
          isolate->GetCurrentContext(),
          V8AtomicString(isolate, "{{attribute.target_attribute_name}}"),
          v8_value).To(&result)) {
    return;
  }
  if (!result)
    return;
  {% else %}{# attribute.is_put_forwards #}
  {% if attribute.is_custom_element_callbacks or attribute.is_reflect %}
  V0CustomElementProcessingStack::CallbackDeliveryScope delivery_scope;
  {% endif %}

  {% if attribute.has_setter_exception_state %}
  {{define_exception_state}}
  {% endif %}
  {% if attribute.is_ce_reactions %}
  CEReactionsScope ce_reactions_scope;
  {% endif %}

  {% if attribute.is_explicit_nullable %}
  bool is_null = IsUndefinedOrNull(v8_value);
  {% endif %}

  // Prepare the value to be set.
  {% if attribute.idl_type != 'EventHandler' %}
  {{v8_value_to_local_cpp_value(attribute) | trim | indent(2)}}
  {% endif %}

  {% if attribute.has_type_checking_interface %}
  // Type check per: http://heycam.github.io/webidl/#es-interface
  if (!cpp_value{% if attribute.is_nullable %} && !IsUndefinedOrNull(v8_value){% endif %}) {
    exception_state.ThrowTypeError("The provided value is not of type '{{attribute.idl_type}}'.");
    return;
  }
  {% endif %}

  {% if attribute.enum_values %}
  // Type check per: http://heycam.github.io/webidl/#dfn-attribute-setter
  // Returns undefined without setting the value if the value is invalid.
  DummyExceptionStateForTesting dummy_exception_state;
  {
    {{declare_enum_validation_variable(attribute.enum_values) | trim | indent(2)}}
    if (!IsValidEnum(cpp_value, kValidValues, base::size(kValidValues),
                     "{{attribute.enum_type}}", dummy_exception_state)) {
      ExecutionContext::ForCurrentRealm(info)->AddConsoleMessage(
          ConsoleMessage::Create(mojom::ConsoleMessageSource::kJavaScript,
                                 mojom::ConsoleMessageLevel::kWarning,
                                 dummy_exception_state.Message()));
      return;
    }
  }
  {% endif %}

  {% if attribute.is_call_with_execution_context or
        attribute.is_setter_call_with_execution_context %}
  {% if attribute.is_static %}
  ExecutionContext* execution_context = ExecutionContext::ForCurrentRealm(info);
  {% else %}
  ExecutionContext* execution_context = ExecutionContext::ForRelevantRealm(info);
  {% endif %}
  {% endif %}

  {% if attribute.is_call_with_script_state or
  	attribute.is_setter_call_with_script_state %}
  {% if attribute.is_static %}
  ScriptState* script_state = ScriptState::ForCurrentRealm(info);
  {% else %}
  ScriptState* script_state = ScriptState::ForRelevantRealm(info);
  {% endif %}
  {% endif %}

  {% if attribute.is_replaceable %}
  v8::Local<v8::String> property_name = V8AtomicString(isolate, "{{attribute.name}}");
  {% endif %}
  {{attribute.cpp_setter | indent(2)}};

  {% if attribute.cached_attribute_validation_method %}
  // [CachedAttribute]
  // Invalidate the cached value.
  V8PrivateProperty::GetSymbol(
      isolate,
      kPrivateProperty{{attribute.camel_case_name}})
      .DeleteProperty(holder);
  {% endif %}
  {% endif %}{# attribute.is_put_forwards #}
  {% endfilter %}{# format_remove_duplicates #}
}
{% endif %}
{% endmacro %}


{##############################################################################}
{% macro attribute_setter_callback(attribute, world_suffix) %}
void {{v8_class_or_partial}}::{{attribute.camel_case_name}}AttributeSetterCallback{{world_suffix}}(
    {% if attribute.is_data_type_property %}
    v8::Local<v8::Name>, v8::Local<v8::Value> v8_value, const v8::PropertyCallbackInfo<void>& info
    {% else %}
    const v8::FunctionCallbackInfo<v8::Value>& info
    {%- endif %}) {
  {% if attribute.is_lenient_setter %}
  // Setter for {{attribute.name}} is no-op because [LenientSetter] is specified.
  {% else %}
  {% if attribute.runtime_call_stats.extended_attribute_defined %}
  {{ runtime_timer_scope(attribute.runtime_call_stats.setter_counter) | trim | indent(2) }}
  {% else %}
  {{ runtime_timer_scope_disabled_by_default(attribute.runtime_call_stats.setter_counter) }}
  {% endif %}

  {% if not attribute.is_data_type_property and not attribute.use_common_reflection_setter %}
  v8::Local<v8::Value> v8_value = info[0];
  {% endif %}

  {% if attribute.deprecate_as %}
  Deprecation::CountDeprecation(CurrentExecutionContext(info.GetIsolate()), WebFeature::k{{attribute.deprecate_as}});
  {% endif %}

  {% if attribute.measure_as %}
  UseCounter::Count(CurrentExecutionContext(info.GetIsolate()), WebFeature::k{{attribute.measure_as('AttributeSetter')}});
  {% endif %}

  {% if world_suffix in attribute.activity_logging_world_list_for_setter %}
  {% if attribute.is_static %}
  ScriptState* script_state = ScriptState::ForCurrentRealm(info);
  {% else %}
  ScriptState* script_state = ScriptState::ForRelevantRealm(info);
  {% endif %}
  V8PerContextData* context_data = script_state->PerContextData();
  if (
  {%- if attribute.activity_logging_world_check -%}
      script_state->World().IsIsolatedWorld() && {# one space at the end #}
  {%- endif -%}
      context_data && context_data->ActivityLogger()) {
    context_data->ActivityLogger()->LogSetter("{{interface_name}}.{{attribute.name}}", v8_value);
  }
  {% endif %}

  {% if attribute.has_custom_setter %}
  {{v8_class}}::{{attribute.camel_case_name}}AttributeSetterCustom(v8_value, info);
  {% elif attribute.has_cross_origin_setter %}
  {{internal_namespace}}::{{attribute.camel_case_name}}AttributeSetter(
      v8_value, V8CrossOriginCallbackInfo(info));
  {% elif attribute.use_common_reflection_setter %}
  {{attribute.cpp_setter}};
  {% else %}
  {{internal_namespace}}::{{attribute.camel_case_name}}AttributeSetter{{world_suffix}}(v8_value, info);
  {% endif %}
  {% endif %}{# attribute.is_lenient_setter #}
}
{% endmacro %}


{##############################################################################}
{% macro build_attribute_or_accessor_configuration(attribute, config_type) %}
{% from 'utilities.cc.tmpl' import property_location %}
{% if attribute.constructor_type %}
  {% set getter_callback = '%s::%sConstructorGetterCallback' % (v8_class_or_partial, attribute.camel_case_name) %}
  {% set setter_callback = 'nullptr' %}
{% else %}{# regular attributes #}
  {% set getter_callback = '%s::%sAttributeGetterCallback' % (v8_class_or_partial, attribute.camel_case_name) %}
  {% set setter_callback = '%s::%sAttributeSetterCallback' % (v8_class_or_partial, attribute.camel_case_name)
      if attribute.has_setter else 'nullptr' %}
{% endif %}

{% set property_attribute = 'static_cast<v8::PropertyAttribute>(%s)' %
                            ' | '.join(attribute.property_attributes) %}
{% set cached_property_key = 'V8PrivateProperty::k' +
    (attribute.cached_accessor_name if attribute.is_cached_accessor
     else 'NoCachedAccessor') %}
{% set holder_check = 'V8DOMConfiguration::kDoNotCheckHolder'
    if attribute.is_lenient_this or attribute.has_promise_type
    else 'V8DOMConfiguration::kCheckHolder' %}
{% set getter_side_effect_type = 'V8DOMConfiguration::kHasNoSideEffect'
    if attribute.getter_has_no_side_effect else 'V8DOMConfiguration::kHasSideEffect' %}
{% set getter_behavior = 'V8DOMConfiguration::kAlwaysCallGetter'
    if not attribute.is_lazy_data_attribute else 'V8DOMConfiguration::kReplaceWithDataProperty' %}
{% if attribute.is_per_world_bindings %}
  {% set getter_callback_for_main_world = '%sForMainWorld' % getter_callback %}
  {% set setter_callback_for_main_world =
      '%sForMainWorld' % setter_callback if attribute.has_setter else 'nullptr' %}
{% endif %}
{% set config_pre = {
    'main' : [
        '"%s"' % attribute.name,
        getter_callback_for_main_world,
        setter_callback_for_main_world,
    ],
    'non_main' : [
        '"%s"' % attribute.name,
        getter_callback,
        setter_callback,
    ],
} %}
{% set accessor_only_fields = [] if config_type == 'attribute' else [cached_property_key] %}
{% set config_post = [
    property_attribute,
    property_location(attribute),
    holder_check,
    getter_side_effect_type,
    getter_behavior,
] %}

{% if attribute.is_per_world_bindings %}
  {% set main_config_list = config_pre["main"] + accessor_only_fields +
      config_post + ['V8DOMConfiguration::kMainWorld'] %}
  {% set non_main_config_list = config_pre["non_main"] + accessor_only_fields +
      config_post + ['V8DOMConfiguration::kNonMainWorlds'] %}
  {# Emit for main world then non-main.#}
{ {{main_config_list | join(', ')}} },
{ {{non_main_config_list | join(', ')}} }
{%- else -%}
  {% set all_worlds_config_list = config_pre["non_main"] + accessor_only_fields +
      config_post + ['V8DOMConfiguration::kAllWorlds'] %}
  {# Emit only for all worlds #}
{ {{all_worlds_config_list | join(', ')}} }
{%- endif -%}
{% endmacro %}


{##############################################################################}
{% macro attribute_configuration(attribute) %}
{{build_attribute_or_accessor_configuration(attribute, 'attribute')}}
{% endmacro %}


{##############################################################################}
{% macro accessor_configuration(attribute) %}
{{build_attribute_or_accessor_configuration(attribute, 'accessor')}}
{% endmacro %}


{##############################################################################}
{# This macro installs |attributes_to_install| conditionally based on Exposed, SecureContext, and RuntimeEnabled checks. #}
{% macro install_conditional_attributes(attributes_to_install) %}
{% for exposed_test, exposed_attribute_list in attributes_to_install | groupby('exposed_test') %}
{% filter exposed(exposed_test) %}
{% for secure_context_test, secure_context_attribute_list in exposed_attribute_list | groupby('secure_context_test') %}
{% filter secure_context(secure_context_test) %}
{% for feature_name, attribute_list in secure_context_attribute_list | groupby('runtime_enabled_feature_name') %}
{% filter runtime_enabled(feature_name) %}
{{install_attributes(attribute_list | sort, 'instance_object', 'prototype_object', 'interface_object')}}
{% endfilter %}{# runtime_enabled #}
{% endfor %}{# secure_context_attribute_list grouped by runtime_enabled #}
{% endfilter %}{# secure_context #}
{% endfor %}{# exposed_attribute_list grouped by secure_context_text #}
{% endfilter %}{# exposed #}
{% endfor %}{# attributes grouped by exposed_test #}
{% endmacro %}


{##############################################################################}
{# This macro installs |attributes_to_install| unconditionally. #}
{% macro install_attributes(attributes_to_install, instance_variable, prototype_variable, interface_variable) %}
static constexpr V8DOMConfiguration::AccessorConfiguration
kAccessorConfigurations[] = {
    {% for attribute in attributes_to_install %}
    {{accessor_configuration(attribute) | trim | indent(4)}},
    {% endfor %}
};
V8DOMConfiguration::InstallAccessors(
    isolate, world, {{instance_variable}}, {{prototype_variable}}, {{interface_variable}},
    signature, kAccessorConfigurations,
    base::size(kAccessorConfigurations));
{% endmacro %}


{##############################################################################}
{# This macro installs |attributes_to_install| conditionally based on Exposed, SecureContext, and RuntimeEnabled checks. #}
{% macro install_conditional_interface_objects(attributes_to_install) %}
{% for exposed_test, exposed_attribute_list in attributes_to_install | groupby('exposed_test') %}
{% filter exposed(exposed_test) %}
{% for secure_context_test, secure_context_attribute_list in exposed_attribute_list | groupby('secure_context_test') %}
{% filter secure_context(secure_context_test) %}
{% for feature_name, attribute_list in secure_context_attribute_list | groupby('runtime_enabled_feature_name') %}
{% filter runtime_enabled(feature_name) %}
{{install_interface_objects(attribute_list | sort, 'instance_object', 'prototype_object')}}
{% endfilter %}{# runtime_enabled #}
{% endfor %}{# secure_context_attribute_list grouped by runtime_enabled #}
{% endfilter %}{# secure_context #}
{% endfor %}{# exposed_attribute_list grouped by secure_context_text #}
{% endfilter %}{# exposed #}
{% endfor %}{# attributes grouped by exposed_test #}
{% endmacro %}


{##############################################################################}
{# This macro installs |attributes_to_install| unconditionally. #}
{% macro install_interface_objects(attributes_to_install, instance_variable, prototype_variable) %}
static constexpr V8DOMConfiguration::AttributeConfiguration
kAttributeConfigurations[] = {
    {% for attribute in attributes_to_install %}
    {{attribute_configuration(attribute) | trim | indent(4)}},
    {% endfor %}
};
V8DOMConfiguration::InstallAttributes(
    isolate, world, {{instance_variable}}, {{prototype_variable}},
    kAttributeConfigurations, base::size(kAttributeConfigurations));
{% endmacro %}