luna_core/jit/storage.rs
1//! 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//! These live in per-`Vm` field storage rather than `thread_local!`.
13//! 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/// Per-`Vm` JIT storage. Held as `Box<dyn JitStorage>` on
20/// [`crate::vm::jit_state::JitState::storage`]. The concrete impl is
21/// chosen by whoever installs the JIT backend (luna-core's default
22/// is [`NullJitStorage`]; the `luna_jit` crate swaps in its
23/// `CraneliftJitStorage` via a setter alongside `install_jit_backend`).
24///
25/// luna-core treats the trait as opaque — readers downcast through
26/// [`std::any::Any`] to reach concrete fields. This keeps the
27/// `JITModule`-bearing types (and therefore the Cranelift dep) out
28/// of luna-core.
29pub trait JitStorage: std::any::Any {
30 /// Mutable downcast hook. luna-jit's `CraneliftBackend`
31 /// implementations call this then `downcast_mut::<CraneliftJitStorage>()`
32 /// to reach the concrete cache + handle collections.
33 fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
34
35 /// Immutable downcast hook. Symmetric with [`Self::as_any_mut`];
36 /// used by read-only diagnostics.
37 fn as_any(&self) -> &dyn std::any::Any;
38}
39
40/// No-op storage installed by [`crate::vm::Vm::new_minimal`]. Holds
41/// nothing; downcasting from luna-jit will fail by design (a
42/// `NullJitBackend` is paired with `NullJitStorage` — neither
43/// `try_compile` nor `try_compile_trace` reaches the downcast site
44/// because both immediately return `Skipped` / `None`).
45#[derive(Default)]
46pub struct NullJitStorage;
47
48impl JitStorage for NullJitStorage {
49 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
50 self
51 }
52
53 fn as_any(&self) -> &dyn std::any::Any {
54 self
55 }
56}