mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
LibWeb: Add non-standard AO set_up_readable_stream
This AO 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.
This commit is contained in:
parent
7a77130923
commit
ce521a196d
Notes:
sideshowbarker
2024-07-17 01:55:29 +09:00
Author: https://github.com/kennethmyhra Commit: https://github.com/LadybirdBrowser/ladybird/commit/ce521a196d Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/108 Reviewed-by: https://github.com/shannonbooth ✅
@ -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)
|
||||
{
|
||||
|
@ -116,6 +116,7 @@ void readable_byte_stream_controller_handle_queue_drain(ReadableByteStreamContro
|
||||
void readable_byte_stream_controller_invalidate_byob_request(ReadableByteStreamController&);
|
||||
bool readable_byte_stream_controller_should_call_pull(ReadableByteStreamController const&);
|
||||
|
||||
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 = {});
|
||||
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 = {});
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> create_readable_byte_stream(JS::Realm& realm, JS::NonnullGCPtr<StartAlgorithm> start_algorithm, JS::NonnullGCPtr<PullAlgorithm> pull_algorithm, JS::NonnullGCPtr<CancelAlgorithm> cancel_algorithm);
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStream>> create_writable_stream(JS::Realm& realm, JS::NonnullGCPtr<StartAlgorithm> start_algorithm, JS::NonnullGCPtr<WriteAlgorithm> write_algorithm, JS::NonnullGCPtr<CloseAlgorithm> close_algorithm, JS::NonnullGCPtr<AbortAlgorithm> abort_algorithm, double high_water_mark, JS::NonnullGCPtr<SizeAlgorithm> size_algorithm);
|
||||
|
Loading…
Reference in New Issue
Block a user