wasmer/lib/spectests/examples/simple/main.rs

83 lines
2.1 KiB
Rust
Raw Normal View History

2019-01-11 03:59:57 +00:00
use wabt::wat2wasm;
2019-11-21 21:36:44 +00:00
use wasmer_runtime::{
error,
2019-11-21 21:36:44 +00:00
func,
Global,
Memory,
imports,
compile,
Table,
Ctx,
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
};
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
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-11-21 21:36:44 +00:00
let inner_module = compile(&wasm_binary)?;
2019-01-09 02:57:28 +00:00
2019-09-20 16:59:36 +00:00
let memory_desc = MemoryDescriptor::new(Pages(1), Some(Pages(1)), false).unwrap();
2019-09-20 16:54:05 +00:00
let memory = Memory::new(memory_desc).unwrap();
2019-01-28 19:55:44 +00:00
let global = Global::new(Value::I32(42));
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-22 00:24:49 +00:00
let import_object = imports! {
"env" => {
2019-02-05 20:01:31 +00:00
"print_i32" => func!(print_num),
"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-11-21 21:36:44 +00:00
let outer_module = compile(EXAMPLE_WASM)?;
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)])?;
println!("ret: {:?}", ret);
2019-01-09 02:57:28 +00:00
Ok(())
}
2019-11-21 21:36:44 +00:00
fn print_num(ctx: &mut 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
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)))
(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)))
(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
call $identity
2019-01-11 03:59:57 +00:00
call $print_i32))
"#;