pub fn host_function_wrapper<Params: InteropValueList, Results: InteropValueList>(
params: Vec<Value>,
f: impl FnOnce(Params) -> Result<Results, HaltExecutionError>,
) -> Result<Vec<Value>, HaltExecutionError>Expand description
Helper function to quickly construct host functions without worrying about wasm to Rust
type conversion. For reading/writing user data into the current configuration, simply move
user_data into the passed closure.
ยงExample
use wasm::{validate, RuntimeInstance, host_function_wrapper, Value, HaltExecutionError};
fn my_wrapped_host_func(user_data: &mut (), params: Vec<Value>) -> Result<Vec<Value>, HaltExecutionError> {
host_function_wrapper(params, |(x, y): (u32, i32)| -> Result<u32, HaltExecutionError> {
let _user_data = user_data;
Ok(x + (y as u32))
})
}
fn main() {
let mut instance = RuntimeInstance::new(());
let foo_bar = instance.add_host_function_typed::<(u32,i32), u32>("foo", "bar", my_wrapped_host_func).unwrap();
}