ReadableByteStreamController.cpp 8.3 KB

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