wasm/execution/
function_ref.rs

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