Skip to content

Truly independent web browser

LibWeb: Expose Crypto tasks to Rust bindings

Shannon Booth03a50c7ad342
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
index 0734e208883..e184f9c9a3e 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
@@ -27,6 +27,8 @@ unsafe extern "C" {
/// the realm is on the stack, as HTML::queue_a_microtask() queues one without a document.
fn web_bindings_queue_a_microtask(realm: *mut c_void, function: *mut c_void);
+ fn web_bindings_deferred_invoke(cell: *mut c_void, steps: unsafe extern "C" fn(*mut c_void, *mut c_void));
+
/// Calls the steps with the VM and the cell in parallel: later, outside of any task, while an execution context of
/// the realm is on the stack. The cell lives until then.
fn web_bindings_run_in_parallel(
@@ -72,6 +74,7 @@ pub(crate) enum TaskSource {
PostedMessage = 6,
FileReading = 13,
FontLoading = 19,
+ Crypto = 27,
Storage = 29,
}
@@ -577,6 +580,13 @@ pub(crate) unsafe extern "C" fn run_pending_steps(vm: *mut c_void, cell: *mut c_
cell.0.take();
}
+/// Defers steps to the event loop without pushing an execution context.
+pub(crate) fn deferred_invoke<C: Trace + Clone + 'static>(vm: &Vm, captures: C, steps: fn(&Vm, C)) {
+ let cell = pending_steps(vm, captures, steps);
+ // SAFETY: The cell is live.
+ unsafe { web_bindings_deferred_invoke(cell, run_pending_steps) };
+}
+
/// Performs the steps with the captures in parallel, https://html.spec.whatwg.org/multipage/infrastructure.html#in-parallel,
/// with an execution context of the realm on the stack.
pub(crate) fn in_parallel<C: Trace + Copy + 'static>(vm: &Vm, realm: Gc<Realm>, captures: C, steps: fn(&Vm, C)) {
@@ -602,7 +612,7 @@ pub(crate) fn queue_a_task<C: Trace + Copy + 'static>(
/// 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>(
+pub(crate) fn queue_a_global_task<C: Trace + Clone + 'static>(
vm: &Vm,
global: Gc<Object>,
task_source: TaskSource,
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index f67068f9415..24686a3c725 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -81,6 +81,7 @@ void* web_bindings_wrapper_world(void* realm, bool* is_main_world);
JSObject* web_bindings_rust_wrapper_for(void* realm, void* cell);
void web_bindings_set_rust_wrapper(void* realm, void* cell, JSObject* wrapper);
void web_bindings_queue_a_microtask(void* realm, JSObject* function);
+void web_bindings_deferred_invoke(void* cell, void (*steps)(JSVM*, void*));
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(JSObject* global_object, u8 task_source, void* cell, void (*steps)(JSVM*, void*));
@@ -301,6 +302,13 @@ void web_bindings_queue_a_microtask(void* realm, JSObject* function)
}));
}
+void web_bindings_deferred_invoke(void* cell, void (*steps)(JSVM*, void*))
+{
+ Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(GC::Heap::the(), GC::captures(GC::Ref { *static_cast<GC::Cell*>(cell) }, steps), [](auto& cell, auto& steps) {
+ steps(JS::HostABI::vm_to_abi(JS::VM::the()), cell.ptr());
+ }));
+}
+
void web_bindings_run_in_parallel(void* realm, void* cell, void (*steps)(JSVM*, void*))
{
GC::Ref<JS::Realm> js_realm = *static_cast<JS::Realm*>(realm);
@@ -315,6 +323,7 @@ void web_bindings_run_in_parallel(void* realm, void* cell, void (*steps)(JSVM*,
static_assert(to_underlying(HTML::Task::Source::Unspecified) == 0);
static_assert(to_underlying(HTML::Task::Source::Networking) == 3);
static_assert(to_underlying(HTML::Task::Source::FileReading) == 13);
+static_assert(to_underlying(HTML::Task::Source::Crypto) == 27);
static_assert(to_underlying(HTML::Task::Source::Storage) == 29);
void web_bindings_queue_a_task(void* realm, u8 task_source, void* cell, void (*steps)(JSVM*, void*))