ReadableByteStreamController.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Streams/ReadableByteStreamController.h>
  9. #include <LibWeb/Streams/ReadableStream.h>
  10. #include <LibWeb/Streams/ReadableStreamBYOBRequest.h>
  11. #include <LibWeb/Streams/ReadableStreamDefaultReader.h>
  12. namespace Web::Streams {
  13. JS_DEFINE_ALLOCATOR(ReadableByteStreamController);
  14. // https://streams.spec.whatwg.org/#rbs-controller-desired-size
  15. Optional<double> ReadableByteStreamController::desired_size() const
  16. {
  17. // 1. Return ! ReadableByteStreamControllerGetDesiredSize(this).
  18. return readable_byte_stream_controller_get_desired_size(*this);
  19. }
  20. // https://streams.spec.whatwg.org/#rbs-controller-close
  21. WebIDL::ExceptionOr<void> ReadableByteStreamController::close()
  22. {
  23. // 1. If this.[[closeRequested]] is true, throw a TypeError exception.
  24. if (m_close_requested)
  25. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Controller is already closed"sv };
  26. // 2. If this.[[stream]].[[state]] is not "readable", throw a TypeError exception.
  27. if (m_stream->state() != ReadableStream::State::Readable) {
  28. auto message = m_stream->state() == ReadableStream::State::Closed ? "Cannot close a closed stream"sv : "Cannot close an errored stream"sv;
  29. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, message };
  30. }
  31. // 3. Perform ? ReadableByteStreamControllerClose(this).
  32. TRY(readable_byte_stream_controller_close(*this));
  33. return {};
  34. }
  35. // https://streams.spec.whatwg.org/#rbs-controller-error
  36. void ReadableByteStreamController::error(JS::Value error)
  37. {
  38. // 1. Perform ! ReadableByteStreamControllerError(this, e).
  39. readable_byte_stream_controller_error(*this, error);
  40. }
  41. ReadableByteStreamController::ReadableByteStreamController(JS::Realm& realm)
  42. : Bindings::PlatformObject(realm)
  43. {
  44. }
  45. void ReadableByteStreamController::initialize(JS::Realm& realm)
  46. {
  47. Base::initialize(realm);
  48. set_prototype(&Bindings::ensure_web_prototype<Bindings::ReadableByteStreamControllerPrototype>(realm, "ReadableByteStreamController"_fly_string));
  49. }
  50. // https://streams.spec.whatwg.org/#rbs-controller-private-cancel
  51. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> ReadableByteStreamController::cancel_steps(JS::Value reason)
  52. {
  53. // 1. Perform ! ReadableByteStreamControllerClearPendingPullIntos(this).
  54. readable_byte_stream_controller_clear_pending_pull_intos(*this);
  55. // 2. Perform ! ResetQueue(this).
  56. reset_queue(*this);
  57. // 3. Let result be the result of performing this.[[cancelAlgorithm]], passing in reason.
  58. auto result = (*m_cancel_algorithm)(reason);
  59. // 4. Perform ! ReadableByteStreamControllerClearAlgorithms(this).
  60. readable_byte_stream_controller_clear_algorithms(*this);
  61. // 5. Return result.
  62. return result;
  63. }
  64. // https://streams.spec.whatwg.org/#rbs-controller-private-pull
  65. WebIDL::ExceptionOr<void> ReadableByteStreamController::pull_steps(JS::NonnullGCPtr<ReadRequest> read_request)
  66. {
  67. auto& vm = this->vm();
  68. auto& realm = this->realm();
  69. // 1. Let stream be this.[[stream]].
  70. // 2. Assert: ! ReadableStreamHasDefaultReader(stream) is true.
  71. VERIFY(readable_stream_has_default_reader(*m_stream));
  72. // 3. If this.[[queueTotalSize]] > 0,
  73. if (m_queue_total_size > 0) {
  74. // 1. Assert: ! ReadableStreamGetNumReadRequests(stream) is 0.
  75. VERIFY(readable_stream_get_num_read_requests(*m_stream) == 0);
  76. // 2. Perform ! ReadableByteStreamControllerFillReadRequestFromQueue(this, readRequest).
  77. TRY(readable_byte_stream_controller_fill_read_request_from_queue(*this, read_request));
  78. // 3. Return.
  79. return {};
  80. }
  81. // 4. Let autoAllocateChunkSize be this.[[autoAllocateChunkSize]].
  82. // 5. If autoAllocateChunkSize is not undefined,
  83. if (m_auto_allocate_chunk_size.has_value()) {
  84. // 1. Let buffer be Construct(%ArrayBuffer%, « autoAllocateChunkSize »).
  85. auto buffer = JS::ArrayBuffer::create(realm, *m_auto_allocate_chunk_size);
  86. // 2. If buffer is an abrupt completion,
  87. if (buffer.is_throw_completion()) {
  88. // 1. Perform readRequest’s error steps, given buffer.[[Value]].
  89. read_request->on_error(*buffer.throw_completion().value());
  90. // 2. Return.
  91. return {};
  92. }
  93. // 3. Let pullIntoDescriptor be a new pull-into descriptor with buffer buffer.[[Value]], buffer byte length autoAllocateChunkSize, byte offset 0,
  94. // byte length autoAllocateChunkSize, bytes filled 0, element size 1, view constructor %Uint8Array%, and reader type "default".
  95. PullIntoDescriptor pull_into_descriptor {
  96. .buffer = buffer.release_value(),
  97. .buffer_byte_length = *m_auto_allocate_chunk_size,
  98. .byte_offset = 0,
  99. .byte_length = *m_auto_allocate_chunk_size,
  100. .bytes_filled = 0,
  101. .element_size = 1,
  102. .view_constructor = *realm.intrinsics().uint8_array_constructor(),
  103. .reader_type = ReaderType::Default,
  104. };
  105. // 4. Append pullIntoDescriptor to this.[[pendingPullIntos]].
  106. TRY_OR_THROW_OOM(vm, m_pending_pull_intos.try_append(move(pull_into_descriptor)));
  107. }
  108. // 6. Perform ! ReadableStreamAddReadRequest(stream, readRequest).
  109. readable_stream_add_read_request(*m_stream, read_request);
  110. // 7. Perform ! ReadableByteStreamControllerCallPullIfNeeded(this).
  111. return readable_byte_stream_controller_call_pull_if_needed(*this);
  112. }
  113. // https://streams.spec.whatwg.org/#rbs-controller-private-pull
  114. WebIDL::ExceptionOr<void> ReadableByteStreamController::release_steps()
  115. {
  116. auto& vm = this->vm();
  117. // 1. If this.[[pendingPullIntos]] is not empty,
  118. if (!m_pending_pull_intos.is_empty()) {
  119. // 1. Let firstPendingPullInto be this.[[pendingPullIntos]][0].
  120. auto first_pending_pull_into = m_pending_pull_intos.first();
  121. // 2. Set firstPendingPullInto’s reader type to "none".
  122. first_pending_pull_into.reader_type = ReaderType::None;
  123. // 3. Set this.[[pendingPullIntos]] to the list « firstPendingPullInto ».
  124. m_pending_pull_intos.clear();
  125. TRY_OR_THROW_OOM(vm, m_pending_pull_intos.try_append(first_pending_pull_into));
  126. }
  127. return {};
  128. }
  129. void ReadableByteStreamController::visit_edges(Cell::Visitor& visitor)
  130. {
  131. Base::visit_edges(visitor);
  132. visitor.visit(m_byob_request);
  133. for (auto const& pending_pull_into : m_pending_pull_intos) {
  134. visitor.visit(pending_pull_into.buffer);
  135. visitor.visit(pending_pull_into.view_constructor);
  136. }
  137. for (auto const& item : m_queue)
  138. visitor.visit(item.buffer);
  139. visitor.visit(m_stream);
  140. }
  141. }