mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-05 02:20:19 +00:00
Improved wasmer script
This commit is contained in:
parent
e97b47e147
commit
ba050f35cc
@ -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;
|
||||
|
@ -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)*) => {};
|
||||
}
|
||||
|
11
src/main.rs
11
src/main.rs
@ -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(())
|
||||
}
|
||||
|
||||
|
@ -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 (),
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user