ReadableStream.cpp 2.2 KB

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