use crate::engine_access::EngineAccess;
use crate::wire::ActionWire;
#[derive(Debug, Clone)]
pub struct RawProviderEvent {
pub payload_type: String,
pub payload: Vec<u8>,
}
pub const DEFAULT_FUEL: u64 = 10_000_000;
pub const DEFAULT_MAX_MEMORY: usize = 16 * 1024 * 1024;
pub struct StoreData {
pub engine_access: *const dyn EngineAccess,
pub now: f64,
pub injected_actions: Vec<ActionWire>,
pub log_messages: Vec<String>,
pub provider_events: Vec<RawProviderEvent>,
pub provider_events_enabled: bool,
}
unsafe impl Send for StoreData {}
unsafe impl Sync for StoreData {}
impl StoreData {
pub unsafe fn engine(&self) -> &dyn EngineAccess {
&*self.engine_access
}
pub fn reset_per_call(
&mut self,
engine_access: *const dyn EngineAccess,
now: f64,
provider_events_enabled: bool,
) {
self.engine_access = engine_access;
self.now = now;
self.injected_actions.clear();
self.log_messages.clear();
self.provider_events.clear();
self.provider_events_enabled = provider_events_enabled;
}
}
pub struct WasmRuntime {
engine: wasmtime::Engine,
fuel: u64,
}
impl WasmRuntime {
pub fn new() -> Result<Self, wasmtime::Error> {
let mut config = wasmtime::Config::new();
config.consume_fuel(true);
let engine = wasmtime::Engine::new(&config)?;
Ok(WasmRuntime {
engine,
fuel: DEFAULT_FUEL,
})
}
pub fn compile(&self, bytes: &[u8]) -> Result<wasmtime::Module, wasmtime::Error> {
wasmtime::Module::new(&self.engine, bytes)
}
pub fn engine(&self) -> &wasmtime::Engine {
&self.engine
}
pub fn fuel(&self) -> u64 {
self.fuel
}
}