inkpad_executor/
executor.rs1use crate::{
3 derive::SealCall, result::ExecResult, Builder, Error, Instance, Memory, Result, Value,
4};
5use inkpad_std::Vec;
6use inkpad_support::traits::Ext;
7
8pub 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 pub fn new(code: [u8; 32], sandbox: &mut T) -> Result<Self> {
20 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 let wasm = sandbox.code(code).ok_or(Error::CodeNotFound)?.to_vec();
27
28 Ok(Self {
30 memory,
31 instance: Instance::new(&wasm, &builder, sandbox)?,
32 })
33 }
34
35 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}