Skip to content

Truly independent web browser

LibWeb: Clone stream chunks with the Rust serializer

Shannon Booth08aed0a18131
diff --git a/Libraries/LibWeb/Bindings/Rust/src/streams/abstract_operations.rs b/Libraries/LibWeb/Bindings/Rust/src/streams/abstract_operations.rs
index d3c2a14a23d..fb795ad3281 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/streams/abstract_operations.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/streams/abstract_operations.rs
@@ -6,19 +6,11 @@
//! Miscellaneous abstract operations, https://streams.spec.whatwg.org/#misc-abstract-ops
-use core::ffi::c_void;
-
-use libjs_rust::api::{
- ArrayBuffer, Gc, Must, RawNativeFunctionResult, Value, clone_array_buffer, construct, same_value,
-};
+use libjs_rust::api::{ArrayBuffer, Gc, Must, Value, clone_array_buffer, construct, same_value};
use crate::bindings::conversions::{ArrayBufferView, BufferSource};
use crate::bindings::{CallContext, WebIdlResult};
-
-unsafe extern "C" {
- /// StructuredDeserialize(? StructuredSerialize(value), realm).
- fn web_streams_structured_clone(realm: *mut c_void, value: u64) -> RawNativeFunctionResult;
-}
+use crate::html::structured_serialize::{structured_deserialize_with_transfer, structured_serialize_with_transfer};
// https://streams.spec.whatwg.org/#can-transfer-array-buffer
pub(crate) fn can_transfer_array_buffer(array_buffer: &ArrayBuffer) -> bool {
@@ -106,9 +98,11 @@ pub(crate) fn clone_as_uint8_array(cx: &CallContext, view: ArrayBufferView) -> W
// https://streams.spec.whatwg.org/#abstract-opdef-structuredclone
pub(crate) fn structured_clone(cx: &CallContext, value: Value) -> WebIdlResult<Value> {
// 1. Let serialized be ? StructuredSerialize(v).
+ // NB: StructuredSerializeWithTransfer with an empty transfer list is StructuredSerialize, in a record that Rust owns.
+ let serialized = structured_serialize_with_transfer(cx, value, &[])?;
+
// 2. Return ? StructuredDeserialize(serialized, the current Realm).
- // SAFETY: The realm and the value are live.
- Ok(unsafe { web_streams_structured_clone(cx.realm().as_ptr().cast(), value.to_raw()) }.into_completion()?)
+ Ok(structured_deserialize_with_transfer(cx, serialized.record())?.deserialized)
}
// https://streams.spec.whatwg.org/#abstract-opdef-cancopydatablockbytes
diff --git a/Libraries/LibWeb/Streams/ReadableStream.cpp b/Libraries/LibWeb/Streams/ReadableStream.cpp
index 4a5f5b88f03..8acaad62096 100644
--- a/Libraries/LibWeb/Streams/ReadableStream.cpp
+++ b/Libraries/LibWeb/Streams/ReadableStream.cpp
@@ -13,7 +13,6 @@
#include <LibWeb/Bindings/RustSerialization.h>
#include <LibWeb/Bindings/WrapperWorld.h>
#include <LibWeb/DOM/AbortSignal.h>
-#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/Page/RenderingStats.h>
#include <LibWeb/Streams/ReadableStream.h>
#include <LibWeb/Streams/ReadableStreamDefaultReader.h>
@@ -56,7 +55,6 @@ void web_streams_read_request_error_steps(ReadRequest*, JSValue error);
void web_streams_call_bytes_steps(ReadableStreamDefaultReader::SuccessSteps*, ReadonlyBytes);
void web_streams_call_value_steps(ReadableStreamDefaultReader::FailureSteps*, JSValue);
void web_streams_complete_rendering_pending_stream(ReadableStream*);
-JSCompletion web_streams_structured_clone(void* realm, JSValue);
void* web_streams_abort_signal_from_wrapper(JSObject* wrapper);
JSObject* web_streams_abort_signal_wrapper(void* realm, DOM::AbortSignal*);
void* web_streams_create_abort_signal();
@@ -276,21 +274,6 @@ void web_streams_complete_rendering_pending_stream(ReadableStream* stream)
complete_rendering_pending_stream(*stream);
}
-// https://streams.spec.whatwg.org/#abstract-opdef-structuredclone
-JSCompletion web_streams_structured_clone(void* realm, JSValue value)
-{
- auto& js_realm = realm_of(realm);
- auto& vm = js_realm.vm();
- auto result = [&]() -> WebIDL::ExceptionOr<JS::Value> {
- // 1. Let serialized be ? StructuredSerialize(v).
- auto serialized = TRY(HTML::structured_serialize(vm, JS::HostABI::value_from_abi(value)));
-
- // 2. Return ? StructuredDeserialize(serialized, the current Realm).
- return TRY(HTML::structured_deserialize(vm, serialized, js_realm));
- }();
- return Bindings::completion_to_abi(js_realm, move(result));
-}
-
void* web_streams_abort_signal_from_wrapper(JSObject* wrapper)
{
auto* signal = Bindings::impl_from<DOM::AbortSignal>(JS::HostABI::object_from_abi(wrapper));