LibWeb: Add special handling for WindowProxy in [Replaceable] setters

Setters for Window object should consider WindowProxy wrapper by:
- Verifying `this_value` is `WindowProxy` (not `HTML::Window`)
- Defining properties on the underlying Window object instead of on
  the WindowProxy itself.
This commit is contained in:
Aliaksandr Kalenik 2024-02-23 20:26:32 +01:00 committed by Andreas Kling
parent 9b1b8828b4
commit 1528e9109c
Notes: sideshowbarker 2024-07-17 10:16:43 +09:00
3 changed files with 24 additions and 1 deletions

View File

@ -3186,7 +3186,21 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
}
)~~~");
} else if (attribute.extended_attributes.contains("Replaceable"sv)) {
attribute_generator.append(R"~~~(
if (interface.name.is_one_of("Window")) {
attribute_generator.append(R"~~~(
JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
{
auto this_value = vm.this_value();
if (!this_value.is_object() || !is<WindowProxy>(this_value.as_object()))
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "@namespaced_name@");
auto& window_proxy = static_cast<WindowProxy&>(this_value.as_object());
TRY(window_proxy.window()->internal_define_own_property("@attribute.name@", JS::PropertyDescriptor { .value = vm.argument(0), .writable = true }));
return JS::js_undefined();
}
)~~~");
} else {
attribute_generator.append(R"~~~(
JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
{
auto this_value = vm.this_value();
@ -3196,6 +3210,7 @@ JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@)
return JS::js_undefined();
}
)~~~");
}
} else if (auto put_forwards_identifier = attribute.extended_attributes.get("PutForwards"sv); put_forwards_identifier.has_value()) {
attribute_generator.set("put_forwards_identifier"sv, *put_forwards_identifier);

View File

@ -0,0 +1 @@
hello

View File

@ -0,0 +1,7 @@
<script src="include.js"></script>
<script>
test(() => {
window.event = "hello";
println(window.event);
});
</script>