wasm/execution/
mod.rs

1use alloc::vec::Vec;
2
3use const_interpreter_loop::run_const_span;
4use store::HaltExecutionError;
5use value_stack::Stack;
6
7use crate::execution::assert_validated::UnwrapValidatedExt;
8use crate::execution::value::Value;
9use crate::interop::InteropValueList;
10
11pub(crate) mod assert_validated;
12pub mod checked;
13pub mod config;
14pub mod const_interpreter_loop;
15pub mod error;
16pub mod interop;
17mod interpreter_loop;
18pub mod linker;
19pub(crate) mod little_endian;
20pub mod resumable;
21pub mod store;
22pub mod value;
23pub mod value_stack;
24
25/// Helper function to quickly construct host functions without worrying about wasm to Rust
26/// type conversion. For reading/writing user data into the current configuration, simply move
27/// `user_data` into the passed closure.
28/// # Example
29/// ```
30/// use wasm::{validate,  Store, host_function_wrapper, Value, HaltExecutionError};
31/// fn my_wrapped_host_func(user_data: &mut (), params: Vec<Value>) -> Result<Vec<Value>, HaltExecutionError> {
32///     host_function_wrapper(params, |(x, y): (u32, i32)| -> Result<u32, HaltExecutionError> {
33///         let _user_data = user_data;
34///         Ok(x + (y as u32))
35///     })
36/// }
37/// fn main() {
38///     let mut store = Store::new(());
39///     let foo_bar = store.func_alloc_typed_unchecked::<(u32, i32), u32>(my_wrapped_host_func);
40/// }
41/// ```
42pub fn host_function_wrapper<Params: InteropValueList, Results: InteropValueList>(
43    params: Vec<Value>,
44    f: impl FnOnce(Params) -> Result<Results, HaltExecutionError>,
45) -> Result<Vec<Value>, HaltExecutionError> {
46    let params =
47        Params::try_from_values(params.into_iter()).expect("Params match the actual parameters");
48    f(params).map(Results::into_values)
49}