inkpad_executor/wasmi/
external.rs

1//! WASMi externals
2use super::func::DefinedHostFunctions;
3use crate::Error;
4use ::wasmi::{Externals, HostError, RuntimeArgs, RuntimeValue, Trap};
5use inkpad_std::Vec;
6
7impl HostError for Error {}
8
9/// WASMi externals
10pub struct External<'a, T> {
11    /// External state
12    pub state: &'a mut T,
13    /// Defined host functions
14    pub defined_host_functions: &'a DefinedHostFunctions<T>,
15}
16
17impl<'a, T> Externals for External<'a, T> {
18    fn invoke_index(
19        &mut self,
20        index: usize,
21        args: RuntimeArgs,
22    ) -> Result<Option<RuntimeValue>, Trap> {
23        let args = args
24            .as_ref()
25            .iter()
26            .cloned()
27            .map(|v| v.into())
28            .collect::<Vec<_>>();
29
30        let res = (self.defined_host_functions.func(index))(self.state, &args)?;
31        Ok(res.map(|v| v.into()))
32    }
33}