|
@@ -2778,6 +2778,38 @@ bool readable_byte_stream_controller_should_call_pull(ReadableByteStreamControll
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+// NON-STANDARD: Can be used instead of CreateReadableStream in cases where we need to set up a newly allocated
|
|
|
+// ReadableStream before initialization of said ReadableStream, i.e. ReadableStream is captured by lambdas in an uninitialized state.
|
|
|
+// Spec steps are taken from: https://streams.spec.whatwg.org/#create-readable-stream
|
|
|
+WebIDL::ExceptionOr<void> set_up_readable_stream(JS::Realm& realm, ReadableStream& stream, JS::NonnullGCPtr<StartAlgorithm> start_algorithm, JS::NonnullGCPtr<PullAlgorithm> pull_algorithm, JS::NonnullGCPtr<CancelAlgorithm> cancel_algorithm, Optional<double> high_water_mark, JS::GCPtr<SizeAlgorithm> size_algorithm)
|
|
|
+{
|
|
|
+ // 1. If highWaterMark was not passed, set it to 1.
|
|
|
+ if (!high_water_mark.has_value())
|
|
|
+ high_water_mark = 1.0;
|
|
|
+
|
|
|
+ // 2. If sizeAlgorithm was not passed, set it to an algorithm that returns 1.
|
|
|
+ if (!size_algorithm)
|
|
|
+ size_algorithm = JS::create_heap_function(realm.heap(), [](JS::Value) { return JS::normal_completion(JS::Value(1)); });
|
|
|
+
|
|
|
+ // 3. Assert: ! IsNonNegativeNumber(highWaterMark) is true.
|
|
|
+ VERIFY(is_non_negative_number(JS::Value { *high_water_mark }));
|
|
|
+
|
|
|
+ // 4. Let stream be a new ReadableStream.
|
|
|
+ // NOTE: The ReadableStream is allocated outside the scope of this method.
|
|
|
+
|
|
|
+ // 5. Perform ! InitializeReadableStream(stream).
|
|
|
+ initialize_readable_stream(stream);
|
|
|
+
|
|
|
+ // 6. Let controller be a new ReadableStreamDefaultController.
|
|
|
+ auto controller = realm.heap().allocate<ReadableStreamDefaultController>(realm, realm);
|
|
|
+
|
|
|
+ // 7. Perform ? SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm).
|
|
|
+ TRY(set_up_readable_stream_default_controller(stream, *controller, start_algorithm, pull_algorithm, cancel_algorithm, *high_water_mark, *size_algorithm));
|
|
|
+
|
|
|
+ return {};
|
|
|
+}
|
|
|
+
|
|
|
// https://streams.spec.whatwg.org/#create-readable-stream
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> create_readable_stream(JS::Realm& realm, JS::NonnullGCPtr<StartAlgorithm> start_algorithm, JS::NonnullGCPtr<PullAlgorithm> pull_algorithm, JS::NonnullGCPtr<CancelAlgorithm> cancel_algorithm, Optional<double> high_water_mark, JS::GCPtr<SizeAlgorithm> size_algorithm)
|
|
|
{
|