LibWeb: Implement the StructuredClone AO

This commit is contained in:
Timothy Flynn 2024-01-28 10:22:52 -05:00 committed by Andreas Kling
parent 59716f807f
commit 5ccd1ff1bf
2 changed files with 14 additions and 0 deletions

View file

@ -16,6 +16,7 @@
#include <LibJS/Runtime/TypedArray.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/DOM/AbortSignal.h>
#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/Streams/AbstractOperations.h>
#include <LibWeb/Streams/QueuingStrategy.h>
#include <LibWeb/Streams/ReadableByteStreamController.h>
@ -3923,6 +3924,18 @@ bool can_transfer_array_buffer(JS::ArrayBuffer const& array_buffer)
return true;
}
// https://streams.spec.whatwg.org/#abstract-opdef-structuredclone
WebIDL::ExceptionOr<JS::Value> structured_clone(JS::Realm& realm, JS::Value value)
{
auto& vm = realm.vm();
// 1. Let serialized be ? StructuredSerialize(v).
auto serialized = TRY(HTML::structured_serialize(vm, value));
// 2. Return ? StructuredDeserialize(serialized, the current Realm).
return TRY(HTML::structured_deserialize(vm, serialized, realm, {}));
}
// https://streams.spec.whatwg.org/#close-sentinel
// Non-standard function that implements the "close sentinel" value.
JS::Value create_close_sentinel()

View file

@ -175,6 +175,7 @@ WebIDL::ExceptionOr<void> transform_stream_set_backpressure(TransformStream&, bo
bool is_non_negative_number(JS::Value);
bool can_transfer_array_buffer(JS::ArrayBuffer const& array_buffer);
WebIDL::ExceptionOr<JS::Value> structured_clone(JS::Realm&, JS::Value value);
JS::Value create_close_sentinel();
bool is_close_sentinel(JS::Value);