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 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)] #[cfg(test)]
mod tests { mod tests {
use crate::webassembly::{ use crate::webassembly::{
instantiate, ErrorKind, Export, ImportObject, Instance, Module, ResultObject, instantiate, ErrorKind, Export, ImportObject, Instance, Module, ResultObject,
}; };
use super::generate_libc_env;
use libc::putchar; use libc::putchar;
#[test] #[test]
fn test_putchar() { fn test_putchar() {
let wasm_bytes = include_wast2wasm_bytes!("tests/putchar.wast"); let wasm_bytes = include_wast2wasm_bytes!("tests/putchar.wast");
let mut import_object = ImportObject::new(); let import_object = generate_libc_env();
import_object.set("env", "putchar", putchar as *const u8);
let result_object = let result_object =
instantiate(wasm_bytes, Some(import_object)).expect("Not compiled properly"); instantiate(wasm_bytes, Some(import_object)).expect("Not compiled properly");
let module = result_object.module; let module = result_object.module;

View File

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

View File

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