1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use alloc::string::String;
use alloc::vec::Vec;

use crate::execution::{hooks::HookSet, value::InteropValueList, RuntimeInstance};
use crate::{RuntimeError, ValType, Value};

pub struct FunctionRef {
    pub(crate) module_name: String,
    pub(crate) function_name: String,
    pub(crate) module_index: usize,
    pub(crate) function_index: usize,
    /// If the function is exported from the module or not. This is used to determine if the function name - index
    /// mapping should be verified. The module name - index mapping is always verified.
    ///
    /// If this is set to false then the user must make sure that the function reference will still be valid when the
    /// function is called. This means that the module must not be unloaded.
    pub(crate) exported: bool,
}

impl FunctionRef {
    pub fn invoke<H: HookSet, Param: InteropValueList, Returns: InteropValueList>(
        &self,
        runtime: &mut RuntimeInstance<H>,
        params: Param,
    ) -> Result<Returns, RuntimeError> {
        runtime.invoke(self, params)
    }

    pub fn invoke_dynamic<H: HookSet>(
        &self,
        runtime: &mut RuntimeInstance<H>,
        params: Vec<Value>,
        ret_types: &[ValType],
    ) -> Result<Vec<Value>, RuntimeError> {
        runtime.invoke_dynamic(self, params, ret_types)
    }
}