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-26 00:40:07 +00:00
|
|
|
use wasmer_runtime_core::{error::Result, prelude::*, memory::Memory, types::MemoryDesc};
|
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");
|
2019-01-22 19:02:06 +00:00
|
|
|
let inner_module = wasmer_runtime_core::compile_with(&wasm_binary, &CraneliftCompiler::new())?;
|
2019-01-09 02:57:28 +00:00
|
|
|
|
2019-01-28 18:59:05 +00:00
|
|
|
let memory = Memory::new(MemoryDesc {
|
2019-01-26 00:40:07 +00:00
|
|
|
min: 1,
|
|
|
|
max: Some(1),
|
|
|
|
shared: false,
|
|
|
|
}).unwrap();
|
|
|
|
|
2019-01-28 18:59:05 +00:00
|
|
|
memory.direct_access_mut(|slice: &mut [u32]| {
|
|
|
|
slice[0] = 42;
|
|
|
|
});
|
2019-01-26 00:40:07 +00:00
|
|
|
|
2019-01-22 00:24:49 +00:00
|
|
|
let import_object = imports! {
|
|
|
|
"env" => {
|
2019-01-26 00:40:07 +00:00
|
|
|
"print_i32" => func!(print_num, [i32] -> [i32]),
|
|
|
|
"memory" => memory,
|
2019-01-22 00:24:49 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let inner_instance = inner_module.instantiate(import_object)?;
|
|
|
|
|
|
|
|
let outer_imports = imports! {
|
|
|
|
"env" => inner_instance,
|
|
|
|
};
|
2019-01-13 03:02:19 +00:00
|
|
|
|
2019-01-22 19:02:06 +00:00
|
|
|
let outer_module = wasmer_runtime_core::compile_with(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)))
|
2019-01-26 00:40:07 +00:00
|
|
|
(import "env" "memory" (memory 1 1))
|
2019-01-11 03:59:57 +00:00
|
|
|
(import "env" "print_i32" (func $print_i32 (type $t0)))
|
|
|
|
(func $print_num (export "print_num") (type $t0) (param $p0 i32) (result i32)
|
2019-01-26 00:40:07 +00:00
|
|
|
i32.const 0
|
|
|
|
i32.load
|
2019-01-11 03:59:57 +00:00
|
|
|
call $print_i32))
|
|
|
|
"#;
|