ladybird/Userland/Libraries/LibWeb/Streams/ReadableStreamGenericReader.cpp
Andrew Kaster 2c3531ab78 LibWeb: Move JS::Promise <-> WebIDL conversion into IDL
This change also removes as much direct use of JS::Promise in LibWeb
as possible. When specs refer to `Promise<T>` they should be assumed
to be referring to the WebIDL Promise type, not the JS::Promise type.

The one exception is the HostPromiseRejectionTracker hook on the JS
VM. This facility and its associated sets and events are intended to
expose the exact opaque object handles that were rejected to author
code. This is not possible with the WebIDL Promise type, so we have
to use JS::Promise or JS::Object to hold onto the promises.

It also exposes which specs need some updates in the area of
promises. WebDriver stands out in this regard. WebAudio could use
some more cross-references to WebIDL as well to clarify things.
2024-10-25 14:04:21 -06:00

48 lines
1.5 KiB
C++

/*
* Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Promise.h>
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibWeb/Streams/AbstractOperations.h>
#include <LibWeb/Streams/ReadableStream.h>
#include <LibWeb/Streams/ReadableStreamGenericReader.h>
#include <LibWeb/WebIDL/Promise.h>
namespace Web::Streams {
// https://streams.spec.whatwg.org/#generic-reader-closed
JS::GCPtr<WebIDL::Promise> ReadableStreamGenericReaderMixin::closed()
{
// 1. Return this.[[closedPromise]].
return m_closed_promise;
}
// https://streams.spec.whatwg.org/#generic-reader-cancel
JS::NonnullGCPtr<WebIDL::Promise> ReadableStreamGenericReaderMixin::cancel(JS::Value reason)
{
// 1. If this.[[stream]] is undefined, return a promise rejected with a TypeError exception.
if (!m_stream) {
WebIDL::SimpleException exception { WebIDL::SimpleExceptionType::TypeError, "No stream present to cancel"sv };
return WebIDL::create_rejected_promise_from_exception(m_realm, move(exception));
}
// 2. Return ! ReadableStreamReaderGenericCancel(this, reason).
return readable_stream_reader_generic_cancel(*this, reason);
}
ReadableStreamGenericReaderMixin::ReadableStreamGenericReaderMixin(JS::Realm& realm)
: m_realm(realm)
{
}
void ReadableStreamGenericReaderMixin::visit_edges(JS::Cell::Visitor& visitor)
{
visitor.visit(m_closed_promise);
visitor.visit(m_stream);
visitor.visit(m_realm);
}
}