wasm/execution/
mod.rs

1use crate::Error;
2
3use alloc::borrow::ToOwned;
4use alloc::vec::Vec;
5
6use const_interpreter_loop::run_const_span;
7use function_ref::FunctionRef;
8use value_stack::Stack;
9
10use crate::core::reader::types::{FuncType, ResultType};
11use crate::execution::assert_validated::UnwrapValidatedExt;
12use crate::execution::hooks::{EmptyHookSet, HookSet};
13use crate::execution::store::Store;
14use crate::execution::value::Value;
15use crate::value::InteropValueList;
16use crate::{Result as CustomResult, RuntimeError, ValidationInfo};
17
18pub(crate) mod assert_validated;
19pub mod const_interpreter_loop;
20pub mod function_ref;
21pub mod hooks;
22mod interpreter_loop;
23pub(crate) mod linear_memory;
24pub(crate) mod locals;
25pub mod registry;
26pub mod store;
27pub mod value;
28pub mod value_stack;
29
30/// The default module name if a [RuntimeInstance] was created using [RuntimeInstance::new].
31pub const DEFAULT_MODULE: &str = "__interpreter_default__";
32
33#[derive(Debug)]
34pub struct RuntimeInstance<'b, H = EmptyHookSet>
35where
36    H: HookSet + core::fmt::Debug,
37{
38    pub hook_set: H,
39    pub store: Store<'b>,
40}
41
42impl Default for RuntimeInstance<'_, EmptyHookSet> {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48impl<'b> RuntimeInstance<'b, EmptyHookSet> {
49    pub fn new() -> Self {
50        Self::new_with_hooks(EmptyHookSet)
51    }
52
53    pub fn new_with_default_module(validation_info: &'_ ValidationInfo<'b>) -> CustomResult<Self> {
54        let mut instance = Self::new_with_hooks(EmptyHookSet);
55        instance.add_module(DEFAULT_MODULE, validation_info)?;
56        Ok(instance)
57    }
58
59    pub fn new_named(
60        module_name: &str,
61        validation_info: &'_ ValidationInfo<'b>,
62        // store: &mut Store,
63    ) -> CustomResult<Self> {
64        let mut instance = Self::new_with_hooks(EmptyHookSet);
65        instance.add_module(module_name, validation_info)?;
66        Ok(instance)
67    }
68}
69
70impl<'b, H> RuntimeInstance<'b, H>
71where
72    H: HookSet + core::fmt::Debug,
73{
74    pub fn add_module(
75        &mut self,
76        module_name: &str,
77        validation_info: &'_ ValidationInfo<'b>,
78    ) -> CustomResult<()> {
79        self.store.add_module(module_name, validation_info)
80    }
81
82    pub fn new_with_hooks(
83        hook_set: H,
84        // store: &mut Store,
85    ) -> Self {
86        RuntimeInstance {
87            hook_set,
88            store: Store::default(),
89        }
90    }
91
92    pub fn get_function_by_name(
93        &self,
94        module_name: &str,
95        function_name: &str,
96    ) -> Result<FunctionRef, RuntimeError> {
97        FunctionRef::new_from_name(module_name, function_name, &self.store)
98            .map_err(|_| RuntimeError::FunctionNotFound)
99    }
100
101    pub fn get_function_by_index(
102        &self,
103        module_addr: usize,
104        function_idx: usize,
105    ) -> Result<FunctionRef, RuntimeError> {
106        let module_inst = self
107            .store
108            .modules
109            .get(module_addr)
110            .ok_or(RuntimeError::ModuleNotFound)?;
111        let func_addr = *module_inst
112            .func_addrs
113            .get(function_idx)
114            .ok_or(RuntimeError::FunctionNotFound)?;
115
116        Ok(FunctionRef { func_addr })
117    }
118
119    /// Invokes a function with the given parameters of type `Param`, and return types of type `Returns`.
120    pub fn invoke_typed<Params: InteropValueList, Returns: InteropValueList>(
121        &mut self,
122        function_ref: &FunctionRef,
123        params: Params,
124        // store: &mut Store,
125    ) -> Result<Returns, RuntimeError> {
126        let FunctionRef { func_addr } = *function_ref;
127        self.store
128            .invoke(func_addr, params.into_values())
129            .map(|values| Returns::from_values(values.into_iter()))
130    }
131
132    /// Invokes a function with the given parameters. The return types depend on the function signature.
133    pub fn invoke(
134        &mut self,
135        function_ref: &FunctionRef,
136        params: Vec<Value>,
137    ) -> Result<Vec<Value>, RuntimeError> {
138        let FunctionRef { func_addr } = *function_ref;
139        self.store.invoke(func_addr, params)
140    }
141
142    /// Adds a host function under module namespace `module_name` with name `name`.
143    /// roughly similar to `func_alloc` in <https://webassembly.github.io/spec/core/appendix/embedding.html#functions>
144    /// except the host function is made visible to other modules through these names.
145    pub fn add_host_function_typed<Params: InteropValueList, Returns: InteropValueList>(
146        &mut self,
147        module_name: &str,
148        name: &str,
149        host_func: fn(Vec<Value>) -> Vec<Value>,
150    ) -> Result<FunctionRef, Error> {
151        let host_func_ty = FuncType {
152            params: ResultType {
153                valtypes: Vec::from(Params::TYS),
154            },
155            returns: ResultType {
156                valtypes: Vec::from(Returns::TYS),
157            },
158        };
159        self.add_host_function(module_name, name, host_func_ty, host_func)
160    }
161
162    pub fn add_host_function(
163        &mut self,
164        module_name: &str,
165        name: &str,
166        host_func_ty: FuncType,
167        host_func: fn(Vec<Value>) -> Vec<Value>,
168    ) -> Result<FunctionRef, Error> {
169        let func_addr = self.store.alloc_host_func(host_func_ty, host_func);
170        self.store.registry.register(
171            module_name.to_owned().into(),
172            name.to_owned().into(),
173            store::ExternVal::Func(func_addr),
174        )?;
175        Ok(FunctionRef { func_addr })
176    }
177}
178
179/// Helper function to quickly construct host functions without worrying about wasm to Rust
180/// type conversion.
181/// # Example
182/// ```
183/// use wasm::{validate, RuntimeInstance, host_function_wrapper, Value};
184/// fn my_wrapped_host_func(params: Vec<Value>) -> Vec<Value> {
185///     host_function_wrapper(params, |(x, y): (u32, i32)| -> u32 {
186///         x + (y as u32)
187///  })
188/// }
189/// fn main() {
190///     let mut instance = RuntimeInstance::new();
191///     let foo_bar = instance.add_host_function_typed::<(u32,i32),u32>("foo", "bar", my_wrapped_host_func).unwrap();
192/// }
193/// ```
194pub fn host_function_wrapper<Params: InteropValueList, Results: InteropValueList>(
195    params: Vec<Value>,
196    f: impl FnOnce(Params) -> Results,
197) -> Vec<Value> {
198    let params = Params::from_values(params.into_iter());
199    let results = f(params);
200    results.into_values()
201}