2019-04-22 22:06:40 +00:00
|
|
|
use wasmer_runtime::{compile, error, error::RuntimeError, imports, Ctx, Func, Value};
|
2019-02-28 21:18:00 +00:00
|
|
|
|
|
|
|
use wabt::wat2wasm;
|
|
|
|
|
|
|
|
static WAT: &'static str = r#"
|
|
|
|
(module
|
2019-03-05 03:56:02 +00:00
|
|
|
(type (;0;) (func (result i32)))
|
2019-04-09 22:53:01 +00:00
|
|
|
(import "env" "do_panic" (func $do_panic (type 0)))
|
2019-03-05 03:56:02 +00:00
|
|
|
(func $dbz (result i32)
|
2019-04-22 22:06:40 +00:00
|
|
|
call $do_panic
|
|
|
|
drop
|
2019-03-05 03:56:02 +00:00
|
|
|
i32.const 42
|
|
|
|
i32.const 0
|
|
|
|
i32.div_u
|
|
|
|
)
|
|
|
|
(export "dbz" (func $dbz))
|
|
|
|
)
|
2019-03-04 22:41:56 +00:00
|
|
|
"#;
|
|
|
|
|
2019-03-05 00:44:46 +00:00
|
|
|
// static WAT2: &'static str = r#"
|
|
|
|
// (module
|
|
|
|
// (type $t0 (func (param i32)))
|
|
|
|
// (type $t1 (func))
|
|
|
|
// (func $print_i32 (export "print_i32") (type $t0) (param $lhs i32))
|
|
|
|
// (func $print (export "print") (type $t1))
|
|
|
|
// (table $table (export "table") 10 20 anyfunc)
|
|
|
|
// (memory $memory (export "memory") 1 2)
|
|
|
|
// (global $global_i32 (export "global_i32") i32 (i32.const 666)))
|
|
|
|
// "#;
|
2019-02-28 21:18:00 +00:00
|
|
|
|
|
|
|
fn get_wasm() -> Vec<u8> {
|
|
|
|
wat2wasm(WAT).unwrap()
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:17:10 +00:00
|
|
|
fn foobar(_ctx: &mut Ctx) -> i32 {
|
2019-03-04 22:41:56 +00:00
|
|
|
42
|
|
|
|
}
|
|
|
|
|
2019-04-22 22:06:40 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct ExitCode {
|
|
|
|
code: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn do_panic(_ctx: &mut Ctx) -> Result<i32, ExitCode> {
|
|
|
|
Err(ExitCode { code: 42 })
|
2019-04-09 22:53:01 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 21:18:00 +00:00
|
|
|
fn main() -> Result<(), error::Error> {
|
|
|
|
let wasm = get_wasm();
|
|
|
|
|
|
|
|
let module = compile(&wasm)?;
|
|
|
|
|
2019-03-05 00:44:46 +00:00
|
|
|
// let import_module = compile(&wat2wasm(WAT2).unwrap())?;
|
|
|
|
// let import_instance = import_module.instantiate(&imports! {})?;
|
2019-03-04 22:41:56 +00:00
|
|
|
|
2019-03-05 00:44:46 +00:00
|
|
|
// let imports = imports! {
|
|
|
|
// "spectest" => import_instance,
|
|
|
|
// };
|
2019-02-28 21:18:00 +00:00
|
|
|
|
2019-03-01 23:48:43 +00:00
|
|
|
println!("instantiating");
|
2019-04-09 22:53:01 +00:00
|
|
|
let instance = module.instantiate(&imports! {
|
|
|
|
"env" => {
|
|
|
|
"do_panic" => Func::new(do_panic),
|
|
|
|
},
|
|
|
|
})?;
|
2019-02-28 21:18:00 +00:00
|
|
|
|
2019-04-18 17:00:15 +00:00
|
|
|
let foo: Func<(), i32> = instance.func("dbz")?;
|
2019-02-28 21:18:00 +00:00
|
|
|
|
2019-04-18 17:00:15 +00:00
|
|
|
let result = foo.call();
|
2019-02-28 21:18:00 +00:00
|
|
|
|
|
|
|
println!("result: {:?}", result);
|
|
|
|
|
2019-04-22 22:06:40 +00:00
|
|
|
if let Err(RuntimeError::Error { data }) = result {
|
|
|
|
if let Ok(exit_code) = data.downcast::<ExitCode>() {
|
|
|
|
println!("exit code: {:?}", exit_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 21:18:00 +00:00
|
|
|
Ok(())
|
|
|
|
}
|