LibWeb: Implement ReadableByteStreamController.close()

This commit is contained in:
Matthew Olsson 2023-04-09 10:40:15 -07:00 committed by Andreas Kling
parent ef3810d03d
commit c9be755367
Notes: sideshowbarker 2024-07-16 20:55:30 +09:00
5 changed files with 71 additions and 1 deletions

View File

@ -864,6 +864,54 @@ void readable_byte_stream_controller_clear_pending_pull_intos(ReadableByteStream
controller.pending_pull_intos().clear();
}
// https://streams.spec.whatwg.org/#readable-byte-stream-controller-close
WebIDL::ExceptionOr<void> readable_byte_stream_controller_close(ReadableByteStreamController& controller)
{
auto& realm = controller.realm();
// 1. Let stream be controller.[[stream]].
auto stream = controller.stream();
// 2. If controller.[[closeRequested]] is true or stream.[[state]] is not "readable", return.
if (controller.close_requested() || stream->state() != ReadableStream::State::Readable)
return {};
// 3. If controller.[[queueTotalSize]] > 0,
if (controller.queue_total_size() > 0.0) {
// 1. Set controller.[[closeRequested]] to true.
controller.set_close_requested(true);
// 2. Return.
return {};
}
// 4. If controller.[[pendingPullIntos]] is not empty,
if (!controller.pending_pull_intos().is_empty()) {
// 1. Let firstPendingPullInto be controller.[[pendingPullIntos]][0].
auto& first_pending_pull_into = controller.pending_pull_intos().first();
// 2. If firstPendingPullIntos bytes filled > 0,
if (first_pending_pull_into.bytes_filled > 0) {
// 1. Let e be a new TypeError exception.
auto error = MUST_OR_THROW_OOM(JS::TypeError::create(realm, "Cannot close controller in the middle of processing a write request"sv));
// 2. Perform ! ReadableByteStreamControllerError(controller, e).
readable_byte_stream_controller_error(controller, error);
// 3. Throw e.
return JS::throw_completion(error);
}
}
// 5. Perform ! ReadableByteStreamControllerClearAlgorithms(controller).
readable_byte_stream_controller_clear_algorithms(controller);
// 6. Perform ! ReadableStreamClose(stream).
readable_stream_close(*stream);
return {};
}
// https://streams.spec.whatwg.org/#readable-byte-stream-controller-error
void readable_byte_stream_controller_error(ReadableByteStreamController& controller, JS::Value error)
{

View File

@ -60,6 +60,7 @@ WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller_from_underly
WebIDL::ExceptionOr<void> readable_byte_stream_controller_call_pull_if_needed(ReadableByteStreamController&);
void readable_byte_stream_controller_clear_algorithms(ReadableByteStreamController&);
void readable_byte_stream_controller_clear_pending_pull_intos(ReadableByteStreamController&);
WebIDL::ExceptionOr<void> readable_byte_stream_controller_close(ReadableByteStreamController&);
void readable_byte_stream_controller_error(ReadableByteStreamController&, JS::Value error);
WebIDL::ExceptionOr<void> readable_byte_stream_controller_fill_read_request_from_queue(ReadableByteStreamController&, NonnullRefPtr<ReadRequest>);
Optional<double> readable_byte_stream_controller_get_desired_size(ReadableByteStreamController const&);

View File

@ -18,6 +18,25 @@ Optional<double> ReadableByteStreamController::desired_size() const
return readable_byte_stream_controller_get_desired_size(*this);
}
// https://streams.spec.whatwg.org/#rbs-controller-close
WebIDL::ExceptionOr<void> ReadableByteStreamController::close()
{
// 1. If this.[[closeRequested]] is true, throw a TypeError exception.
if (m_close_requested)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Controller is already closed"sv };
// 2. If this.[[stream]].[[state]] is not "readable", throw a TypeError exception.
if (m_stream->state() != ReadableStream::State::Readable) {
auto message = m_stream->state() == ReadableStream::State::Closed ? "Cannot close a closed stream"sv : "Cannot close an errored stream"sv;
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, message };
}
// 3. Perform ? ReadableByteStreamControllerClose(this).
TRY(readable_byte_stream_controller_close(*this));
return {};
}
ReadableByteStreamController::ReadableByteStreamController(JS::Realm& realm)
: Bindings::PlatformObject(realm)
{

View File

@ -80,6 +80,7 @@ public:
void set_byob_request(JS::GCPtr<ReadableStreamBYOBRequest> request) { m_byob_request = request; }
Optional<double> desired_size() const;
WebIDL::ExceptionOr<void> close();
Optional<u32> const& auto_allocate_chunk_size() { return m_auto_allocate_chunk_size; }
void set_auto_allocate_chunk_size(Optional<u32> value) { m_auto_allocate_chunk_size = value; }

View File

@ -5,8 +5,9 @@ interface ReadableByteStreamController {
readonly attribute ReadableStreamBYOBRequest? byobRequest;
readonly attribute unrestricted double? desiredSize;
undefined close();
// FIXME: Implement
// undefined close();
// undefined enqueue(ArrayBufferView chunk);
// undefined error(optional any e);
};