ReadableByteStreamController.cpp 6.3 KB

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