luna_core/jit/storage.rs
1//! v2.0 Track J sub-step J-B — per-`Vm` JIT storage trait + null impl.
2//!
3//! The Cranelift JIT keeps three thread-local collections that own
4//! mmap'd code pages for compiled chunk fns and compiled traces:
5//!
6//! - `JIT_CACHE` — hash map of bytecode-key → cached compile result
7//! - `JIT_CACHE_HANDLES` — `Vec<JitHandle>` holding each compiled
8//! chunk's `JITModule` so the entry pointer stays callable
9//! - `TRACE_JIT_HANDLES` — `Vec<TraceHandle>` holding each compiled
10//! trace's `JITModule`
11//!
12//! J-B moves these three from `thread_local!` to per-`Vm` field
13//! storage. The Cranelift types (`JITModule`, `CacheEntry`,
14//! `JitHandle`, `TraceHandle`) live in luna-jit, so luna-core only
15//! sees an opaque [`JitStorage`] trait + a no-op
16//! [`NullJitStorage`] default; the concrete `CraneliftJitStorage`
17//! impl lives in `luna_jit::jit_backend::storage`.
18//!
19//! See `.dev/rfcs/v2.0-track-j-b-design.md` for the migration design
20//! (integration pattern, soundness preservation, phase plan).
21//!
22//! Single-thread semantics are preserved by this sub-step; cross-
23//! thread Send transfer is J-D / J-E's lift.
24
25/// Per-`Vm` JIT storage. Held as `Box<dyn JitStorage>` on
26/// [`crate::vm::jit_state::JitState::storage`]. The concrete impl is
27/// chosen by whoever installs the JIT backend (luna-core's default
28/// is [`NullJitStorage`]; the `luna_jit` crate swaps in its
29/// `CraneliftJitStorage` via a setter alongside `install_jit_backend`).
30///
31/// luna-core treats the trait as opaque — readers downcast through
32/// [`std::any::Any`] to reach concrete fields. This keeps the
33/// `JITModule`-bearing types (and therefore the Cranelift dep) out
34/// of luna-core.
35pub trait JitStorage: std::any::Any {
36 /// Mutable downcast hook. luna-jit's `CraneliftBackend`
37 /// implementations call this then `downcast_mut::<CraneliftJitStorage>()`
38 /// to reach the concrete cache + handle collections.
39 fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
40
41 /// Immutable downcast hook. Symmetric with [`Self::as_any_mut`];
42 /// used by read-only diagnostics.
43 fn as_any(&self) -> &dyn std::any::Any;
44}
45
46/// No-op storage installed by [`crate::vm::Vm::new_minimal`]. Holds
47/// nothing; downcasting from luna-jit will fail by design (a
48/// `NullJitBackend` is paired with `NullJitStorage` — neither
49/// `try_compile` nor `try_compile_trace` reaches the downcast site
50/// because both immediately return `Skipped` / `None`).
51#[derive(Default)]
52pub struct NullJitStorage;
53
54impl JitStorage for NullJitStorage {
55 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
56 self
57 }
58
59 fn as_any(&self) -> &dyn std::any::Any {
60 self
61 }
62}