ReadableByteStreamController.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. ReadableByteStreamController::ReadableByteStreamController(JS::Realm& realm)
  18. : Bindings::PlatformObject(realm)
  19. {
  20. }
  21. // https://streams.spec.whatwg.org/#rbs-controller-private-cancel
  22. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> ReadableByteStreamController::cancel_steps(JS::Value reason)
  23. {
  24. // 1. Perform ! ReadableByteStreamControllerClearPendingPullIntos(this).
  25. readable_byte_stream_controller_clear_pending_pull_intos(*this);
  26. // 2. Perform ! ResetQueue(this).
  27. reset_queue(*this);
  28. // 3. Let result be the result of performing this.[[cancelAlgorithm]], passing in reason.
  29. auto result = (*m_cancel_algorithm)(reason);
  30. // 4. Perform ! ReadableByteStreamControllerClearAlgorithms(this).
  31. readable_byte_stream_controller_clear_algorithms(*this);
  32. // 5. Return result.
  33. return result;
  34. }
  35. // https://streams.spec.whatwg.org/#rbs-controller-private-pull
  36. WebIDL::ExceptionOr<void> ReadableByteStreamController::pull_steps(NonnullRefPtr<ReadRequest> read_request)
  37. {
  38. auto& vm = this->vm();
  39. auto& realm = this->realm();
  40. // 1. Let stream be this.[[stream]].
  41. // 2. Assert: ! ReadableStreamHasDefaultReader(stream) is true.
  42. VERIFY(readable_stream_has_default_reader(*m_stream));
  43. // 3. If this.[[queueTotalSize]] > 0,
  44. if (m_queue_total_size > 0) {
  45. // 1. Assert: ! ReadableStreamGetNumReadRequests(stream) is 0.
  46. VERIFY(readable_stream_get_num_read_requests(*m_stream) == 0);
  47. // 2. Perform ! ReadableByteStreamControllerFillReadRequestFromQueue(this, readRequest).
  48. TRY(readable_byte_stream_controller_fill_read_request_from_queue(*this, read_request));
  49. // 3. Return.
  50. return {};
  51. }
  52. // 4. Let autoAllocateChunkSize be this.[[autoAllocateChunkSize]].
  53. // 5. If autoAllocateChunkSize is not undefined,
  54. if (m_auto_allocate_chunk_size.has_value()) {
  55. // 1. Let buffer be Construct(%ArrayBuffer%, « autoAllocateChunkSize »).
  56. auto buffer = JS::ArrayBuffer::create(realm, *m_auto_allocate_chunk_size);
  57. // 2. If buffer is an abrupt completion,
  58. if (buffer.is_throw_completion()) {
  59. // 1. Perform readRequest’s error steps, given buffer.[[Value]].
  60. read_request->on_error(*buffer.throw_completion().value());
  61. // 2. Return.
  62. return {};
  63. }
  64. // 3. Let pullIntoDescriptor be a new pull-into descriptor with buffer buffer.[[Value]], buffer byte length autoAllocateChunkSize, byte offset 0,
  65. // byte length autoAllocateChunkSize, bytes filled 0, element size 1, view constructor %Uint8Array%, and reader type "default".
  66. PullIntoDescriptor pull_into_descriptor {
  67. .buffer = buffer.release_value(),
  68. .buffer_byte_length = *m_auto_allocate_chunk_size,
  69. .byte_offset = 0,
  70. .byte_length = *m_auto_allocate_chunk_size,
  71. .bytes_filled = 0,
  72. .element_size = 1,
  73. .view_constructor = *realm.intrinsics().uint8_array_constructor(),
  74. .reader_type = ReaderType::Default,
  75. };
  76. // 4. Append pullIntoDescriptor to this.[[pendingPullIntos]].
  77. TRY_OR_THROW_OOM(vm, m_pending_pull_intos.try_append(move(pull_into_descriptor)));
  78. }
  79. // 6. Perform ! ReadableStreamAddReadRequest(stream, readRequest).
  80. readable_stream_add_read_request(*m_stream, read_request);
  81. // 7. Perform ! ReadableByteStreamControllerCallPullIfNeeded(this).
  82. return readable_byte_stream_controller_call_pull_if_needed(*this);
  83. }
  84. // https://streams.spec.whatwg.org/#rbs-controller-private-pull
  85. WebIDL::ExceptionOr<void> ReadableByteStreamController::release_steps()
  86. {
  87. auto& vm = this->vm();
  88. // 1. If this.[[pendingPullIntos]] is not empty,
  89. if (!m_pending_pull_intos.is_empty()) {
  90. // 1. Let firstPendingPullInto be this.[[pendingPullIntos]][0].
  91. auto first_pending_pull_into = m_pending_pull_intos.first();
  92. // 2. Set firstPendingPullInto’s reader type to "none".
  93. first_pending_pull_into.reader_type = ReaderType::None;
  94. // 3. Set this.[[pendingPullIntos]] to the list « firstPendingPullInto ».
  95. m_pending_pull_intos.clear();
  96. TRY_OR_THROW_OOM(vm, m_pending_pull_intos.try_append(first_pending_pull_into));
  97. }
  98. return {};
  99. }
  100. void ReadableByteStreamController::visit_edges(Cell::Visitor& visitor)
  101. {
  102. Base::visit_edges(visitor);
  103. visitor.visit(m_byob_request);
  104. for (auto const& pending_pull_into : m_pending_pull_intos) {
  105. visitor.visit(pending_pull_into.buffer);
  106. visitor.visit(pending_pull_into.view_constructor);
  107. }
  108. for (auto const& item : m_queue)
  109. visitor.visit(item.buffer);
  110. visitor.visit(m_stream);
  111. }
  112. }