2018-11-13 19:19:23 -08:00
|
|
|
use crate::webassembly::{ImportObject, ImportValue};
|
|
|
|
|
2018-11-15 13:31:37 -08:00
|
|
|
mod abort;
|
2018-11-13 19:19:23 -08:00
|
|
|
mod printf;
|
|
|
|
mod putchar;
|
2018-11-19 01:04:08 +01:00
|
|
|
mod syscalls;
|
2018-11-13 19:19:23 -08:00
|
|
|
|
|
|
|
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));
|
2018-11-13 19:42:36 -08:00
|
|
|
import_object.set("env", "abort", ImportValue::Func(abort::abort as *const u8));
|
|
|
|
import_object.set("env", "_abort", ImportValue::Func(abort::abort as *const u8));
|
2018-11-19 01:04:08 +01:00
|
|
|
// SYSCALLS
|
|
|
|
import_object.set("env", "__syscall1", ImportValue::Func(syscalls::__syscall1 as *const u8));
|
|
|
|
import_object.set("env", "__syscall3", ImportValue::Func(syscalls::__syscall3 as *const u8));
|
|
|
|
import_object.set("env", "__syscall5", ImportValue::Func(syscalls::__syscall5 as *const u8));
|
2018-11-13 19:19:23 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|