Skip to content

Truly independent web browser

LibJS: Implement Trace for what owners traced field by field

Shannon Booth34f11e71d5f1
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index 7b15571f463..b2ab3203bfe 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -2014,6 +2014,14 @@ impl Drop for Vm {
}
}
+// SAFETY: gather_roots() visits the VM's roots. The caches it skips are weak, and the VM clears them of dead cells after
+// each collection.
+unsafe impl Trace for Vm {
+ fn trace(&self, visitor: &mut Visitor) {
+ self.gather_roots(visitor);
+ }
+}
+
unsafe extern "C" fn gather_roots(context: *mut c_void, visitor: *mut GCVisitor) {
// SAFETY: The heap was created with the VM as its context, and LibGC passes a live visitor.
let (vm, mut visitor) = unsafe { (&*context.cast::<Vm>(), Visitor::from_raw(visitor)) };
diff --git a/Libraries/LibJS/Rust/src/jit/code.rs b/Libraries/LibJS/Rust/src/jit/code.rs
index ad5986441bc..cf9646fbe0a 100644
--- a/Libraries/LibJS/Rust/src/jit/code.rs
+++ b/Libraries/LibJS/Rust/src/jit/code.rs
@@ -12,6 +12,7 @@ use core::ffi::c_void;
use libjs_jit::bytecode::{EnvironmentCoordinate, decode_instruction};
pub use libjs_jit::code::INLINED_CALL_SITE_BIT;
use libjs_jit::code::{ExitKind, Site};
+use libjs_runtime_macros::Trace;
use super::entry_table::{JIT_ENTRY_SLOT_MASK, JitEntryTable, NO_JIT_ENTRY_SLOT};
use super::executable_memory::ExecutableMemory;
@@ -86,17 +87,20 @@ impl CompileState {
/// An executable of a compile job's snapshot, by index. Index 0 is the compiled executable (without a function); the
/// others are inlining candidates, whose frames exits materialize.
-#[derive(Clone)]
+#[derive(Clone, Trace)]
pub struct SnapshotExecutable {
pub executable: Gc<Executable>,
pub function: Option<InlinedFunction>,
/// How many sites where JIT code exited the executable had recorded when the code was compiled, which the code
/// knew of (see `Executable::add_jit_exit_site()`). The sites are only ever appended to.
+ #[gc(untraced)]
pub known_exit_sites: usize,
/// For the compiled executable, how many sites in inlined builtins it had recorded then.
+ #[gc(untraced)]
pub known_builtin_exit_sites: usize,
/// The size of each property lookup cache of the executable when the code was compiled (see
/// `speculated_cache_size()`): how many shapes the code speculated on there.
+ #[gc(untraced)]
pub property_cache_sizes: Box<[u8]>,
}
@@ -115,22 +119,13 @@ pub fn speculated_cache_size(cache: &PropertyLookupCache) -> u8 {
}
/// The function object of an inlined executable, which its materialized frames run.
-#[derive(Clone, Copy, PartialEq, Eq, Hash)]
+#[derive(Clone, Copy, PartialEq, Eq, Hash, Trace)]
pub enum InlinedFunction {
Ecmascript(Gc<EcmascriptFunctionObject>),
/// A builtin written in JavaScript, whose frames are set up like those of other builtins.
Builtin(Gc<NativeJavaScriptBackedFunction>),
}
-impl InlinedFunction {
- pub fn visit(self, visitor: &mut Visitor) {
- match self {
- InlinedFunction::Ecmascript(function) => visitor.visit(function),
- InlinedFunction::Builtin(function) => visitor.visit(function),
- }
- }
-}
-
/// Installed machine code for one executable, with what exits need to rebuild interpreter frames. While JIT code runs
/// frames of its executable, it stays attached to the executable (in its slot of the JIT entry table, where
/// `libjs_jit_exit()` finds it through the frame's executable), even once discarded.
@@ -377,12 +372,7 @@ unsafe impl Trace for JitCode {
for materialized in self.materialized_objects.borrow().iter() {
visitor.visit(materialized.object);
}
- for executable in &self.snapshot_executables {
- visitor.visit(executable.executable);
- if let Some(function) = executable.function {
- function.visit(visitor);
- }
- }
+ self.snapshot_executables.trace(visitor);
}
}
diff --git a/Libraries/LibJS/Rust/src/jit/mod.rs b/Libraries/LibJS/Rust/src/jit/mod.rs
index c592d3865f5..83b805a0724 100644
--- a/Libraries/LibJS/Rust/src/jit/mod.rs
+++ b/Libraries/LibJS/Rust/src/jit/mod.rs
@@ -33,6 +33,8 @@ use core::ptr::NonNull;
use std::collections::{BTreeMap, HashMap};
use std::rc::Rc;
+use libjs_runtime_macros::Trace;
+
use crate::bytecode::executable::Executable;
use crate::gc::defer_gc::{DeferGc, Deferred};
use crate::gc::visitor::{Trace, Visitor};
@@ -111,6 +113,7 @@ impl InterpreterTier {
/// A compile job running on a compile thread. The VM keeps the executable and every cell its snapshot refers to
/// alive until the job's code is installed or the job is abandoned.
+#[derive(Trace)]
pub struct CompileJob {
pub executable: Gc<Executable>,
pub cells: Vec<Gc<CellHeader>>,
@@ -456,16 +459,7 @@ impl Drop for JitState {
unsafe impl Trace for JitState {
fn trace(&self, visitor: &mut Visitor) {
for job in self.jobs.borrow().values() {
- visitor.visit(job.executable);
- for cell in &job.cells {
- visitor.visit(*cell);
- }
- for executable in &job.snapshot_executables {
- visitor.visit(executable.executable);
- if let Some(function) = executable.function {
- function.visit(visitor);
- }
- }
+ job.trace(visitor);
}
}
}
diff --git a/Libraries/LibJS/Rust/src/runtime/json_object.rs b/Libraries/LibJS/Rust/src/runtime/json_object.rs
index cee72d8010f..a64c445474d 100644
--- a/Libraries/LibJS/Rust/src/runtime/json_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/json_object.rs
@@ -244,15 +244,18 @@ impl Default for JsonStringifyCache {
}
}
+// SAFETY: The objects being serialized are the only cells the cache keeps alive; its shapes are weak.
+unsafe impl Trace for JsonStringifyCache {
+ fn trace(&self, visitor: &mut Visitor) {
+ self.objects.borrow().trace(visitor);
+ }
+}
+
impl JsonStringifyCache {
const ENTRY_COUNT: usize = 64;
/// The most that the builder of a JSON text starts out with room for.
const MAXIMUM_INITIAL_TEXT_CAPACITY: usize = 64 * 1024;
- pub fn trace(&self, visitor: &mut Visitor) {
- self.objects.borrow().trace(visitor);
- }
-
fn slot(shape: Gc<Shape>) -> usize {
let address = address_of(shape) as u64;
(address ^ (address >> 6) ^ (address >> 12)) as usize % Self::ENTRY_COUNT
diff --git a/Libraries/LibJS/Rust/src/runtime/json_text_parser.rs b/Libraries/LibJS/Rust/src/runtime/json_text_parser.rs
index f5839507eb9..ff422e4bea4 100644
--- a/Libraries/LibJS/Rust/src/runtime/json_text_parser.rs
+++ b/Libraries/LibJS/Rust/src/runtime/json_text_parser.rs
@@ -110,8 +110,10 @@ pub struct JsonParseCache {
tables: Cell<Option<Box<JsonParseTables>>>,
}
-impl JsonParseCache {
- pub fn trace(&self, visitor: &mut Visitor) {
+// SAFETY: The values being parsed are the only cells the cache keeps alive; its keys are strings and its shapes are
+// weak.
+unsafe impl Trace for JsonParseCache {
+ fn trace(&self, visitor: &mut Visitor) {
self.values.borrow().trace(visitor);
}
}
diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rs
index 886df9a13c2..738fbadde83 100644
--- a/Libraries/LibJS/Rust/src/runtime/shape.rs
+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs
@@ -150,8 +150,7 @@ impl Default for ShapeStorage {
}
// SAFETY: Visits the descriptor array, the keys this storage owns, the previous shape, the special property cache and
-// the watchpoint sets of slots. The shapes of transitions are
-// weak, and so are the keys of prototype transitions, which are only compared.
+// the rare data. The shapes of transitions are weak.
unsafe impl Trace for ShapeStorage {
fn trace(&self, visitor: &mut Visitor) {
self.previous.trace(visitor);
@@ -178,25 +177,37 @@ unsafe impl Trace for ShapeStorage {
}
}
+ self.rare_data.trace(visitor);
+ }
+}
+
+// SAFETY: Visits the keys.
+unsafe impl Trace for OwnStringKeys {
+ fn trace(&self, visitor: &mut Visitor) {
+ visitor.visit_values(&self.keys);
+ }
+}
+
+// SAFETY: Visits the keys of delete transitions, the own string keys, the assign copy, the shapes of integrity levels and
+// the watchpoint sets of slots. The shapes of transitions and child prototype shapes are weak, and so are the keys of
+// prototype transitions, which are only compared.
+unsafe impl Trace for RareData {
+ fn trace(&self, visitor: &mut Visitor) {
// FIXME: The delete transition keys should be weak, but we have to mark them for now in case they go stale.
- if let Some(rare_data) = &*self.rare_data.borrow() {
- for property_key in rare_data.delete_transitions.keys() {
- property_key.trace(visitor);
- }
- if let Some(own_string_keys) = &rare_data.own_string_keys {
- visitor.visit_values(&own_string_keys.keys);
- }
- if let Some(assign_copy) = &rare_data.assign_copy {
- visitor.visit(assign_copy.prototype_validity);
- visitor.visit(assign_copy.copy_shape);
- }
- for shape in rare_data.integrity_level_shapes.iter().flatten() {
- visitor.visit(*shape);
- }
- for watched_slot in rare_data.watched_slots.values() {
- if let WatchedSlot::Watched(watchpoint_set) = watched_slot {
- visitor.visit(*watchpoint_set);
- }
+ for property_key in self.delete_transitions.keys() {
+ property_key.trace(visitor);
+ }
+ self.own_string_keys.trace(visitor);
+ if let Some(assign_copy) = &self.assign_copy {
+ visitor.visit(assign_copy.prototype_validity);
+ visitor.visit(assign_copy.copy_shape);
+ }
+ for shape in self.integrity_level_shapes.iter().flatten() {
+ visitor.visit(*shape);
+ }
+ for watched_slot in self.watched_slots.values() {
+ if let WatchedSlot::Watched(watchpoint_set) = watched_slot {
+ visitor.visit(*watchpoint_set);
}
}
}