From 5777e4d2cb617154c4cf83e4a1324926f8d5e1c2 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 18 Aug 2024 15:07:54 +1200 Subject: [PATCH] LibWeb: Port ReadLoopReadRequest to JS::HeapFunction (cherry picked from commit a622abde776bbb2e3059c1ae6acde3ef78cfcca3) --- .../LibWeb/Streams/AbstractOperations.cpp | 14 +++---- .../Streams/ReadableStreamDefaultReader.cpp | 37 ++++++++++--------- .../Streams/ReadableStreamDefaultReader.h | 18 ++++----- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp index 95c510171b1..97174cd619e 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -307,29 +307,29 @@ JS::NonnullGCPtr readable_stream_pipe_to(ReadableStream& source // FIXME: Currently a naive implementation that uses ReadableStreamDefaultReader::read_all_chunks() to read all chunks // from the source and then through the callback success_steps writes those chunks to the destination. - auto chunk_steps = [&realm, writer](ByteBuffer buffer) { + auto chunk_steps = JS::create_heap_function(realm.heap(), [&realm, writer](ByteBuffer buffer) { auto array_buffer = JS::ArrayBuffer::create(realm, move(buffer)); auto chunk = JS::Uint8Array::create(realm, array_buffer->byte_length(), *array_buffer); auto promise = writable_stream_default_writer_write(writer, chunk); WebIDL::resolve_promise(realm, promise, JS::js_undefined()); - }; + }); - auto success_steps = [promise, &realm, writer](ByteBuffer) { + auto success_steps = JS::create_heap_function(realm.heap(), [promise, &realm, writer](ByteBuffer) { // Make sure we close the acquired writer. WebIDL::resolve_promise(realm, writable_stream_default_writer_close(*writer), JS::js_undefined()); WebIDL::resolve_promise(realm, promise, JS::js_undefined()); - }; + }); - auto failure_steps = [promise, &realm, writer](JS::Value error) { + auto failure_steps = JS::create_heap_function(realm.heap(), [promise, &realm, writer](JS::Value error) { // Make sure we close the acquired writer. WebIDL::resolve_promise(realm, writable_stream_default_writer_close(*writer), JS::js_undefined()); WebIDL::reject_promise(realm, promise, error); - }; + }); - reader->read_all_chunks(move(chunk_steps), move(success_steps), move(failure_steps)); + reader->read_all_chunks(chunk_steps, success_steps, failure_steps); // 16. Return promise. return promise; diff --git a/Userland/Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp b/Userland/Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp index a02522a5001..cd5ff70a3fc 100644 --- a/Userland/Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp +++ b/Userland/Libraries/LibWeb/Streams/ReadableStreamDefaultReader.cpp @@ -32,6 +32,9 @@ void ReadLoopReadRequest::visit_edges(Visitor& visitor) Base::visit_edges(visitor); visitor.visit(m_realm); visitor.visit(m_reader); + visitor.visit(m_success_steps); + visitor.visit(m_failure_steps); + visitor.visit(m_chunk_steps); } // https://streams.spec.whatwg.org/#default-reader-constructor @@ -66,13 +69,13 @@ void ReadableStreamDefaultReader::visit_edges(Cell::Visitor& visitor) } // https://streams.spec.whatwg.org/#read-loop -ReadLoopReadRequest::ReadLoopReadRequest(JS::VM& vm, JS::Realm& realm, ReadableStreamDefaultReader& reader, SuccessSteps success_steps, FailureSteps failure_steps, ChunkSteps chunk_steps) +ReadLoopReadRequest::ReadLoopReadRequest(JS::VM& vm, JS::Realm& realm, ReadableStreamDefaultReader& reader, JS::NonnullGCPtr success_steps, JS::NonnullGCPtr failure_steps, JS::GCPtr chunk_steps) : m_vm(vm) , m_realm(realm) , m_reader(reader) - , m_success_steps(move(success_steps)) - , m_failure_steps(move(failure_steps)) - , m_chunk_steps(move(chunk_steps)) + , m_success_steps(success_steps) + , m_failure_steps(failure_steps) + , m_chunk_steps(chunk_steps) { } @@ -81,7 +84,7 @@ void ReadLoopReadRequest::on_chunk(JS::Value chunk) { // 1. If chunk is not a Uint8Array object, call failureSteps with a TypeError and abort these steps. if (!chunk.is_object() || !is(chunk.as_object())) { - m_failure_steps(JS::TypeError::create(m_realm, "Chunk data is not Uint8Array"sv)); + m_failure_steps->function()(JS::TypeError::create(m_realm, "Chunk data is not Uint8Array"sv)); return; } @@ -93,7 +96,7 @@ void ReadLoopReadRequest::on_chunk(JS::Value chunk) if (m_chunk_steps) { // FIXME: Can we move the buffer out of the `chunk`? Unclear if that is safe. - m_chunk_steps(MUST(ByteBuffer::copy(buffer))); + m_chunk_steps->function()(MUST(ByteBuffer::copy(buffer))); } // FIXME: As the spec suggests, implement this non-recursively - instead of directly. It is not too big of a deal currently @@ -108,14 +111,14 @@ void ReadLoopReadRequest::on_chunk(JS::Value chunk) void ReadLoopReadRequest::on_close() { // 1. Call successSteps with bytes. - m_success_steps(move(m_bytes)); + m_success_steps->function()(move(m_bytes)); } // error steps, given e void ReadLoopReadRequest::on_error(JS::Value error) { // 1. Call failureSteps with e. - m_failure_steps(error); + m_failure_steps->function()(error); } class DefaultReaderReadRequest final : public ReadRequest { @@ -196,20 +199,20 @@ void ReadableStreamDefaultReader::read_a_chunk(Fetch::Infrastructure::Incrementa } // https://streams.spec.whatwg.org/#readablestreamdefaultreader-read-all-bytes -void ReadableStreamDefaultReader::read_all_bytes(ReadLoopReadRequest::SuccessSteps success_steps, ReadLoopReadRequest::FailureSteps failure_steps) +void ReadableStreamDefaultReader::read_all_bytes(JS::NonnullGCPtr success_steps, JS::NonnullGCPtr failure_steps) { auto& realm = this->realm(); auto& vm = realm.vm(); // 1. Let readRequest be a new read request with the following items: // NOTE: items and steps in ReadLoopReadRequest. - auto read_request = heap().allocate_without_realm(vm, realm, *this, move(success_steps), move(failure_steps)); + auto read_request = heap().allocate_without_realm(vm, realm, *this, success_steps, failure_steps); // 2. Perform ! ReadableStreamDefaultReaderRead(this, readRequest). readable_stream_default_reader_read(*this, read_request); } -void ReadableStreamDefaultReader::read_all_chunks(ReadLoopReadRequest::ChunkSteps chunk_steps, ReadLoopReadRequest::SuccessSteps success_steps, ReadLoopReadRequest::FailureSteps failure_steps) +void ReadableStreamDefaultReader::read_all_chunks(JS::NonnullGCPtr chunk_steps, JS::NonnullGCPtr success_steps, JS::NonnullGCPtr failure_steps) { // AD-HOC: Some spec steps direct us to "read all chunks" from a stream, but there isn't an AO defined to do that. // We implement those steps by using the "read all bytes" definition, with a custom callback to receive @@ -219,7 +222,7 @@ void ReadableStreamDefaultReader::read_all_chunks(ReadLoopReadRequest::ChunkStep // 1. Let readRequest be a new read request with the following items: // NOTE: items and steps in ReadLoopReadRequest. - auto read_request = heap().allocate_without_realm(vm, realm, *this, move(success_steps), move(failure_steps), move(chunk_steps)); + auto read_request = heap().allocate_without_realm(vm, realm, *this, success_steps, failure_steps, chunk_steps); // 2. Perform ! ReadableStreamDefaultReaderRead(this, readRequest). readable_stream_default_reader_read(*this, read_request); @@ -234,16 +237,16 @@ JS::NonnullGCPtr ReadableStreamDefaultReader::read_all_bytes_de auto promise = WebIDL::create_promise(realm); - auto success_steps = [promise, &realm](ByteBuffer bytes) { + auto success_steps = JS::create_heap_function(realm.heap(), [promise, &realm](ByteBuffer bytes) { auto buffer = JS::ArrayBuffer::create(realm, move(bytes)); WebIDL::resolve_promise(realm, promise, buffer); - }; + }); - auto failure_steps = [promise, &realm](JS::Value error) { + auto failure_steps = JS::create_heap_function(realm.heap(), [promise, &realm](JS::Value error) { WebIDL::reject_promise(realm, promise, error); - }; + }); - read_all_bytes(move(success_steps), move(failure_steps)); + read_all_bytes(success_steps, failure_steps); return promise; } diff --git a/Userland/Libraries/LibWeb/Streams/ReadableStreamDefaultReader.h b/Userland/Libraries/LibWeb/Streams/ReadableStreamDefaultReader.h index cf0209007ee..0d69271b942 100644 --- a/Userland/Libraries/LibWeb/Streams/ReadableStreamDefaultReader.h +++ b/Userland/Libraries/LibWeb/Streams/ReadableStreamDefaultReader.h @@ -37,15 +37,15 @@ class ReadLoopReadRequest final : public ReadRequest { public: // successSteps, which is an algorithm accepting a byte sequence - using SuccessSteps = JS::SafeFunction; + using SuccessSteps = JS::HeapFunction; // failureSteps, which is an algorithm accepting a JavaScript value - using FailureSteps = JS::SafeFunction; + using FailureSteps = JS::HeapFunction; // AD-HOC: callback triggered on every chunk received from the stream. - using ChunkSteps = JS::SafeFunction; + using ChunkSteps = JS::HeapFunction; - ReadLoopReadRequest(JS::VM& vm, JS::Realm& realm, ReadableStreamDefaultReader& reader, SuccessSteps success_steps, FailureSteps failure_steps, ChunkSteps chunk_steps = {}); + ReadLoopReadRequest(JS::VM& vm, JS::Realm& realm, ReadableStreamDefaultReader& reader, JS::NonnullGCPtr success_steps, JS::NonnullGCPtr failure_steps, JS::GCPtr chunk_steps = {}); virtual void on_chunk(JS::Value chunk) override; @@ -60,9 +60,9 @@ private: JS::NonnullGCPtr m_realm; JS::NonnullGCPtr m_reader; ByteBuffer m_bytes; - SuccessSteps m_success_steps; - FailureSteps m_failure_steps; - ChunkSteps m_chunk_steps; + JS::NonnullGCPtr m_success_steps; + JS::NonnullGCPtr m_failure_steps; + JS::GCPtr m_chunk_steps; }; // https://streams.spec.whatwg.org/#readablestreamdefaultreader @@ -80,8 +80,8 @@ public: JS::NonnullGCPtr read(); void read_a_chunk(Fetch::Infrastructure::IncrementalReadLoopReadRequest& read_request); - void read_all_bytes(ReadLoopReadRequest::SuccessSteps, ReadLoopReadRequest::FailureSteps); - void read_all_chunks(ReadLoopReadRequest::ChunkSteps, ReadLoopReadRequest::SuccessSteps, ReadLoopReadRequest::FailureSteps); + void read_all_bytes(JS::NonnullGCPtr, JS::NonnullGCPtr); + void read_all_chunks(JS::NonnullGCPtr, JS::NonnullGCPtr, JS::NonnullGCPtr); JS::NonnullGCPtr read_all_bytes_deprecated(); void release_lock();