Skip to content

Truly independent web browser

LibWeb: Represent C++ global objects in Rust

Shannon Booth94c8f58b1e3f
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/global_object.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/global_object.rs
new file mode 100644
index 00000000000..d4e4aeeefcf
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/global_object.rs
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! Handles to the C++ global objects and Documents.
+
+use core::ffi::c_void;
+use core::ptr::NonNull;
+
+use libjs_rust::api::{Gc, Object, Trace};
+
+use crate::bindings::cpp_cell::CppCell;
+use crate::bindings::interface::Wrappable;
+
+unsafe extern "C" {
+ fn web_bindings_window_from_global_object(global_object: *mut c_void) -> *mut c_void;
+ fn web_bindings_window_associated_document(window: *mut c_void) -> *mut c_void;
+ fn web_bindings_document_is_fully_active(document: *mut c_void) -> bool;
+ fn web_bindings_worker_global_scope_from_global_object(global_object: *mut c_void) -> *mut c_void;
+ fn web_bindings_worker_global_scope_is_closing(worker_global_scope: *mut c_void) -> bool;
+}
+
+/// https://html.spec.whatwg.org/multipage/nav-history-apis.html#window
+#[derive(Clone, Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct CppWindow(CppCell);
+
+/// https://html.spec.whatwg.org/multipage/workers.html#workerglobalscope
+#[derive(Clone, Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct CppWorkerGlobalScope(CppCell);
+
+/// https://dom.spec.whatwg.org/#document
+#[derive(Clone, Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct CppDocument(CppCell);
+
+/// The cell of a C++ object that LibWeb returns, or None for a null pointer.
+///
+/// # Safety
+///
+/// `cell` must be null or a live cell of LibWeb's C++ code.
+unsafe fn cell_if_not_null(cell: *mut c_void) -> Option<CppCell> {
+ // SAFETY: The caller passes a live cell.
+ NonNull::new(cell).map(|cell| unsafe { CppCell::from_raw(cell.as_ptr()) })
+}
+
+/// https://html.spec.whatwg.org/multipage/webappapis.html#concept-relevant-global
+pub(crate) fn relevant_global_object<T: Wrappable>(object: &T) -> Gc<Object> {
+ object
+ .relevant_realm()
+ .expect("the implementation knows the realm of the relevant global object")
+ .global_object()
+}
+
+/// The Window that the global object is, if it is one.
+pub(crate) fn window_from_global_object(global_object: Gc<Object>) -> Option<CppWindow> {
+ // SAFETY: The global object is live, and LibWeb returns null or the live Window that it is.
+ unsafe { cell_if_not_null(web_bindings_window_from_global_object(global_object.as_ptr().cast())) }.map(CppWindow)
+}
+
+/// The WorkerGlobalScope that the global object is, if it is one.
+pub(crate) fn worker_global_scope_from_global_object(global_object: Gc<Object>) -> Option<CppWorkerGlobalScope> {
+ // SAFETY: The global object is live, and LibWeb returns null or the live WorkerGlobalScope that it is.
+ unsafe {
+ cell_if_not_null(web_bindings_worker_global_scope_from_global_object(
+ global_object.as_ptr().cast(),
+ ))
+ }
+ .map(CppWorkerGlobalScope)
+}
+
+impl CppWindow {
+ /// https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-document-window
+ pub(crate) fn associated_document(&self) -> CppDocument {
+ // SAFETY: The Window is live, and LibWeb returns its live associated Document.
+ CppDocument(unsafe { CppCell::from_raw(web_bindings_window_associated_document(self.0.as_ptr())) })
+ }
+}
+
+impl CppDocument {
+ /// https://html.spec.whatwg.org/multipage/document-sequences.html#fully-active
+ pub(crate) fn is_fully_active(&self) -> bool {
+ // SAFETY: The Document is live.
+ unsafe { web_bindings_document_is_fully_active(self.0.as_ptr()) }
+ }
+}
+
+impl CppWorkerGlobalScope {
+ /// https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-closing
+ pub(crate) fn is_closing(&self) -> bool {
+ // SAFETY: The WorkerGlobalScope is live.
+ unsafe { web_bindings_worker_global_scope_is_closing(self.0.as_ptr()) }
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs
index 0f66a0cedf5..3ef529468a9 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs
@@ -25,6 +25,7 @@ pub(crate) mod cpp_cell;
pub(crate) mod event_target;
#[cfg(web_idl_bindings)]
pub(crate) mod generated;
+pub(crate) mod global_object;
pub(crate) mod interface;
pub(crate) mod iterable;
pub(crate) mod promise;
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index 393558dafa4..29155822914 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -15,6 +15,7 @@
#include <LibWeb/Bindings/RustSerialization.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Bindings/WrapperWorld.h>
+#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/Scripting/Agent.h>
#include <LibWeb/HTML/Scripting/Environments.h>
@@ -22,6 +23,7 @@
#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/WindowProxy.h>
+#include <LibWeb/HTML/WorkerGlobalScope.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
#include <LibWeb/WebIDL/CallbackType.h>
@@ -105,6 +107,11 @@ void* web_bindings_create_callback_function(JSObject* callable, bool returns_pro
JSCompletion web_bindings_invoke_callback_function(void* callback, bool has_this_value, JSValue this_value, ReadonlySpan<JSValue> arguments);
void* web_bindings_invoke_promise_callback_function(void* callback, bool has_this_value, JSValue this_value, ReadonlySpan<JSValue> arguments);
bool web_bindings_realm_is_window(void* realm);
+HTML::Window* web_bindings_window_from_global_object(JSObject* global_object);
+DOM::Document* web_bindings_window_associated_document(HTML::Window*);
+bool web_bindings_document_is_fully_active(DOM::Document*);
+HTML::WorkerGlobalScope* web_bindings_worker_global_scope_from_global_object(JSObject* global_object);
+bool web_bindings_worker_global_scope_is_closing(HTML::WorkerGlobalScope*);
void web_bindings_with_temporary_execution_context(void* realm, bool callbacks_enabled, void* context, void (*steps)(void*));
bool web_bindings_object_implements_interface(JSObject* object, u16 interface_name);
JSObject* web_bindings_create_dom_exception(void* realm, ReadonlyBytes name, FlatPtr message);
@@ -154,6 +161,31 @@ bool web_bindings_realm_is_window(void* realm)
return HTML::window_from_global_object(static_cast<JS::Realm*>(realm)->global_object());
}
+HTML::Window* web_bindings_window_from_global_object(JSObject* global_object)
+{
+ return HTML::window_from_global_object(*JS::HostABI::object_from_abi(global_object));
+}
+
+DOM::Document* web_bindings_window_associated_document(HTML::Window* window)
+{
+ return &window->associated_document();
+}
+
+bool web_bindings_document_is_fully_active(DOM::Document* document)
+{
+ return document->is_fully_active();
+}
+
+HTML::WorkerGlobalScope* web_bindings_worker_global_scope_from_global_object(JSObject* global_object)
+{
+ return Bindings::worker_global_scope_from_global_object(*JS::HostABI::object_from_abi(global_object));
+}
+
+bool web_bindings_worker_global_scope_is_closing(HTML::WorkerGlobalScope* worker_global_scope)
+{
+ return worker_global_scope->is_closing();
+}
+
void web_bindings_with_temporary_execution_context(void* realm, bool callbacks_enabled, void* context, void (*steps)(void*))
{
HTML::TemporaryExecutionContext execution_context { *static_cast<JS::Realm*>(realm), callbacks_enabled ? HTML::TemporaryExecutionContext::CallbacksEnabled::Yes : HTML::TemporaryExecutionContext::CallbacksEnabled::No };