From 5e5b42730cd39e7c908fbcee20fd3be69d5df072 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 28 Jan 2024 12:20:55 -0500 Subject: [PATCH] LibWeb: Implement the CloneAsUint8Array AO --- .../LibWeb/Streams/AbstractOperations.cpp | 21 +++++++++++++++++++ .../LibWeb/Streams/AbstractOperations.h | 1 + 2 files changed, 22 insertions(+) diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp index fcc9f29f65..4668741108 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -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 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 structured_clone(JS::Realm& realm, JS::Value value) { diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h index ab63a65104..fcf2bd8d86 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h @@ -178,6 +178,7 @@ WebIDL::ExceptionOr 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 clone_as_uint8_array(JS::Realm&, WebIDL::ArrayBufferView&); WebIDL::ExceptionOr structured_clone(JS::Realm&, JS::Value value); JS::Value create_close_sentinel();