use crate::{error::VmError, vm::Context, Error};
use wasmtime::{Linker, Module, Store};
pub struct Instance {
pub linker: Linker<Context>,
pub module: Module,
pub store: Store<Context>,
}
impl Instance {
pub fn run(&mut self, fname: &str) -> Result<bool, Error> {
let instance = self
.linker
.instantiate(&mut self.store, &self.module)
.map_err(|e| VmError::InstantiationError {
message: format!("failed to instantiate WASM module: {e}"),
})?;
let func = instance
.get_typed_func::<(), i32>(&mut self.store, fname)
.map_err(|e| VmError::ExecutionError {
function: fname.to_string(),
message: format!("failed to get typed function '{fname}': {e}"),
})?;
let result = func
.call(&mut self.store, ())
.map_err(|e| VmError::ExecutionError {
function: fname.to_string(),
message: format!("function '{fname}' execution failed: {e}"),
})?;
Ok(result != 0)
}
#[must_use]
pub fn log(&self) -> Vec<u8> {
self.store.data().log.clone()
}
}