LibWeb: Add ByteStreamController to ReadableStreamController type

This commit is contained in:
Matthew Olsson 2023-04-08 12:25:32 -07:00 committed by Linus Groh
parent bd7809cc18
commit f9d6a161e8
Notes: sideshowbarker 2024-07-17 07:11:12 +09:00
7 changed files with 23 additions and 16 deletions

View file

@ -84,7 +84,9 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> readable_stream_cancel(Re
(void)reader;
// 7. Let sourceCancelPromise be ! stream.[[controller]].[[CancelSteps]](reason).
auto source_cancel_promise = MUST(stream.controller()->cancel_steps(reason));
auto source_cancel_promise = TRY(stream.controller()->visit([&](auto const& controller) {
return controller->cancel_steps(reason);
}));
// 8. Return the result of reacting to sourceCancelPromise with a fulfillment step that returns undefined.
auto react_result = WebIDL::react_to_promise(*source_cancel_promise,
@ -319,7 +321,7 @@ WebIDL::ExceptionOr<void> readable_stream_reader_generic_release(ReadableStreamG
WebIDL::mark_promise_as_handled(*reader.closed_promise_capability());
// 7. Perform ! stream.[[controller]].[[ReleaseSteps]]().
stream->controller()->release_steps();
TRY(stream->controller()->visit([](auto const& controller) { return controller->release_steps(); }));
// 8. Set stream.[[reader]] to undefined.
stream->set_reader({});
@ -345,7 +347,7 @@ void readable_stream_default_reader_error_read_requests(ReadableStreamDefaultRea
}
// https://streams.spec.whatwg.org/#readable-stream-default-reader-read
void readable_stream_default_reader_read(ReadableStreamDefaultReader& reader, ReadRequest& read_request)
WebIDL::ExceptionOr<void> readable_stream_default_reader_read(ReadableStreamDefaultReader& reader, ReadRequest& read_request)
{
// 1. Let stream be reader.[[stream]].
auto stream = reader.stream();
@ -370,8 +372,12 @@ void readable_stream_default_reader_read(ReadableStreamDefaultReader& reader, Re
VERIFY(stream->is_readable());
// 2. Perform ! stream.[[controller]].[[PullSteps]](readRequest).
MUST(stream->controller()->pull_steps(read_request));
TRY(stream->controller()->visit([&](auto const& controller) {
return controller->pull_steps(read_request);
}));
}
return {};
}
// https://streams.spec.whatwg.org/#abstract-opdef-readablestreamdefaultreaderrelease
@ -635,7 +641,7 @@ WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller(ReadableStre
auto& realm = stream.realm();
// 1. Assert: stream.[[controller]] is undefined.
VERIFY(!stream.controller());
VERIFY(!stream.controller().has_value());
// 2. Set controller.[[stream]] to stream.
controller.set_stream(stream);
@ -660,7 +666,7 @@ WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller(ReadableStre
controller.set_cancel_algorithm(move(cancel_algorithm));
// 8. Set stream.[[controller]] to controller.
stream.set_controller(controller);
stream.set_controller(ReadableStreamController { controller });
// 9. Let startResult be the result of performing startAlgorithm. (This might throw an exception.)
auto start_result = TRY(start_algorithm());

View file

@ -38,7 +38,7 @@ void readable_stream_reader_generic_initialize(ReadableStreamGenericReaderMixin&
WebIDL::ExceptionOr<void> readable_stream_reader_generic_release(ReadableStreamGenericReaderMixin&);
void readable_stream_default_reader_error_read_requests(ReadableStreamDefaultReader&, JS::Value error);
void readable_stream_default_reader_read(ReadableStreamDefaultReader&, ReadRequest&);
WebIDL::ExceptionOr<void> readable_stream_default_reader_read(ReadableStreamDefaultReader&, ReadRequest&);
WebIDL::ExceptionOr<void> readable_stream_default_reader_release(ReadableStreamDefaultReader&);
WebIDL::ExceptionOr<void> set_up_readable_stream_default_reader(ReadableStreamDefaultReader&, ReadableStream&);
void readable_stream_default_controller_close(ReadableStreamDefaultController&);

View file

@ -7,6 +7,7 @@
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Streams/AbstractOperations.h>
#include <LibWeb/Streams/ReadableByteStreamController.h>
#include <LibWeb/Streams/ReadableStream.h>
#include <LibWeb/Streams/ReadableStreamDefaultController.h>
#include <LibWeb/Streams/ReadableStreamDefaultReader.h>
@ -107,7 +108,7 @@ JS::ThrowCompletionOr<void> ReadableStream::initialize(JS::Realm& realm)
void ReadableStream::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_controller);
m_controller->visit([&](auto& controller) { visitor.visit(controller); });
visitor.visit(m_stored_error);
visitor.visit(m_reader);
}

View file

@ -17,9 +17,8 @@ namespace Web::Streams {
// https://streams.spec.whatwg.org/#typedefdef-readablestreamreader
using ReadableStreamReader = JS::GCPtr<ReadableStreamDefaultReader>;
// FIXME: Variant<DefaultController, ByteStreamController>
// https://streams.spec.whatwg.org/#typedefdef-readablestreamcontroller
using ReadableStreamController = JS::GCPtr<ReadableStreamDefaultController>;
using ReadableStreamController = Variant<JS::NonnullGCPtr<ReadableStreamDefaultController>, JS::NonnullGCPtr<ReadableByteStreamController>>;
// https://streams.spec.whatwg.org/#readablestream
class ReadableStream final : public Bindings::PlatformObject {
@ -40,8 +39,8 @@ public:
WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> cancel(JS::Value view);
WebIDL::ExceptionOr<ReadableStreamReader> get_reader();
ReadableStreamController controller() { return m_controller; }
void set_controller(ReadableStreamController value) { m_controller = value; }
Optional<ReadableStreamController>& controller() { return m_controller; }
void set_controller(Optional<ReadableStreamController> value) { m_controller = move(value); }
JS::Value stored_error() const { return m_stored_error; }
void set_stored_error(JS::Value value) { m_stored_error = value; }
@ -68,7 +67,7 @@ private:
// https://streams.spec.whatwg.org/#readablestream-controller
// A ReadableStreamDefaultController or ReadableByteStreamController created with the ability to control the state and queue of this stream
ReadableStreamController m_controller;
Optional<ReadableStreamController> m_controller;
// https://streams.spec.whatwg.org/#readablestream-detached
// A boolean flag set to true when the stream is transferred

View file

@ -118,9 +118,10 @@ WebIDL::ExceptionOr<void> ReadableStreamDefaultController::pull_steps(Web::Strea
}
// https://streams.spec.whatwg.org/#abstract-opdef-readablestreamdefaultcontroller-releasesteps
void ReadableStreamDefaultController::release_steps()
WebIDL::ExceptionOr<void> ReadableStreamDefaultController::release_steps()
{
// 1. Return.
return {};
}
JS::ThrowCompletionOr<void> ReadableStreamDefaultController::initialize(JS::Realm& realm)

View file

@ -65,7 +65,7 @@ public:
WebIDL::ExceptionOr<JS::GCPtr<WebIDL::Promise>> cancel_steps(JS::Value reason);
WebIDL::ExceptionOr<void> pull_steps(ReadRequest&);
void release_steps();
WebIDL::ExceptionOr<void> release_steps();
private:
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;

View file

@ -102,7 +102,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> ReadableStreamDefaultReader::
auto read_request = adopt_ref(*new DefaultReaderReadRequest(realm, promise_capability));
// 4. Perform ! ReadableStreamDefaultReaderRead(this, readRequest).
readable_stream_default_reader_read(*this, read_request);
TRY(readable_stream_default_reader_read(*this, read_request));
// 5. Return promise.
return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise_capability->promise()) };