2019-01-11 03:59:57 +00:00
|
|
|
use wabt::wat2wasm;
|
2019-01-08 21:04:03 +00:00
|
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
2019-01-21 22:43:04 +00:00
|
|
|
use wasmer_runtime::{self as runtime, error::Result, prelude::*};
|
2019-01-08 21:04:03 +00:00
|
|
|
|
|
|
|
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
|
|
|
|
|
2019-01-18 18:54:16 +00:00
|
|
|
fn main() -> Result<()> {
|
2019-01-11 03:59:57 +00:00
|
|
|
let wasm_binary = wat2wasm(IMPORT_MODULE.as_bytes()).expect("WAST not valid or malformed");
|
|
|
|
let inner_module = runtime::compile(&wasm_binary, &CraneliftCompiler::new())?;
|
2019-01-09 02:57:28 +00:00
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
let mut env_namespace = Namespace::new();
|
2019-01-21 23:10:07 +00:00
|
|
|
env_namespace.insert(
|
|
|
|
"print_i32",
|
2019-01-21 22:43:04 +00:00
|
|
|
export_func!(
|
|
|
|
print_num,
|
2019-01-21 23:21:51 +00:00
|
|
|
[i32] -> [i32]
|
2019-01-21 23:10:07 +00:00
|
|
|
),
|
|
|
|
);
|
2019-01-21 22:43:04 +00:00
|
|
|
|
|
|
|
let mut imports = ImportObject::new();
|
2019-01-12 21:24:17 +00:00
|
|
|
imports.register("env", env_namespace);
|
2019-01-09 02:57:28 +00:00
|
|
|
|
2019-01-17 22:13:28 +00:00
|
|
|
let inner_instance = inner_module.instantiate(imports)?;
|
2019-01-09 02:57:28 +00:00
|
|
|
|
2019-01-21 22:43:04 +00:00
|
|
|
let mut outer_imports = ImportObject::new();
|
2019-01-12 21:24:17 +00:00
|
|
|
outer_imports.register("env", inner_instance);
|
2019-01-13 03:02:19 +00:00
|
|
|
|
2019-01-11 03:59:57 +00:00
|
|
|
let outer_module = runtime::compile(EXAMPLE_WASM, &CraneliftCompiler::new())?;
|
2019-01-17 22:13:28 +00:00
|
|
|
let mut outer_instance = outer_module.instantiate(outer_imports)?;
|
2019-01-11 03:59:57 +00:00
|
|
|
let ret = outer_instance.call("main", &[Value::I32(42)])?;
|
2019-01-08 21:04:03 +00:00
|
|
|
println!("ret: {:?}", ret);
|
2019-01-09 02:57:28 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-21 23:10:07 +00:00
|
|
|
extern "C" fn print_num(n: i32, _vmctx: &mut vm::Ctx) -> i32 {
|
2019-01-09 02:57:28 +00:00
|
|
|
println!("print_num({})", n);
|
|
|
|
n + 1
|
|
|
|
}
|
2019-01-11 03:59:57 +00:00
|
|
|
|
|
|
|
static IMPORT_MODULE: &str = r#"
|
|
|
|
(module
|
|
|
|
(type $t0 (func (param i32) (result i32)))
|
|
|
|
(import "env" "print_i32" (func $print_i32 (type $t0)))
|
|
|
|
(func $print_num (export "print_num") (type $t0) (param $p0 i32) (result i32)
|
|
|
|
get_local $p0
|
|
|
|
call $print_i32))
|
|
|
|
"#;
|