Skip to content

Truly independent web browser

LibJS: Move DeferGc to the heap

Shannon Boothb5435ddb9a23
diff --git a/Libraries/LibJS/Rust/src/gc/defer_gc.rs b/Libraries/LibJS/Rust/src/gc/defer_gc.rs
new file mode 100644
index 00000000000..2afbcaa7258
--- /dev/null
+++ b/Libraries/LibJS/Rust/src/gc/defer_gc.rs
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+use super::capi;
+use super::heap::Heap;
+use crate::interpreter::vm::Vm;
+
+/// Keeps the heap from collecting garbage while it lives.
+pub struct DeferGc<'heap>(&'heap Heap);
+
+impl<'heap> DeferGc<'heap> {
+ pub(crate) fn new(vm: &'heap Vm) -> Self {
+ Self::of_heap(vm.heap())
+ }
+
+ pub(super) fn of_heap(heap: &'heap Heap) -> Self {
+ capi::gc_heap_defer_gc(heap.handle());
+ Self(heap)
+ }
+}
+
+impl Drop for DeferGc<'_> {
+ fn drop(&mut self) {
+ capi::gc_heap_undefer_gc(self.0.handle());
+ }
+}
diff --git a/Libraries/LibJS/Rust/src/gc/mod.rs b/Libraries/LibJS/Rust/src/gc/mod.rs
index cae5c86f776..137b4f25f57 100644
--- a/Libraries/LibJS/Rust/src/gc/mod.rs
+++ b/Libraries/LibJS/Rust/src/gc/mod.rs
@@ -10,6 +10,7 @@
pub mod capi;
pub mod class;
pub mod class_id;
+pub mod defer_gc;
pub mod embedder_cell;
pub mod foreign;
pub mod gc_ref_cell;
diff --git a/Libraries/LibJS/Rust/src/jit/disabled.rs b/Libraries/LibJS/Rust/src/jit/disabled.rs
index fb228681c05..e8981b10f2e 100644
--- a/Libraries/LibJS/Rust/src/jit/disabled.rs
+++ b/Libraries/LibJS/Rust/src/jit/disabled.rs
@@ -57,15 +57,6 @@ unsafe impl Trace for JitState {
fn trace(&self, _visitor: &mut Visitor) {}
}
-/// Keeps the heap from collecting garbage while it lives, which only matters for frames of inlined calls.
-pub struct DeferGc;
-
-impl DeferGc {
- pub fn new(_vm: &Vm) -> Self {
- Self
- }
-}
-
pub mod options {
/// The options of the JIT, which these builds ignore.
pub struct Options;
diff --git a/Libraries/LibJS/Rust/src/jit/mod.rs b/Libraries/LibJS/Rust/src/jit/mod.rs
index e93cc91aab5..fbf606b01c7 100644
--- a/Libraries/LibJS/Rust/src/jit/mod.rs
+++ b/Libraries/LibJS/Rust/src/jit/mod.rs
@@ -34,7 +34,6 @@ use std::collections::{BTreeMap, HashMap};
use std::rc::Rc;
use crate::bytecode::executable::Executable;
-use crate::gc::capi;
use crate::gc::visitor::{Trace, Visitor};
use crate::interpreter::dispatch_tables::DispatchTable;
use crate::interpreter::vm::Vm;
@@ -470,23 +469,6 @@ unsafe impl Trace for JitState {
}
}
-/// Keeps the heap from collecting garbage while it lives.
-pub(crate) struct DeferGc<'vm>(&'vm Vm);
-
-impl<'vm> DeferGc<'vm> {
- pub(crate) fn new(vm: &'vm Vm) -> Self {
- capi::gc_heap_defer_gc(vm.heap().handle());
- Self(vm)
- }
-}
-
-impl Drop for DeferGc<'_> {
- fn drop(&mut self) {
- // NB: This undoes the deferral of new().
- capi::gc_heap_undefer_gc(self.0.heap().handle());
- }
-}
-
/// The name of an executable in logs and perf maps, with the place of its code: "name (file:line:column)" of its
/// first instruction that has a source position, leaving out the file if the source has no name.
pub fn describe_executable(executable: &Executable) -> String {
diff --git a/Libraries/LibJS/Rust/src/jit/tier_up.rs b/Libraries/LibJS/Rust/src/jit/tier_up.rs
index c968325f12d..1f92b985973 100644
--- a/Libraries/LibJS/Rust/src/jit/tier_up.rs
+++ b/Libraries/LibJS/Rust/src/jit/tier_up.rs
@@ -14,9 +14,10 @@ use super::code::{CompileState, JitCode, SnapshotExecutable};
use super::compile_queue::CompileResult;
use super::executable_memory::ExecutableMemory;
use super::snapshot::capture_snapshot;
-use super::{CompileJob, DeferGc, JitState};
+use super::{CompileJob, JitState};
use crate::bytecode::executable::Executable;
use crate::bytecode::feedback::tier_up_costs;
+use crate::gc::defer_gc::DeferGc;
use crate::interpreter::vm::Vm;
use crate::jit::InterpreterTier;
use crate::jit::options::Options;
diff --git a/Libraries/LibJS/Rust/src/jit/translate.rs b/Libraries/LibJS/Rust/src/jit/translate.rs
index 3ce5dec91e7..2e034c35308 100644
--- a/Libraries/LibJS/Rust/src/jit/translate.rs
+++ b/Libraries/LibJS/Rust/src/jit/translate.rs
@@ -27,11 +27,12 @@ use libjs_abi::register;
use libjs_jit::code::{CALLEE_SLOT, FrameState, Repr, ResumeMode, Site, SiteKind, ValueLocation};
use super::code::{CompileState, EXIT_COUNT_BEFORE_DISCARD, InlinedFunction, JitCode};
-use super::{DeferGc, executable_of, frame_of, frames_above};
+use super::{executable_of, frame_of, frames_above};
use crate::bytecode::executable::Executable;
use crate::bytecode::instruction::{OpCode, instruction_length_from_bytes};
use crate::bytecode::instruction_at;
use crate::bytecode::op;
+use crate::gc::defer_gc::DeferGc;
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::execution_context::ExecutionContext;
diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
index eea514fcb75..0ef6a9f9b48 100644
--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
@@ -14,6 +14,7 @@ use libjs_runtime_macros::Trace;
use crate::bytecode::executable::Executable;
use crate::gc::class::{Extends, GcCell, cast_ref, define_cell};
+use crate::gc::defer_gc::DeferGc;
use crate::gc::gc_ref_cell::GcRefCell;
use crate::gc::root::MarkedVec;
use crate::gc::visitor::{Trace, Visitor};
@@ -989,7 +990,7 @@ impl EcmascriptFunctionObject {
let this_function = self.as_function_object_gc();
// NB: The arguments of a frame of an inlined call may be objects JIT code never created, which only the
// arguments object holds once it exists.
- let _defer_gc = crate::jit::DeferGc::new(vm);
+ let _defer_gc = DeferGc::new(vm);
let mut passed_arguments: Option<Vec<Cell<Value>>> = None;
vm.for_each_stack_frame_top_to_bottom(|frame| {