wasmer/lib/runtime/examples/simple/main.rs

39 lines
939 B
Rust
Raw Normal View History

use wasmer_clif_backend::CraneliftCompiler;
2019-01-09 02:57:28 +00:00
use wasmer_runtime::{
self as runtime,
types::{FuncSig, Type, Value},
2019-01-09 03:09:09 +00:00
vm, Import, Imports, FuncRef,
2019-01-09 02:57:28 +00:00
};
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
2019-01-09 02:57:28 +00:00
fn main() -> Result<(), String> {
let module = runtime::compile(EXAMPLE_WASM, &CraneliftCompiler::new())?;
let mut imports = Imports::new();
imports.add(
"env".to_string(),
"print_num".to_string(),
Import::Func(
2019-01-09 03:09:09 +00:00
unsafe { FuncRef::new(print_num as _) },
2019-01-09 02:57:28 +00:00
FuncSig {
params: vec![Type::I32],
returns: vec![Type::I32],
},
),
);
let mut instance = module.instantiate(&imports)?;
let ret = instance.call("main", &[Value::I32(42)])?;
println!("ret: {:?}", ret);
2019-01-09 02:57:28 +00:00
Ok(())
}
extern "C" fn print_num(n: i32, _vmctx: *mut vm::Ctx) -> i32 {
println!("print_num({})", n);
n + 1
}