mirror of
https://github.com/fluencelabs/wasmer
synced 2025-04-25 18:32:15 +00:00
44 lines
1.7 KiB
Rust
44 lines
1.7 KiB
Rust
|
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);
|
||
|
}
|
||
|
}
|