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,32 +1,44 @@
use std::rc::Rc;
use wabt::wat2wasm;
use wasmer_clif_backend::CraneliftCompiler;
use wasmer_runtime::{
self as runtime,
export::{Context, Export},
import::Imports,
types::{FuncSig, Type, Value},
vm, Import, Imports, FuncRef,
vm, FuncRef,
};
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
fn main() -> Result<(), String> {
let module = runtime::compile(EXAMPLE_WASM, &CraneliftCompiler::new())?;
let wasm_binary = wat2wasm(IMPORT_MODULE.as_bytes()).expect("WAST not valid or malformed");
let inner_module = runtime::compile(&wasm_binary, &CraneliftCompiler::new())?;
let mut imports = Imports::new();
imports.add(
imports.register_export(
"env",
"print_num",
Import::Func(
unsafe { FuncRef::new(print_num as _) },
FuncSig {
"print_i32",
Export::Function {
func: unsafe { FuncRef::new(print_num as _) },
ctx: Context::Internal,
signature: FuncSig {
params: vec![Type::I32],
returns: vec![Type::I32],
},
),
},
);
let mut instance = module.instantiate(&imports)?;
let imports = Rc::new(imports);
let ret = instance.call("main", &[Value::I32(42)])?;
let inner_instance = inner_module.instantiate(imports)?;
let mut outer_imports = Imports::new();
outer_imports.register_instance("env", inner_instance);
let outer_imports = Rc::new(outer_imports);
let outer_module = runtime::compile(EXAMPLE_WASM, &CraneliftCompiler::new())?;
let mut outer_instance = outer_module.instantiate(outer_imports)?;
let ret = outer_instance.call("main", &[Value::I32(42)])?;
println!("ret: {:?}", ret);
Ok(())
@@ -36,3 +48,24 @@ extern "C" fn print_num(n: i32, _vmctx: *mut vm::Ctx) -> i32 {
println!("print_num({})", n);
n + 1
}
static IMPORT_MODULE: &str = r#"
(module
(type $t0 (func (param i32) (result i32)))
(import "env" "print_i32" (func $print_i32 (type $t0)))
(func $print_num (export "print_num") (type $t0) (param $p0 i32) (result i32)
get_local $p0
call $print_i32))
"#;
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 mut imports = Imports::new();
imports.register_instance("env", instance);
Rc::new(imports)
}

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;
}