Skip to content

Truly independent web browser

LibJS: Give weak containers no VM while they remove dead cells

Shannon Booth8b4f51eed641
diff --git a/Libraries/LibJS/Rust/src/gc/weak_container.rs b/Libraries/LibJS/Rust/src/gc/weak_container.rs
index 5b85e3218cf..a98c2f5cd6c 100644
--- a/Libraries/LibJS/Rust/src/gc/weak_container.rs
+++ b/Libraries/LibJS/Rust/src/gc/weak_container.rs
@@ -10,10 +10,23 @@
use crate::interpreter::vm::Vm;
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 must not allocate.
-pub type RemoveDeadCells = fn(&Object, &Vm);
+/// cells that died are intact and before they are swept. It gets no VM, as it must not allocate.
+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>,
@@ -32,7 +45,7 @@ impl WeakContainer {
self.owner
}
- pub fn remove_dead_cells(&self, vm: &Vm) {
- (self.remove_dead_cells)(&self.owner, vm);
+ pub(crate) fn remove_dead_cells(&self, vm: &Vm) {
+ (self.remove_dead_cells)(&self.owner, &Sweep(vm));
}
}
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index b2ab3203bfe..ffa30fb0fa0 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -1146,8 +1146,8 @@ impl Vm {
}
/// Hands `finalization_registry` to HostEnqueueFinalizationRegistryCleanupJob once the collection in progress is
- /// over. Only a weak container's remove_dead_cells() calls this.
- pub fn enqueue_finalization_registry_cleanup_job_after_collection(
+ /// over. Only a weak container's remove_dead_cells() calls this, through its Sweep.
+ pub(crate) fn enqueue_finalization_registry_cleanup_job_after_collection(
&self,
finalization_registry: Gc<FinalizationRegistry>,
) {
diff --git a/Libraries/LibJS/Rust/src/runtime/finalization_registry.rs b/Libraries/LibJS/Rust/src/runtime/finalization_registry.rs
index 75c69aa25e6..0593a62e6da 100644
--- a/Libraries/LibJS/Rust/src/runtime/finalization_registry.rs
+++ b/Libraries/LibJS/Rust/src/runtime/finalization_registry.rs
@@ -12,7 +12,7 @@ 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::WeakContainer;
+use crate::gc::weak_container::{Sweep, WeakContainer};
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
use crate::layout::object::Object;
@@ -149,7 +149,7 @@ impl FinalizationRegistry {
self.records.borrow().0.iter().any(|record| record.target.is_none())
}
- fn remove_dead_cells(object: &Object, vm: &Vm) {
+ fn remove_dead_cells(object: &Object, sweep: &Sweep<'_>) {
let finalization_registry = object
.as_gc()
.downcast::<FinalizationRegistry>()
@@ -168,7 +168,7 @@ impl FinalizationRegistry {
if any_cells_were_removed {
// NOTE: The VM keeps the FinalizationRegistry alive even if a subsequent GC is triggered before the
// callback has a chance to run.
- vm.enqueue_finalization_registry_cleanup_job_after_collection(finalization_registry);
+ sweep.enqueue_finalization_registry_cleanup_job(finalization_registry);
}
}
diff --git a/Libraries/LibJS/Rust/src/runtime/weak_map.rs b/Libraries/LibJS/Rust/src/runtime/weak_map.rs
index b86dfd98fe1..35cd5cd3f37 100644
--- a/Libraries/LibJS/Rust/src/runtime/weak_map.rs
+++ b/Libraries/LibJS/Rust/src/runtime/weak_map.rs
@@ -13,7 +13,7 @@ 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::WeakContainer;
+use crate::gc::weak_container::{Sweep, WeakContainer};
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
use crate::layout::object::Object;
@@ -120,7 +120,7 @@ impl WeakMap {
self.values.borrow().0.len()
}
- fn remove_dead_cells(object: &Object, _: &Vm) {
+ fn remove_dead_cells(object: &Object, _: &Sweep<'_>) {
let weak_map = object
.as_gc()
.downcast::<WeakMap>()
diff --git a/Libraries/LibJS/Rust/src/runtime/weak_ref.rs b/Libraries/LibJS/Rust/src/runtime/weak_ref.rs
index 67634f57182..b480fe983a6 100644
--- a/Libraries/LibJS/Rust/src/runtime/weak_ref.rs
+++ b/Libraries/LibJS/Rust/src/runtime/weak_ref.rs
@@ -10,7 +10,7 @@ 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::WeakContainer;
+use crate::gc::weak_container::{Sweep, WeakContainer};
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
@@ -78,7 +78,7 @@ impl WeakRef {
self.last_execution_generation.set(vm.head.execution_generation.get());
}
- fn remove_dead_cells(object: &Object, _: &Vm) {
+ fn remove_dead_cells(object: &Object, _: &Sweep<'_>) {
let weak_ref = object
.as_gc()
.downcast::<WeakRef>()
diff --git a/Libraries/LibJS/Rust/src/runtime/weak_set.rs b/Libraries/LibJS/Rust/src/runtime/weak_set.rs
index ac78510ed5f..01fe8c94925 100644
--- a/Libraries/LibJS/Rust/src/runtime/weak_set.rs
+++ b/Libraries/LibJS/Rust/src/runtime/weak_set.rs
@@ -13,7 +13,7 @@ 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::WeakContainer;
+use crate::gc::weak_container::{Sweep, WeakContainer};
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
use crate::layout::object::Object;
@@ -111,7 +111,7 @@ impl WeakSet {
self.values.borrow().0.len()
}
- fn remove_dead_cells(object: &Object, _: &Vm) {
+ fn remove_dead_cells(object: &Object, _: &Sweep<'_>) {
let weak_set = object
.as_gc()
.downcast::<WeakSet>()