Skip to content

Truly independent web browser

LibWeb: Create interface instances in the constructor's realm

Shannon Booth1993c7f255f0
diff --git a/Meta/Generators/libweb_bindings/constructors.py b/Meta/Generators/libweb_bindings/constructors.py
index b9441503ae6..713a3576c39 100644
--- a/Meta/Generators/libweb_bindings/constructors.py
+++ b/Meta/Generators/libweb_bindings/constructors.py
@@ -112,21 +112,14 @@ def write_constructor_steps(
// 3.2. Let prototype be ? Get(newTarget, "prototype").
auto prototype = TRY(new_target.get(vm.names.prototype));
- JS::Realm* target_realm = nullptr;
-
// 3.3. If Type(prototype) is not Object, then:
if (!prototype.is_object()) {{
// 1. Let targetRealm be ? GetFunctionRealm(newTarget).
- target_realm = TRY(JS::get_function_realm(vm, new_target));
+ auto* target_realm = TRY(JS::get_function_realm(vm, new_target));
VERIFY(target_realm);
// 2. Set prototype to the interface prototype object for interface in targetRealm.
prototype = &Bindings::ensure_web_prototype<{interface.prototype_class}>(*target_realm, interface_name);
- }} else {{
- // This path does not need targetRealm, but the shared helper takes a
- // realm for default-prototype recovery. Use the explicit prototype's
- // realm without observing newTarget again.
- target_realm = &prototype.as_object().shape().realm();
}}
"""
@@ -172,8 +165,8 @@ def write_constructor_steps(
f"[&]<typename StaticImplementation>() -> decltype(StaticImplementation::construct_impl(realm{arguments_with_leading_comma})) {{ return StaticImplementation::construct_impl(realm{arguments_with_leading_comma}); }}",
f"[&]<typename StaticImplementation>() -> decltype(StaticImplementation::construct_impl({arguments_with_vm})) {{ return StaticImplementation::construct_impl({arguments_with_vm}); }}",
f"[&]<typename StaticImplementation>() -> decltype(StaticImplementation::construct_impl({arguments})) {{ return StaticImplementation::construct_impl({arguments}); }}",
- f"[&]<typename StaticImplementation>() -> decltype(StaticImplementation::create_for_constructor(target_realm->global_object(){arguments_with_leading_comma})) {{ return StaticImplementation::create_for_constructor(target_realm->global_object(){arguments_with_leading_comma}); }}",
- f"[&]<typename StaticImplementation>() -> decltype(StaticImplementation::create_for_constructor(*target_realm{arguments_with_leading_comma})) {{ return StaticImplementation::create_for_constructor(*target_realm{arguments_with_leading_comma}); }}",
+ f"[&]<typename StaticImplementation>() -> decltype(StaticImplementation::create_for_constructor(realm.global_object(){arguments_with_leading_comma})) {{ return StaticImplementation::create_for_constructor(realm.global_object(){arguments_with_leading_comma}); }}",
+ f"[&]<typename StaticImplementation>() -> decltype(StaticImplementation::create_for_constructor(realm{arguments_with_leading_comma})) {{ return StaticImplementation::create_for_constructor(realm{arguments_with_leading_comma}); }}",
f"[&]<typename StaticImplementation>() -> decltype(StaticImplementation::create_for_constructor({arguments_with_vm})) {{ return StaticImplementation::create_for_constructor({arguments_with_vm}); }}",
f"[&]<typename StaticImplementation>() -> decltype(StaticImplementation::create_for_constructor({arguments})) {{ return StaticImplementation::create_for_constructor({arguments}); }}",
]
@@ -220,7 +213,7 @@ def write_constructor_steps(
f""" auto wrapper = wrap(host_defined_wrapper_world(realm), realm, impl);
// 7. Set instance.[[Prototype]] to prototype.
- TRY(set_prototype_from_new_target<{interface.prototype_class}>(*target_realm, prototype, interface_name, *wrapper));
+ TRY(set_prototype_from_new_target<{interface.prototype_class}>(realm, prototype, interface_name, *wrapper));
// FIXME: Steps 8...11. of the "internally create a new object implementing the interface {interface.name}" algorithm
// (https://webidl.spec.whatwg.org/#js-platform-objects) are currently not handled, or are handled within {fully_qualified_name_for_interface(interface)}::construct_impl().
diff --git a/Tests/LibWeb/Text/expected/DOM/Range-constructor-new-target-realm.txt b/Tests/LibWeb/Text/expected/DOM/Range-constructor-new-target-realm.txt
new file mode 100644
index 00000000000..a5bcd1d544b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/DOM/Range-constructor-new-target-realm.txt
@@ -0,0 +1,4 @@
+same-process cross-origin frame: true
+same-origin window prototype: prototype from NewTarget: true, container is caller's document: true
+cross-origin window prototype: prototype from NewTarget: true, container is caller's document: true
+foreign NewTarget without prototype: prototype from NewTarget's realm: true, container is caller's document: true
diff --git a/Tests/LibWeb/Text/input/DOM/Range-constructor-new-target-realm.html b/Tests/LibWeb/Text/input/DOM/Range-constructor-new-target-realm.html
new file mode 100644
index 00000000000..d7cfcf965dd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/DOM/Range-constructor-new-target-realm.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const sameOrigin = document.createElement("iframe");
+ document.body.append(sameOrigin);
+ const crossOrigin = document.createElement("iframe");
+ crossOrigin.src = "data:text/html,<!DOCTYPE html>";
+ await new Promise(resolve => {
+ crossOrigin.onload = resolve;
+ document.body.append(crossOrigin);
+ });
+ println(`same-process cross-origin frame: ${remoteFrameCount() === 0}`);
+
+ const commonAncestorContainer = Object.getOwnPropertyDescriptor(Range.prototype, "commonAncestorContainer").get;
+ for (const [label, prototype] of [["same-origin", sameOrigin.contentWindow], ["cross-origin", crossOrigin.contentWindow]]) {
+ function NewTarget() {}
+ NewTarget.prototype = prototype;
+ const range = Reflect.construct(Range, [], NewTarget);
+ println(`${label} window prototype: prototype from NewTarget: ${Object.getPrototypeOf(range) === prototype}, container is caller's document: ${commonAncestorContainer.call(range) === document}`);
+ }
+
+ const foreignNewTarget = new sameOrigin.contentWindow.Function();
+ foreignNewTarget.prototype = 1;
+ const range = Reflect.construct(Range, [], foreignNewTarget);
+ println(`foreign NewTarget without prototype: prototype from NewTarget's realm: ${Object.getPrototypeOf(range) === sameOrigin.contentWindow.Range.prototype}, container is caller's document: ${range.commonAncestorContainer === document}`);
+ done();
+ });
+</script>