diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2023-03-30 19:27:45 -0700 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-04-06 22:54:58 +0200 |
commit | bdab61ad93b5509943cafcf3ea339e04f91ec660 (patch) | |
tree | f8453ca5a6796a4c4797d713045484f8114f0e9c | |
parent | e93560b769f5749e96ca66977886e2be94554550 (diff) | |
download | serenity-bdab61ad93b5509943cafcf3ea339e04f91ec660.zip |
LibWeb: Add the WritableStreamDefaultWriter interface
11 files changed, 270 insertions, 3 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index abdcb9e14b..90ac677f2e 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -57,6 +57,7 @@ static bool is_platform_object(Type const& type) "URLSearchParams"sv, "WebGLRenderingContext"sv, "Window"sv, + "WritableStream"sv, }; if (type.name().ends_with("Element"sv)) return true; diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index f9d315127f..90dcae1ba8 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -465,6 +465,7 @@ set(SOURCES Streams/UnderlyingSink.cpp Streams/UnderlyingSource.cpp Streams/WritableStream.cpp + Streams/WritableStreamDefaultWriter.cpp SVG/AttributeNames.cpp SVG/AttributeParser.cpp SVG/SVGAnimatedLength.cpp diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index a1fc6b9f60..cf900b0de2 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -429,6 +429,7 @@ class ReadRequest; struct UnderlyingSink; struct UnderlyingSource; class WritableStream; +class WritableStreamDefaultWriter; } namespace Web::SVG { diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp index 367883635a..c7d633350a 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -16,6 +16,7 @@ #include <LibWeb/Streams/UnderlyingSink.h> #include <LibWeb/Streams/UnderlyingSource.h> #include <LibWeb/Streams/WritableStream.h> +#include <LibWeb/Streams/WritableStreamDefaultWriter.h> #include <LibWeb/WebIDL/AbstractOperations.h> #include <LibWeb/WebIDL/ExceptionOr.h> #include <LibWeb/WebIDL/Promise.h> @@ -747,6 +748,113 @@ bool is_writable_stream_locked(WritableStream const& stream) return true; } +// https://streams.spec.whatwg.org/#set-up-writable-stream-default-writer +WebIDL::ExceptionOr<void> set_up_writable_stream_default_writer(WritableStreamDefaultWriter& writer, WritableStream& stream) +{ + auto& realm = writer.realm(); + + // 1. If ! IsWritableStreamLocked(stream) is true, throw a TypeError exception. + if (is_writable_stream_locked(stream)) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Stream is locked"sv }; + + // 2. Set writer.[[stream]] to stream. + writer.set_stream(stream); + + // 3. Set stream.[[writer]] to writer. + stream.set_writer(writer); + + // 4. Let state be stream.[[state]]. + auto state = stream.state(); + + // 5. If state is "writable", + if (state == WritableStream::State::Writable) { + // 1. If ! WritableStreamCloseQueuedOrInFlight(stream) is false and stream.[[backpressure]] is true, set writer.[[readyPromise]] to a new promise. + if (!writable_stream_close_queued_or_in_flight(stream) && stream.backpressure()) { + writer.set_ready_promise(WebIDL::create_promise(realm)); + } + // 2. Otherwise, set writer.[[readyPromise]] to a promise resolved with undefined. + else { + writer.set_ready_promise(WebIDL::create_resolved_promise(realm, JS::js_undefined())); + } + + // 3. Set writer.[[closedPromise]] to a new promise. + writer.set_closed_promise(WebIDL::create_promise(realm)); + } + // 6. Otherwise, if state is "erroring", + else if (state == WritableStream::State::Erroring) { + // 1. Set writer.[[readyPromise]] to a promise rejected with stream.[[storedError]]. + writer.set_ready_promise(WebIDL::create_rejected_promise(realm, stream.stored_error())); + + // 2. Set writer.[[readyPromise]].[[PromiseIsHandled]] to true. + WebIDL::mark_promise_as_handled(*writer.ready_promise()); + + // 3. Set writer.[[closedPromise]] to a new promise. + writer.set_closed_promise(WebIDL::create_promise(realm)); + } + // 7. Otherwise, if state is "closed", + else if (state == WritableStream::State::Closed) { + // 1. Set writer.[[readyPromise]] to a promise resolved with undefined. + writer.set_ready_promise(WebIDL::create_resolved_promise(realm, JS::js_undefined())); + + // 2. Set writer.[[closedPromise]] to a promise resolved with undefined. + writer.set_closed_promise(WebIDL::create_resolved_promise(realm, JS::js_undefined())); + } + // 8. Otherwise, + else { + // 1. Assert: state is "errored". + VERIFY(state == WritableStream::State::Errored); + + // 2. Let storedError be stream.[[storedError]]. + auto stored_error = stream.stored_error(); + + // 3. Set writer.[[readyPromise]] to a promise rejected with storedError. + writer.set_ready_promise(WebIDL::create_rejected_promise(realm, stored_error)); + + // 4. Set writer.[[readyPromise]].[[PromiseIsHandled]] to true. + WebIDL::mark_promise_as_handled(*writer.ready_promise()); + + // 5. Set writer.[[closedPromise]] to a promise rejected with storedError. + writer.set_closed_promise(WebIDL::create_rejected_promise(realm, stored_error)); + + // 6. Set writer.[[closedPromise]].[[PromiseIsHandled]] to true. + WebIDL::mark_promise_as_handled(*writer.closed_promise()); + } + + return {}; +} + +// https://streams.spec.whatwg.org/#writable-stream-close-queued-or-in-flight +bool writable_stream_close_queued_or_in_flight(WritableStream const& stream) +{ + // 1. If stream.[[closeRequest]] is undefined and stream.[[inFlightCloseRequest]] is undefined, return false. + if (!stream.close_request() && !stream.in_flight_write_request()) + return false; + + // 2. Return true. + return true; +} + +// https://streams.spec.whatwg.org/#writable-stream-default-writer-get-desired-size +Optional<double> writable_stream_default_writer_get_desired_size(WritableStreamDefaultWriter const& writer) +{ + // 1. Let stream be writer.[[stream]]. + auto stream = writer.stream(); + + // 2. Let state be stream.[[state]]. + auto state = stream->state(); + + // 3. If state is "errored" or "erroring", return null. + if (state == WritableStream::State::Errored || state == WritableStream::State::Erroring) + return {}; + + // 4. If state is "closed", return 0. + if (state == WritableStream::State::Closed) + return 0.0; + + // FIXME: 5. Return ! WritableStreamDefaultControllerGetDesiredSize(stream.[[controller]]). + return 0.0; +} + // Non-standard function to aid in converting a user-provided function into a WebIDL::Callback. This is essentially // what the Bindings generator would do at compile time, but at runtime instead. JS::ThrowCompletionOr<JS::Handle<WebIDL::CallbackType>> property_to_callback(JS::VM& vm, JS::Value value, JS::PropertyKey const& property_key) diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h index 2ca1356610..3b77dc9226 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h @@ -51,6 +51,11 @@ WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller(ReadableStre WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller_from_underlying_source(ReadableStream&, JS::Value underlying_source_value, UnderlyingSource, double high_water_mark, SizeAlgorithm&&); bool is_writable_stream_locked(WritableStream const&); +WebIDL::ExceptionOr<void> set_up_writable_stream_default_writer(WritableStreamDefaultWriter&, WritableStream&); + +bool writable_stream_close_queued_or_in_flight(WritableStream const&); + +Optional<double> writable_stream_default_writer_get_desired_size(WritableStreamDefaultWriter const&); JS::ThrowCompletionOr<JS::Handle<WebIDL::CallbackType>> property_to_callback(JS::VM& vm, JS::Value value, JS::PropertyKey const& property_key); diff --git a/Userland/Libraries/LibWeb/Streams/WritableStream.cpp b/Userland/Libraries/LibWeb/Streams/WritableStream.cpp index b855f13e6f..b277ffcbbb 100644 --- a/Userland/Libraries/LibWeb/Streams/WritableStream.cpp +++ b/Userland/Libraries/LibWeb/Streams/WritableStream.cpp @@ -10,6 +10,7 @@ #include <LibWeb/Streams/AbstractOperations.h> #include <LibWeb/Streams/UnderlyingSink.h> #include <LibWeb/Streams/WritableStream.h> +#include <LibWeb/Streams/WritableStreamDefaultWriter.h> #include <LibWeb/WebIDL/ExceptionOr.h> namespace Web::Streams { diff --git a/Userland/Libraries/LibWeb/Streams/WritableStream.h b/Userland/Libraries/LibWeb/Streams/WritableStream.h index e0a471eb24..6486f5874d 100644 --- a/Userland/Libraries/LibWeb/Streams/WritableStream.h +++ b/Userland/Libraries/LibWeb/Streams/WritableStream.h @@ -72,8 +72,8 @@ public: JS::Value stored_error() const { return m_stored_error; } void set_stored_error(JS::Value value) { m_stored_error = value; } - JS::GCPtr<JS::Object const> writer() const { return m_writer; } - void set_writer(JS::GCPtr<JS::Object> value) { m_writer = value; } + JS::GCPtr<WritableStreamDefaultWriter const> writer() const { return m_writer; } + void set_writer(JS::GCPtr<WritableStreamDefaultWriter> value) { m_writer = value; } SinglyLinkedList<JS::NonnullGCPtr<WebIDL::Promise>>& write_requests() { return m_write_requests; } @@ -122,7 +122,7 @@ private: // https://streams.spec.whatwg.org/#writablestream-writer // A WritableStreamDefaultWriter instance, if the stream is locked to a writer, or undefined if it is not - JS::GCPtr<JS::Object> m_writer; + JS::GCPtr<WritableStreamDefaultWriter> m_writer; // https://streams.spec.whatwg.org/#writablestream-writerequests // A list of promises representing the stream’s internal queue of write requests not yet processed by the underlying sink diff --git a/Userland/Libraries/LibWeb/Streams/WritableStreamDefaultWriter.cpp b/Userland/Libraries/LibWeb/Streams/WritableStreamDefaultWriter.cpp new file mode 100644 index 0000000000..058b4f0b65 --- /dev/null +++ b/Userland/Libraries/LibWeb/Streams/WritableStreamDefaultWriter.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibJS/Runtime/PromiseCapability.h> +#include <LibWeb/Bindings/Intrinsics.h> +#include <LibWeb/Bindings/WritableStreamDefaultWriterPrototype.h> +#include <LibWeb/Streams/AbstractOperations.h> +#include <LibWeb/Streams/WritableStream.h> +#include <LibWeb/Streams/WritableStreamDefaultWriter.h> +#include <LibWeb/WebIDL/ExceptionOr.h> + +namespace Web::Streams { + +WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStreamDefaultWriter>> WritableStreamDefaultWriter::construct_impl(JS::Realm& realm, JS::NonnullGCPtr<WritableStream> stream) +{ + auto writer = MUST_OR_THROW_OOM(realm.heap().allocate<WritableStreamDefaultWriter>(realm, realm)); + + // 1. Perform ? SetUpWritableStreamDefaultWriter(this, stream). + TRY(set_up_writable_stream_default_writer(*writer, stream)); + + return writer; +} + +// https://streams.spec.whatwg.org/#default-writer-closed +JS::GCPtr<JS::Object> WritableStreamDefaultWriter::closed() +{ + // 1. Return this.[[closedPromise]]. + return m_closed_promise->promise(); +} + +// https://streams.spec.whatwg.org/#default-writer-desired-size +WebIDL::ExceptionOr<Optional<double>> WritableStreamDefaultWriter::desired_size() const +{ + // 1. If this.[[stream]] is undefined, throw a TypeError exception. + if (!m_stream) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot get desired size of writer that has no locked stream"sv }; + + // 2. Return ! WritableStreamDefaultWriterGetDesiredSize(this). + return writable_stream_default_writer_get_desired_size(*this); +} + +// https://streams.spec.whatwg.org/#default-writer-ready +JS::GCPtr<JS::Object> WritableStreamDefaultWriter::ready() +{ + // 1. Return this.[[readyPromise]]. + return m_ready_promise->promise(); +} + +WritableStreamDefaultWriter::WritableStreamDefaultWriter(JS::Realm& realm) + : Bindings::PlatformObject(realm) +{ +} + +JS::ThrowCompletionOr<void> WritableStreamDefaultWriter::initialize(JS::Realm& realm) +{ + MUST_OR_THROW_OOM(Base::initialize(realm)); + set_prototype(&Bindings::ensure_web_prototype<Bindings::WritableStreamDefaultWriterPrototype>(realm, "WritableStreamDefaultWriter")); + + return {}; +} + +void WritableStreamDefaultWriter::visit_edges(Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_closed_promise); + visitor.visit(m_ready_promise); + visitor.visit(m_stream); +} + +} diff --git a/Userland/Libraries/LibWeb/Streams/WritableStreamDefaultWriter.h b/Userland/Libraries/LibWeb/Streams/WritableStreamDefaultWriter.h new file mode 100644 index 0000000000..dff7c269c3 --- /dev/null +++ b/Userland/Libraries/LibWeb/Streams/WritableStreamDefaultWriter.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/Function.h> +#include <AK/SinglyLinkedList.h> +#include <LibJS/Forward.h> +#include <LibWeb/Bindings/PlatformObject.h> +#include <LibWeb/Forward.h> +#include <LibWeb/WebIDL/Promise.h> + +namespace Web::Streams { + +// https://streams.spec.whatwg.org/#writablestreamdefaultwriter +class WritableStreamDefaultWriter final : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(WritableStreamDefaultWriter, Bindings::PlatformObject); + +public: + static WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStreamDefaultWriter>> construct_impl(JS::Realm&, JS::NonnullGCPtr<WritableStream>); + + virtual ~WritableStreamDefaultWriter() override = default; + + JS::GCPtr<JS::Object> closed(); + WebIDL::ExceptionOr<Optional<double>> desired_size() const; + JS::GCPtr<JS::Object> ready(); + + JS::GCPtr<WebIDL::Promise> closed_promise() { return m_closed_promise; } + void set_closed_promise(JS::GCPtr<WebIDL::Promise> value) { m_closed_promise = value; } + + JS::GCPtr<WebIDL::Promise> ready_promise() { return m_ready_promise; } + void set_ready_promise(JS::GCPtr<WebIDL::Promise> value) { m_ready_promise = value; } + + JS::GCPtr<WritableStream const> stream() const { return m_stream; } + void set_stream(JS::GCPtr<WritableStream> value) { m_stream = value; } + +private: + explicit WritableStreamDefaultWriter(JS::Realm&); + + virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override; + + virtual void visit_edges(Cell::Visitor&) override; + + // https://streams.spec.whatwg.org/#writablestreamdefaultwriter-closedpromise + // A promise returned by the writer’s closed getter + JS::GCPtr<WebIDL::Promise> m_closed_promise; + + // https://streams.spec.whatwg.org/#writablestreamdefaultwriter-readypromise + // A promise returned by the writer’s ready getter + JS::GCPtr<WebIDL::Promise> m_ready_promise; + + // https://streams.spec.whatwg.org/#writablestreamdefaultwriter-stream + // A WritableStream instance that owns this reader + JS::GCPtr<WritableStream> m_stream; +}; + +} diff --git a/Userland/Libraries/LibWeb/Streams/WritableStreamDefaultWriter.idl b/Userland/Libraries/LibWeb/Streams/WritableStreamDefaultWriter.idl new file mode 100644 index 0000000000..dd6fff7e21 --- /dev/null +++ b/Userland/Libraries/LibWeb/Streams/WritableStreamDefaultWriter.idl @@ -0,0 +1,16 @@ +#import <Streams/WritableStream.idl> + +[Exposed=*] +interface WritableStreamDefaultWriter { + constructor(WritableStream stream); + + readonly attribute Promise<undefined> closed; + readonly attribute unrestricted double? desiredSize; + readonly attribute Promise<undefined> ready; + + // FIXME: + // Promise<undefined> abort(optional any reason); + // Promise<undefined> close(); + // undefined releaseLock(); + // Promise<undefined> write(optional any chunk); +}; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index 79e9020d2f..3f4461e2c0 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -180,6 +180,7 @@ libweb_js_bindings(Streams/ReadableStream) libweb_js_bindings(Streams/ReadableStreamDefaultController) libweb_js_bindings(Streams/ReadableStreamDefaultReader) libweb_js_bindings(Streams/WritableStream) +libweb_js_bindings(Streams/WritableStreamDefaultWriter) libweb_js_bindings(SVG/SVGAnimatedLength) libweb_js_bindings(SVG/SVGClipPathElement) libweb_js_bindings(SVG/SVGDefsElement) |