ReadableByteStreamController.cpp 8.2 KB

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