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