From 894f1e9d62cdb444fa55edb022f9887e344c4975 Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Sat, 8 Jul 2023 06:51:08 +0200 Subject: [PATCH] LibWeb: Add AO create_writable_stream() --- .../LibWeb/Streams/AbstractOperations.cpp | 22 +++++++++++++++++++ .../LibWeb/Streams/AbstractOperations.h | 1 + 2 files changed, 23 insertions(+) diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp index 83b4ac35ab..6ef1a874ac 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -1156,6 +1156,28 @@ bool readable_byte_stream_controller_should_call_pull(ReadableByteStreamControll return false; } +// 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) +{ + // 1. Assert: ! IsNonNegativeNumber(highWaterMark) is true. + VERIFY(is_non_negative_number(JS::Value { high_water_mark })); + + // 2. Let stream be a new WritableStream. + auto stream = MUST_OR_THROW_OOM(realm.heap().allocate(realm, realm)); + + // 3. Perform ! InitializeWritableStream(stream). + initialize_writable_stream(*stream); + + // 4. Let controller be a new WritableStreamDefaultController. + auto controller = MUST_OR_THROW_OOM(realm.heap().allocate(realm, realm)); + + // 5. Perform ? SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm). + TRY(set_up_writable_stream_default_controller(*stream, *controller, move(start_algorithm), move(write_algorithm), move(close_algorithm), move(abort_algorithm), high_water_mark, move(size_algorithm))); + + // 6. Return stream. + return stream; +} + // https://streams.spec.whatwg.org/#initialize-writable-stream void initialize_writable_stream(WritableStream& stream) { diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h index e8f17b6d33..a5168ba3ff 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_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_writable_stream(WritableStream&); WebIDL::ExceptionOr> acquire_writable_stream_default_writer(WritableStream&);