LibWeb: Start fleshing out the ReadableStream interface

This is so we can just assume it exists in Fetch APIs (while still
skipping functionality that relies on a full implementation, of
course).
This commit is contained in:
Linus Groh 2022-09-21 23:27:13 +01:00
parent 1ace80235b
commit 87654f5b51
Notes: sideshowbarker 2024-07-18 02:13:10 +09:00
9 changed files with 199 additions and 4 deletions

View file

@ -301,6 +301,8 @@
#include <LibWeb/Bindings/PromiseRejectionEventPrototype.h>
#include <LibWeb/Bindings/RangeConstructor.h>
#include <LibWeb/Bindings/RangePrototype.h>
#include <LibWeb/Bindings/ReadableStreamConstructor.h>
#include <LibWeb/Bindings/ReadableStreamPrototype.h>
#include <LibWeb/Bindings/ResizeObserverConstructor.h>
#include <LibWeb/Bindings/ResizeObserverPrototype.h>
#include <LibWeb/Bindings/SVGAnimatedLengthConstructor.h>
@ -543,6 +545,7 @@
ADD_WINDOW_OBJECT_INTERFACE(ProgressEvent) \
ADD_WINDOW_OBJECT_INTERFACE(PromiseRejectionEvent) \
ADD_WINDOW_OBJECT_INTERFACE(Range) \
ADD_WINDOW_OBJECT_INTERFACE(ReadableStream) \
ADD_WINDOW_OBJECT_INTERFACE(ResizeObserver) \
ADD_WINDOW_OBJECT_INTERFACE(Screen) \
ADD_WINDOW_OBJECT_INTERFACE(Selection) \

View file

@ -364,6 +364,8 @@ set(SOURCES
Platform/TimerSerenity.cpp
RequestIdleCallback/IdleDeadline.cpp
ResizeObserver/ResizeObserver.cpp
Streams/AbstractOperations.cpp
Streams/ReadableStream.cpp
SVG/AttributeNames.cpp
SVG/AttributeParser.cpp
SVG/SVGAnimatedLength.cpp

View file

@ -366,6 +366,14 @@ namespace Web::ResizeObserver {
class ResizeObserver;
}
namespace Web::Selection {
class Selection;
}
namespace Web::Streams {
class ReadableStream;
}
namespace Web::SVG {
class SVGAnimatedLength;
class SVGCircleElement;
@ -384,10 +392,6 @@ class SVGRectElement;
class SVGSVGElement;
}
namespace Web::Selection {
class Selection;
}
namespace Web::WebSockets {
class WebSocket;
}

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Streams/AbstractOperations.h>
#include <LibWeb/Streams/ReadableStream.h>
namespace Web::Streams {
// https://streams.spec.whatwg.org/#is-readable-stream-locked
bool is_readable_stream_locked(ReadableStream const& stream)
{
// 1. If stream.[[reader]] is undefined, return false.
if (stream.reader() == nullptr)
return false;
// 2. Return true.
return true;
}
}

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Forward.h>
namespace Web::Streams {
bool is_readable_stream_locked(ReadableStream const&);
}

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Streams/AbstractOperations.h>
#include <LibWeb/Streams/ReadableStream.h>
namespace Web::Streams {
// https://streams.spec.whatwg.org/#rs-constructor
DOM::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::create_with_global_object(HTML::Window& window)
{
auto* readable_stream = window.heap().allocate<ReadableStream>(window.realm(), window);
return JS::NonnullGCPtr { *readable_stream };
}
ReadableStream::ReadableStream(HTML::Window& window)
: PlatformObject(window.realm())
{
set_prototype(&window.cached_web_prototype("ReadableStream"));
}
ReadableStream::~ReadableStream() = default;
void ReadableStream::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_controller);
visitor.visit(m_reader);
visitor.visit(m_stored_error);
}
// https://streams.spec.whatwg.org/#readablestream-locked
bool ReadableStream::is_readable() const
{
// A ReadableStream stream is readable if stream.[[state]] is "readable".
return m_state == State::Readable;
}
// https://streams.spec.whatwg.org/#readablestream-closed
bool ReadableStream::is_closed() const
{
// A ReadableStream stream is closed if stream.[[state]] is "closed".
return m_state == State::Closed;
}
// https://streams.spec.whatwg.org/#readablestream-errored
bool ReadableStream::is_errored() const
{
// A ReadableStream stream is errored if stream.[[state]] is "errored".
return m_state == State::Errored;
}
// https://streams.spec.whatwg.org/#readablestream-locked
bool ReadableStream::is_locked() const
{
// A ReadableStream stream is locked if ! IsReadableStreamLocked(stream) returns true.
return is_readable_stream_locked(*this);
}
// https://streams.spec.whatwg.org/#is-readable-stream-disturbed
bool ReadableStream::is_disturbed() const
{
// A ReadableStream stream is disturbed if stream.[[disturbed]] is true.
return m_disturbed;
}
}

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Forward.h>
namespace Web::Streams {
// https://streams.spec.whatwg.org/#readablestream
class ReadableStream final : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(Request, Bindings::PlatformObject);
public:
enum class State {
Readable,
Closed,
Errored,
};
static DOM::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> create_with_global_object(HTML::Window&);
virtual ~ReadableStream() override;
JS::GCPtr<JS::Object> controller() const { return m_controller; }
JS::GCPtr<JS::Object> reader() const { return m_reader; }
JS::Value stored_error() const { return m_stored_error; }
bool is_readable() const;
bool is_closed() const;
bool is_errored() const;
bool is_locked() const;
bool is_disturbed() const;
private:
explicit ReadableStream(HTML::Window&);
virtual void visit_edges(Cell::Visitor&) override;
// https://streams.spec.whatwg.org/#readablestream-controller
// A ReadableStreamDefaultController or ReadableByteStreamController created with the ability to control the state and queue of this stream
JS::GCPtr<JS::Object> m_controller;
// https://streams.spec.whatwg.org/#readablestream-detached
// A boolean flag set to true when the stream is transferred
bool m_detached { false };
// https://streams.spec.whatwg.org/#readablestream-disturbed
// A boolean flag set to true when the stream has been read from or canceled
bool m_disturbed { false };
// https://streams.spec.whatwg.org/#readablestream-reader
// A ReadableStreamDefaultReader or ReadableStreamBYOBReader instance, if the stream is locked to a reader, or undefined if it is not
JS::GCPtr<JS::Object> m_reader;
// https://streams.spec.whatwg.org/#readablestream-state
// A string containing the streams current state, used internally; one of "readable", "closed", or "errored"
State m_state { State::Readable };
// https://streams.spec.whatwg.org/#readablestream-storederror
// A value indicating how the stream failed, to be given as a failure reason or exception when trying to operate on an errored stream
JS::Value m_stored_error { JS::js_undefined() };
};
}

View file

@ -0,0 +1,5 @@
// Dummy definition so we can use ReadableStream as a type in Fetch.
// https://streams.spec.whatwg.org/#readablestream
interface ReadableStream {
};

View file

@ -159,6 +159,7 @@ libweb_js_bindings(IntersectionObserver/IntersectionObserver)
libweb_js_bindings(NavigationTiming/PerformanceTiming)
libweb_js_bindings(RequestIdleCallback/IdleDeadline)
libweb_js_bindings(ResizeObserver/ResizeObserver)
libweb_js_bindings(Streams/ReadableStream)
libweb_js_bindings(SVG/SVGAnimatedLength)
libweb_js_bindings(SVG/SVGClipPathElement)
libweb_js_bindings(SVG/SVGDefsElement)