Skip to content

Truly independent web browser

LibJS: Root a cell for as long as a value lives

Shannon Bootha83eb443b0ea
diff --git a/Libraries/LibJS/Rust/src/api.rs b/Libraries/LibJS/Rust/src/api.rs
index d91d12b1311..caef7457f30 100644
--- a/Libraries/LibJS/Rust/src/api.rs
+++ b/Libraries/LibJS/Rust/src/api.rs
@@ -18,7 +18,7 @@ pub use crate::gc::defer_gc::DeferGc;
pub use crate::gc::embedder_cell::{EmbedderCell, EmbedderClass, EmbedderValue};
pub use crate::gc::foreign::ForeignCellSlot;
pub use crate::gc::gc_ref_cell::{GcRefCell, GcRefMut};
-pub use crate::gc::root::{MarkedVec, Root};
+pub use crate::gc::root::{CellRoot, MarkedVec, Root};
pub use crate::gc::visitor::{Trace, Visitor};
pub use crate::gc::weak::GcWeak;
pub use crate::interpreter::vm::{TypeErrorRealmScope, Vm};
diff --git a/Libraries/LibJS/Rust/src/gc/lint_markers.rs b/Libraries/LibJS/Rust/src/gc/lint_markers.rs
index b1141f3df8c..ade79e4fb9c 100644
--- a/Libraries/LibJS/Rust/src/gc/lint_markers.rs
+++ b/Libraries/LibJS/Rust/src/gc/lint_markers.rs
@@ -10,7 +10,7 @@
use super::defer_gc::{DeferGc, Deferred};
use super::gc_ref_cell::GcRefCell;
-use super::root::{MarkedVec, Root};
+use super::root::{CellRoot, MarkedVec, Root};
use super::sweep::Sweep;
use super::visitor::Trace;
use crate::interpreter::vm::Vm;
@@ -26,6 +26,8 @@ pub unsafe trait KeepsAlive {}
unsafe impl<T: Trace + 'static> KeepsAlive for Root<'_, T> {}
// SAFETY: The values are held by a Root.
unsafe impl<T: Trace + Clone + 'static> KeepsAlive for MarkedVec<'_, T> {}
+// SAFETY: The cell is a root of LibGC while the CellRoot lives.
+unsafe impl<T> KeepsAlive for CellRoot<T> {}
// SAFETY: The value cannot outlive the deferral it is tied to, while which nothing is collected.
unsafe impl<T> KeepsAlive for Deferred<'_, T> {}
// SAFETY: The VM is the root set: the heap traces it through gather_roots() wherever it is held.
diff --git a/Libraries/LibJS/Rust/src/gc/root.rs b/Libraries/LibJS/Rust/src/gc/root.rs
index 772010252a4..211307d8fe8 100644
--- a/Libraries/LibJS/Rust/src/gc/root.rs
+++ b/Libraries/LibJS/Rust/src/gc/root.rs
@@ -7,11 +7,15 @@
//! Precise roots for values that live outside cells and outside the stack, where the collector would not find them.
use core::cell::{OnceCell, Ref, RefCell};
+use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};
+use core::ptr::NonNull;
+use super::capi::{self, GCRoot};
use super::gc_ref_cell::{GcRefCell, GcRefMut};
use super::visitor::{Trace, Visitor};
use crate::interpreter::vm::Vm;
+use crate::layout::cell::Gc;
/// The roots a VM keeps alive. Entries are boxed values registered by address, so slots never move.
#[derive(Default)]
@@ -77,6 +81,32 @@ impl<T: Trace + 'static> Drop for Root<'_, T> {
}
}
+/// Keeps a cell alive for as long as it exists, through a root of LibGC, without borrowing the VM: for a cell to hold
+/// while it must stay alive without being referenced, and to drop once it may be collected.
+pub struct CellRoot<T> {
+ root: NonNull<GCRoot>,
+ // NB: A CellRoot holds its cell through LibGC, not through a pointer of its own.
+ _cell: PhantomData<fn() -> Gc<T>>,
+}
+
+impl<T> CellRoot<T> {
+ pub fn new(cell: Gc<T>) -> Self {
+ // SAFETY: The cell is live. The root comes with a reference this value owns.
+ let root = unsafe { capi::gc_root_create(cell.as_ptr().cast()) };
+ Self {
+ root: NonNull::new(root).expect("LibGC creates the root"),
+ _cell: PhantomData,
+ }
+ }
+}
+
+impl<T> Drop for CellRoot<T> {
+ fn drop(&mut self) {
+ // SAFETY: This value owns the root.
+ unsafe { capi::gc_root_destroy(self.root.as_ptr()) };
+ }
+}
+
/// A growable list of values that are kept alive, for the temporaries a Vec would hide from the collector. Elements
/// are copied in and out, or lent by values() and values_mut(), while which the list cannot change. The list registers
/// its root with the first element, so an empty one costs nothing.