AbstractOperations.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
  4. * Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
  5. * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <LibJS/Heap/GCPtr.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/Streams/ReadableStream.h>
  13. #include <LibWeb/WebIDL/CallbackType.h>
  14. #include <LibWeb/WebIDL/ExceptionOr.h>
  15. #include <LibWeb/WebIDL/Promise.h>
  16. namespace Web::Streams {
  17. using SizeAlgorithm = JS::SafeFunction<JS::Completion(JS::Value)>;
  18. using PullAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>>()>;
  19. using CancelAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>>(JS::Value)>;
  20. using StartAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::Value>()>;
  21. using AbortAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>>(JS::Value)>;
  22. using CloseAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>>()>;
  23. using WriteAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>>(JS::Value)>;
  24. using FlushAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>>()>;
  25. using TransformAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>>(JS::Value)>;
  26. WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStreamDefaultReader>> acquire_readable_stream_default_reader(ReadableStream&);
  27. WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStreamBYOBReader>> acquire_readable_stream_byob_reader(ReadableStream&);
  28. bool is_readable_stream_locked(ReadableStream const&);
  29. SizeAlgorithm extract_size_algorithm(QueuingStrategy const&);
  30. WebIDL::ExceptionOr<double> extract_high_water_mark(QueuingStrategy const&, double default_hwm);
  31. void readable_stream_close(ReadableStream&);
  32. void readable_stream_error(ReadableStream&, JS::Value error);
  33. void readable_stream_add_read_request(ReadableStream&, ReadRequest const&);
  34. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> readable_stream_cancel(ReadableStream&, JS::Value reason);
  35. void readable_stream_fulfill_read_request(ReadableStream&, JS::Value chunk, bool done);
  36. size_t readable_stream_get_num_read_into_requests(ReadableStream const&);
  37. size_t readable_stream_get_num_read_requests(ReadableStream const&);
  38. bool readable_stream_has_byob_reader(ReadableStream const&);
  39. bool readable_stream_has_default_reader(ReadableStream const&);
  40. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> readable_stream_reader_generic_cancel(ReadableStreamGenericReaderMixin&, JS::Value reason);
  41. void readable_stream_reader_generic_initialize(ReadableStreamReader, ReadableStream&);
  42. WebIDL::ExceptionOr<void> readable_stream_reader_generic_release(ReadableStreamGenericReaderMixin&);
  43. void readable_stream_default_reader_error_read_requests(ReadableStreamDefaultReader&, JS::Value error);
  44. WebIDL::ExceptionOr<void> readable_stream_default_reader_read(ReadableStreamDefaultReader&, ReadRequest&);
  45. WebIDL::ExceptionOr<void> readable_stream_default_reader_release(ReadableStreamDefaultReader&);
  46. WebIDL::ExceptionOr<void> set_up_readable_stream_default_reader(ReadableStreamDefaultReader&, ReadableStream&);
  47. WebIDL::ExceptionOr<void> set_up_readable_stream_byob_reader(ReadableStreamBYOBReader&, ReadableStream&);
  48. void readable_stream_default_controller_close(ReadableStreamDefaultController&);
  49. bool readable_stream_default_controller_has_backpressure(ReadableStreamDefaultController&);
  50. WebIDL::ExceptionOr<void> readable_stream_default_controller_enqueue(ReadableStreamDefaultController&, JS::Value chunk);
  51. WebIDL::ExceptionOr<void> readable_stream_default_controller_can_pull_if_needed(ReadableStreamDefaultController&);
  52. bool readable_stream_default_controller_should_call_pull(ReadableStreamDefaultController&);
  53. void readable_stream_default_controller_clear_algorithms(ReadableStreamDefaultController&);
  54. void readable_stream_default_controller_error(ReadableStreamDefaultController&, JS::Value error);
  55. Optional<double> readable_stream_default_controller_get_desired_size(ReadableStreamDefaultController&);
  56. bool readable_stream_default_controller_can_close_or_enqueue(ReadableStreamDefaultController&);
  57. WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller(ReadableStream&, ReadableStreamDefaultController&, StartAlgorithm&&, PullAlgorithm&&, CancelAlgorithm&&, double high_water_mark, SizeAlgorithm&&);
  58. WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller_from_underlying_source(ReadableStream&, JS::Value underlying_source_value, UnderlyingSource, double high_water_mark, SizeAlgorithm&&);
  59. WebIDL::ExceptionOr<void> set_up_readable_stream_controller_with_byte_reading_support(ReadableStream&, Optional<PullAlgorithm>&& = {}, Optional<CancelAlgorithm>&& = {}, double high_water_mark = 0);
  60. WebIDL::ExceptionOr<void> set_up_readable_byte_stream_controller(ReadableStream&, ReadableByteStreamController&, StartAlgorithm&&, PullAlgorithm&&, CancelAlgorithm&&, double high_water_mark, JS::Value auto_allocate_chunk_size);
  61. WebIDL::ExceptionOr<void> set_up_readable_byte_stream_controller_from_underlying_source(ReadableStream&, JS::Value underlying_source, UnderlyingSource const& underlying_source_dict, double high_water_mark);
  62. WebIDL::ExceptionOr<void> readable_stream_enqueue(ReadableStreamController& controller, JS::Value chunk);
  63. WebIDL::ExceptionOr<void> readable_byte_stream_controller_enqueue(ReadableByteStreamController& controller, JS::Value chunk);
  64. WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> transfer_array_buffer(JS::Realm& realm, JS::ArrayBuffer& buffer);
  65. WebIDL::ExceptionOr<void> readable_byte_stream_controller_enqueue_detached_pull_into_queue(ReadableByteStreamController& controller, PullIntoDescriptor& pull_into_descriptor);
  66. WebIDL::ExceptionOr<void> readable_byte_stream_controller_process_read_requests_using_queue(ReadableByteStreamController& controller);
  67. void readable_byte_stream_controller_enqueue_chunk_to_queue(ReadableByteStreamController& controller, JS::NonnullGCPtr<JS::ArrayBuffer> buffer, u32 byte_offset, u32 byte_length);
  68. WebIDL::ExceptionOr<void> readable_byte_stream_controller_enqueue_cloned_chunk_to_queue(ReadableByteStreamController& controller, JS::ArrayBuffer& buffer, u64 byte_offset, u64 byte_length);
  69. PullIntoDescriptor readable_byte_stream_controller_shift_pending_pull_into(ReadableByteStreamController& controller);
  70. WebIDL::ExceptionOr<void> readable_byte_stream_controller_call_pull_if_needed(ReadableByteStreamController&);
  71. void readable_byte_stream_controller_clear_algorithms(ReadableByteStreamController&);
  72. void readable_byte_stream_controller_clear_pending_pull_intos(ReadableByteStreamController&);
  73. WebIDL::ExceptionOr<void> readable_byte_stream_controller_close(ReadableByteStreamController&);
  74. void readable_byte_stream_controller_error(ReadableByteStreamController&, JS::Value error);
  75. WebIDL::ExceptionOr<void> readable_byte_stream_controller_fill_read_request_from_queue(ReadableByteStreamController&, NonnullRefPtr<ReadRequest>);
  76. Optional<double> readable_byte_stream_controller_get_desired_size(ReadableByteStreamController const&);
  77. WebIDL::ExceptionOr<void> readable_byte_stream_controller_handle_queue_drain(ReadableByteStreamController&);
  78. void readable_byte_stream_controller_invalidate_byob_request(ReadableByteStreamController&);
  79. bool readable_byte_stream_controller_should_call_pull(ReadableByteStreamController const&);
  80. WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> create_readable_stream(JS::Realm& realm, StartAlgorithm&& start_algorithm, PullAlgorithm&& pull_algorithm, CancelAlgorithm&& cancel_algorithm, Optional<double> high_water_mark = {}, Optional<SizeAlgorithm>&& size_algorithm = {});
  81. WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStream>> create_writable_stream(JS::Realm& realm, StartAlgorithm&& start_algorithm, WriteAlgorithm&& write_algorithm, CloseAlgorithm&& close_algorithm, AbortAlgorithm&& abort_algorithm, double high_water_mark, SizeAlgorithm&& size_algorithm);
  82. void initialize_readable_stream(ReadableStream&);
  83. void initialize_writable_stream(WritableStream&);
  84. WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStreamDefaultWriter>> acquire_writable_stream_default_writer(WritableStream&);
  85. bool is_writable_stream_locked(WritableStream const&);
  86. WebIDL::ExceptionOr<void> set_up_writable_stream_default_writer(WritableStreamDefaultWriter&, WritableStream&);
  87. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_abort(WritableStream&, JS::Value reason);
  88. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_close(WritableStream&);
  89. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_add_write_request(WritableStream&);
  90. bool writable_stream_close_queued_or_in_flight(WritableStream const&);
  91. WebIDL::ExceptionOr<void> writable_stream_deal_with_rejection(WritableStream&, JS::Value error);
  92. WebIDL::ExceptionOr<void> writable_stream_finish_erroring(WritableStream&);
  93. void writable_stream_finish_in_flight_close(WritableStream&);
  94. WebIDL::ExceptionOr<void> writable_stream_finish_in_flight_close_with_error(WritableStream&, JS::Value error);
  95. void writable_stream_finish_in_flight_write(WritableStream&);
  96. WebIDL::ExceptionOr<void> writable_stream_finish_in_flight_write_with_error(WritableStream&, JS::Value error);
  97. bool writable_stream_has_operation_marked_in_flight(WritableStream const&);
  98. void writable_stream_mark_close_request_in_flight(WritableStream&);
  99. void writable_stream_mark_first_write_request_in_flight(WritableStream&);
  100. void writable_stream_reject_close_and_closed_promise_if_needed(WritableStream&);
  101. WebIDL::ExceptionOr<void> writable_stream_start_erroring(WritableStream&, JS::Value reason);
  102. void writable_stream_update_backpressure(WritableStream&, bool backpressure);
  103. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_default_writer_abort(WritableStreamDefaultWriter&, JS::Value reason);
  104. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_default_writer_close(WritableStreamDefaultWriter&);
  105. void writable_stream_default_writer_ensure_closed_promise_rejected(WritableStreamDefaultWriter&, JS::Value error);
  106. void writable_stream_default_writer_ensure_ready_promise_rejected(WritableStreamDefaultWriter&, JS::Value error);
  107. Optional<double> writable_stream_default_writer_get_desired_size(WritableStreamDefaultWriter const&);
  108. WebIDL::ExceptionOr<void> writable_stream_default_writer_release(WritableStreamDefaultWriter&);
  109. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_default_writer_write(WritableStreamDefaultWriter&, JS::Value chunk);
  110. WebIDL::ExceptionOr<void> set_up_writable_stream_default_controller(WritableStream&, WritableStreamDefaultController&, StartAlgorithm&&, WriteAlgorithm&&, CloseAlgorithm&&, AbortAlgorithm&&, double high_water_mark, SizeAlgorithm&&);
  111. WebIDL::ExceptionOr<void> set_up_writable_stream_default_controller_from_underlying_sink(WritableStream&, JS::Value underlying_sink_value, UnderlyingSink&, double high_water_mark, SizeAlgorithm&& size_algorithm);
  112. WebIDL::ExceptionOr<void> writable_stream_default_controller_advance_queue_if_needed(WritableStreamDefaultController&);
  113. void writable_stream_default_controller_clear_algorithms(WritableStreamDefaultController&);
  114. WebIDL::ExceptionOr<void> writable_stream_default_controller_close(WritableStreamDefaultController&);
  115. WebIDL::ExceptionOr<void> writable_stream_default_controller_error(WritableStreamDefaultController&, JS::Value error);
  116. WebIDL::ExceptionOr<void> writable_stream_default_controller_error_if_needed(WritableStreamDefaultController&, JS::Value error);
  117. bool writable_stream_default_controller_get_backpressure(WritableStreamDefaultController const&);
  118. WebIDL::ExceptionOr<JS::Value> writable_stream_default_controller_get_chunk_size(WritableStreamDefaultController&, JS::Value chunk);
  119. double writable_stream_default_controller_get_desired_size(WritableStreamDefaultController const&);
  120. WebIDL::ExceptionOr<void> writable_stream_default_controller_process_close(WritableStreamDefaultController&);
  121. WebIDL::ExceptionOr<void> writable_stream_default_controller_process_write(WritableStreamDefaultController&, JS::Value chunk);
  122. WebIDL::ExceptionOr<void> writable_stream_default_controller_write(WritableStreamDefaultController&, JS::Value chunk, JS::Value chunk_size);
  123. void transform_stream_default_controller_clear_algorithms(TransformStreamDefaultController&);
  124. WebIDL::ExceptionOr<void> transform_stream_default_controller_enqueue(TransformStreamDefaultController&, JS::Value chunk);
  125. WebIDL::ExceptionOr<void> transform_stream_default_controller_error(TransformStreamDefaultController&, JS::Value error);
  126. WebIDL::ExceptionOr<void> transform_stream_default_controller_terminate(TransformStreamDefaultController&);
  127. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> transform_stream_default_controller_perform_transform(TransformStreamDefaultController&, JS::Value chunk);
  128. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> transform_stream_default_sink_abort_algorithm(TransformStream&, JS::Value reason);
  129. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> transform_stream_default_sink_close_algorithm(TransformStream&);
  130. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> transform_stream_default_sink_write_algorithm(TransformStream&, JS::Value chunk);
  131. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> transform_stream_default_source_pull_algorithm(TransformStream&);
  132. WebIDL::ExceptionOr<void> transform_stream_error(TransformStream&, JS::Value error);
  133. WebIDL::ExceptionOr<void> transform_stream_error_writable_and_unblock_write(TransformStream&, JS::Value error);
  134. WebIDL::ExceptionOr<void> transform_stream_set_backpressure(TransformStream&, bool backpressure);
  135. bool is_non_negative_number(JS::Value);
  136. JS::Value create_close_sentinel();
  137. bool is_close_sentinel(JS::Value);
  138. JS::ThrowCompletionOr<JS::Handle<WebIDL::CallbackType>> property_to_callback(JS::VM& vm, JS::Value value, JS::PropertyKey const& property_key, WebIDL::OperationReturnsPromise);
  139. // https://streams.spec.whatwg.org/#value-with-size
  140. struct ValueWithSize {
  141. JS::Value value;
  142. double size;
  143. };
  144. // https://streams.spec.whatwg.org/#dequeue-value
  145. template<typename T>
  146. JS::Value dequeue_value(T& container)
  147. {
  148. // 1. Assert: container has [[queue]] and [[queueTotalSize]] internal slots.
  149. // 2. Assert: container.[[queue]] is not empty.
  150. VERIFY(!container.queue().is_empty());
  151. // 3. Let valueWithSize be container.[[queue]][0].
  152. // 4. Remove valueWithSize from container.[[queue]].
  153. auto value_with_size = container.queue().take_first();
  154. // 5. Set container.[[queueTotalSize]] to container.[[queueTotalSize]] − valueWithSize’s size.
  155. container.set_queue_total_size(container.queue_total_size() - value_with_size.size);
  156. // 6. If container.[[queueTotalSize]] < 0, set container.[[queueTotalSize]] to 0. (This can occur due to rounding errors.)
  157. if (container.queue_total_size() < 0.0)
  158. container.set_queue_total_size(0.0);
  159. // 7. Return valueWithSize’s value.
  160. return value_with_size.value;
  161. }
  162. // https://streams.spec.whatwg.org/#enqueue-value-with-size
  163. template<typename T>
  164. WebIDL::ExceptionOr<void> enqueue_value_with_size(T& container, JS::Value value, JS::Value size_value)
  165. {
  166. // 1. Assert: container has [[queue]] and [[queueTotalSize]] internal slots.
  167. // 2. If ! IsNonNegativeNumber(size) is false, throw a RangeError exception.
  168. if (!is_non_negative_number(size_value))
  169. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Chunk has non-positive size"sv };
  170. auto size = size_value.as_double();
  171. // 3. If size is +∞, throw a RangeError exception.
  172. if (size == HUGE_VAL)
  173. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Chunk has infinite size"sv };
  174. // 4. Append a new value-with-size with value value and size size to container.[[queue]].
  175. container.queue().append({ value, size });
  176. // 5. Set container.[[queueTotalSize]] to container.[[queueTotalSize]] + size.
  177. container.set_queue_total_size(container.queue_total_size() + size);
  178. return {};
  179. }
  180. // https://streams.spec.whatwg.org/#peek-queue-value
  181. template<typename T>
  182. JS::Value peek_queue_value(T& container)
  183. {
  184. // 1. Assert: container has [[queue]] and [[queueTotalSize]] internal slots.
  185. // 2. Assert: container.[[queue]] is not empty.
  186. VERIFY(!container.queue().is_empty());
  187. // 3. Let valueWithSize be container.[[queue]][0].
  188. auto& value_with_size = container.queue().first();
  189. // 4. Return valueWithSize’s value.
  190. return value_with_size.value;
  191. }
  192. // https://streams.spec.whatwg.org/#reset-queue
  193. template<typename T>
  194. void reset_queue(T& container)
  195. {
  196. // 1. Assert: container has [[queue]] and [[queueTotalSize]] internal slots.
  197. // 2. Set container.[[queue]] to a new empty list.
  198. container.queue().clear();
  199. // 3. Set container.[[queueTotalSize]] to 0.
  200. container.set_queue_total_size(0);
  201. }
  202. }