wasmer/lib/runtime/examples/test.rs

47 lines
1.6 KiB
Rust
Raw Normal View History

2019-01-11 03:59:57 +00:00
use std::rc::Rc;
use wabt::wat2wasm;
use wasmer_clif_backend::CraneliftCompiler;
2019-01-11 03:59:57 +00:00
use wasmer_runtime::{
export::{Context, Export},
import::Imports,
module::Module,
table::TableBacking,
types::{ElementType, FuncSig, Table, Type, Value},
FuncRef, Instance,
};
fn main() {
let mut instance = create_module_1();
2019-01-11 03:59:57 +00:00
let result = instance.call("type-i64", &[]);
println!("result: {:?}", result);
}
2019-01-11 03:59:57 +00:00
fn generate_imports() -> Rc<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_instance("spectest", instance);
Rc::new(imports)
}
fn create_module_1() -> Box<Instance> {
let module_str = "(module
2019-01-11 03:59:57 +00:00
(type (;0;) (func (result i64)))
(func (;0;) (type 0) (result i64)
i64.const 356)
(func (;1;) (type 0) (result i64)
i32.const 0
2019-01-11 03:59:57 +00:00
call_indirect (type 0))
(table (;0;) 2 anyfunc)
(export \"type-i64\" (func 1))
(elem (;0;) (i32.const 0) 0 1))
";
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
.instantiate(generate_imports())
.expect("WASM can't be instantiated")
2019-01-10 04:43:18 +00:00
}