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