ReadableStream.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. auto* readable_stream = realm.heap().allocate<ReadableStream>(realm, realm);
  15. return JS::NonnullGCPtr { *readable_stream };
  16. }
  17. ReadableStream::ReadableStream(JS::Realm& realm)
  18. : PlatformObject(realm)
  19. {
  20. set_prototype(&Bindings::cached_web_prototype(realm, "ReadableStream"));
  21. }
  22. ReadableStream::~ReadableStream() = default;
  23. void ReadableStream::visit_edges(Cell::Visitor& visitor)
  24. {
  25. Base::visit_edges(visitor);
  26. visitor.visit(m_controller);
  27. visitor.visit(m_reader);
  28. visitor.visit(m_stored_error);
  29. }
  30. // https://streams.spec.whatwg.org/#readablestream-locked
  31. bool ReadableStream::is_readable() const
  32. {
  33. // A ReadableStream stream is readable if stream.[[state]] is "readable".
  34. return m_state == State::Readable;
  35. }
  36. // https://streams.spec.whatwg.org/#readablestream-closed
  37. bool ReadableStream::is_closed() const
  38. {
  39. // A ReadableStream stream is closed if stream.[[state]] is "closed".
  40. return m_state == State::Closed;
  41. }
  42. // https://streams.spec.whatwg.org/#readablestream-errored
  43. bool ReadableStream::is_errored() const
  44. {
  45. // A ReadableStream stream is errored if stream.[[state]] is "errored".
  46. return m_state == State::Errored;
  47. }
  48. // https://streams.spec.whatwg.org/#readablestream-locked
  49. bool ReadableStream::is_locked() const
  50. {
  51. // A ReadableStream stream is locked if ! IsReadableStreamLocked(stream) returns true.
  52. return is_readable_stream_locked(*this);
  53. }
  54. // https://streams.spec.whatwg.org/#is-readable-stream-disturbed
  55. bool ReadableStream::is_disturbed() const
  56. {
  57. // A ReadableStream stream is disturbed if stream.[[disturbed]] is true.
  58. return m_disturbed;
  59. }
  60. }