Improved wasmer script

This commit is contained in:
Syrus Akbary 2018-10-17 16:45:24 +02:00
parent e97b47e147
commit ba050f35cc
4 changed files with 29 additions and 6 deletions

View File

@ -1,18 +1,24 @@
use libc::putchar;
use crate::webassembly::ImportObject;
pub fn generate_libc_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
let mut import_object = ImportObject::new();
import_object.set("env", "putchar", putchar as *const u8);
import_object
}
#[cfg(test)]
mod tests {
use crate::webassembly::{
instantiate, ErrorKind, Export, ImportObject, Instance, Module, ResultObject,
};
use super::generate_libc_env;
use libc::putchar;
#[test]
fn test_putchar() {
let wasm_bytes = include_wast2wasm_bytes!("tests/putchar.wast");
let mut import_object = ImportObject::new();
import_object.set("env", "putchar", putchar as *const u8);
let import_object = generate_libc_env();
let result_object =
instantiate(wasm_bytes, Some(import_object)).expect("Not compiled properly");
let module = result_object.module;

View File

@ -9,6 +9,7 @@ macro_rules! get_instance_function {
}};
}
#[macro_export]
macro_rules! include_wast2wasm_bytes {
($x:expr) => {{
use wabt::wat2wasm;
@ -17,9 +18,16 @@ macro_rules! include_wast2wasm_bytes {
}};
}
// #[cfg(feature = "debug")]
#[cfg(feature= "debug")]
#[macro_export]
macro_rules! debug {
($fmt:expr) => (println!(concat!("Wasmer::", $fmt)));
($fmt:expr, $($arg:tt)*) => (println!(concat!("Wasmer::", $fmt, "\n"), $($arg)*));
}
#[cfg(not(feature= "debug"))]
#[macro_export]
macro_rules! debug {
($fmt:expr) => {};
($fmt:expr, $($arg:tt)*) => {};
}

View File

@ -70,7 +70,16 @@ fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
wasm_binary = wat2wasm(wasm_binary).map_err(|err| String::from(err.description()))?;
}
webassembly::instantiate(wasm_binary, None).map_err(|err| String::from(err.description()))?;
let import_object = integrations::generate_libc_env();
let webassembly::ResultObject {module, instance} = webassembly::instantiate(wasm_binary, Some(import_object)).map_err(|err| String::from(err.description()))?;
let func_index = instance.start_func.unwrap_or_else(|| {
match module.info.exports.get("main") {
Some(&webassembly::Export::Function(index)) => index,
_ => panic!("Main function not found"),
}
});
let main: fn() = get_instance_function!(instance, func_index);
main();
Ok(())
}

View File

@ -121,7 +121,7 @@ pub struct Instance {
import_functions: Vec<*const u8>,
/// The module start function
start_func: Option<FuncIndex>,
pub start_func: Option<FuncIndex>,
// Region start memory location
// code_base: *const (),
}