Skip to content

Truly independent web browser

LibJS: Ask whether a cell died only of the sweep in progress

Shannon Booth0ce469d98a81
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index 0bf69ea4f97..faabda4ad66 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -25,7 +25,7 @@ use crate::bytecode::generator::{LocalVariable, PendingClassBlueprint};
use crate::bytecode_cache::{DecodedBytecodeBytes, DecodedExecutableRecord};
use crate::gc::class::{Finalize, GcCell, define_cell};
use crate::gc::defer_gc::DeferGc;
-use crate::gc::heap::cell_is_dead;
+use crate::gc::sweep::Sweep;
use crate::gc::root::MarkedVec;
use crate::gc::visitor::{Trace, Visitor};
use crate::interpreter::vm::Vm;
@@ -129,12 +129,12 @@ impl PropertyLookupCacheEntryData {
}
}
- fn has_dead_cell(&self) -> bool {
- (self.key != 0 && cell_is_dead(Value::from_bits(self.key).as_cell()))
- || self.from_shape.is_some_and(cell_is_dead)
- || self.shape.is_some_and(cell_is_dead)
- || self.prototype.is_some_and(cell_is_dead)
- || self.prototype_chain_validity.is_some_and(cell_is_dead)
+ fn has_dead_cell(&self, sweep: &Sweep<'_>) -> bool {
+ (self.key != 0 && sweep.is_dead(Value::from_bits(self.key).as_cell()))
+ || self.from_shape.is_some_and(|shape| sweep.is_dead(shape))
+ || self.shape.is_some_and(|shape| sweep.is_dead(shape))
+ || self.prototype.is_some_and(|prototype| sweep.is_dead(prototype))
+ || self.prototype_chain_validity.is_some_and(|validity| sweep.is_dead(validity))
}
}
@@ -187,11 +187,11 @@ impl PropertyLookupCacheEntry {
}
/// Forgets the whole entry if one of its cells died in this collection, as clear_cache_entry_if_dead does.
- fn clear_if_it_has_a_dead_cell(&self) {
- if self.get().has_dead_cell() {
+ fn clear_if_it_has_a_dead_cell(&self, sweep: &Sweep<'_>) {
+ if self.get().has_dead_cell(sweep) {
self.set(PropertyLookupCacheEntryData::default());
}
- if self.getter.get().is_some_and(cell_is_dead) {
+ if self.getter.get().is_some_and(|getter| sweep.is_dead(getter)) {
self.getter.set(None);
}
}
@@ -651,8 +651,8 @@ impl PropertyLookupCache {
}
}
- /// Forgets the entries with a cell that died in this collection. Only the VM's sweep callback calls this.
- pub fn remove_dead_entries(&self) {
+ /// Forgets the entries with a cell that died in this collection.
+ pub fn remove_dead_entries(&self, sweep: &Sweep<'_>) {
if let Some(data) = self.megamorphic_data() {
for entry in data
.primary_entries
@@ -660,13 +660,13 @@ impl PropertyLookupCache {
.chain(&data.secondary_entries)
.chain(core::iter::once(&data.entry))
{
- entry.clear_if_it_has_a_dead_cell();
+ entry.clear_if_it_has_a_dead_cell(sweep);
}
return;
}
for entry in self.entries() {
- entry.clear_if_it_has_a_dead_cell();
+ entry.clear_if_it_has_a_dead_cell(sweep);
}
}
}
@@ -828,9 +828,9 @@ impl StaticPropertyLookupCaches {
&self.caches[site as usize]
}
- pub fn remove_dead_entries(&self) {
+ pub fn remove_dead_entries(&self, sweep: &Sweep<'_>) {
for cache in &self.caches {
- cache.remove_dead_entries();
+ cache.remove_dead_entries(sweep);
}
}
}
@@ -997,15 +997,15 @@ impl KeyedPropertyLookupCache {
}
}
- /// Forgets the entries with a cell that died in this collection. Only the VM's sweep callback calls this.
- pub fn remove_dead_entries(&self) {
+ /// Forgets the entries with a cell that died in this collection.
+ pub fn remove_dead_entries(&self, sweep: &Sweep<'_>) {
for entry in self.entries.borrow_mut().iter_mut() {
let lookup = &entry.lookup;
// NB: Entries of the keyed property store cache for puts it does not make have a shape but no type.
- if lookup.shape.is_some_and(cell_is_dead)
- || lookup.prototype.is_some_and(cell_is_dead)
- || lookup.prototype_chain_validity.is_some_and(cell_is_dead)
- || lookup.new_shape.is_some_and(cell_is_dead)
+ if lookup.shape.is_some_and(|shape| sweep.is_dead(shape))
+ || lookup.prototype.is_some_and(|prototype| sweep.is_dead(prototype))
+ || lookup.prototype_chain_validity.is_some_and(|validity| sweep.is_dead(validity))
+ || lookup.new_shape.is_some_and(|shape| sweep.is_dead(shape))
{
*entry = KeyedPropertyLookupCacheEntry::default();
}
@@ -1377,24 +1377,24 @@ impl Executable {
}
/// Forgets the cells the inline caches remember that died in this collection.
- pub fn remove_dead_cells(&self) {
+ pub fn remove_dead_cells(&self, sweep: &Sweep<'_>) {
for cache in &self.property_lookup_caches {
- cache.remove_dead_entries();
+ cache.remove_dead_entries(sweep);
}
for cache in self
.global_variable_caches
.iter()
.chain(self.dynamic_global_variable_caches.get().into_iter().flatten())
{
- cache.entry.clear_if_it_has_a_dead_cell();
+ cache.entry.clear_if_it_has_a_dead_cell(sweep);
}
for cache in &self.object_shape_caches {
- if cache.shape.get().is_some_and(cell_is_dead) {
+ if cache.shape.get().is_some_and(|shape| sweep.is_dead(shape)) {
cache.shape.set(None);
}
}
if let Some(feedback) = self.feedback() {
- feedback.remove_dead_cells();
+ feedback.remove_dead_cells(sweep);
}
}
diff --git a/Libraries/LibJS/Rust/src/bytecode/feedback.rs b/Libraries/LibJS/Rust/src/bytecode/feedback.rs
index fdb41cba4ae..bfdb4dfe4f7 100644
--- a/Libraries/LibJS/Rust/src/bytecode/feedback.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/feedback.rs
@@ -17,8 +17,8 @@ use core::ptr::NonNull;
use crate::build_configuration::HEAP_REGION_OFFSET_MASK;
use crate::gc::capi;
-use crate::gc::heap::cell_is_dead;
use crate::gc::primitive_storage::{OwnedPrimitiveStorage, ZeroFillNewBytes};
+use crate::gc::sweep::Sweep;
use crate::layout::cell::{CellHeader, Gc};
pub use crate::layout::feedback::*;
use crate::layout::object::Object;
@@ -110,10 +110,10 @@ impl ExecutableFeedback {
/// Forgets the call targets and keys that died in this collection, which the feedback holds weakly. The value
/// buckets are not references, but they are folded into the value feedback here as well.
- pub fn remove_dead_cells(&self) {
+ pub fn remove_dead_cells(&self, sweep: &Sweep<'_>) {
self.update_value_feedback();
let clear_if_dead = |cell: &Cell<Option<Gc<CellHeader>>>| {
- if decode_weak_reference(cell).is_some_and(cell_is_dead) {
+ if decode_weak_reference(cell).is_some_and(|cell| sweep.is_dead(cell)) {
cell.set(None);
}
};
diff --git a/Libraries/LibJS/Rust/src/debugger.rs b/Libraries/LibJS/Rust/src/debugger.rs
index 9bb6a1a0fa2..c3f8b523fbe 100644
--- a/Libraries/LibJS/Rust/src/debugger.rs
+++ b/Libraries/LibJS/Rust/src/debugger.rs
@@ -21,8 +21,8 @@ use crate::breakpoint::{Breakpoint, BreakpointID};
use crate::bytecode::basic_block::SourceMapEntry;
use crate::bytecode::executable::{Executable, LocalVariableMetadata};
use crate::gc::foreign::ForeignCellSlot;
-use crate::gc::heap::cell_is_dead;
use crate::gc::root::MarkedVec;
+use crate::gc::sweep::Sweep;
use crate::gc::visitor::{Trace, Visitor};
use crate::gc::weak::GcWeak;
use crate::interpreter::vm::{EvalMode, StackTraceElement, Vm};
@@ -643,10 +643,10 @@ impl Debugger {
}
/// Forgets the executables that died in this collection, as LibGC prunes a WeakHashSet.
- pub fn remove_dead_executables(&self) {
+ pub fn remove_dead_executables(&self, sweep: &Sweep<'_>) {
self.executables
.borrow_mut()
- .retain(|_, executable| executable.get().is_some_and(|executable| !cell_is_dead(executable)));
+ .retain(|_, executable| executable.get().is_some_and(|executable| !sweep.is_dead(executable)));
}
fn resolve_breakpoint(&self, breakpoint: &Breakpoint) {
diff --git a/Libraries/LibJS/Rust/src/gc/heap.rs b/Libraries/LibJS/Rust/src/gc/heap.rs
index a49a8dc4191..9c98d396d95 100644
--- a/Libraries/LibJS/Rust/src/gc/heap.rs
+++ b/Libraries/LibJS/Rust/src/gc/heap.rs
@@ -16,7 +16,7 @@ use super::class_id::{CLASS_COUNT, ClassId};
use super::defer_gc::DeferGc;
use super::weak::WEAK_IMPL_POINTER_OFFSET;
use crate::build_configuration::{HEAP_REGION_OFFSET_MASK, PRIMITIVE_STORAGE_CAGE_OFFSET_MASK};
-use crate::layout::cell::{CellHeader, CellState, Gc};
+use crate::layout::cell::{CellHeader, Gc};
/// A LibGC heap, with one allocator per class of cell.
///
@@ -618,14 +618,6 @@ impl Drop for Heap {
}
}
-/// Whether a cell that something holds without keeping it alive did not survive the collection in progress. Only
-/// meaningful in a sweep callback, when the marks are final and dead cells are not swept yet.
-pub fn cell_is_dead<T>(cell: Gc<T>) -> bool {
- // SAFETY: Every cell starts with its header, and a weakly held cell is intact until the sweep that follows.
- let header = unsafe { &*cell.as_ptr().cast::<CellHeader>() };
- header.state.get() != CellState::Live || !header.mark.get()
-}
-
/// Checks the layouts this crate mirrors against the LibGC it is linked with, and returns LibGC's layout.
fn check_layout() -> GCLayout {
let mut layout = GCLayout::default();
diff --git a/Libraries/LibJS/Rust/src/gc/mod.rs b/Libraries/LibJS/Rust/src/gc/mod.rs
index 137b4f25f57..ac61d455c8c 100644
--- a/Libraries/LibJS/Rust/src/gc/mod.rs
+++ b/Libraries/LibJS/Rust/src/gc/mod.rs
@@ -20,6 +20,7 @@ pub mod interpreter_buffer;
pub mod primitive_storage;
pub mod root;
pub mod shared_memory;
+pub mod sweep;
pub mod visitor;
pub mod weak;
pub mod weak_container;
diff --git a/Libraries/LibJS/Rust/src/gc/sweep.rs b/Libraries/LibJS/Rust/src/gc/sweep.rs
new file mode 100644
index 00000000000..8e581043d74
--- /dev/null
+++ b/Libraries/LibJS/Rust/src/gc/sweep.rs
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The sweep callback of a collection, which runs once the marks are final and before the cells that died are swept,
+//! and has what holds cells without keeping them alive forget the ones that died.
+
+use crate::interpreter::vm::Vm;
+use crate::layout::cell::{CellHeader, CellState, Gc};
+use crate::runtime::finalization_registry::FinalizationRegistry;
+
+/// What code can do while the sweep callback of a collection runs: tell which cells died, which only means anything
+/// then, and hand finalization registries over for cleanup. It gives no VM, as nothing may allocate meanwhile.
+pub struct Sweep<'vm>(&'vm Vm);
+
+impl<'vm> Sweep<'vm> {
+ /// # Safety
+ ///
+ /// Only the VM's sweep callback makes one, while the marks of its collection are final and the cells that died are
+ /// not swept yet.
+ pub(crate) unsafe fn new(vm: &'vm Vm) -> Self {
+ Self(vm)
+ }
+
+ /// Whether `cell`, which something held without keeping it alive, did not survive the collection.
+ pub fn is_dead<T>(&self, cell: Gc<T>) -> bool {
+ // SAFETY: Every cell starts with its header, and a cell that died is intact until the sweep that follows.
+ let header = unsafe { &*cell.as_ptr().cast::<CellHeader>() };
+ header.state.get() != CellState::Live || !header.mark.get()
+ }
+
+ /// Hands `finalization_registry` to HostEnqueueFinalizationRegistryCleanupJob once the collection is over.
+ pub fn enqueue_finalization_registry_cleanup_job(&self, finalization_registry: Gc<FinalizationRegistry>) {
+ self.0
+ .enqueue_finalization_registry_cleanup_job_after_collection(finalization_registry);
+ }
+}
diff --git a/Libraries/LibJS/Rust/src/gc/weak_container.rs b/Libraries/LibJS/Rust/src/gc/weak_container.rs
index a98c2f5cd6c..38366aacdcc 100644
--- a/Libraries/LibJS/Rust/src/gc/weak_container.rs
+++ b/Libraries/LibJS/Rust/src/gc/weak_container.rs
@@ -7,27 +7,14 @@
//! GC::WeakContainer: a cell that holds other cells without keeping them alive, and forgets them once they die. The
//! VM keeps a list of them, which its sweep callback prunes the way LibGC prunes its weak containers.
-use crate::interpreter::vm::Vm;
+use super::sweep::Sweep;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
-use crate::runtime::finalization_registry::FinalizationRegistry;
/// WeakContainer::remove_dead_cells(), called during every collection on each container that survived it, while the
-/// cells that died are intact and before they are swept. It gets no VM, as it must not allocate.
+/// cells that died are intact and before they are swept.
pub type RemoveDeadCells = fn(&Object, &Sweep<'_>);
-/// What a weak container can do while it removes its dead cells during a collection.
-pub struct Sweep<'vm>(&'vm Vm);
-
-impl Sweep<'_> {
- /// Hands `finalization_registry` to HostEnqueueFinalizationRegistryCleanupJob once the collection in progress is
- /// over.
- pub fn enqueue_finalization_registry_cleanup_job(&self, finalization_registry: Gc<FinalizationRegistry>) {
- self.0
- .enqueue_finalization_registry_cleanup_job_after_collection(finalization_registry);
- }
-}
-
pub struct WeakContainer {
owner: Gc<Object>,
remove_dead_cells: RemoveDeadCells,
@@ -45,7 +32,7 @@ impl WeakContainer {
self.owner
}
- pub(crate) fn remove_dead_cells(&self, vm: &Vm) {
- (self.remove_dead_cells)(&self.owner, &Sweep(vm));
+ pub(crate) fn remove_dead_cells(&self, sweep: &Sweep<'_>) {
+ (self.remove_dead_cells)(&self.owner, sweep);
}
}
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index ffa30fb0fa0..c5aa71b7d03 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -29,9 +29,10 @@ use crate::gc::capi::{self, GCVisitor};
use crate::gc::class::{GcCell, define_cell};
use crate::gc::foreign::ForeignCellSlot;
use crate::gc::gc_ref_cell::GcRefCell;
-use crate::gc::heap::{Heap, cell_is_dead};
+use crate::gc::heap::Heap;
use crate::gc::heap_function::HeapFunction;
use crate::gc::root::{MarkedVec, RootSet};
+use crate::gc::sweep::Sweep;
use crate::gc::visitor::{Trace, Visitor};
use crate::gc::weak_container::WeakContainer;
use crate::jit::code::INLINED_CALL_SITE_BIT;
@@ -1135,18 +1136,18 @@ impl Vm {
/// Has the weak containers that survived this collection forget the cells that died in it, as LibGC does before
/// it runs its sweep callbacks.
- fn remove_dead_cells_from_weak_containers(&self) {
+ fn remove_dead_cells_from_weak_containers(&self, sweep: &Sweep<'_>) {
self.weak_containers.borrow_mut().retain(|weak_container| {
- if cell_is_dead(weak_container.owner()) {
+ if sweep.is_dead(weak_container.owner()) {
return false;
}
- weak_container.remove_dead_cells(self);
+ weak_container.remove_dead_cells(sweep);
true
});
}
/// Hands `finalization_registry` to HostEnqueueFinalizationRegistryCleanupJob once the collection in progress is
- /// over. Only a weak container's remove_dead_cells() calls this, through its Sweep.
+ /// over. Only the sweep callback calls this, through its Sweep.
pub(crate) fn enqueue_finalization_registry_cleanup_job_after_collection(
&self,
finalization_registry: Gc<FinalizationRegistry>,
@@ -1178,12 +1179,12 @@ impl Vm {
}
/// Forgets the intrinsic accessors and unimplemented properties of objects that died in this collection.
- fn remove_dead_objects_from_intrinsic_accessors_and_unimplemented_properties(&self) {
+ fn remove_dead_objects_from_intrinsic_accessors_and_unimplemented_properties(&self, sweep: &Sweep<'_>) {
let is_live = |object: usize| {
// SAFETY: The maps only hold objects that were live when their entries were added, and a dead object is
// intact until the sweep that follows this callback.
let object = unsafe { Gc::from_non_null(NonNull::new_unchecked(object as *mut Object)) };
- !cell_is_dead(object)
+ !sweep.is_dead(object)
};
self.intrinsic_accessors
.borrow_mut()
@@ -1357,8 +1358,8 @@ impl Vm {
}
/// Drops the weak cache entries of strings that died in this collection.
- fn remove_dead_strings_from_weak_caches(&self) {
- let is_dead = |string: Gc<PrimitiveString>| !string.header.mark.get();
+ fn remove_dead_strings_from_weak_caches(&self, sweep: &Sweep<'_>) {
+ let is_dead = |string: Gc<PrimitiveString>| sweep.is_dead(string);
for cache_slot in self.fly_string_cache.iter() {
if cache_slot.get().is_some_and(is_dead) {
cache_slot.set(None);
@@ -1373,20 +1374,20 @@ impl Vm {
}
/// Forgets the cells that died in this collection from the inline caches of executables and static call sites.
- fn remove_dead_cells_from_property_lookup_caches(&self) {
+ fn remove_dead_cells_from_property_lookup_caches(&self, sweep: &Sweep<'_>) {
self.executables.borrow_mut().retain(|executable| {
- if cell_is_dead(*executable) {
+ if sweep.is_dead(*executable) {
executable.free_jit_entry_slot(self);
return false;
}
- executable.remove_dead_cells();
+ executable.remove_dead_cells(sweep);
true
});
- self.static_property_lookup_caches.remove_dead_entries();
- self.keyed_property_lookup_cache.remove_dead_entries();
- self.prototype_transition_cache.remove_dead_entries();
- self.keyed_property_store_cache.remove_dead_entries();
- self.jit.dependents.remove_dead_entries();
+ self.static_property_lookup_caches.remove_dead_entries(sweep);
+ self.keyed_property_lookup_cache.remove_dead_entries(sweep);
+ self.prototype_transition_cache.remove_dead_entries(sweep);
+ self.keyed_property_store_cache.remove_dead_entries(sweep);
+ self.jit.dependents.remove_dead_entries(sweep);
}
pub fn register_executable(&self, executable: Gc<Executable>) {
@@ -2031,12 +2032,14 @@ unsafe extern "C" fn gather_roots(context: *mut c_void, visitor: *mut GCVisitor)
unsafe extern "C" fn sweep(context: *mut c_void) {
// SAFETY: The sweep callback was registered with the VM as its context.
let vm = unsafe { &*context.cast::<Vm>() };
- vm.remove_dead_cells_from_weak_containers();
- vm.remove_dead_strings_from_weak_caches();
- vm.remove_dead_cells_from_property_lookup_caches();
- vm.remove_dead_objects_from_intrinsic_accessors_and_unimplemented_properties();
+ // SAFETY: LibGC runs sweep callbacks once the marks are final, before it sweeps the cells that died.
+ let sweep = unsafe { Sweep::new(vm) };
+ vm.remove_dead_cells_from_weak_containers(&sweep);
+ vm.remove_dead_strings_from_weak_caches(&sweep);
+ vm.remove_dead_cells_from_property_lookup_caches(&sweep);
+ vm.remove_dead_objects_from_intrinsic_accessors_and_unimplemented_properties(&sweep);
if let Some(debugger) = vm.debugger() {
- debugger.remove_dead_executables();
+ debugger.remove_dead_executables(&sweep);
}
}
diff --git a/Libraries/LibJS/Rust/src/jit/dependencies.rs b/Libraries/LibJS/Rust/src/jit/dependencies.rs
index e18d19e8423..b3993db082c 100644
--- a/Libraries/LibJS/Rust/src/jit/dependencies.rs
+++ b/Libraries/LibJS/Rust/src/jit/dependencies.rs
@@ -22,7 +22,7 @@ pub use libjs_jit::snapshot::CellId;
use super::code::CompileState;
use crate::bytecode::executable::Executable;
-use crate::gc::heap::cell_is_dead;
+use crate::gc::sweep::Sweep;
use crate::gc::weak::GcWeak;
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
@@ -87,7 +87,7 @@ impl Dependents {
}
/// Forgets the entries of code whose executable died, and the slots of global objects that died.
- pub fn remove_dead_entries(&self) {
+ pub fn remove_dead_entries(&self, sweep: &Sweep<'_>) {
self.code.borrow_mut().retain(|_, dependents| {
dependents.retain(|dependent| dependent.executable.get().is_some());
!dependents.is_empty()
@@ -96,7 +96,7 @@ impl Dependents {
// SAFETY: The map only holds objects that were live when their entries were added, and a dead object is
// intact until the sweep that follows this callback.
let object = unsafe { Gc::from_non_null(NonNull::new_unchecked(object as *mut Object)) };
- !cell_is_dead(object)
+ !sweep.is_dead(object)
});
}
}
diff --git a/Libraries/LibJS/Rust/src/jit/disabled.rs b/Libraries/LibJS/Rust/src/jit/disabled.rs
index 8ea50b762ce..49657040ec6 100644
--- a/Libraries/LibJS/Rust/src/jit/disabled.rs
+++ b/Libraries/LibJS/Rust/src/jit/disabled.rs
@@ -251,7 +251,7 @@ pub mod dependencies {
pub struct Dependents;
impl Dependents {
- pub fn remove_dead_entries(&self) {}
+ pub fn remove_dead_entries(&self, _: &crate::gc::sweep::Sweep<'_>) {}
}
pub fn invalidate_dependents(_vm: &Vm, _dependency: Dependency) {}
diff --git a/Libraries/LibJS/Rust/src/runtime/finalization_registry.rs b/Libraries/LibJS/Rust/src/runtime/finalization_registry.rs
index 0593a62e6da..d78228b6e08 100644
--- a/Libraries/LibJS/Rust/src/runtime/finalization_registry.rs
+++ b/Libraries/LibJS/Rust/src/runtime/finalization_registry.rs
@@ -10,9 +10,9 @@ use libjs_runtime_macros::Trace;
use crate::gc::class::{Finalize, GcCell, define_cell};
use crate::gc::gc_ref_cell::GcRefCell;
-use crate::gc::heap::cell_is_dead;
use crate::gc::visitor::{Trace, Visitor};
-use crate::gc::weak_container::{Sweep, WeakContainer};
+use crate::gc::sweep::Sweep;
+use crate::gc::weak_container::WeakContainer;
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
use crate::layout::object::Object;
@@ -159,7 +159,7 @@ impl FinalizationRegistry {
let Some(target) = record.target else {
continue;
};
- if !cell_is_dead(target) {
+ if !sweep.is_dead(target) {
continue;
}
record.target = None;
diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rs
index 738fbadde83..c8a378616a9 100644
--- a/Libraries/LibJS/Rust/src/runtime/shape.rs
+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs
@@ -12,7 +12,7 @@ use indexmap::IndexMap;
use crate::gc::class::{GcCell, define_cell};
use crate::gc::gc_ref_cell::GcRefCell;
-use crate::gc::heap::cell_is_dead;
+use crate::gc::sweep::Sweep;
use crate::gc::root::MarkedVec;
use crate::gc::visitor::{Trace, Visitor};
use crate::gc::weak::GcWeak;
@@ -279,10 +279,11 @@ impl PrototypeTransitionCache {
});
}
- pub fn remove_dead_entries(&self) {
+ pub fn remove_dead_entries(&self, sweep: &Sweep<'_>) {
for entry in &self.entries {
let transition = entry.get();
- if transition.from.is_some_and(cell_is_dead) || transition.target.is_some_and(cell_is_dead) {
+ let is_dead = |shape| sweep.is_dead(shape);
+ if transition.from.is_some_and(is_dead) || transition.target.is_some_and(is_dead) {
entry.set(PrototypeTransition::default());
}
}
diff --git a/Libraries/LibJS/Rust/src/runtime/weak_map.rs b/Libraries/LibJS/Rust/src/runtime/weak_map.rs
index 35cd5cd3f37..a4d7aa7bdc7 100644
--- a/Libraries/LibJS/Rust/src/runtime/weak_map.rs
+++ b/Libraries/LibJS/Rust/src/runtime/weak_map.rs
@@ -11,9 +11,9 @@ use libjs_runtime_macros::Trace;
use crate::gc::class::{ExternalMemorySize, Finalize, GcCell, define_cell};
use crate::gc::gc_ref_cell::GcRefCell;
-use crate::gc::heap::cell_is_dead;
use crate::gc::visitor::{Trace, Visitor};
-use crate::gc::weak_container::{Sweep, WeakContainer};
+use crate::gc::sweep::Sweep;
+use crate::gc::weak_container::WeakContainer;
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
use crate::layout::object::Object;
@@ -120,11 +120,11 @@ impl WeakMap {
self.values.borrow().0.len()
}
- fn remove_dead_cells(object: &Object, _: &Sweep<'_>) {
+ fn remove_dead_cells(object: &Object, sweep: &Sweep<'_>) {
let weak_map = object
.as_gc()
.downcast::<WeakMap>()
.expect("the weak container is a WeakMap");
- weak_map.values.borrow_mut().0.retain(|&key, _| !cell_is_dead(key));
+ weak_map.values.borrow_mut().0.retain(|&key, _| !sweep.is_dead(key));
}
}
diff --git a/Libraries/LibJS/Rust/src/runtime/weak_ref.rs b/Libraries/LibJS/Rust/src/runtime/weak_ref.rs
index b480fe983a6..ca5732b5a95 100644
--- a/Libraries/LibJS/Rust/src/runtime/weak_ref.rs
+++ b/Libraries/LibJS/Rust/src/runtime/weak_ref.rs
@@ -8,9 +8,9 @@ use core::cell::Cell;
use core::ptr::NonNull;
use crate::gc::class::{GcCell, define_cell};
-use crate::gc::heap::cell_is_dead;
use crate::gc::visitor::{Trace, Visitor};
-use crate::gc::weak_container::{Sweep, WeakContainer};
+use crate::gc::sweep::Sweep;
+use crate::gc::weak_container::WeakContainer;
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
@@ -78,13 +78,13 @@ impl WeakRef {
self.last_execution_generation.set(vm.head.execution_generation.get());
}
- fn remove_dead_cells(object: &Object, _: &Sweep<'_>) {
+ fn remove_dead_cells(object: &Object, sweep: &Sweep<'_>) {
let weak_ref = object
.as_gc()
.downcast::<WeakRef>()
.expect("the weak container is a WeakRef");
let value = weak_ref.value.get();
- let is_alive = value.is_empty() || !cell_is_dead(value.as_cell());
+ let is_alive = value.is_empty() || !sweep.is_dead(value.as_cell());
if is_alive {
return;
}
diff --git a/Libraries/LibJS/Rust/src/runtime/weak_set.rs b/Libraries/LibJS/Rust/src/runtime/weak_set.rs
index 01fe8c94925..a584dd3b9ab 100644
--- a/Libraries/LibJS/Rust/src/runtime/weak_set.rs
+++ b/Libraries/LibJS/Rust/src/runtime/weak_set.rs
@@ -11,9 +11,9 @@ use libjs_runtime_macros::Trace;
use crate::gc::class::{ExternalMemorySize, Finalize, GcCell, define_cell};
use crate::gc::gc_ref_cell::GcRefCell;
-use crate::gc::heap::cell_is_dead;
use crate::gc::visitor::{Trace, Visitor};
-use crate::gc::weak_container::{Sweep, WeakContainer};
+use crate::gc::sweep::Sweep;
+use crate::gc::weak_container::WeakContainer;
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
use crate::layout::object::Object;
@@ -111,11 +111,11 @@ impl WeakSet {
self.values.borrow().0.len()
}
- fn remove_dead_cells(object: &Object, _: &Sweep<'_>) {
+ fn remove_dead_cells(object: &Object, sweep: &Sweep<'_>) {
let weak_set = object
.as_gc()
.downcast::<WeakSet>()
.expect("the weak container is a WeakSet");
- weak_set.values.borrow_mut().0.retain(|&cell| !cell_is_dead(cell));
+ weak_set.values.borrow_mut().0.retain(|&cell| !sweep.is_dead(cell));
}
}