Serenity Operating System
1/*
2 * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibWeb/Bindings/Intrinsics.h>
8#include <LibWeb/Streams/AbstractOperations.h>
9#include <LibWeb/Streams/ReadableStream.h>
10#include <LibWeb/WebIDL/ExceptionOr.h>
11
12namespace Web::Streams {
13
14// https://streams.spec.whatwg.org/#rs-constructor
15WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::construct_impl(JS::Realm& realm)
16{
17 return MUST_OR_THROW_OOM(realm.heap().allocate<ReadableStream>(realm, realm));
18}
19
20ReadableStream::ReadableStream(JS::Realm& realm)
21 : PlatformObject(realm)
22{
23}
24
25ReadableStream::~ReadableStream() = default;
26
27JS::ThrowCompletionOr<void> ReadableStream::initialize(JS::Realm& realm)
28{
29 MUST_OR_THROW_OOM(Base::initialize(realm));
30 set_prototype(&Bindings::ensure_web_prototype<Bindings::ReadableStreamPrototype>(realm, "ReadableStream"));
31
32 return {};
33}
34
35void ReadableStream::visit_edges(Cell::Visitor& visitor)
36{
37 Base::visit_edges(visitor);
38 visitor.visit(m_controller);
39 visitor.visit(m_reader);
40 visitor.visit(m_stored_error);
41}
42
43// https://streams.spec.whatwg.org/#readablestream-locked
44bool ReadableStream::is_readable() const
45{
46 // A ReadableStream stream is readable if stream.[[state]] is "readable".
47 return m_state == State::Readable;
48}
49
50// https://streams.spec.whatwg.org/#readablestream-closed
51bool ReadableStream::is_closed() const
52{
53 // A ReadableStream stream is closed if stream.[[state]] is "closed".
54 return m_state == State::Closed;
55}
56
57// https://streams.spec.whatwg.org/#readablestream-errored
58bool ReadableStream::is_errored() const
59{
60 // A ReadableStream stream is errored if stream.[[state]] is "errored".
61 return m_state == State::Errored;
62}
63// https://streams.spec.whatwg.org/#readablestream-locked
64bool ReadableStream::is_locked() const
65{
66 // A ReadableStream stream is locked if ! IsReadableStreamLocked(stream) returns true.
67 return is_readable_stream_locked(*this);
68}
69
70// https://streams.spec.whatwg.org/#is-readable-stream-disturbed
71bool ReadableStream::is_disturbed() const
72{
73 // A ReadableStream stream is disturbed if stream.[[disturbed]] is true.
74 return m_disturbed;
75}
76
77}