Refactored emscripten integratoin

# Conflicts:
#	src/integrations/mod.rs
This commit is contained in:
Syrus Akbary 2018-11-13 19:19:23 -08:00
parent c452ff1c04
commit 7bd2af98b3
8 changed files with 61 additions and 55 deletions

View File

@ -1,53 +0,0 @@
use crate::webassembly::{ImportObject, ImportValue, Instance};
use libc::{printf, putchar};
extern "C" fn _printf(memory_offset: i32, extra: i32, instance: &Instance) -> i32 {
let mem = &instance.memories[0];
return unsafe {
let base_memory_offset = mem.mmap.as_ptr().offset(memory_offset as isize) as *const i8;
printf(base_memory_offset, extra)
};
}
pub fn generate_libc_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
let mut import_object = ImportObject::new();
import_object.set("env", "printf", ImportValue::Func(_printf as *const u8));
import_object.set("env", "putchar", ImportValue::Func(putchar as *const u8));
import_object
}
#[cfg(test)]
mod tests {
use super::generate_libc_env;
use crate::webassembly::{instantiate, Export, Instance};
#[test]
fn test_putchar() {
let wasm_bytes = include_wast2wasm_bytes!("tests/putchar.wast");
let import_object = generate_libc_env();
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
let module = result_object.module;
let instance = result_object.instance;
let func_index = match module.info.exports.get("main") {
Some(&Export::Function(index)) => index,
_ => panic!("Function not found"),
};
let main: fn(&Instance) = get_instance_function!(instance, func_index);
main(&instance)
}
#[test]
fn test_print() {
let wasm_bytes = include_wast2wasm_bytes!("tests/printf.wast");
let import_object = generate_libc_env();
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
let module = result_object.module;
let instance = result_object.instance;
let func_index = match module.info.exports.get("main") {
Some(&Export::Function(index)) => index,
_ => panic!("Function not found"),
};
let main: fn(&Instance) = get_instance_function!(instance, func_index);
main(&instance);
}
}

View File

@ -0,0 +1,43 @@
use crate::webassembly::{ImportObject, ImportValue};
mod printf;
mod putchar;
pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
let mut import_object = ImportObject::new();
import_object.set("env", "printf", ImportValue::Func(printf::printf as *const u8));
import_object.set("env", "putchar", ImportValue::Func(putchar::putchar as *const u8));
import_object
}
#[cfg(test)]
mod tests {
use super::generate_emscripten_env;
use crate::webassembly::{instantiate, Export, Instance};
#[test]
fn test_putchar() {
let wasm_bytes = include_wast2wasm_bytes!("tests/putchar.wast");
let import_object = generate_emscripten_env();
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
let func_index = match result_object.module.info.exports.get("main") {
Some(&Export::Function(index)) => index,
_ => panic!("Function not found"),
};
let main: fn(&Instance) = get_instance_function!(result_object.instance, func_index);
main(&result_object.instance);
}
#[test]
fn test_print() {
let wasm_bytes = include_wast2wasm_bytes!("tests/printf.wast");
let import_object = generate_emscripten_env();
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
let func_index = match result_object.module.info.exports.get("main") {
Some(&Export::Function(index)) => index,
_ => panic!("Function not found"),
};
let main: fn(&Instance) = get_instance_function!(result_object.instance, func_index);
main(&result_object.instance);
}
}

View File

@ -0,0 +1,11 @@
use libc::printf as _printf;
use crate::webassembly::Instance;
pub extern "C" fn printf(memory_offset: i32, extra: i32, instance: &Instance) -> i32 {
let mem = &instance.memories[0];
return unsafe {
let base_memory_offset = mem.mmap.as_ptr().offset(memory_offset as isize) as *const i8;
_printf(base_memory_offset, extra)
};
}

View File

@ -0,0 +1 @@
pub use libc::putchar;

4
src/linkers/mod.rs Normal file
View File

@ -0,0 +1,4 @@
pub mod emscripten;
pub use self::emscripten::generate_emscripten_env;

View File

@ -26,7 +26,7 @@ use wabt::wat2wasm;
#[macro_use]
mod macros;
pub mod common;
pub mod integrations;
pub mod linkers;
pub mod sighandler;
#[cfg(test)]
mod spectests;
@ -66,7 +66,7 @@ fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
wasm_binary = wat2wasm(wasm_binary).map_err(|err| String::from(err.description()))?;
}
let import_object = integrations::generate_libc_env();
let import_object = linkers::generate_emscripten_env();
let webassembly::ResultObject { module, instance } =
webassembly::instantiate(wasm_binary, import_object)
.map_err(|err| format!("{}", err))?;