From 9ccadf61a257ae1018681e31e4fba8daac5b5479 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Thu, 29 Jun 2023 20:52:35 +1200 Subject: [PATCH] LibWeb: Implement the Streams SetUpReadableStreamBYOBReader AO Co-Authored-By: Matthew Olsson --- .../LibWeb/Streams/AbstractOperations.cpp | 20 +++++++++++++++++++ .../LibWeb/Streams/AbstractOperations.h | 1 + 2 files changed, 21 insertions(+) diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp index a6eb47dc778..7fcefbd463f 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -481,6 +481,26 @@ WebIDL::ExceptionOr set_up_readable_stream_default_reader(ReadableStreamDe return {}; } +// https://streams.spec.whatwg.org/#set-up-readable-stream-byob-reader +WebIDL::ExceptionOr set_up_readable_stream_byob_reader(ReadableStreamBYOBReader& reader, ReadableStream& stream) +{ + // 1. If ! IsReadableStreamLocked(stream) is true, throw a TypeError exception. + if (is_readable_stream_locked(stream)) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot create stream reader for a locked stream"sv }; + + // 2. If stream.[[controller]] does not implement ReadableByteStreamController, throw a TypeError exception. + if (!stream.controller()->has>()) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "BYOB reader cannot set up reader from non-byte stream"sv }; + + // 3. Perform ! ReadableStreamReaderGenericInitialize(reader, stream). + readable_stream_reader_generic_initialize(ReadableStreamReader { reader }, stream); + + // 4. Set reader.[[readIntoRequests]] to a new empty list. + reader.read_into_requests().clear(); + + return {}; +} + // https://streams.spec.whatwg.org/#readable-stream-default-controller-close void readable_stream_default_controller_close(ReadableStreamDefaultController& controller) { diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h index 5b9949a746f..c6c5824ce6f 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h @@ -49,6 +49,7 @@ void readable_stream_default_reader_error_read_requests(ReadableStreamDefaultRea WebIDL::ExceptionOr readable_stream_default_reader_read(ReadableStreamDefaultReader&, ReadRequest&); WebIDL::ExceptionOr readable_stream_default_reader_release(ReadableStreamDefaultReader&); WebIDL::ExceptionOr set_up_readable_stream_default_reader(ReadableStreamDefaultReader&, ReadableStream&); +WebIDL::ExceptionOr set_up_readable_stream_byob_reader(ReadableStreamBYOBReader&, ReadableStream&); void readable_stream_default_controller_close(ReadableStreamDefaultController&); WebIDL::ExceptionOr readable_stream_default_controller_enqueue(ReadableStreamDefaultController&, JS::Value chunk); WebIDL::ExceptionOr readable_stream_default_controller_can_pull_if_needed(ReadableStreamDefaultController&);