inkpad_executor/
derive.rs

1//! Derive traits
2pub use crate::{
3    func::HostFuncType,
4    value::{Type, Value},
5    Result,
6};
7use inkpad_std::Vec;
8
9/// Host function parcel
10pub type HostCall<M, F, T> = (M, F, HostFuncType<T>);
11
12/// Custom SealCall
13pub type SealCall<T> = HostCall<&'static str, &'static str, T>;
14
15/// Inkpad wasm executor memory
16pub trait Memory: Sized + Clone {
17    /// Construct a new linear memory instance
18    fn new(initial: u32, maximum: Option<u32>) -> Result<Self>;
19
20    /// Read a memory area at the address `ptr` with the size of the provided slice `buf`.
21    fn get(&self, ptr: u32, buf: &mut [u8]) -> Result<()>;
22
23    /// Write a memory area at the address `ptr` with contents of the provided slice `buf`.
24    fn set(&self, ptr: u32, value: &[u8]) -> Result<()>;
25}
26
27/// Inkpad executor instance
28pub trait Instance<T>: Sized {
29    type Builder: Builder<T>;
30
31    /// Instantiate a module with the given env builder
32    fn new(code: &[u8], builder: &Self::Builder, state: &mut T) -> Result<Self>;
33
34    /// invoke an exported function
35    fn invoke(&mut self, name: &str, args: &[Value], state: &mut T) -> Result<Value>;
36
37    /// Get global value
38    fn get_global_val(&self, name: &str) -> Option<Value>;
39}
40
41/// Inkpad environment builder
42pub trait Builder<T>: Sized {
43    type Memory: Memory;
44
45    /// New builder
46    fn new() -> Self;
47
48    /// Register a host function in this environment definition
49    fn add_host_func<M, F>(&mut self, module: M, field: F, f: HostFuncType<T>)
50    where
51        F: Into<Vec<u8>>,
52        M: Into<Vec<u8>>;
53
54    /// Register a memory in this environment definition.
55    fn add_memory<M, F>(&mut self, module: M, field: F, mem: Self::Memory)
56    where
57        M: Into<Vec<u8>>,
58        F: Into<Vec<u8>>;
59}