wasmer/lib/spectests/examples/test.rs

80 lines
2.6 KiB
Rust
Raw Normal View History

use wabt::wat2wasm;
use wasmer_runtime_core::{backend::Compiler, import::ImportObject, Instance};
fn main() {
2019-01-29 18:16:39 +00:00
let instance = create_module_1();
2019-01-18 22:18:06 +00:00
let result = instance.call("call-overwritten-element", &[]);
println!("result: {:?}", result);
}
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)))
2019-01-18 22:18:06 +00:00
(import "spectest" "table" (table (;0;) 10 anyfunc))
2019-01-17 21:09:05 +00:00
(func (;0;) (type 0) (result i32)
2019-01-18 22:18:06 +00:00
i32.const 65)
2019-01-17 21:09:05 +00:00
(func (;1;) (type 0) (result i32)
2019-01-18 22:18:06 +00:00
i32.const 66)
(func (;2;) (type 0) (result i32)
i32.const 9
call_indirect (type 0))
(export "call-overwritten-element" (func 2))
(elem (;0;) (i32.const 9) 0)
(elem (;1;) (i32.const 9) 1))
2019-01-17 21:09:05 +00:00
"#;
let wasm_binary = wat2wasm(module_str.as_bytes()).expect("WAST not valid or malformed");
let module = wasmer_runtime_core::compile_with(&wasm_binary[..], &get_compiler())
2019-01-11 03:59:57 +00:00
.expect("WASM can't be compiled");
module
.instantiate(&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
#[cfg(feature = "clif")]
fn get_compiler() -> impl Compiler {
use wasmer_clif_backend::CraneliftCompiler;
CraneliftCompiler::new()
}
#[cfg(feature = "llvm")]
fn get_compiler() -> impl Compiler {
use wasmer_llvm_backend::LLVMCompiler;
LLVMCompiler::new()
}
#[cfg(feature = "singlepass")]
fn get_compiler() -> impl Compiler {
use wasmer_singlepass_backend::SinglePassCompiler;
SinglePassCompiler::new()
}
#[cfg(not(any(feature = "llvm", feature = "clif", feature = "singlepass")))]
fn get_compiler() -> impl Compiler {
panic!("compiler not specified, activate a compiler via features");
use wasmer_clif_backend::CraneliftCompiler;
CraneliftCompiler::new()
}
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() -> ImportObject {
2019-01-17 21:09:05 +00:00
let wasm_binary = wat2wasm(IMPORT_MODULE.as_bytes()).expect("WAST not valid or malformed");
let module = wasmer_runtime_core::compile_with(&wasm_binary[..], &get_compiler())
2019-01-17 21:09:05 +00:00
.expect("WASM can't be compiled");
let instance = module
.instantiate(&ImportObject::new())
2019-01-17 21:09:05 +00:00
.expect("WASM can't be instantiated");
let mut imports = ImportObject::new();
2019-01-17 21:09:05 +00:00
imports.register("spectest", instance);
imports
}