Skip to main content

rns_hooks/
runtime.rs

1#[cfg(feature = "wasm")]
2use crate::engine_access::EngineAccess;
3#[cfg(feature = "wasm")]
4use crate::wire::ActionWire;
5
6#[derive(Debug, Clone)]
7pub struct RawProviderEvent {
8    pub payload_type: String,
9    pub payload: Vec<u8>,
10}
11
12/// Default fuel budget per WASM invocation.
13pub const DEFAULT_FUEL: u64 = 10_000_000;
14/// Default maximum memory for WASM modules (16 MB).
15pub const DEFAULT_MAX_MEMORY: usize = 16 * 1024 * 1024;
16
17/// Data stored in each wasmtime `Store`.
18///
19/// Uses a raw pointer for `EngineAccess` because `Linker<T>` requires `T`
20/// without lifetime parameters. The Store is cached across calls for instance
21/// persistence, but `engine_access` is refreshed each call via `reset_per_call`
22/// and must only be dereferenced during the active call.
23#[cfg(feature = "wasm")]
24pub struct StoreData {
25    pub engine_access: *const dyn EngineAccess,
26    pub now: f64,
27    pub injected_actions: Vec<ActionWire>,
28    pub log_messages: Vec<String>,
29    pub provider_events: Vec<RawProviderEvent>,
30    pub provider_events_enabled: bool,
31}
32
33// Safety: StoreData is only used within a single-threaded driver loop.
34// The `engine_access` raw pointer is refreshed each call and only dereferenced
35// during that call while the borrow is live.
36#[cfg(feature = "wasm")]
37unsafe impl Send for StoreData {}
38#[cfg(feature = "wasm")]
39unsafe impl Sync for StoreData {}
40
41#[cfg(feature = "wasm")]
42impl StoreData {
43    /// Access the engine through the raw pointer.
44    ///
45    /// # Safety
46    /// The caller must ensure the pointer is still valid.
47    pub unsafe fn engine(&self) -> &dyn EngineAccess {
48        &*self.engine_access
49    }
50
51    /// Reset per-call fields while preserving the store (and WASM linear memory).
52    pub fn reset_per_call(
53        &mut self,
54        engine_access: *const dyn EngineAccess,
55        now: f64,
56        provider_events_enabled: bool,
57    ) {
58        self.engine_access = engine_access;
59        self.now = now;
60        self.injected_actions.clear();
61        self.log_messages.clear();
62        self.provider_events.clear();
63        self.provider_events_enabled = provider_events_enabled;
64    }
65}
66
67/// Wrapper around `wasmtime::Engine` with fuel-metering enabled.
68#[cfg(feature = "wasm")]
69pub struct WasmRuntime {
70    engine: wasmtime::Engine,
71    fuel: u64,
72}
73
74#[cfg(feature = "wasm")]
75impl WasmRuntime {
76    pub fn new() -> Result<Self, wasmtime::Error> {
77        let mut config = wasmtime::Config::new();
78        config.consume_fuel(true);
79        let engine = wasmtime::Engine::new(&config)?;
80        Ok(WasmRuntime {
81            engine,
82            fuel: DEFAULT_FUEL,
83        })
84    }
85
86    pub fn compile(&self, bytes: &[u8]) -> Result<wasmtime::Module, wasmtime::Error> {
87        wasmtime::Module::new(&self.engine, bytes)
88    }
89
90    pub fn engine(&self) -> &wasmtime::Engine {
91        &self.engine
92    }
93
94    pub fn fuel(&self) -> u64 {
95        self.fuel
96    }
97}