Skip to content

Truly independent web browser

LibWeb: Create Rust wrappers with their interface's own prototype

Andreas Kling0a7e6339610f
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
index 6c87276e5fe..91d245daccc 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
@@ -284,12 +284,7 @@ pub(crate) fn wrap<T: Interface>(cx: &CallContext, object: Ref<T>) -> Gc<Object>
.relevant_realm()
.filter(|relevant_realm| wrapper_world_of(vm, *relevant_realm) == wrapper_world)
.unwrap_or(realm);
- let wrapper = create_wrapper(
- vm,
- wrapper_realm,
- interface_prototype_object(wrapper_realm, T::INTERFACE_NAME),
- object,
- );
+ let wrapper = create_wrapper(vm, wrapper_realm, object);
object.main_world_wrapper.set(Some(wrapper));
return wrapper.upcast();
}
@@ -300,7 +295,7 @@ pub(crate) fn wrap<T: Interface>(cx: &CallContext, object: Ref<T>) -> Gc<Object>
// SAFETY: LibWeb returns a live wrapper.
return unsafe { Gc::from_non_null(wrapper) };
}
- let wrapper = create_wrapper(vm, realm, interface_prototype_object(realm, T::INTERFACE_NAME), object);
+ let wrapper = create_wrapper(vm, realm, object);
register_wrapper_outside_main_world(realm, object, wrapper);
wrapper.upcast()
}
@@ -318,7 +313,8 @@ pub(crate) fn construct_wrapper<T: Interface>(
object.main_world_wrapper.get().is_none(),
"the constructor steps create no wrapper of the new object"
);
- let wrapper = create_wrapper(vm, realm, prototype, object);
+ let wrapper = create_wrapper(vm, realm, object);
+ wrapper.set_prototype(vm, Some(prototype));
if wrapper_world_of(vm, realm).is_main_world() {
object.main_world_wrapper.set(Some(wrapper));
} else {
@@ -334,7 +330,12 @@ fn register_wrapper_outside_main_world<T: Interface>(realm: Gc<Realm>, object: R
}
}
-fn create_wrapper<T: Interface>(vm: &Vm, realm: Gc<Realm>, prototype: Gc<Object>, object: Ref<T>) -> Gc<HostObject> {
+/// A new wrapper of the object, which always starts from the interface's own prototype. Shapes do not know the class of
+/// their objects, so a wrapper created directly with the prototype of another interface would share the shape of that
+/// interface's wrappers, and a direct getter validated on those would read a field of the other interface out of this
+/// wrapper's implementation.
+fn create_wrapper<T: Interface>(vm: &Vm, realm: Gc<Realm>, object: Ref<T>) -> Gc<HostObject> {
+ let prototype = interface_prototype_object(realm, T::INTERFACE_NAME);
// SAFETY: The wrapper class's hooks expect a wrappable exactly for an interface that inherits from an interface
// implemented in C++, whose objects own one, and the wrappable and the host data are live cells.
let wrapper = unsafe {
diff --git a/Tests/LibWeb/Text/expected/direct-getter-wrapper-constructed-with-other-interface-prototype.txt b/Tests/LibWeb/Text/expected/direct-getter-wrapper-constructed-with-other-interface-prototype.txt
new file mode 100644
index 00000000000..bc5f15952b9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/direct-getter-wrapper-constructed-with-other-interface-prototype.txt
@@ -0,0 +1,18 @@
+Constructor
+true
+true
+TypeError
+TypeError
+true
+Subclass
+true
+true
+TypeError
+TypeError
+true
+Other realm
+true
+true
+TypeError
+TypeError
+true
diff --git a/Tests/LibWeb/Text/input/direct-getter-wrapper-constructed-with-other-interface-prototype.html b/Tests/LibWeb/Text/input/direct-getter-wrapper-constructed-with-other-interface-prototype.html
new file mode 100644
index 00000000000..e8182e07117
--- /dev/null
+++ b/Tests/LibWeb/Text/input/direct-getter-wrapper-constructed-with-other-interface-prototype.html
@@ -0,0 +1,65 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ // Enough reads for the JIT to compile the function with the getter of the element's shape inlined.
+ const WARM_UP_READS = 10000;
+
+ function check(name, element, construct) {
+ println(name);
+
+ // NB: Each case needs a function of its own, whose cache only has seen the element.
+ const parentNodeOf = new Function("object", "return object.parentNode;");
+ const parent = element.parentNode;
+ for (let i = 0; i < WARM_UP_READS; ++i)
+ parentNodeOf(element);
+
+ const reader = construct();
+ println(Object.getPrototypeOf(reader) === Object.getPrototypeOf(element));
+
+ // Reading the target caches the wrapper as the main world wrapper of the reader's EventTarget.
+ const event = new Event("x");
+ EventTarget.prototype.dispatchEvent.call(reader, event);
+ println(event.target === reader);
+
+ for (let i = 0; i < 2; ++i) {
+ try {
+ parentNodeOf(reader);
+ println("FAIL: no exception");
+ } catch (e) {
+ println(e.name);
+ }
+ }
+ println(parentNodeOf(element) === parent);
+ }
+
+ function newTargetWithPrototype(prototype) {
+ function NewTarget() {}
+ NewTarget.prototype = prototype;
+ return NewTarget;
+ }
+
+ test(() => {
+ const element = document.createElement("div");
+ document.body.appendChild(element);
+
+ check("Constructor", element, () => {
+ return Reflect.construct(FileReader, [], newTargetWithPrototype(HTMLDivElement.prototype));
+ });
+
+ class Subclass extends FileReader {}
+ check("Subclass", element, () => {
+ return Reflect.construct(Subclass, [], newTargetWithPrototype(HTMLDivElement.prototype));
+ });
+
+ const iframe = document.createElement("iframe");
+ document.body.appendChild(iframe);
+ const otherWindow = iframe.contentWindow;
+ const otherElement = otherWindow.document.createElement("div");
+ otherWindow.document.body.appendChild(otherElement);
+
+ check("Other realm", otherElement, () => {
+ const newTarget = newTargetWithPrototype(otherWindow.HTMLDivElement.prototype);
+ return Reflect.construct(otherWindow.FileReader, [], newTarget);
+ });
+ });
+</script>