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