mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-07 22:21:19 +00:00
create vfs module guarded by flag for emscripten vfs behavior cleanup warning fixes wip emscripten vfs wip metadata for virtual file make an fd an i32 fix the feature flag for llvm on emscripten more wip wip add logging and fix one logical error
118 lines
4.0 KiB
Rust
118 lines
4.0 KiB
Rust
macro_rules! assert_emscripten_output {
|
|
($file:expr, $name:expr, $args:expr, $expected:expr) => {{
|
|
|
|
use wasmer_emscripten::{
|
|
EmscriptenGlobals,
|
|
generate_emscripten_env,
|
|
stdio::StdioCapturer
|
|
};
|
|
use wasmer_runtime_core::backend::Compiler;
|
|
|
|
#[cfg(feature = "clif")]
|
|
fn get_compiler() -> impl Compiler {
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
|
CraneliftCompiler::new()
|
|
}
|
|
|
|
#[cfg(feature = "llvm")]
|
|
fn get_compiler() -> impl Compiler {
|
|
use wasmer_llvm_backend::LLVMCompiler;
|
|
LLVMCompiler::new()
|
|
}
|
|
|
|
#[cfg(not(any(feature = "llvm", feature = "clif")))]
|
|
fn get_compiler() -> impl Compiler {
|
|
panic!("compiler not specified, activate a compiler via features");
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
|
CraneliftCompiler::new()
|
|
}
|
|
|
|
let wasm_bytes = include_bytes!($file);
|
|
|
|
let module = wasmer_runtime_core::compile_with(&wasm_bytes[..], &get_compiler())
|
|
.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 ??
|
|
let mut emscripten_globals = EmscriptenGlobals::new(&module);
|
|
let import_object = generate_emscripten_env(&mut emscripten_globals);
|
|
|
|
let mut instance = module.instantiate(&import_object)
|
|
.map_err(|err| format!("Can't instantiate the WebAssembly module: {:?}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ??
|
|
|
|
let capturer = StdioCapturer::new();
|
|
|
|
wasmer_emscripten::run_emscripten_instance(
|
|
&module,
|
|
&mut instance,
|
|
$name,
|
|
$args,
|
|
).expect("run_emscripten_instance finishes");
|
|
|
|
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
|
|
);
|
|
}};
|
|
}
|
|
|
|
pub fn assert_emscripten_output(wasm_bytes: &[u8], raw_expected_str: &str) {
|
|
use wasmer_emscripten::{generate_emscripten_env, stdio::StdioCapturer, EmscriptenGlobals};
|
|
use wasmer_runtime_core::backend::Compiler;
|
|
|
|
#[cfg(feature = "clif")]
|
|
fn get_compiler() -> impl Compiler {
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
|
CraneliftCompiler::new()
|
|
}
|
|
|
|
#[cfg(feature = "llvm")]
|
|
fn get_compiler() -> impl Compiler {
|
|
use wasmer_llvm_backend::LLVMCompiler;
|
|
LLVMCompiler::new()
|
|
}
|
|
|
|
#[cfg(not(any(feature = "llvm", feature = "clif")))]
|
|
fn get_compiler() -> impl Compiler {
|
|
panic!("compiler not specified, activate a compiler via features");
|
|
use wasmer_clif_backend::CraneliftCompiler;
|
|
CraneliftCompiler::new()
|
|
}
|
|
|
|
let compiler = get_compiler();
|
|
|
|
let module = wasmer_runtime_core::compile_with(&wasm_bytes[..], &compiler)
|
|
.expect("WASM can't be compiled");
|
|
|
|
let mut emscripten_globals = EmscriptenGlobals::new(&module);
|
|
let import_object = generate_emscripten_env(&mut emscripten_globals);
|
|
let mut instance = module
|
|
.instantiate(&import_object)
|
|
.map_err(|err| format!("Can't instantiate the WebAssembly module: {:?}", err))
|
|
.unwrap();
|
|
|
|
let capturer = StdioCapturer::new();
|
|
|
|
wasmer_emscripten::run_emscripten_instance(&module, &mut instance, "test", vec![])
|
|
.expect("run_emscripten_instance finishes");
|
|
|
|
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);
|
|
|
|
assert!(
|
|
contains_output,
|
|
"Output: `{}` does not contain expected output: `{}`",
|
|
output, expected_output
|
|
);
|
|
}
|