2019-04-27 01:45:18 +00:00
|
|
|
use wasmer_runtime::{func, imports, instantiate};
|
|
|
|
use wasmer_runtime_core::vm::Ctx;
|
|
|
|
use wasmer_wasi::generate_import_object;
|
|
|
|
|
|
|
|
static PLUGIN_WASM: &'static [u8] = include_bytes!("plugin-for-example.wasm");
|
|
|
|
|
|
|
|
fn it_works(_ctx: &mut Ctx) -> i32 {
|
|
|
|
println!("Hello from outside WASI");
|
|
|
|
5
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// WASI imports
|
|
|
|
let mut base_imports = generate_import_object(vec![], vec![], vec![]);
|
|
|
|
// env is the default namespace for extern functions
|
|
|
|
let custom_imports = imports! {
|
|
|
|
"env" => {
|
|
|
|
"it_works" => func!(it_works),
|
|
|
|
},
|
|
|
|
};
|
2019-04-29 18:18:17 +00:00
|
|
|
// The WASI imports object contains all required import functions for a WASI module to run.
|
|
|
|
// Extend this imports with our custom imports containing "it_works" function so that our custom wasm code may run.
|
2019-04-27 01:45:18 +00:00
|
|
|
base_imports.extend(custom_imports);
|
|
|
|
let instance =
|
|
|
|
instantiate(PLUGIN_WASM, &base_imports).expect("failed to instantiate wasm module");
|
|
|
|
|
2019-04-29 18:18:35 +00:00
|
|
|
// get a reference to the function "plugin_entrypoint" which takes an i32 and returns an i32
|
2019-04-27 01:45:18 +00:00
|
|
|
let entry_point = instance.func::<(i32), i32>("plugin_entrypoint").unwrap();
|
|
|
|
let result = entry_point.call(2).expect("failed to execute plugin");
|
|
|
|
println!("result: {}", result);
|
|
|
|
}
|