Skip to content

Truly independent web browser

LibWeb: Queue global tasks from Rust on a global object

Shannon Booth93494f80eff4
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
index 6bd20b7b21e..f2a1cb0b954 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
@@ -45,11 +45,11 @@ unsafe extern "C" {
steps: unsafe extern "C" fn(*mut c_void, *mut c_void),
);
- /// Queues a global task on the task source, an HTML::Task::Source, with the global object of the realm, that calls
- /// the steps with the VM and the cell, as HTML::queue_global_task() queues one. The cell lives until then. Returns
- /// the task's HTML::TaskID.
+ /// Queues a global task on the task source, an HTML::Task::Source, with the global object, that calls the steps with
+ /// the VM and the cell, as HTML::queue_global_task() queues one. The cell lives until then. Returns the task's
+ /// HTML::TaskID.
fn web_bindings_queue_a_global_task(
- realm: *mut c_void,
+ global_object: *mut c_void,
task_source: u8,
cell: *mut c_void,
steps: unsafe extern "C" fn(*mut c_void, *mut c_void),
@@ -598,20 +598,20 @@ pub(crate) fn queue_a_task<C: Trace + Copy + 'static>(
unsafe { web_bindings_queue_a_task(realm.as_ptr().cast(), task_source as u8, cell, run_pending_steps) };
}
-/// Queues a global task on the task source, given the global object of the realm, that performs the steps with the
-/// captures, https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-global-task. Unlike queue_a_task(), the
-/// steps run with no execution context of their own on the stack.
+/// Queues a global task on the task source, given the global object, that performs the steps with the captures,
+/// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-global-task. Unlike queue_a_task(), the steps run
+/// with no execution context of their own on the stack.
pub(crate) fn queue_a_global_task<C: Trace + Copy + 'static>(
vm: &Vm,
- realm: Gc<Realm>,
+ global: Gc<Object>,
task_source: TaskSource,
captures: C,
steps: fn(&Vm, C),
) -> TaskId {
let cell = pending_steps(vm, captures, steps);
- // SAFETY: The realm and the cell are live.
+ // SAFETY: The global object and the cell are live.
TaskId(unsafe {
- web_bindings_queue_a_global_task(realm.as_ptr().cast(), task_source as u8, cell, run_pending_steps)
+ web_bindings_queue_a_global_task(global.as_ptr().cast(), task_source as u8, cell, run_pending_steps)
})
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/file_api/file_reader.rs b/Libraries/LibWeb/Bindings/Rust/src/file_api/file_reader.rs
index 887cdd90336..e2a018ee6f5 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/file_api/file_reader.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/file_api/file_reader.rs
@@ -305,7 +305,7 @@ impl FileReader {
let id = queue_a_global_task(
vm,
- this.realm,
+ this.realm.global_object(),
TaskSource::FileReading,
(this, QueuedSteps { captures, steps }),
|vm, (this, task)| {
@@ -377,7 +377,7 @@ impl FileReader {
queue_a_global_task(
vm,
- realm,
+ realm.global_object(),
TaskSource::FileReading,
(*this, state, chunk_promise),
|vm, (this, state, chunk_promise)| {
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs b/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs
index a95c2c79ed0..6cac9264c20 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs
@@ -305,7 +305,7 @@ impl BroadcastChannel {
for destination in destinations.values().iter() {
queue_a_global_task(
vm,
- destination.realm,
+ destination.realm.global_object(),
TaskSource::DOMManipulation,
(*destination, message),
|vm, (destination, message)| Self::message_task_steps(destination, vm, &message),
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/message_port.rs b/Libraries/LibWeb/Bindings/Rust/src/html/message_port.rs
index 69ece8d468f..a716cd3c712 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/message_port.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/message_port.rs
@@ -358,7 +358,7 @@ impl MessagePort {
let message_task_generation = this.message_task_generation.get();
queue_a_global_task(
vm,
- this.realm,
+ this.realm.global_object(),
TaskSource::PostedMessage,
(this, message_task_generation),
|vm, (this, message_task_generation)| {
@@ -437,9 +437,15 @@ impl MessagePort {
}
if this.enabled.get() && this.should_shutdown_on_enable.replace(false) {
- queue_a_global_task(vm, this.realm, TaskSource::PostedMessage, this, |_, this| {
- this.close_steps();
- });
+ queue_a_global_task(
+ vm,
+ this.realm.global_object(),
+ TaskSource::PostedMessage,
+ this,
+ |_, this| {
+ this.close_steps();
+ },
+ );
}
}
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index 3c0b2ae59ab..ee041f31d7d 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -104,7 +104,7 @@ JSObject* web_bindings_rejected_promise(void* current_realm, JSValue this_value,
void web_bindings_queue_a_microtask(void* realm, JSObject* function);
void web_bindings_run_in_parallel(void* realm, void* cell, void (*steps)(JSVM*, void*));
void web_bindings_queue_a_task(void* realm, u8 task_source, void* cell, void (*steps)(JSVM*, void*));
-u64 web_bindings_queue_a_global_task(void* realm, u8 task_source, void* cell, void (*steps)(JSVM*, void*));
+u64 web_bindings_queue_a_global_task(JSObject* global_object, u8 task_source, void* cell, void (*steps)(JSVM*, void*));
void web_bindings_remove_tasks(void* realm, ReadonlySpan<u64> task_ids);
u64 web_bindings_currently_running_task(void* realm);
void* web_bindings_create_callback_function(JSObject* callable, bool returns_promise);
@@ -483,12 +483,12 @@ void web_bindings_queue_a_task(void* realm, u8 task_source, void* cell, void (*s
}));
}
-u64 web_bindings_queue_a_global_task(void* realm, u8 task_source, void* cell, void (*steps)(JSVM*, void*))
+u64 web_bindings_queue_a_global_task(JSObject* global_object, u8 task_source, void* cell, void (*steps)(JSVM*, void*))
{
- GC::Ref<JS::Realm> js_realm = *static_cast<JS::Realm*>(realm);
+ auto& global = *JS::HostABI::object_from_abi(global_object);
// NB: The task's captures, which the collector scans, keep the cell alive until the steps run.
- auto task_id = HTML::queue_global_task(static_cast<HTML::Task::Source>(task_source), js_realm->global_object(), GC::create_function(GC::Heap::the(), [js_realm, cell, steps] {
- steps(JS::HostABI::vm_to_abi(js_realm->vm()), cell);
+ auto task_id = HTML::queue_global_task(static_cast<HTML::Task::Source>(task_source), global, GC::create_function(GC::Heap::the(), [&vm = global.vm(), cell, steps] {
+ steps(JS::HostABI::vm_to_abi(vm), cell);
}));
return task_id.value();
}