use crate::error::Error;
pub use sc_allocator::AllocationStats;
pub const DEFAULT_HEAP_ALLOC_STRATEGY: HeapAllocStrategy =
HeapAllocStrategy::Static { extra_pages: DEFAULT_HEAP_ALLOC_PAGES };
pub const DEFAULT_HEAP_ALLOC_PAGES: u32 = 2048;
pub trait WasmModule: Sync + Send {
fn new_instance(
&self,
heap_alloc_strategy: HeapAllocStrategy,
) -> Result<Box<dyn WasmInstance>, Error>;
}
pub trait WasmInstance: Send {
fn call(&mut self, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
self.call_with_allocation_stats(method, data).0
}
fn call_with_allocation_stats(
&mut self,
method: &str,
data: &[u8],
) -> (Result<Vec<u8>, Error>, Option<AllocationStats>);
fn call_export(&mut self, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
self.call(method.into(), data)
}
fn set_heap_alloc_strategy(&mut self, _heap_alloc_strategy: HeapAllocStrategy) {}
}
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
pub enum HeapAllocStrategy {
Static {
extra_pages: u32,
},
Dynamic {
maximum_pages: Option<u32>,
},
}
impl HeapAllocStrategy {
pub const fn double(self) -> Self {
match self {
Self::Static { extra_pages } => {
Self::Static { extra_pages: extra_pages.saturating_mul(2) }
},
Self::Dynamic { maximum_pages } => Self::Dynamic {
maximum_pages: match maximum_pages {
Some(p) => Some(p.saturating_mul(2)),
None => None,
},
},
}
}
}