Skip to content

Truly independent web browser

LibWeb: Run structuredClone() in Rust

Shannon Boothada1e0e10596
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
index 68e67ace2f2..766aea367a8 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
@@ -25,6 +25,7 @@ use super::message_port::MessagePort;
use super::storage::{Storage, StorageType};
use super::storage_event::{StorageEvent, StorageEventInit};
use super::window::{PostedMessage, queue_posted_message, window_post_message_steps};
+use super::window_or_worker_global_scope::structured_clone;
use crate::bindings::CallContext;
use crate::bindings::conversions::UsvString;
use crate::bindings::cpp_cell::CppCell;
@@ -606,3 +607,23 @@ pub unsafe extern "C" fn web_window_queue_posted_message(
queue_posted_message(vm, posted_message);
}
}
+
+/// # Safety
+///
+/// The VM and the realm must be live, and `transfer` must point to `count` live objects.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_rust_structured_clone(
+ vm: *mut c_void,
+ realm: *mut c_void,
+ value: u64,
+ transfer: *const *mut c_void,
+ count: usize,
+) -> RawNativeFunctionResult {
+ // SAFETY: The caller passes the live VM and a live realm.
+ let (vm, realm) = unsafe { realm_arguments(vm, realm) };
+ let cx = CallContext::new(vm, realm);
+ // SAFETY: The caller passes a live value and `count` live objects.
+ let (value, transfer) = unsafe { (Value::from_raw(value), objects_from_abi(vm, transfer, count)) };
+ let result = structured_clone(&cx, value, &transfer.values());
+ RawNativeFunctionResult::from_completion(result.map_err(|exception| exception.throw(&cx)))
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
index d4bd261dadd..1f383a23900 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/mod.rs
@@ -82,3 +82,5 @@ pub(crate) mod structured_serialize;
pub(crate) mod text_metrics;
#[cfg(not(test))]
pub(crate) mod window;
+#[cfg(not(test))]
+pub(crate) mod window_or_worker_global_scope;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/window_or_worker_global_scope.rs b/Libraries/LibWeb/Bindings/Rust/src/html/window_or_worker_global_scope.rs
new file mode 100644
index 00000000000..988ee287806
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/window_or_worker_global_scope.rs
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2022, Andrew Kaster <[email protected]>
+ * Copyright (c) 2023, Linus Groh <[email protected]>
+ * Copyright (c) 2023, Luke Wilde <[email protected]>
+ * Copyright (c) 2025-2026, Shannon Booth <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! WindowOrWorkerGlobalScope, https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope
+
+use libjs_rust::api::{Gc, Object, Value};
+
+use crate::bindings::{CallContext, WebIdlResult};
+use crate::html::structured_serialize::transfer_record::TransferRecord;
+use crate::html::structured_serialize::{structured_deserialize_with_transfer, structured_serialize_with_transfer};
+
+// https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone
+pub(crate) fn structured_clone(cx: &CallContext, value: Value, transfer: &[Gc<Object>]) -> WebIdlResult<Value> {
+ // 1. Let serialized be ? StructuredSerializeWithTransfer(value, options["transfer"]).
+ let serialized = structured_serialize_with_transfer(cx, value, transfer)?;
+
+ // 2. Let deserializeRecord be ? StructuredDeserializeWithTransfer(serialized, this's relevant realm).
+ let deserialize_record = structured_deserialize_with_transfer(cx, serialized);
+ TransferRecord::discard(serialized);
+
+ // 3. Return deserializeRecord.[[Deserialized]].
+ Ok(deserialize_record?.deserialized)
+}
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
index dbb7d628214..385e3f84f84 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
@@ -22,6 +22,7 @@
#include <LibGfx/Bitmap.h>
#include <LibGfx/DecodedImageFrame.h>
#include <LibGfx/ScalingMode.h>
+#include <LibJS/HostClassBuilder.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/TypedArray.h>
@@ -253,22 +254,20 @@ void WindowOrWorkerGlobalScopeMixin::create_image_bitmap(JS::Realm& realm, Image
create_image_bitmap_impl(realm, promise, image, sx, sy, sw, sh, move(options));
}
-// https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone
-WebIDL::ExceptionOr<JS::Value> WindowOrWorkerGlobalScopeMixin::structured_clone(JS::Realm& realm, JS::Value value, Bindings::StructuredSerializeOptions const& options)
-{
- // 1. Let serialized be ? StructuredSerializeWithTransfer(value, options["transfer"]).
- auto serialized = TRY(HTML::structured_serialize_with_transfer(realm, value, options.transfer));
-
- // 2. Let deserializeRecord be ? StructuredDeserializeWithTransfer(serialized, this's relevant realm).
- auto deserialized = TRY(HTML::structured_deserialize_with_transfer(serialized, realm));
-
- // 3. Return deserializeRecord.[[Deserialized]].
- return deserialized.deserialized;
-}
-
extern "C" {
// Defined in Bindings/Rust/src/canvas_2d/exports.rs.
bool web_canvas_2d_read_back_video_frame(void* video_wrapper, Optional<Gfx::DecodedImageFrame>* out_frame);
+// Defined in Bindings/Rust/src/html/exports.rs.
+JSCompletion web_rust_structured_clone(JSVM*, void* realm, JSValue, JSObject* const* transfer, size_t count);
+}
+
+// https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone
+WebIDL::ExceptionOr<JS::Value> WindowOrWorkerGlobalScopeMixin::structured_clone(JS::Realm& realm, JS::Value value, Bindings::StructuredSerializeOptions const& options)
+{
+ Vector<JSObject*> transfer;
+ for (auto const& object : options.transfer)
+ transfer.append(JS::HostABI::object_to_abi(object.ptr()));
+ return TRY(JS::HostABI::completion_from_abi<JS::Value>(web_rust_structured_clone(JS::HostABI::vm_to_abi(realm.vm()), &realm, JS::HostABI::value_to_abi(value), transfer.data(), transfer.size())));
}
static Compositor::CompositorHost* compositor_host_for(JS::Object& global_object)