From a14711a9d7e6edecd61846b883b7eea0290681f8 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Mon, 21 Apr 2025 20:56:55 +1200 Subject: [PATCH] LibWeb/Bindings: Use realm's globalObject as thisValue if nullish We were missing the step to use realm's global object if thisValue was nullish. This is very trivial to fix, as `impl_this` already handles everything as it should, allowing us to also remove the special casing for WindowProxy. --- .../BindingsGenerator/IDLGenerators.cpp | 36 ++----------------- .../global-object-implicit-this-value.any.txt | 5 ++- 2 files changed, 5 insertions(+), 36 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index b6b24f5bb56..55733767d77 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -4254,48 +4254,18 @@ MUST(impl->set_attribute("@attribute.reflect_name@"_fly_string, cpp_value)); } )~~~"); } else if (attribute.extended_attributes.contains("Replaceable"sv)) { - if (interface.name == "Window"sv) { - attribute_generator.append(R"~~~( + attribute_generator.append(R"~~~( JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@) { WebIDL::log_trace(vm, "@class_name@::@attribute.setter_callback@"); - auto this_value = vm.this_value(); if (vm.argument_count() < 1) return vm.throw_completion(JS::ErrorType::BadArgCountOne, "@namespaced_name@ setter"); - GC::Ptr window; - if (this_value.is_object()) { - if (is(this_value.as_object())) { - auto& window_proxy = static_cast(this_value.as_object()); - window = window_proxy.window(); - } else if (is(this_value.as_object())) { - window = &static_cast(this_value.as_object()); - } - } - if (window) { - TRY(window->internal_define_own_property("@attribute.name@"_fly_string, JS::PropertyDescriptor { .value = vm.argument(0), .writable = true })); - return JS::js_undefined(); - } - - return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "@namespaced_name@"); -} -)~~~"); - } else { - - attribute_generator.append(R"~~~( -JS_DEFINE_NATIVE_FUNCTION(@class_name@::@attribute.setter_callback@) -{ - WebIDL::log_trace(vm, "@class_name@::@attribute.setter_callback@"); - auto this_value = vm.this_value(); - if (!this_value.is_object() || !is<@fully_qualified_name@>(this_value.as_object())) - return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "@namespaced_name@"); - if (vm.argument_count() < 1) - return vm.throw_completion(JS::ErrorType::BadArgCountOne, "@namespaced_name@ setter"); - TRY(this_value.as_object().internal_define_own_property("@attribute.name@"_fly_string, JS::PropertyDescriptor { .value = vm.argument(0), .writable = true })); + auto* impl = TRY(impl_from(vm)); + TRY(impl->internal_define_own_property("@attribute.name@"_fly_string, JS::PropertyDescriptor { .value = vm.argument(0), .writable = true })); 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); VERIFY(!put_forwards_identifier->is_empty() && !is_ascii_digit(put_forwards_identifier->byte_at(0))); // Ensure `PropertyKey`s are not Numbers. diff --git a/Tests/LibWeb/Text/expected/wpt-import/webidl/ecmascript-binding/global-object-implicit-this-value.any.txt b/Tests/LibWeb/Text/expected/wpt-import/webidl/ecmascript-binding/global-object-implicit-this-value.any.txt index e37a7a9bd6d..7c20a9f3216 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/webidl/ecmascript-binding/global-object-implicit-this-value.any.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/webidl/ecmascript-binding/global-object-implicit-this-value.any.txt @@ -2,8 +2,7 @@ Harness status: OK Found 9 tests -8 Pass -1 Fail +9 Pass Pass Global object's getter throws when called on incompatible object Pass Global object's setter throws when called on incompatible object Pass Global object's operation throws when called on incompatible object @@ -11,5 +10,5 @@ Pass Global object's getter throws when called on incompatible object (document. Pass Global object's setter throws when called on incompatible object (document.all) Pass Global object's operation throws when called on incompatible object (document.all) Pass Global object's getter works when called on null / undefined -Fail Global object's setter works when called on null / undefined +Pass Global object's setter works when called on null / undefined Pass Global object's operation works when called on null / undefined \ No newline at end of file