#[cfg(not(feature = "wasmtime-backend"))]
compile_error!("The wasmtime-backend feature must be enabled.");
#[cfg(feature = "wasmtime-backend")]
mod wasmtime_engine;
use super::ContractError;
use super::runtime::RuntimeConfig;
pub(crate) const DEFAULT_MAX_MEMORY_PAGES: u32 = 4096;
pub(crate) const WASM_PAGE_SIZE: usize = 65536;
pub(crate) struct InstanceHandle {
pub(super) id: i64,
}
#[derive(Debug, thiserror::Error)]
pub(crate) enum WasmError {
#[error("compile: {0}")]
Compile(String),
#[error("export not found: {0}")]
Export(String),
#[error("instantiation: {0}")]
Instantiation(String),
#[error("memory: {0}")]
Memory(String),
#[error("runtime: {0}")]
Runtime(String),
#[error("out of gas")]
OutOfGas,
#[error("execution timeout")]
Timeout,
#[error(transparent)]
Other(anyhow::Error),
}
pub(crate) trait WasmEngine: Send {
type Module: Clone + Send;
fn new(config: &RuntimeConfig, host_mem: bool) -> Result<Self, ContractError>
where
Self: Sized;
fn is_healthy(&self) -> bool;
fn compile(&mut self, code: &[u8]) -> Result<Self::Module, WasmError>;
fn module_has_async_imports(&self, module: &Self::Module) -> bool;
fn create_instance(
&mut self,
module: &Self::Module,
id: i64,
req_bytes: usize,
) -> Result<InstanceHandle, WasmError>;
fn drop_instance(&mut self, handle: &InstanceHandle);
fn memory_info(&mut self, handle: &InstanceHandle) -> Result<(*const u8, usize), WasmError>;
fn initiate_buffer(&mut self, handle: &InstanceHandle, size: u32) -> Result<i64, WasmError>;
#[allow(dead_code)] fn call_void(&mut self, handle: &InstanceHandle, name: &str) -> Result<(), WasmError>;
fn call_3i64(
&mut self,
handle: &InstanceHandle,
name: &str,
a: i64,
b: i64,
c: i64,
) -> Result<i64, WasmError>;
fn call_3i64_async_imports(
&mut self,
handle: &InstanceHandle,
name: &str,
a: i64,
b: i64,
c: i64,
) -> Result<i64, WasmError>;
fn call_2i64_blocking(
&mut self,
handle: &InstanceHandle,
name: &str,
a: i64,
b: i64,
) -> Result<i64, WasmError>;
fn call_3i64_blocking(
&mut self,
handle: &InstanceHandle,
name: &str,
a: i64,
b: i64,
c: i64,
) -> Result<i64, WasmError>;
}
#[cfg(feature = "wasmtime-backend")]
pub(crate) type Engine = wasmtime_engine::WasmtimeEngine;
#[cfg(feature = "wasmtime-backend")]
pub(crate) type BackendEngine = wasmtime::Engine;