2019-02-28 21:18:00 +00:00
|
|
|
use wasmer_runtime::{compile, error, imports, Func};
|
|
|
|
|
|
|
|
use wabt::wat2wasm;
|
|
|
|
|
|
|
|
static WAT: &'static str = r#"
|
|
|
|
(module
|
|
|
|
(type $t0 (func (param i32) (result i32)))
|
|
|
|
(type $t1 (func (result i32)))
|
|
|
|
(memory 1)
|
|
|
|
(global $g0 (mut i32) (i32.const 0))
|
|
|
|
(export "foo" (func $foo))
|
|
|
|
(func $foo (type $t0) (param i32) (result i32)
|
|
|
|
get_local 0
|
2019-03-01 01:20:18 +00:00
|
|
|
call $bar
|
|
|
|
)
|
|
|
|
(func $bar (type $t0) (param i32) (result i32)
|
|
|
|
get_local 0
|
|
|
|
i32.const 10
|
|
|
|
i32.add
|
2019-02-28 21:18:00 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
"#;
|
|
|
|
|
|
|
|
fn get_wasm() -> Vec<u8> {
|
|
|
|
wat2wasm(WAT).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<(), error::Error> {
|
|
|
|
let wasm = get_wasm();
|
|
|
|
|
|
|
|
let module = compile(&wasm)?;
|
|
|
|
|
|
|
|
let imports = imports! {};
|
|
|
|
|
|
|
|
let instance = module.instantiate(&imports)?;
|
|
|
|
|
|
|
|
let foo: Func<i32, i32> = instance.func("foo")?;
|
|
|
|
|
|
|
|
let result = foo.call(42);
|
|
|
|
|
|
|
|
println!("result: {:?}", result);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|