wasm/execution/
function_ref.rs1use alloc::vec::Vec;
2
3use crate::execution::{hooks::HookSet, value::InteropValueList, RuntimeInstance};
4use crate::{
5 Error, ExportInst, ExternVal, Result as CustomResult, RuntimeError, Store, ValType, Value,
6};
7
8pub struct FunctionRef {
9 pub func_addr: usize,
10}
11
12impl FunctionRef {
13 pub fn new_from_name(
14 module_name: &str,
15 function_name: &str,
16 store: &Store,
17 ) -> CustomResult<Self> {
18 let module_addr = *store
21 .module_names
22 .get(module_name)
23 .ok_or(Error::RuntimeError(RuntimeError::ModuleNotFound))?;
24 Ok(Self {
25 func_addr: store.modules[module_addr]
26 .exports
27 .iter()
28 .find_map(|ExportInst { name, value }| {
29 if *name == function_name {
30 match value {
31 ExternVal::Func(func_addr) => Some(*func_addr),
32 _ => None,
33 }
34 } else {
35 None
36 }
37 })
38 .ok_or(Error::RuntimeError(RuntimeError::FunctionNotFound))?,
39 })
40 }
41
42 pub fn invoke<
43 H: HookSet + core::fmt::Debug,
44 Param: InteropValueList,
45 Returns: InteropValueList,
46 >(
47 &self,
48 runtime: &mut RuntimeInstance<H>,
49 params: Param,
50 ) -> Result<Returns, RuntimeError> {
52 runtime.invoke(self, params )
53 }
54
55 pub fn invoke_dynamic<H: HookSet + core::fmt::Debug>(
56 &self,
57 runtime: &mut RuntimeInstance<H>,
58 params: Vec<Value>,
59 ret_types: &[ValType],
60 ) -> Result<Vec<Value>, RuntimeError> {
62 runtime.invoke_dynamic(self, params, ret_types )
63 }
64}