2019-01-11 03:59:57 +00:00
|
|
|
use wabt::wat2wasm;
|
2019-01-28 19:55:44 +00:00
|
|
|
use wasmer_runtime_core::{
|
2019-03-06 06:15:07 +00:00
|
|
|
backend::Compiler,
|
2019-02-08 21:08:03 +00:00
|
|
|
error,
|
2019-01-28 19:55:44 +00:00
|
|
|
global::Global,
|
|
|
|
memory::Memory,
|
|
|
|
prelude::*,
|
2019-01-29 18:16:39 +00:00
|
|
|
table::Table,
|
2019-01-29 21:04:42 +00:00
|
|
|
types::{ElementType, MemoryDescriptor, TableDescriptor, Value},
|
2019-01-29 23:44:15 +00:00
|
|
|
units::Pages,
|
2019-01-28 19:55:44 +00:00
|
|
|
};
|
2019-01-08 21:04:03 +00:00
|
|
|
|
2019-03-06 06:15:07 +00:00
|
|
|
#[cfg(feature = "clif")]
|
|
|
|
fn get_compiler() -> impl Compiler {
|
|
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
|
|
|
CraneliftCompiler::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "llvm")]
|
|
|
|
fn get_compiler() -> impl Compiler {
|
2019-05-01 01:11:44 +00:00
|
|
|
use wasmer_llvm_backend::LLVMCompiler;
|
|
|
|
LLVMCompiler::new()
|
2019-03-06 06:15:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 19:44:03 +00:00
|
|
|
#[cfg(feature = "singlepass")]
|
2019-03-13 02:14:50 +00:00
|
|
|
fn get_compiler() -> impl Compiler {
|
2019-04-11 19:44:03 +00:00
|
|
|
use wasmer_singlepass_backend::SinglePassCompiler;
|
2019-03-13 02:14:50 +00:00
|
|
|
SinglePassCompiler::new()
|
|
|
|
}
|
|
|
|
|
2019-04-11 19:44:03 +00:00
|
|
|
#[cfg(not(any(feature = "llvm", feature = "clif", feature = "singlepass")))]
|
2019-03-06 06:15:07 +00:00
|
|
|
fn get_compiler() -> impl Compiler {
|
|
|
|
panic!("compiler not specified, activate a compiler via features");
|
|
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
|
|
|
CraneliftCompiler::new()
|
|
|
|
}
|
|
|
|
|
2019-01-08 21:04:03 +00:00
|
|
|
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
|
|
|
|
|
2019-02-08 21:08:03 +00:00
|
|
|
fn main() -> error::Result<()> {
|
2019-01-11 03:59:57 +00:00
|
|
|
let wasm_binary = wat2wasm(IMPORT_MODULE.as_bytes()).expect("WAST not valid or malformed");
|
2019-02-07 00:26:45 +00:00
|
|
|
|
2019-03-06 06:15:07 +00:00
|
|
|
let inner_module = wasmer_runtime_core::compile_with(&wasm_binary, &get_compiler())?;
|
2019-01-09 02:57:28 +00:00
|
|
|
|
2019-02-05 07:07:58 +00:00
|
|
|
let memory = Memory::new(MemoryDescriptor {
|
2019-01-29 23:44:15 +00:00
|
|
|
minimum: Pages(1),
|
|
|
|
maximum: Some(Pages(1)),
|
2019-01-26 00:40:07 +00:00
|
|
|
shared: false,
|
2019-01-28 19:55:44 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let global = Global::new(Value::I32(42));
|
2019-01-26 00:40:07 +00:00
|
|
|
|
2019-01-29 21:04:42 +00:00
|
|
|
let table = Table::new(TableDescriptor {
|
|
|
|
element: ElementType::Anyfunc,
|
2019-01-29 23:44:15 +00:00
|
|
|
minimum: 10,
|
|
|
|
maximum: None,
|
2019-01-29 18:16:39 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
2019-02-05 20:01:31 +00:00
|
|
|
memory.view()[0].set(42);
|
2019-01-26 00:40:07 +00:00
|
|
|
|
2019-01-22 00:24:49 +00:00
|
|
|
let import_object = imports! {
|
|
|
|
"env" => {
|
2019-02-05 20:01:31 +00:00
|
|
|
"print_i32" => func!(print_num),
|
2019-01-26 00:40:07 +00:00
|
|
|
"memory" => memory,
|
2019-01-28 19:55:44 +00:00
|
|
|
"global" => global,
|
2019-01-29 18:16:39 +00:00
|
|
|
"table" => table,
|
2019-01-22 00:24:49 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-02-04 23:07:32 +00:00
|
|
|
let inner_instance = inner_module.instantiate(&import_object)?;
|
2019-01-22 00:24:49 +00:00
|
|
|
|
|
|
|
let outer_imports = imports! {
|
|
|
|
"env" => inner_instance,
|
|
|
|
};
|
2019-01-13 03:02:19 +00:00
|
|
|
|
2019-03-06 06:15:07 +00:00
|
|
|
let outer_module = wasmer_runtime_core::compile_with(EXAMPLE_WASM, &get_compiler())?;
|
2019-02-04 23:07:32 +00:00
|
|
|
let 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-02-09 22:19:04 +00:00
|
|
|
fn print_num(ctx: &mut vm::Ctx, n: i32) -> Result<i32, ()> {
|
2019-01-09 02:57:28 +00:00
|
|
|
println!("print_num({})", n);
|
2019-01-29 20:12:37 +00:00
|
|
|
|
2019-02-04 23:07:32 +00:00
|
|
|
let memory: &Memory = ctx.memory(0);
|
2019-01-29 20:12:37 +00:00
|
|
|
|
2019-02-05 20:01:31 +00:00
|
|
|
let a: i32 = memory.view()[0].get();
|
2019-01-29 20:12:37 +00:00
|
|
|
|
2019-02-08 21:08:03 +00:00
|
|
|
Ok(a + n + 1)
|
2019-01-09 02:57:28 +00:00
|
|
|
}
|
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-29 18:16:39 +00:00
|
|
|
(import "env" "table" (table 10 anyfunc))
|
2019-01-28 19:55:44 +00:00
|
|
|
(import "env" "global" (global i32))
|
2019-01-11 03:59:57 +00:00
|
|
|
(import "env" "print_i32" (func $print_i32 (type $t0)))
|
2019-02-07 00:26:45 +00:00
|
|
|
(func $identity (type $t0) (param $p0 i32) (result i32)
|
|
|
|
get_local $p0)
|
2019-01-11 03:59:57 +00:00
|
|
|
(func $print_num (export "print_num") (type $t0) (param $p0 i32) (result i32)
|
2019-01-28 19:55:44 +00:00
|
|
|
get_global 0
|
2019-02-07 00:26:45 +00:00
|
|
|
call $identity
|
2019-01-11 03:59:57 +00:00
|
|
|
call $print_i32))
|
|
|
|
"#;
|