use mielin_hal::capabilities::HardwareCapabilities;
use mielin_wasm::executor::WasmExecutor;
use mielin_wasm::host::HostState;
use wasmtime::{Caller, Linker};
#[allow(dead_code)]
fn add_greeting_function(linker: &mut Linker<HostState>) -> anyhow::Result<()> {
linker.func_wrap("env", "greet", |caller: Caller<'_, HostState>| -> i32 {
println!("Hello from host function!");
let _state = caller.data();
0
})?;
Ok(())
}
fn main() -> anyhow::Result<()> {
println!("=== Basic Host Function Example ===\n");
let executor = WasmExecutor::new()?;
let wasm = wat::parse_str(
r#"
(module
;; Import the host function
(import "env" "greet" (func $greet (result i32)))
;; Export a function that calls the host function
(func (export "call_greet") (result i32)
call $greet
)
)
"#,
)?;
println!("Compiling WASM module...");
let module = executor.compile_module(&wasm)?;
println!("Instantiating module...");
let (instance, mut store) = executor.instantiate(&module, HardwareCapabilities::NONE)?;
println!("\nCalling WASM function that invokes host function...");
let call_greet = instance.get_typed_func::<(), i32>(&mut store, "call_greet")?;
let result = call_greet.call(&mut store, ())?;
println!("Result: {}", result);
println!("\n=== Example Complete ===");
Ok(())
}