Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
use core::cell::Cell;
use core::ffi::c_void;
use super::cell::Gc;
use super::execution_context::ExecutionContext;
use super::function_object::NativeFunctionTableEntry;
use super::property_lookup_cache::KeyedPropertyLookupCacheEntryLayout;
use super::realm::Realm;
/// The memory that execution contexts and their value slots are bump-allocated from.
#[repr(C)]
pub struct InterpreterStack {
pub base: Cell<*mut u8>,
pub top: Cell<*mut u8>,
pub limit: Cell<*mut u8>,
pub next_frame_id: Cell<u64>,
}
/// An entry of the execution context stack: a context pushed onto it, and the context that was running when it was
/// pushed, with the current realm of then, which the pop of the entry makes current again. That realm is the realm
/// of the previous running context (the running context's realm is always the current realm), so the collector keeps
/// it alive by tracing that context.
#[repr(C)]
pub struct ExecutionContextStackEntry {
pub execution_context: Cell<*mut ExecutionContext>,
pub previous_running_execution_context: Cell<*mut ExecutionContext>,
pub previous_realm: Cell<Option<Gc<Realm>>>,
}
/// How many dispatch tables the VM has, a power of two (see `VmHead::dispatch_tables`).
pub const DISPATCH_TABLE_COUNT: usize = 8;
/// An executable's dispatch table index is masked with this, which keeps it inside the VM's dispatch tables.
pub const DISPATCH_TABLE_INDEX_MASK: u8 = (DISPATCH_TABLE_COUNT - 1) as u8;
/// The part of the VM that the interpreter reads and writes. The rest of the VM follows it.
#[repr(C)]
pub struct VmHead {
pub running_execution_context: Cell<*mut ExecutionContext>,
pub interpreter_stack: InterpreterStack,
/// The lowest address of the stack of the thread that runs JavaScript.
pub stack_base: Cell<usize>,
/// The lowest native stack address JavaScript runs at: the stack base plus VM_STACK_SPACE_LIMIT (see
/// Vm::did_reach_stack_space_limit()). JIT code stops above it, see Vm::jit_native_stack_limit.
pub native_stack_limit: Cell<usize>,
/// Advanced at the end of every synchronous run of code (see `finish_execution_generation()`). WeakRef targets
/// used during the current generation stay alive. Interpreter and JIT code advance it with a 32-bit increment.
pub execution_generation: Cell<u32>,
pub primitive_storage_cage_base: Cell<usize>,
pub heap_region_base: Cell<usize>,
pub native_function_table_data: Cell<*const NativeFunctionTableEntry>,
/// Non-null while a debugger is attached; the interpreter then dispatches through its breakpoint-checking table.
pub debugger: Cell<*mut c_void>,
/// The execution context stack: storage for `execution_context_stack_capacity` entries, of which the first
/// `execution_context_stack_length` are on the stack. Only the VM grows the storage, but an embedder may push and
/// pop entries in place while there is room, and walk the stack.
pub execution_context_stack_entries: Cell<*mut ExecutionContextStackEntry>,
pub execution_context_stack_length: Cell<usize>,
pub execution_context_stack_capacity: Cell<usize>,
/// The realm TypeErrors are created in while a TypeErrorRealmScope is active, at the execution context stack
/// length it was created at. An embedder's TypeErrorRealmScope sets and restores both directly.
pub type_error_realm_override: Cell<Option<Gc<Realm>>>,
pub type_error_realm_override_depth: Cell<usize>,
/// The entries of the VM's keyed property lookup cache, which stay where they are for as long as the VM lives.
pub keyed_property_lookup_cache_entries: Cell<*const KeyedPropertyLookupCacheEntryLayout>,
/// The dispatch tables the interpreter runs frames with, by the dispatch table index of their executable (see
/// `ExecutableHead::dispatch_table_index`). Indices without a table of their own have the plain one.
pub dispatch_tables: [Cell<*const c_void>; DISPATCH_TABLE_COUNT],
/// The current Realm Record, the [[Realm]] of the running execution context (see
/// `Vm::set_running_execution_context()`). A frame of an ECMAScript function that the interpreter or JIT code
/// enters for a call runs in the realm of its caller: they leave a call of one of another realm to the runtime. The
/// frame of a native function they call runs in the function's realm, which they make current for the call.
pub current_realm: Cell<Option<Gc<Realm>>>,
}
/// The storage an embedder reserves to construct a Vm in place, and its alignment. build.rs cannot see the Vm, only its
/// head, so these are a capacity that the crate checks the Vm against rather than its exact size and alignment.
pub const VM_SIZE: usize = 16 * 1024;
pub const VM_ALIGN: usize = 16;
impl VmHead {
/// Starts a new execution generation. The counter wraps around, which at worst keeps a WeakRef target that was
/// last used 2^32 generations earlier alive until the next one.
pub fn finish_execution_generation(&self) {
self.execution_generation
.set(self.execution_generation.get().wrapping_add(1));
}
}