LibWeb: Implement WritableStreamDefaultWriter.releaseLock()

This commit is contained in:
Matthew Olsson 2023-04-02 10:52:01 -07:00 committed by Linus Groh
parent 5f4da20a56
commit 6bbd920008
Notes: sideshowbarker 2024-07-17 00:49:59 +09:00
5 changed files with 69 additions and 1 deletions

View file

@ -1334,6 +1334,25 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_default_w
return writable_stream_close(*stream);
}
// https://streams.spec.whatwg.org/#writable-stream-default-writer-ensure-closed-promise-rejected
void writable_stream_default_writer_ensure_closed_promise_rejected(WritableStreamDefaultWriter& writer, JS::Value error)
{
auto& realm = writer.realm();
// 1. If writer.[[closedPromise]].[[PromiseState]] is "pending", reject writer.[[closedPromise]] with error.
auto& closed_promise = verify_cast<JS::Promise>(*writer.closed_promise()->promise());
if (closed_promise.state() == JS::Promise::State::Pending) {
WebIDL::reject_promise(realm, *writer.closed_promise(), error);
}
// 2. Otherwise, set writer.[[closedPromise]] to a promise rejected with error.
else {
writer.set_closed_promise(WebIDL::create_rejected_promise(realm, error));
}
// 3. Set writer.[[closedPromise]].[[PromiseIsHandled]] to true.
WebIDL::mark_promise_as_handled(*writer.closed_promise());
}
// https://streams.spec.whatwg.org/#writable-stream-default-writer-ensure-ready-promise-rejected
void writable_stream_default_writer_ensure_ready_promise_rejected(WritableStreamDefaultWriter& writer, JS::Value error)
{
@ -1374,6 +1393,36 @@ Optional<double> writable_stream_default_writer_get_desired_size(WritableStreamD
return writable_stream_default_controller_get_desired_size(*stream->controller());
}
// https://streams.spec.whatwg.org/#writable-stream-default-writer-release
WebIDL::ExceptionOr<void> writable_stream_default_writer_release(WritableStreamDefaultWriter& writer)
{
// 1. Let stream be writer.[[stream]].
auto stream = writer.stream();
// 2. Assert: stream is not undefined.
VERIFY(stream);
// 3. Assert: stream.[[writer]] is writer.
VERIFY(stream->writer().ptr() == &writer);
// 4. Let releasedError be a new TypeError.
auto released_error = MUST_OR_THROW_OOM(JS::TypeError::create(writer.realm(), "Writer's stream lock has been released"sv));
// 5. Perform ! WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, releasedError).
writable_stream_default_writer_ensure_ready_promise_rejected(writer, released_error);
// 6. Perform ! WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer, releasedError).
writable_stream_default_writer_ensure_closed_promise_rejected(writer, released_error);
// 7. Set stream.[[writer]] to undefined.
stream->set_writer({});
// 8. Set writer.[[stream]] to undefined.
writer.set_stream({});
return {};
}
// https://streams.spec.whatwg.org/#writable-stream-default-controller-advance-queue-if-needed
WebIDL::ExceptionOr<void> writable_stream_default_controller_advance_queue_if_needed(WritableStreamDefaultController& controller)
{

View file

@ -75,8 +75,10 @@ void writable_stream_update_backpressure(WritableStream&, bool backpressure);
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_default_writer_abort(WritableStreamDefaultWriter&, JS::Value reason);
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_default_writer_close(WritableStreamDefaultWriter&);
void writable_stream_default_writer_ensure_closed_promise_rejected(WritableStreamDefaultWriter&, JS::Value error);
void writable_stream_default_writer_ensure_ready_promise_rejected(WritableStreamDefaultWriter&, JS::Value error);
Optional<double> writable_stream_default_writer_get_desired_size(WritableStreamDefaultWriter const&);
WebIDL::ExceptionOr<void> writable_stream_default_writer_release(WritableStreamDefaultWriter&);
WebIDL::ExceptionOr<void> writable_stream_default_controller_advance_queue_if_needed(WritableStreamDefaultController&);
void writable_stream_default_controller_clear_algorithms(WritableStreamDefaultController&);

View file

@ -87,6 +87,22 @@ WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> WritableStreamDefaultWriter::close()
return TRY(writable_stream_default_writer_close(*this))->promise();
}
// https://streams.spec.whatwg.org/#default-writer-release-lock
WebIDL::ExceptionOr<void> WritableStreamDefaultWriter::release_lock()
{
// 1. Let stream be this.[[stream]].
// 2. If stream is undefined, return.
if (!m_stream)
return {};
// 3. Assert: stream.[[writer]] is not undefined.
VERIFY(m_stream->writer());
// 4. Perform ! WritableStreamDefaultWriterRelease(this).
return writable_stream_default_writer_release(*this);
}
WritableStreamDefaultWriter::WritableStreamDefaultWriter(JS::Realm& realm)
: Bindings::PlatformObject(realm)
{

View file

@ -29,6 +29,7 @@ public:
JS::GCPtr<JS::Object> ready();
WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> abort(JS::Value reason);
WebIDL::ExceptionOr<JS::GCPtr<JS::Object>> close();
WebIDL::ExceptionOr<void> release_lock();
JS::GCPtr<WebIDL::Promise> closed_promise() { return m_closed_promise; }
void set_closed_promise(JS::GCPtr<WebIDL::Promise> value) { m_closed_promise = value; }

View file

@ -10,8 +10,8 @@ interface WritableStreamDefaultWriter {
Promise<undefined> abort(optional any reason);
Promise<undefined> close();
undefined releaseLock();
// FIXME:
// undefined releaseLock();
// Promise<undefined> write(optional any chunk);
};