Add export_func macro, prelude, and rename imports stuff

This commit is contained in:
Lachlan Sneff
2019-01-21 14:43:04 -08:00
parent c25fce1721
commit 10c5aa02a2
13 changed files with 81 additions and 63 deletions

View File

@ -1,13 +1,6 @@
use wabt::wat2wasm;
use wasmer_clif_backend::CraneliftCompiler;
use wasmer_runtime::{
self as runtime,
error::Result,
export::{Context, Export, FuncPointer},
import::{Imports, NamespaceMap},
types::{FuncSig, Type, Value},
vm,
};
use wasmer_runtime::{self as runtime, error::Result, prelude::*};
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
@ -15,24 +8,20 @@ fn main() -> Result<()> {
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 env_namespace = NamespaceMap::new();
env_namespace.insert(
"print_i32",
Export::Function {
func: unsafe { FuncPointer::new(print_num as _) },
ctx: Context::Internal,
signature: FuncSig {
params: vec![Type::I32],
returns: vec![Type::I32],
},
},
);
let mut imports = Imports::new();
let mut env_namespace = Namespace::new();
env_namespace.insert("print_i32", unsafe {
export_func!(
print_num,
[I32] -> [I32]
)
});
let mut imports = ImportObject::new();
imports.register("env", env_namespace);
let inner_instance = inner_module.instantiate(imports)?;
let mut outer_imports = Imports::new();
let mut outer_imports = ImportObject::new();
outer_imports.register("env", inner_instance);
let outer_module = runtime::compile(EXAMPLE_WASM, &CraneliftCompiler::new())?;

View File

@ -1,6 +1,6 @@
use wabt::wat2wasm;
use wasmer_clif_backend::CraneliftCompiler;
use wasmer_runtime::{import::Imports, Instance};
use wasmer_runtime::{import::ImportObject, Instance};
fn main() {
let mut instance = create_module_1();
@ -42,14 +42,14 @@ static IMPORT_MODULE: &str = r#"
(global $global_i32 (export "global_i32") i32 (i32.const 666)))
"#;
pub fn generate_imports() -> Imports {
pub fn generate_imports() -> ImportObject {
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(Imports::new())
.instantiate(ImportObject::new())
.expect("WASM can't be instantiated");
let mut imports = Imports::new();
let mut imports = ImportObject::new();
imports.register("spectest", instance);
imports
}