inkpad_executor/
executor.rs

1//! WASM executor wrapper
2use crate::{
3    derive::SealCall, result::ExecResult, Builder, Error, Instance, Memory, Result, Value,
4};
5use inkpad_std::Vec;
6use inkpad_support::traits::Ext;
7
8/// Inkpad WASM executor
9pub struct Executor<T> {
10    pub memory: Memory,
11    instance: Instance<T>,
12}
13
14impl<T> Executor<T>
15where
16    T: Ext<Memory, Vec<SealCall<T>>>,
17{
18    /// New executor
19    pub fn new(code: [u8; 32], sandbox: &mut T) -> Result<Self> {
20        // construct builder
21        let memory = sandbox.memory().ok_or(Error::MemoryNotFound)?;
22        let mut builder = Builder::new().add_host_parcels(sandbox.seal_call());
23        builder.add_memory("env", "memory", memory.clone());
24
25        // get wasm code
26        let wasm = sandbox.code(code).ok_or(Error::CodeNotFound)?.to_vec();
27
28        // new executor
29        Ok(Self {
30            memory,
31            instance: Instance::new(&wasm, &builder, sandbox)?,
32        })
33    }
34
35    // invoke method
36    pub fn invoke(&mut self, method: &str, data: &[Value], sandbox: &mut T) -> Result<ExecResult> {
37        let res = ExecResult::from_res(self.instance.invoke(method, data, sandbox));
38        if let Ok(r) = res.clone() {
39            log::debug!("{:?}", &r.data);
40        }
41
42        res
43    }
44}