use examples_common::get_wasm_module_path;
use hyperlight_wasm::{LoadedWasmSandbox, Result, SandboxBuilder};
fn main() -> Result<()> {
type TestFn = fn(&mut LoadedWasmSandbox) -> Result<i32>;
let tests: &[(String, TestFn)] = &[
("hello_world".to_string(), |sb| {
sb.call_guest_function("hello_world", ())
}),
("add".to_string(), |sb| {
sb.call_guest_function("add", (5i32, 3i32))
}),
("call_host_function".to_string(), |sb| {
sb.call_guest_function("call_host_function", 5i32)
}),
];
for (idx, case) in tests.iter().enumerate() {
let (fn_name, func) = case;
let host_func = |a: i32| {
println!("host_func called with {}", a);
a + 1
};
let mut proto_wasm_sandbox = SandboxBuilder::new()
.with_guest_input_buffer_size(256 * 1024)
.with_guest_heap_size(1280 * 1024)
.build()?;
proto_wasm_sandbox.register("TestHostFunc", host_func)?;
let wasm_sandbox = proto_wasm_sandbox.load_runtime()?;
let mod_path = get_wasm_module_path("rust_wasm_samples.aot")?;
let mut loaded_wasm_sandbox = wasm_sandbox.load_module(mod_path)?;
let result: i32 = func(&mut loaded_wasm_sandbox)?;
println!("test case {idx} fn_name: {fn_name}\nresult: {}", result)
}
Ok(())
}