mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-05 02:20:19 +00:00
46 lines
1.5 KiB
Rust
46 lines
1.5 KiB
Rust
/// This example demonstrates the use of callbacks: calling functions (Host and Wasm)
|
|
/// passed to us from the Wasm via hostcall
|
|
use wasmer::{compiler::compile, func, imports, vm::Ctx, Func};
|
|
use wasmer_runtime_core::{structures::TypedIndex, types::TableIndex};
|
|
|
|
static WASM: &'static str = "examples/callback-guest/callback-guest.wasm";
|
|
|
|
/// This function matches our arbitrarily decided callback signature
|
|
/// in this example we'll only call functions that take no arguments and return one value
|
|
fn host_callback(_ctx: &mut Ctx) -> u32 {
|
|
55
|
|
}
|
|
|
|
fn call_guest_fn(ctx: &mut Ctx, guest_fn: u32) -> u32 {
|
|
// We get a TableIndex from our raw value passed in
|
|
let guest_fn_typed = TableIndex::new(guest_fn as usize);
|
|
// and use it to call the corresponding function
|
|
let result = ctx.call_with_table_index(guest_fn_typed, &[]).unwrap();
|
|
|
|
println!("Guest fn {} returned {:?}", guest_fn, result);
|
|
|
|
0
|
|
}
|
|
|
|
fn main() {
|
|
let wasm_bytes =
|
|
std::fs::read(WASM).expect(&format!("Could not read in WASM plugin at {}", WASM));
|
|
|
|
let imports = imports! {
|
|
"env" => {
|
|
"call_guest_fn" => func!(call_guest_fn),
|
|
"call_guest_fn2" => func!(call_guest_fn),
|
|
"host_callback" => func!(host_callback),
|
|
},
|
|
};
|
|
|
|
let module = compile(&wasm_bytes[..]).unwrap();
|
|
let instance = module
|
|
.instantiate(&imports)
|
|
.expect("failed to instantiate wasm module");
|
|
|
|
let entry_point: Func<(u32, u32), u32> = instance.exports.get("main").unwrap();
|
|
|
|
entry_point.call(0, 0).expect("START");
|
|
}
|