Rewrite exports and imports

This commit is contained in:
Lachlan Sneff
2019-01-10 22:59:57 -05:00
parent 1a5ef3aea2
commit 230e43a894
81 changed files with 75027 additions and 16753 deletions

View File

@ -1,75 +1,46 @@
use std::rc::Rc;
use wabt::wat2wasm;
use wasmer_runtime::{Instance, Imports, Import, FuncRef, table::TableBacking, types::{Value, Type, Table, FuncSig, ElementType}, module::Module};
use std::sync::Arc;
use wasmer_clif_backend::CraneliftCompiler;
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();
let result = instance.call("signature-implicit-reused", &[]);
let result = instance.call("type-i64", &[]);
println!("result: {:?}", result);
}
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
(import \"spectest\" \"memory\" (memory (;0;) 0))
(data (;0;) (i32.const 0) \"\"))
(type (;0;) (func (result i64)))
(func (;0;) (type 0) (result i64)
i64.const 356)
(func (;1;) (type 0) (result i64)
i32.const 1
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");
let module = wasmer_runtime::compile(&wasm_binary[..], &CraneliftCompiler::new()).expect("WASM can't be compiled");
module.instantiate(&spectest_importobject()).expect("WASM can't be instantiated")
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")
}
extern "C" fn print_i32(num: i32) {
println!("{}", num);
}
extern "C" fn print() {}
static GLOBAL_I32: i32 = 666;
pub fn spectest_importobject() -> Imports {
let mut import_object = Imports::new();
import_object.add(
"spectest",
"print_i32",
Import::Func(
unsafe { FuncRef::new(print_i32 as _) },
FuncSig {
params: vec![Type::I32],
returns: vec![],
},
),
);
import_object.add(
"spectest",
"print",
Import::Func(
unsafe { FuncRef::new(print as _) },
FuncSig {
params: vec![],
returns: vec![],
},
),
);
import_object.add(
"spectest".to_string(),
"global_i32".to_string(),
Import::Global(Value::I64(GLOBAL_I32 as _)),
);
let table = Table {
ty: ElementType::Anyfunc,
min: 0,
max: Some(30),
};
import_object.add(
"spectest".to_string(),
"table".to_string(),
Import::Table(Arc::new(TableBacking::new(&table)), table),
);
return import_object;
}