From aca63fd947408bfd22321774d7acbc13e55b2663 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 19 Nov 2023 12:11:47 +1300 Subject: [PATCH] LibWeb: Implement ReadableByteStreamControllerCommitPullIntoDescriptor --- .../LibWeb/Streams/AbstractOperations.cpp | 39 +++++++++++++++++++ .../LibWeb/Streams/AbstractOperations.h | 1 + 2 files changed, 40 insertions(+) diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp index 73b6c0d597..6a72ed3803 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -1719,6 +1719,45 @@ WebIDL::ExceptionOr readable_byte_stream_controller_enqueue_detached_pull_ return {}; } +// https://streams.spec.whatwg.org/#readable-byte-stream-controller-commit-pull-into-descriptor +void readable_byte_stream_controller_commit_pull_into_descriptor(ReadableStream& stream, PullIntoDescriptor const& pull_into_descriptor) +{ + // 1. Assert: stream.[[state]] is not "errored". + VERIFY(!stream.is_errored()); + + // 2. Assert: pullIntoDescriptor.reader type is not "none". + VERIFY(pull_into_descriptor.reader_type != ReaderType::None); + + // 3. Let done be false. + bool done = false; + + // 4. If stream.[[state]] is "closed", + if (stream.is_closed()) { + // 1. Assert: pullIntoDescriptor’s bytes filled is 0. + VERIFY(pull_into_descriptor.bytes_filled == 0); + + // 2. Set done to true. + done = true; + } + + // 5. Let filledView be ! ReadableByteStreamControllerConvertPullIntoDescriptor(pullIntoDescriptor). + auto filled_view = readable_byte_stream_controller_convert_pull_into_descriptor(stream.realm(), pull_into_descriptor); + + // 6. If pullIntoDescriptor’s reader type is "default", + if (pull_into_descriptor.reader_type == ReaderType::Default) { + // 1. Perform ! ReadableStreamFulfillReadRequest(stream, filledView, done). + readable_stream_fulfill_read_request(stream, filled_view, done); + } + // 7. Otherwise, + else { + // 1. Assert: pullIntoDescriptor’s reader type is "byob". + VERIFY(pull_into_descriptor.reader_type == ReaderType::Byob); + + // 2. Perform ! ReadableStreamFulfillReadIntoRequest(stream, filledView, done). + readable_stream_fulfill_read_into_request(stream, filled_view, done); + } +} + // https://streams.spec.whatwg.org/#abstract-opdef-readablebytestreamcontrollerprocessreadrequestsusingqueue WebIDL::ExceptionOr readable_byte_stream_controller_process_read_requests_using_queue(ReadableByteStreamController& controller) { diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h index a7c17a5888..3d32b9ab8b 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h @@ -81,6 +81,7 @@ WebIDL::ExceptionOr readable_stream_enqueue(ReadableStreamController& cont WebIDL::ExceptionOr readable_byte_stream_controller_enqueue(ReadableByteStreamController& controller, JS::Value chunk); WebIDL::ExceptionOr> transfer_array_buffer(JS::Realm& realm, JS::ArrayBuffer& buffer); WebIDL::ExceptionOr readable_byte_stream_controller_enqueue_detached_pull_into_queue(ReadableByteStreamController& controller, PullIntoDescriptor& pull_into_descriptor); +void readable_byte_stream_controller_commit_pull_into_descriptor(ReadableStream&, PullIntoDescriptor const&); WebIDL::ExceptionOr readable_byte_stream_controller_process_read_requests_using_queue(ReadableByteStreamController& controller); void readable_byte_stream_controller_enqueue_chunk_to_queue(ReadableByteStreamController& controller, JS::NonnullGCPtr buffer, u32 byte_offset, u32 byte_length); WebIDL::ExceptionOr readable_byte_stream_controller_enqueue_cloned_chunk_to_queue(ReadableByteStreamController& controller, JS::ArrayBuffer& buffer, u64 byte_offset, u64 byte_length);