Support imported functions

This commit is contained in:
Lachlan Sneff
2019-01-08 21:57:28 -05:00
parent 55b7cae523
commit bba168e61e
14 changed files with 265 additions and 151 deletions

View File

@ -1,13 +1,38 @@
use wasmer_runtime as runtime;
use wasmer_clif_backend::CraneliftCompiler;
use wasmer_runtime::{
self as runtime,
types::{FuncSig, Type, Value},
vm, Import, Imports,
};
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
fn main() {
let compiler = CraneliftCompiler::new();
let module = runtime::compile(EXAMPLE_WASM, &compiler).unwrap();
let imports = runtime::Imports::new();
let mut instance = module.instantiate(&imports).unwrap();
let ret = instance.call("main", &[runtime::types::Value::I32(42)]);
fn main() -> Result<(), String> {
let module = runtime::compile(EXAMPLE_WASM, &CraneliftCompiler::new())?;
let mut imports = Imports::new();
imports.add(
"env".to_string(),
"print_num".to_string(),
Import::Func(
print_num as _,
FuncSig {
params: vec![Type::I32],
returns: vec![Type::I32],
},
),
);
let mut instance = module.instantiate(&imports)?;
let ret = instance.call("main", &[Value::I32(42)])?;
println!("ret: {:?}", ret);
}
Ok(())
}
extern "C" fn print_num(n: i32, _vmctx: *mut vm::Ctx) -> i32 {
println!("print_num({})", n);
n + 1
}