From a52f9970bcb7cbb0af02bd087d464a37c8045798 Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Sat, 8 Jul 2023 08:20:02 +0200 Subject: [PATCH] LibWeb: Add AO create_readable_stream() --- .../LibWeb/Streams/AbstractOperations.cpp | 30 +++++++++++++++++++ .../LibWeb/Streams/AbstractOperations.h | 1 + 2 files changed, 31 insertions(+) diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp index af3d9a72bd..7421a9643d 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -1156,6 +1156,36 @@ bool readable_byte_stream_controller_should_call_pull(ReadableByteStreamControll return false; } +// https://streams.spec.whatwg.org/#create-readable-stream +WebIDL::ExceptionOr> create_readable_stream(JS::Realm& realm, StartAlgorithm&& start_algorithm, PullAlgorithm&& pull_algorithm, CancelAlgorithm&& cancel_algorithm, Optional high_water_mark, Optional&& size_algorithm) +{ + // 1. If highWaterMark was not passed, set it to 1. + if (!high_water_mark.has_value()) + high_water_mark = 1.0; + + // 2. If sizeAlgorithm was not passed, set it to an algorithm that returns 1. + if (!size_algorithm.has_value()) + size_algorithm = [](auto const&) { return JS::normal_completion(JS::Value(1)); }; + + // 3. Assert: ! IsNonNegativeNumber(highWaterMark) is true. + VERIFY(is_non_negative_number(JS::Value { *high_water_mark })); + + // 4. Let stream be a new ReadableStream. + auto stream = MUST_OR_THROW_OOM(realm.heap().allocate(realm, realm)); + + // 5. Perform ! InitializeReadableStream(stream). + initialize_readable_stream(*stream); + + // 6. Let controller be a new ReadableStreamDefaultController. + auto controller = MUST_OR_THROW_OOM(realm.heap().allocate(realm, realm)); + + // 7. Perform ? SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm). + TRY(set_up_readable_stream_default_controller(*stream, *controller, move(start_algorithm), move(pull_algorithm), move(cancel_algorithm), *high_water_mark, move(*size_algorithm))); + + // 8. Return stream. + return stream; +} + // https://streams.spec.whatwg.org/#create-writable-stream WebIDL::ExceptionOr> create_writable_stream(JS::Realm& realm, StartAlgorithm&& start_algorithm, WriteAlgorithm&& write_algorithm, CloseAlgorithm&& close_algorithm, AbortAlgorithm&& abort_algorithm, double high_water_mark, SizeAlgorithm&& size_algorithm) { diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h index f45f5c55cf..abb0ed174b 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h @@ -90,6 +90,7 @@ WebIDL::ExceptionOr readable_byte_stream_controller_handle_queue_drain(Rea void readable_byte_stream_controller_invalidate_byob_request(ReadableByteStreamController&); bool readable_byte_stream_controller_should_call_pull(ReadableByteStreamController const&); +WebIDL::ExceptionOr> create_readable_stream(JS::Realm& realm, StartAlgorithm&& start_algorithm, PullAlgorithm&& pull_algorithm, CancelAlgorithm&& cancel_algorithm, Optional high_water_mark = {}, Optional&& size_algorithm = {}); WebIDL::ExceptionOr> create_writable_stream(JS::Realm& realm, StartAlgorithm&& start_algorithm, WriteAlgorithm&& write_algorithm, CloseAlgorithm&& close_algorithm, AbortAlgorithm&& abort_algorithm, double high_water_mark, SizeAlgorithm&& size_algorithm); void initialize_readable_stream(ReadableStream&); void initialize_writable_stream(WritableStream&);