wasm/execution/
function_ref.rs

1use alloc::borrow::ToOwned;
2use alloc::vec::Vec;
3
4use crate::execution::{hooks::HookSet, value::InteropValueList, RuntimeInstance};
5use crate::{Error, ExternVal, Result as CustomResult, RuntimeError, Store, Value};
6
7pub struct FunctionRef {
8    pub func_addr: usize,
9}
10
11impl FunctionRef {
12    pub fn new_from_name(
13        module_name: &str,
14        function_name: &str,
15        store: &Store,
16    ) -> CustomResult<Self> {
17        // https://webassembly.github.io/spec/core/appendix/embedding.html#module-instances
18        // inspired by instance_export
19        let extern_val = store
20            .registry
21            .lookup(
22                module_name.to_owned().into(),
23                function_name.to_owned().into(),
24            )
25            .map_err(|_| Error::RuntimeError(RuntimeError::FunctionNotFound))?;
26        match extern_val {
27            ExternVal::Func(func_addr) => Ok(Self {
28                func_addr: *func_addr,
29            }),
30            _ => Err(Error::RuntimeError(RuntimeError::FunctionNotFound)),
31        }
32    }
33
34    pub fn invoke_typed<
35        H: HookSet + core::fmt::Debug,
36        Param: InteropValueList,
37        Returns: InteropValueList,
38    >(
39        &self,
40        runtime: &mut RuntimeInstance<H>,
41        params: Param,
42        // store: &mut Store,
43    ) -> Result<Returns, RuntimeError> {
44        runtime.invoke_typed(self, params /* , store */)
45    }
46
47    pub fn invoke<H: HookSet + core::fmt::Debug>(
48        &self,
49        runtime: &mut RuntimeInstance<H>,
50        params: Vec<Value>,
51        // store: &mut Store,
52    ) -> Result<Vec<Value>, RuntimeError> {
53        runtime.invoke(self, params)
54    }
55}