LibWeb: Implement the CloneAsUint8Array AO

This commit is contained in:
Timothy Flynn 2024-01-28 12:20:55 -05:00 committed by Andreas Kling
parent debfe996d7
commit 5e5b42730c
Notes: sideshowbarker 2024-07-16 23:38:54 +09:00
2 changed files with 22 additions and 0 deletions

View File

@ -4243,6 +4243,27 @@ bool can_transfer_array_buffer(JS::ArrayBuffer const& array_buffer)
return true;
}
// https://streams.spec.whatwg.org/#abstract-opdef-cloneasuint8array
WebIDL::ExceptionOr<JS::Value> clone_as_uint8_array(JS::Realm& realm, WebIDL::ArrayBufferView& view)
{
auto& vm = realm.vm();
// 1. Assert: Type(O) is Object.
// 2. Assert: O has an [[ViewedArrayBuffer]] internal slot.
// 3. Assert: ! IsDetachedBuffer(O.[[ViewedArrayBuffer]]) is false.
VERIFY(!view.viewed_array_buffer()->is_detached());
// 4. Let buffer be ? CloneArrayBuffer(O.[[ViewedArrayBuffer]], O.[[ByteOffset]], O.[[ByteLength]], %ArrayBuffer%).
auto* buffer = TRY(JS::clone_array_buffer(vm, *view.viewed_array_buffer(), view.byte_offset(), view.byte_length()));
// 5. Let array be ! Construct(%Uint8Array%, « buffer »).
auto array = MUST(JS::construct(vm, *realm.intrinsics().uint8_array_constructor(), buffer));
// 5. Return array.
return array;
}
// https://streams.spec.whatwg.org/#abstract-opdef-structuredclone
WebIDL::ExceptionOr<JS::Value> structured_clone(JS::Realm& realm, JS::Value value)
{

View File

@ -178,6 +178,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> clone_as_uint8_array(JS::Realm&, WebIDL::ArrayBufferView&);
WebIDL::ExceptionOr<JS::Value> structured_clone(JS::Realm&, JS::Value value);
JS::Value create_close_sentinel();