2019-01-11 05:37:59 +00:00
|
|
|
macro_rules! assert_emscripten_output {
|
|
|
|
($file:expr, $name:expr, $args:expr, $expected:expr) => {{
|
2019-01-18 00:25:05 +00:00
|
|
|
|
2019-01-11 05:37:59 +00:00
|
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
2019-01-18 00:25:05 +00:00
|
|
|
use wasmer_emscripten::{
|
|
|
|
EmscriptenGlobals,
|
|
|
|
generate_emscripten_env,
|
2019-01-18 05:55:44 +00:00
|
|
|
stdio::StdioCapturer
|
2019-01-18 00:25:05 +00:00
|
|
|
};
|
2019-01-11 05:37:59 +00:00
|
|
|
|
|
|
|
let wasm_bytes = include_bytes!($file);
|
2019-01-18 00:25:05 +00:00
|
|
|
|
2019-01-23 07:27:13 +00:00
|
|
|
let module = wasmer_runtime_core::compile_with(&wasm_bytes[..], &CraneliftCompiler::new())
|
2019-01-18 05:55:44 +00:00
|
|
|
.expect("WASM can't be compiled");
|
|
|
|
|
|
|
|
// let module = compile(&wasm_bytes[..])
|
|
|
|
// .map_err(|err| format!("Can't create the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ??
|
2019-01-28 22:31:03 +00:00
|
|
|
let mut emscripten_globals = EmscriptenGlobals::new(&module);
|
2019-01-23 20:25:56 +00:00
|
|
|
let import_object = generate_emscripten_env(&mut emscripten_globals);
|
2019-01-18 00:25:05 +00:00
|
|
|
|
2019-02-05 00:46:10 +00:00
|
|
|
let mut instance = module.instantiate(&import_object)
|
2019-01-19 06:28:41 +00:00
|
|
|
.map_err(|err| format!("Can't instantiate the WebAssembly module: {:?}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ??
|
2019-01-18 00:25:05 +00:00
|
|
|
|
2019-01-24 21:04:12 +00:00
|
|
|
let capturer = StdioCapturer::new();
|
2019-01-18 05:55:44 +00:00
|
|
|
|
2019-01-24 00:47:34 +00:00
|
|
|
wasmer_emscripten::run_emscripten_instance(
|
|
|
|
&module,
|
|
|
|
&mut instance,
|
|
|
|
$name,
|
|
|
|
$args,
|
2019-01-24 06:05:07 +00:00
|
|
|
).expect("run_emscripten_instance finishes");
|
2019-01-24 00:47:34 +00:00
|
|
|
|
2019-02-07 18:45:48 +00:00
|
|
|
let output = capturer.end().unwrap().0;
|
|
|
|
let expected_output = include_str!($expected);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
output.contains(expected_output),
|
|
|
|
"Output: `{}` does not contain expected output: `{}`",
|
|
|
|
output,
|
|
|
|
expected_output
|
|
|
|
);
|
2019-01-11 05:37:59 +00:00
|
|
|
}};
|
|
|
|
}
|
2019-03-01 18:53:40 +00:00
|
|
|
|
|
|
|
pub fn assert_emscripten_output(wasm_bytes: &[u8], raw_expected_str: &str) {
|
|
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
2019-03-04 16:11:36 +00:00
|
|
|
use wasmer_emscripten::{generate_emscripten_env, stdio::StdioCapturer, EmscriptenGlobals};
|
2019-03-01 18:53:40 +00:00
|
|
|
|
|
|
|
let module = wasmer_runtime_core::compile_with(&wasm_bytes[..], &CraneliftCompiler::new())
|
|
|
|
.expect("WASM can't be compiled");
|
|
|
|
|
|
|
|
let mut emscripten_globals = EmscriptenGlobals::new(&module);
|
|
|
|
let import_object = generate_emscripten_env(&mut emscripten_globals);
|
2019-03-04 16:11:36 +00:00
|
|
|
let mut instance = module
|
|
|
|
.instantiate(&import_object)
|
|
|
|
.map_err(|err| format!("Can't instantiate the WebAssembly module: {:?}", err))
|
|
|
|
.unwrap();
|
2019-03-01 18:53:40 +00:00
|
|
|
|
|
|
|
let capturer = StdioCapturer::new();
|
|
|
|
|
2019-03-04 16:11:36 +00:00
|
|
|
wasmer_emscripten::run_emscripten_instance(&module, &mut instance, "test", vec![])
|
|
|
|
.expect("run_emscripten_instance finishes");
|
2019-03-01 18:53:40 +00:00
|
|
|
|
|
|
|
let raw_output_string = capturer.end().unwrap().0;
|
|
|
|
|
|
|
|
// trim the strings to avoid cross-platform line ending and white space issues
|
|
|
|
let output = raw_output_string.trim();
|
|
|
|
let expected_output = raw_expected_str.trim();
|
|
|
|
|
|
|
|
let contains_output = output.contains(expected_output);
|
|
|
|
|
2019-03-04 16:11:36 +00:00
|
|
|
assert!(
|
|
|
|
contains_output,
|
|
|
|
"Output: `{}` does not contain expected output: `{}`",
|
|
|
|
output, expected_output
|
|
|
|
);
|
2019-03-01 18:53:40 +00:00
|
|
|
}
|