2019-01-09 23:31:11 +00:00
|
|
|
use wabt::wat2wasm;
|
|
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
2019-01-12 22:53:17 +00:00
|
|
|
use wasmer_runtime::{import::Imports, Instance};
|
2019-01-09 23:31:11 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut instance = create_module_1();
|
2019-01-17 21:09:05 +00:00
|
|
|
let result = instance.call("get-0-ref", &[]);
|
2019-01-09 23:31:11 +00:00
|
|
|
println!("result: {:?}", result);
|
|
|
|
}
|
|
|
|
|
2019-01-13 04:48:21 +00:00
|
|
|
// fn generate_imports() -> Imports {
|
|
|
|
// // let wasm_binary = wat2wasm(IMPORT_MODULE.as_bytes()).expect("WAST not valid or malformed");
|
|
|
|
// // let module = wasmer_runtime::compile(&wasm_binary[..], &CraneliftCompiler::new()).expect("WASM can't be compiled");
|
|
|
|
// // let instance = module.instantiate(Rc::new(Imports::new())).expect("WASM can't be instantiated");
|
|
|
|
// let imports = Imports::new();
|
|
|
|
// // imports.register("spectest", instance);
|
|
|
|
// imports
|
|
|
|
// }
|
2019-01-11 03:59:57 +00:00
|
|
|
|
2019-01-12 22:52:14 +00:00
|
|
|
fn create_module_1() -> Instance {
|
2019-01-17 21:09:05 +00:00
|
|
|
let module_str = r#"(module
|
|
|
|
(type (;0;) (func (result i32)))
|
|
|
|
(import "spectest" "global_i32" (global (;0;) i32))
|
|
|
|
(func (;0;) (type 0) (result i32)
|
|
|
|
get_global 0)
|
|
|
|
(func (;1;) (type 0) (result i32)
|
|
|
|
get_global 1)
|
|
|
|
(global (;1;) i32 (get_global 0))
|
|
|
|
(export "get-0" (func 0))
|
|
|
|
(export "get-0-ref" (func 1)))
|
|
|
|
"#;
|
2019-01-09 23:31:11 +00:00
|
|
|
let wasm_binary = wat2wasm(module_str.as_bytes()).expect("WAST not valid or malformed");
|
2019-01-11 03:59:57 +00:00
|
|
|
let module = wasmer_runtime::compile(&wasm_binary[..], &CraneliftCompiler::new())
|
|
|
|
.expect("WASM can't be compiled");
|
|
|
|
module
|
2019-01-17 21:09:05 +00:00
|
|
|
.instantiate(&mut generate_imports())
|
2019-01-11 03:59:57 +00:00
|
|
|
.expect("WASM can't be instantiated")
|
2019-01-10 04:43:18 +00:00
|
|
|
}
|
2019-01-17 21:09:05 +00:00
|
|
|
|
|
|
|
static IMPORT_MODULE: &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)))
|
|
|
|
"#;
|
|
|
|
|
|
|
|
pub fn generate_imports() -> Imports {
|
|
|
|
let wasm_binary = wat2wasm(IMPORT_MODULE.as_bytes()).expect("WAST not valid or malformed");
|
|
|
|
let module = wasmer_runtime::compile(&wasm_binary[..], &CraneliftCompiler::new())
|
|
|
|
.expect("WASM can't be compiled");
|
|
|
|
let instance = module
|
|
|
|
.instantiate(&mut Imports::new())
|
|
|
|
.expect("WASM can't be instantiated");
|
|
|
|
let mut imports = Imports::new();
|
|
|
|
imports.register("spectest", instance);
|
|
|
|
imports
|
|
|
|
}
|