Skip to content

Truly independent web browser

LibWeb: Let Rust constructors return the object they create

Shannon Booth537a2d416e07
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
index b1d214f6c56..8b7a08754c6 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
@@ -145,6 +145,25 @@ pub(crate) fn allocate<T: Interface>(vm: &Vm, implementation: T) -> Ref<T> {
)
}
+/// What the constructor steps or the transfer-receiving steps of an interface create: the implementation of the new
+/// object, which the bindings allocate, or the new object itself, for steps that hand the object to something else
+/// before they finish, as the constructor of ReadableStream gives it to its controller.
+pub(crate) trait ConstructedObject<T: Interface> {
+ fn into_object(self, vm: &Vm) -> Ref<T>;
+}
+
+impl<T: Interface> ConstructedObject<T> for T {
+ fn into_object(self, vm: &Vm) -> Ref<T> {
+ allocate(vm, self)
+ }
+}
+
+impl<T: Interface> ConstructedObject<T> for Ref<T> {
+ fn into_object(self, _: &Vm) -> Ref<T> {
+ self
+ }
+}
+
/// The object an attribute or an operation of an interface runs on, if it is a wrapper of the interface or of an
/// interface that inherits from it, with the object's realm. A nullish this value stands for the global object of
/// the current realm.
@@ -248,9 +267,13 @@ pub(crate) fn construct_wrapper<T: Interface>(
vm: &Vm,
realm: Gc<Realm>,
prototype: Gc<Object>,
- implementation: T,
+ object: impl ConstructedObject<T>,
) -> Gc<HostObject> {
- let object = allocate(vm, implementation);
+ let object = object.into_object(vm);
+ assert!(
+ 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);
if wrapper_world_of(vm, realm).is_main_world() {
object.main_world_wrapper.set(Some(wrapper));
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs
index c55f786a10b..66a74aff7e2 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs
@@ -23,7 +23,7 @@ use core::ptr::NonNull;
use ak::Utf16String;
use libjs_rust::api::{RawNativeFunctionResult, ThrowCompletionOr, Value};
-use crate::bindings::interface::{Interface, allocate, cell_from_abi, realm_arguments, wrap};
+use crate::bindings::interface::{ConstructedObject, Interface, Ref, cell_from_abi, realm_arguments, wrap};
use crate::bindings::{CallContext, WebIdlResult};
unsafe extern "C" {
@@ -230,10 +230,11 @@ pub(crate) trait Transferable: Interface {
fn detached(&self) -> &Cell<bool>;
/// https://html.spec.whatwg.org/multipage/structured-data.html#transfer-steps
- fn transfer_steps(&self, cx: &mut CallContext, data_holder: &mut TransferDataWriter) -> WebIdlResult<()>;
+ fn transfer_steps(this: &Ref<Self>, cx: &mut CallContext, data_holder: &mut TransferDataWriter)
+ -> WebIdlResult<()>;
/// https://html.spec.whatwg.org/multipage/structured-data.html#transfer-receiving-steps, which create the object.
- fn transfer_receiving_steps(cx: &mut CallContext, data_holder: &mut TransferDataReader) -> WebIdlResult<Self>;
+ fn transfer_receiving_steps(cx: &mut CallContext, data_holder: &mut TransferDataReader) -> WebIdlResult<Ref<Self>>;
}
/// The completion of steps, as the entry points return it to C++.
@@ -243,8 +244,8 @@ fn completion(cx: &CallContext, result: WebIdlResult<Value>) -> RawNativeFunctio
}
/// The wrapper in the realm of the call context of the object that steps created.
-fn wrap_new_object<T: Interface>(cx: &CallContext, implementation: T) -> Value {
- Value::from_object(wrap(cx, allocate(cx.vm(), implementation)))
+fn wrap_new_object<T: Interface>(cx: &CallContext, object: impl ConstructedObject<T>) -> Value {
+ Value::from_object(wrap(cx, object.into_object(cx.vm())))
}
/// The entry point of the serialization steps of `T`.
@@ -331,7 +332,7 @@ pub(crate) unsafe fn transfer_steps<T: Transferable>(
raw: NonNull::new(data_holder).expect("C++ lends a data holder"),
_lent: PhantomData,
};
- let result = object.transfer_steps(&mut cx, &mut data_holder);
+ let result = T::transfer_steps(&object, &mut cx, &mut data_holder);
completion(&cx, result.map(|()| Value::UNDEFINED))
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs b/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs
index 211bc7f0cba..0d70426ce7b 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs
@@ -225,9 +225,9 @@ impl Transferable for ImageBitmap {
}
/// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#the-imagebitmap-interface:transfer-steps
- fn transfer_steps(&self, _: &mut CallContext, data_holder: &mut TransferDataWriter) -> WebIdlResult<()> {
+ fn transfer_steps(this: &Ref<Self>, _: &mut CallContext, data_holder: &mut TransferDataWriter) -> WebIdlResult<()> {
// 1. If value's origin-clean flag is not set, then throw a "DataCloneError" DOMException.
- if !self.origin_clean.get() {
+ if !this.origin_clean.get() {
return Err(Exception::Dom(
"DataCloneError",
Utf16String::from_ascii(b"Cannot transfer an ImageBitmap that is not origin-clean"),
@@ -237,21 +237,24 @@ impl Transferable for ImageBitmap {
// 2. Set dataHolder.[[BitmapData]] to value's bitmap data.
// NB: The pixels go to the shared memory of the transfer if there is room for them there, and inline otherwise.
// SAFETY: The data holder is lent for the steps, and the frame lives while the bitmap data is set.
- unsafe { web_image_bitmap_transfer_bitmap(data_holder.as_raw(), self.raw_frame()) }.into_completion()?;
+ unsafe { web_image_bitmap_transfer_bitmap(data_holder.as_raw(), this.raw_frame()) }.into_completion()?;
// 3. Unset value's bitmap data.
- self.bitmap.replace(None);
+ this.bitmap.replace(None);
Ok(())
}
/// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#the-imagebitmap-interface:transfer-receiving-steps
- fn transfer_receiving_steps(_: &mut CallContext, data_holder: &mut TransferDataReader) -> WebIdlResult<Self> {
+ fn transfer_receiving_steps(cx: &mut CallContext, data_holder: &mut TransferDataReader) -> WebIdlResult<Ref<Self>> {
// 1. Set value's bitmap data to dataHolder.[[BitmapData]].
let mut frame = core::ptr::null_mut();
// SAFETY: The data holder is lent for the steps.
unsafe { web_image_bitmap_receive_bitmap(data_holder.as_raw(), &mut frame) }.into_completion()?;
// SAFETY: C++ hands over the frame it decoded, or null.
- Ok(Self::new(unsafe { BitmapData::from_handed_over_frame(frame) }))
+ Ok(allocate(
+ cx.vm(),
+ Self::new(unsafe { BitmapData::from_handed_over_frame(frame) }),
+ ))
}
}
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index fb74a08271d..4ee186917c6 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -448,7 +448,9 @@ def implementation_method_signatures(
signatures = []
for constructor in members.constructors:
parameters = parameter_list(constructor_description(interface), constructor.constructor.parameters)
- signatures.append(f"fn {constructor.method}(cx: &mut CallContext{parameters}) -> WebIdlResult<Self>")
+ signatures.append(
+ f"fn {constructor.method}(cx: &mut CallContext{parameters}) -> WebIdlResult<impl ConstructedObject<Self>>"
+ )
for attribute in members.attributes:
what = f"{interface.name}.{attribute.attribute.name}"
attribute_type = rust_type(attribute.attribute.type, context, what)
@@ -1300,7 +1302,7 @@ def write_rust_constructor(
f""" let mut cx = CallContext::new(vm, realm);
let implementation =
Implementation::{constructor.method}(&mut cx{arguments}).map_err(|exception| exception.throw(&cx))?;
- Ok(Value::from_object(construct_wrapper(vm, realm, prototype, implementation)))
+ Ok(Value::from_object(construct_wrapper::<Implementation>(vm, realm, prototype, implementation)))
}}
"""
)