Skip to content

Truly independent web browser

LibWeb: Let Rust deserialization steps return the object they create

Shannon Booth7e2f64d71b66
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs
index fffe0362716..5eacff436dd 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs
@@ -197,12 +197,13 @@ impl SerializationReader<'_> {
Ok(value)
}
- /// Reads what SerializationWriter::write_object_in_place() writes.
- pub(crate) fn read_object_in_place<T: Serializable>(&mut self, cx: &mut CallContext) -> WebIdlResult<T> {
+ /// Reads what SerializationWriter::write_object_in_place() writes, and returns the new object.
+ pub(crate) fn read_object_in_place<T: Serializable>(&mut self, cx: &mut CallContext) -> WebIdlResult<Ref<T>> {
// SAFETY: The reader is lent for the steps.
unsafe { web_bindings_deserialize_inline_object_header(self.raw.as_ptr(), T::INTERFACE_NAME) }
.into_completion()?;
- T::deserialization_steps(cx, self)
+ let object = T::deserialization_steps(cx, self)?;
+ Ok(object.into_object(cx.vm()))
}
pub(crate) fn read_bool(&mut self) -> WebIdlResult<bool> {
@@ -260,7 +261,10 @@ pub(crate) trait Serializable: Interface {
) -> WebIdlResult<()>;
/// https://html.spec.whatwg.org/multipage/structured-data.html#deserialization-steps, which create the object.
- fn deserialization_steps(cx: &mut CallContext, serialized: &mut SerializationReader) -> WebIdlResult<Self>;
+ fn deserialization_steps(
+ cx: &mut CallContext,
+ serialized: &mut SerializationReader,
+ ) -> WebIdlResult<impl ConstructedObject<Self> + use<Self>>;
}
/// The data holder of a transferred object, which its transfer steps write to: a Web::Bindings::RustTransferDataWriter
diff --git a/Libraries/LibWeb/Bindings/Rust/src/geometry/dom_quad.rs b/Libraries/LibWeb/Bindings/Rust/src/geometry/dom_quad.rs
index e2c14a8aff9..6ccd4373391 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/geometry/dom_quad.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/geometry/dom_quad.rs
@@ -229,19 +229,15 @@ impl Serializable for DOMQuad {
fn deserialization_steps(cx: &mut CallContext, serialized: &mut SerializationReader) -> WebIdlResult<Self> {
// 1. Set value’s point 1 to the sub-deserialization of serialized.[[P1]].
let p1 = serialized.read_object_in_place::<DOMPoint>(cx)?;
- let p1 = allocate(cx.vm(), |_| p1);
// 2. Set value’s point 2 to the sub-deserialization of serialized.[[P2]].
let p2 = serialized.read_object_in_place::<DOMPoint>(cx)?;
- let p2 = allocate(cx.vm(), |_| p2);
// 3. Set value’s point 3 to the sub-deserialization of serialized.[[P3]].
let p3 = serialized.read_object_in_place::<DOMPoint>(cx)?;
- let p3 = allocate(cx.vm(), |_| p3);
// 4. Set value’s point 4 to the sub-deserialization of serialized.[[P4]].
let p4 = serialized.read_object_in_place::<DOMPoint>(cx)?;
- let p4 = allocate(cx.vm(), |_| p4);
Ok(Self { p1, p2, p3, p4 })
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/lib.rs b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
index 27185a22412..1bffb0cb593 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/lib.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
@@ -13,6 +13,10 @@
//! every crate it links, so linking the runtime would make it link every C function the runtime imports as well; the
//! modules that use the runtime are left out of the tests instead, as the runtime's own crate leaves out its engine.
+// NB: The implementations of Serializable return concrete types where the trait has an opaque one. The trait is
+// pub(crate), so refining it exposes nothing.
+#![allow(refining_impl_trait)]
+
#[cfg(feature = "allocator")]
extern crate ladybird_allocator;