Skip to content

Truly independent web browser

LibJS: Share the helpers that find the VM of a cell or helper call

Andreas Klingaabff044cbc2
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
index 2c7474a1461..50c30627573 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/mod.rs
@@ -1451,8 +1451,7 @@ impl RuntimeFunctions for Runtime {
}
fn helper_handle_raw_native_exception(vm: u64, encoded_exception: u64) -> u64 {
- // SAFETY: The interpreter passes its VM.
- let vm = unsafe { &*core::ptr::with_exposed_provenance::<Vm>(vm as usize) };
+ let vm = vm_from_helper_argument(vm);
calls::handle_raw_native_exception(vm, Value(encoded_exception)).0 as u64
}
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index 08089e2eca9..8b61746658b 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -1789,6 +1789,12 @@ impl Vm {
script_or_module
}
+ /// The VM whose heap `cell` lives in.
+ pub(crate) fn of_cell<T: GcCell>(cell: &T) -> &Vm {
+ // SAFETY: The VM creates its heap with itself as the context, and outlives every cell in it.
+ unsafe { &*capi::gc_cell_heap_context(core::ptr::from_ref(cell).cast()).cast::<Vm>() }
+ }
+
pub(crate) fn running_execution_context_ref(&self) -> &ExecutionContext {
self.running_execution_context_if_any()
.expect("there is a running execution context")
diff --git a/Libraries/LibJS/Rust/src/jit/code.rs b/Libraries/LibJS/Rust/src/jit/code.rs
index 8dfc8ada39c..eebb7df0f08 100644
--- a/Libraries/LibJS/Rust/src/jit/code.rs
+++ b/Libraries/LibJS/Rust/src/jit/code.rs
@@ -16,7 +16,6 @@ use libjs_jit::code::{ExitKind, Site};
use super::entry_table::{JIT_ENTRY_SLOT_MASK, JitEntryTable, NO_JIT_ENTRY_SLOT};
use super::executable_memory::ExecutableMemory;
use crate::bytecode::executable::Executable;
-use crate::gc::capi;
use crate::gc::visitor::{Trace, Visitor};
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
@@ -281,8 +280,7 @@ impl Executable {
/// The VM whose heap the executable lives in.
fn heap_vm(&self) -> &Vm {
- // SAFETY: The VM creates its heap with itself as the context, and outlives every cell in it.
- unsafe { &*capi::gc_cell_heap_context(core::ptr::from_ref(self).cast()).cast::<Vm>() }
+ Vm::of_cell(self)
}
/// The executable's slot in the JIT entry table and the table, if it has one.
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index 54e132df880..0dd0b37e415 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -16,7 +16,6 @@ use libjs_runtime_macros::Trace;
use crate::bytecode::executable::{PropertyLookupCache, StaticPropertyLookupCacheSite};
use crate::bytecode::property_access::{Strict, put_by_property_key};
use crate::embedding::abi_types::JSRealm;
-use crate::gc::capi;
use crate::gc::class::{Class, Extends, ExternalMemorySize, GcCell, define_cell};
use crate::gc::gc_ref_cell::GcRefCell;
use crate::gc::heap::{Heap, RuntimeClassAllocator, SizeClassAllocator};
@@ -3646,8 +3645,7 @@ impl Object {
/// The VM whose heap the object lives in, for operations that allocate storage without being handed the VM.
fn vm(&self) -> &Vm {
- // SAFETY: The VM creates its heap with itself as the context, and outlives every cell in it.
- unsafe { &*capi::gc_cell_heap_context(core::ptr::from_ref(self).cast()).cast::<Vm>() }
+ Vm::of_cell(self)
}
/// Replaces the named or indexed storage `storage` points at with `new_storage`, and frees the old storage if it is