LibWeb: Port ReadLoopReadRequest to JS::HeapFunction

This commit is contained in:
Shannon Booth 2024-08-18 15:07:54 +12:00 committed by Andreas Kling
parent 87d964b2f5
commit a622abde77
Notes: github-actions[bot] 2024-08-18 09:16:31 +00:00
3 changed files with 36 additions and 33 deletions

View file

@ -307,29 +307,29 @@ JS::NonnullGCPtr<WebIDL::Promise> 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;

View file

@ -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<SuccessSteps> success_steps, JS::NonnullGCPtr<FailureSteps> failure_steps, JS::GCPtr<ChunkSteps> 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<JS::Uint8Array>(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<ReadLoopReadRequest::SuccessSteps> success_steps, JS::NonnullGCPtr<ReadLoopReadRequest::FailureSteps> 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<ReadLoopReadRequest>(vm, realm, *this, move(success_steps), move(failure_steps));
auto read_request = heap().allocate_without_realm<ReadLoopReadRequest>(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<ReadLoopReadRequest::ChunkSteps> chunk_steps, JS::NonnullGCPtr<ReadLoopReadRequest::SuccessSteps> success_steps, JS::NonnullGCPtr<ReadLoopReadRequest::FailureSteps> 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<ReadLoopReadRequest>(vm, realm, *this, move(success_steps), move(failure_steps), move(chunk_steps));
auto read_request = heap().allocate_without_realm<ReadLoopReadRequest>(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<WebIDL::Promise> 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;
}

View file

@ -37,15 +37,15 @@ class ReadLoopReadRequest final : public ReadRequest {
public:
// successSteps, which is an algorithm accepting a byte sequence
using SuccessSteps = JS::SafeFunction<void(ByteBuffer)>;
using SuccessSteps = JS::HeapFunction<void(ByteBuffer)>;
// failureSteps, which is an algorithm accepting a JavaScript value
using FailureSteps = JS::SafeFunction<void(JS::Value error)>;
using FailureSteps = JS::HeapFunction<void(JS::Value error)>;
// AD-HOC: callback triggered on every chunk received from the stream.
using ChunkSteps = JS::SafeFunction<void(ByteBuffer)>;
using ChunkSteps = JS::HeapFunction<void(ByteBuffer)>;
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<SuccessSteps> success_steps, JS::NonnullGCPtr<FailureSteps> failure_steps, JS::GCPtr<ChunkSteps> chunk_steps = {});
virtual void on_chunk(JS::Value chunk) override;
@ -60,9 +60,9 @@ private:
JS::NonnullGCPtr<JS::Realm> m_realm;
JS::NonnullGCPtr<ReadableStreamDefaultReader> m_reader;
ByteBuffer m_bytes;
SuccessSteps m_success_steps;
FailureSteps m_failure_steps;
ChunkSteps m_chunk_steps;
JS::NonnullGCPtr<SuccessSteps> m_success_steps;
JS::NonnullGCPtr<FailureSteps> m_failure_steps;
JS::GCPtr<ChunkSteps> m_chunk_steps;
};
// https://streams.spec.whatwg.org/#readablestreamdefaultreader
@ -80,8 +80,8 @@ public:
JS::NonnullGCPtr<JS::Promise> 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<ReadLoopReadRequest::SuccessSteps>, JS::NonnullGCPtr<ReadLoopReadRequest::FailureSteps>);
void read_all_chunks(JS::NonnullGCPtr<ReadLoopReadRequest::ChunkSteps>, JS::NonnullGCPtr<ReadLoopReadRequest::SuccessSteps>, JS::NonnullGCPtr<ReadLoopReadRequest::FailureSteps>);
JS::NonnullGCPtr<WebIDL::Promise> read_all_bytes_deprecated();
void release_lock();