AbstractOperations.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2023, Matthew Olsson <mattco@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibJS/Heap/GCPtr.h>
  9. #include <LibWeb/Forward.h>
  10. #include <LibWeb/WebIDL/ExceptionOr.h>
  11. #include <LibWeb/WebIDL/Promise.h>
  12. namespace Web::Streams {
  13. using SizeAlgorithm = JS::SafeFunction<JS::Completion(JS::Value)>;
  14. using PullAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::GCPtr<WebIDL::Promise>>()>;
  15. using CancelAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::GCPtr<WebIDL::Promise>>(JS::Value)>;
  16. using StartAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::GCPtr<WebIDL::Promise>>()>;
  17. using AbortAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::GCPtr<WebIDL::Promise>>(JS::Value)>;
  18. using CloseAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::GCPtr<WebIDL::Promise>>()>;
  19. using WriteAlgorithm = JS::SafeFunction<WebIDL::ExceptionOr<JS::GCPtr<WebIDL::Promise>>(JS::Value)>;
  20. WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStreamDefaultReader>> acquire_readable_stream_default_reader(ReadableStream&);
  21. bool is_readable_stream_locked(ReadableStream const&);
  22. void readable_stream_close(ReadableStream&);
  23. void readable_stream_error(ReadableStream&, JS::Value error);
  24. void readable_stream_add_read_request(ReadableStream&, ReadRequest const&);
  25. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> readable_stream_cancel(ReadableStream&, JS::Value reason);
  26. void readable_stream_fulfill_read_request(ReadableStream&, JS::Value chunk, bool done);
  27. size_t readable_stream_get_num_read_requests(ReadableStream&);
  28. bool readable_stream_has_default_reader(ReadableStream&);
  29. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> readable_stream_reader_generic_cancel(ReadableStreamGenericReaderMixin&, JS::Value reason);
  30. void readable_stream_reader_generic_initialize(ReadableStreamGenericReaderMixin&, ReadableStream&);
  31. WebIDL::ExceptionOr<void> readable_stream_reader_generic_release(ReadableStreamGenericReaderMixin&);
  32. void readable_stream_default_reader_error_read_requests(ReadableStreamDefaultReader&, JS::Value error);
  33. WebIDL::ExceptionOr<void> readable_stream_default_reader_read(ReadableStreamDefaultReader&, ReadRequest&);
  34. WebIDL::ExceptionOr<void> readable_stream_default_reader_release(ReadableStreamDefaultReader&);
  35. WebIDL::ExceptionOr<void> set_up_readable_stream_default_reader(ReadableStreamDefaultReader&, ReadableStream&);
  36. void readable_stream_default_controller_close(ReadableStreamDefaultController&);
  37. WebIDL::ExceptionOr<void> readable_stream_default_controller_enqueue(ReadableStreamDefaultController&, JS::Value chunk);
  38. WebIDL::ExceptionOr<void> readable_stream_default_controller_can_pull_if_needed(ReadableStreamDefaultController&);
  39. bool readable_stream_default_controller_should_call_pull(ReadableStreamDefaultController&);
  40. void readable_stream_default_controller_clear_algorithms(ReadableStreamDefaultController&);
  41. void readable_stream_default_controller_error(ReadableStreamDefaultController&, JS::Value error);
  42. Optional<float> readable_stream_default_controller_get_desired_size(ReadableStreamDefaultController&);
  43. bool readable_stream_default_controller_can_close_or_enqueue(ReadableStreamDefaultController&);
  44. WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller(ReadableStream&, ReadableStreamDefaultController&, StartAlgorithm&&, PullAlgorithm&&, CancelAlgorithm&&, double high_water_mark, SizeAlgorithm&&);
  45. WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller_from_underlying_source(ReadableStream&, JS::Value underlying_source_value, UnderlyingSource, double high_water_mark, SizeAlgorithm&&);
  46. void readable_byte_stream_controller_clear_algorithms(ReadableByteStreamController&);
  47. void readable_byte_stream_controller_clear_pending_pull_intos(ReadableByteStreamController&);
  48. WebIDL::ExceptionOr<void> readable_byte_stream_controller_fill_read_request_from_queue(ReadableByteStreamController&, NonnullRefPtr<ReadRequest>);
  49. Optional<double> readable_byte_stream_controller_get_desired_size(ReadableByteStreamController const&);
  50. void readable_byte_stream_controller_handle_queue_drain(ReadableByteStreamController&);
  51. void readable_byte_stream_controller_invalidate_byob_request(ReadableByteStreamController&);
  52. WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStreamDefaultWriter>> acquire_writable_stream_default_writer(WritableStream&);
  53. bool is_writable_stream_locked(WritableStream const&);
  54. WebIDL::ExceptionOr<void> set_up_writable_stream_default_writer(WritableStreamDefaultWriter&, WritableStream&);
  55. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_abort(WritableStream&, JS::Value reason);
  56. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_close(WritableStream&);
  57. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_add_write_request(WritableStream&);
  58. bool writable_stream_close_queued_or_in_flight(WritableStream const&);
  59. WebIDL::ExceptionOr<void> writable_stream_deal_with_rejection(WritableStream&, JS::Value error);
  60. WebIDL::ExceptionOr<void> writable_stream_finish_erroring(WritableStream&);
  61. void writable_stream_finish_in_flight_close(WritableStream&);
  62. WebIDL::ExceptionOr<void> writable_stream_finish_in_flight_close_with_error(WritableStream&, JS::Value error);
  63. void writable_stream_finish_in_flight_write(WritableStream&);
  64. WebIDL::ExceptionOr<void> writable_stream_finish_in_flight_write_with_error(WritableStream&, JS::Value error);
  65. bool writable_stream_has_operation_marked_in_flight(WritableStream const&);
  66. void writable_stream_mark_close_request_in_flight(WritableStream&);
  67. void writable_stream_mark_first_write_request_in_flight(WritableStream&);
  68. void writable_stream_reject_close_and_closed_promise_if_needed(WritableStream&);
  69. WebIDL::ExceptionOr<void> writable_stream_start_erroring(WritableStream&, JS::Value reason);
  70. void writable_stream_update_backpressure(WritableStream&, bool backpressure);
  71. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_default_writer_abort(WritableStreamDefaultWriter&, JS::Value reason);
  72. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_default_writer_close(WritableStreamDefaultWriter&);
  73. void writable_stream_default_writer_ensure_closed_promise_rejected(WritableStreamDefaultWriter&, JS::Value error);
  74. void writable_stream_default_writer_ensure_ready_promise_rejected(WritableStreamDefaultWriter&, JS::Value error);
  75. Optional<double> writable_stream_default_writer_get_desired_size(WritableStreamDefaultWriter const&);
  76. WebIDL::ExceptionOr<void> writable_stream_default_writer_release(WritableStreamDefaultWriter&);
  77. WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> writable_stream_default_writer_write(WritableStreamDefaultWriter&, JS::Value chunk);
  78. WebIDL::ExceptionOr<void> set_up_writable_stream_default_controller(WritableStream&, WritableStreamDefaultController&, StartAlgorithm&&, WriteAlgorithm&&, CloseAlgorithm&&, AbortAlgorithm&&, double high_water_mark, SizeAlgorithm&&);
  79. 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);
  80. WebIDL::ExceptionOr<void> writable_stream_default_controller_advance_queue_if_needed(WritableStreamDefaultController&);
  81. void writable_stream_default_controller_clear_algorithms(WritableStreamDefaultController&);
  82. WebIDL::ExceptionOr<void> writable_stream_default_controller_close(WritableStreamDefaultController&);
  83. WebIDL::ExceptionOr<void> writable_stream_default_controller_error(WritableStreamDefaultController&, JS::Value error);
  84. WebIDL::ExceptionOr<void> writable_stream_default_controller_error_if_needed(WritableStreamDefaultController&, JS::Value error);
  85. bool writable_stream_default_controller_get_backpressure(WritableStreamDefaultController const&);
  86. WebIDL::ExceptionOr<JS::Value> writable_stream_default_controller_get_chunk_size(WritableStreamDefaultController&, JS::Value chunk);
  87. double writable_stream_default_controller_get_desired_size(WritableStreamDefaultController const&);
  88. WebIDL::ExceptionOr<void> writable_stream_default_controller_process_close(WritableStreamDefaultController&);
  89. WebIDL::ExceptionOr<void> writable_stream_default_controller_process_write(WritableStreamDefaultController&, JS::Value chunk);
  90. WebIDL::ExceptionOr<void> writable_stream_default_controller_write(WritableStreamDefaultController&, JS::Value chunk, JS::Value chunk_size);
  91. bool is_non_negative_number(JS::Value);
  92. JS::Value create_close_sentinel();
  93. bool is_close_sentinel(JS::Value);
  94. JS::ThrowCompletionOr<JS::Handle<WebIDL::CallbackType>> property_to_callback(JS::VM& vm, JS::Value value, JS::PropertyKey const& property_key);
  95. // https://streams.spec.whatwg.org/#value-with-size
  96. struct ValueWithSize {
  97. JS::Value value;
  98. double size;
  99. };
  100. // https://streams.spec.whatwg.org/#dequeue-value
  101. template<typename T>
  102. JS::Value dequeue_value(T& container)
  103. {
  104. // 1. Assert: container has [[queue]] and [[queueTotalSize]] internal slots.
  105. // 2. Assert: container.[[queue]] is not empty.
  106. VERIFY(!container.queue().is_empty());
  107. // 3. Let valueWithSize be container.[[queue]][0].
  108. // 4. Remove valueWithSize from container.[[queue]].
  109. auto value_with_size = container.queue().take_first();
  110. // 5. Set container.[[queueTotalSize]] to container.[[queueTotalSize]] − valueWithSize’s size.
  111. container.set_queue_total_size(container.queue_total_size() - value_with_size.size);
  112. // 6. If container.[[queueTotalSize]] < 0, set container.[[queueTotalSize]] to 0. (This can occur due to rounding errors.)
  113. if (container.queue_total_size() < 0.0)
  114. container.set_queue_total_size(0.0);
  115. // 7. Return valueWithSize’s value.
  116. return value_with_size.value;
  117. }
  118. // https://streams.spec.whatwg.org/#enqueue-value-with-size
  119. template<typename T>
  120. WebIDL::ExceptionOr<void> enqueue_value_with_size(T& container, JS::Value value, JS::Value size_value)
  121. {
  122. // 1. Assert: container has [[queue]] and [[queueTotalSize]] internal slots.
  123. // 2. If ! IsNonNegativeNumber(size) is false, throw a RangeError exception.
  124. if (!is_non_negative_number(size_value))
  125. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Chunk has non-positive size"sv };
  126. auto size = size_value.as_double();
  127. // 3. If size is +∞, throw a RangeError exception.
  128. if (size == HUGE_VAL)
  129. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Chunk has infinite size"sv };
  130. // 4. Append a new value-with-size with value value and size size to container.[[queue]].
  131. container.queue().append({ value, size });
  132. // 5. Set container.[[queueTotalSize]] to container.[[queueTotalSize]] + size.
  133. container.set_queue_total_size(container.queue_total_size() + size);
  134. return {};
  135. }
  136. // https://streams.spec.whatwg.org/#peek-queue-value
  137. template<typename T>
  138. JS::Value peek_queue_value(T& container)
  139. {
  140. // 1. Assert: container has [[queue]] and [[queueTotalSize]] internal slots.
  141. // 2. Assert: container.[[queue]] is not empty.
  142. VERIFY(!container.queue().is_empty());
  143. // 3. Let valueWithSize be container.[[queue]][0].
  144. auto& value_with_size = container.queue().first();
  145. // 4. Return valueWithSize’s value.
  146. return value_with_size.value;
  147. }
  148. // https://streams.spec.whatwg.org/#reset-queue
  149. template<typename T>
  150. void reset_queue(T& container)
  151. {
  152. // 1. Assert: container has [[queue]] and [[queueTotalSize]] internal slots.
  153. // 2. Set container.[[queue]] to a new empty list.
  154. container.queue().clear();
  155. // 3. Set container.[[queueTotalSize]] to 0.
  156. container.set_queue_total_size(0);
  157. }
  158. }