2020-04-06 16:47:06 -07:00
use std ::env ;
use wasmer_runtime ::Backend ;
pub fn get_backend ( ) -> Option < Backend > {
#[ cfg(feature = " backend-cranelift " ) ]
{
2020-04-06 17:14:27 -07:00
if let Ok ( v ) = env ::var ( " WASMER_TEST_CRANELIFT " ) {
2020-04-06 16:47:06 -07:00
if v = = " 1 " {
return Some ( Backend ::Cranelift ) ;
}
}
}
#[ cfg(feature = " backend-llvm " ) ]
{
if let Ok ( v ) = env ::var ( " WASMER_TEST_LLVM " ) {
if v = = " 1 " {
return Some ( Backend ::LLVM ) ;
}
}
}
#[ cfg(feature = " backend-singlepass " ) ]
{
if let Ok ( v ) = env ::var ( " WASMER_TEST_SINGLEPASS " ) {
if v = = " 1 " {
return Some ( Backend ::Singlepass ) ;
}
}
}
None
}
2019-01-10 21:37:59 -08:00
macro_rules ! assert_emscripten_output {
( $file :expr , $name :expr , $args :expr , $expected :expr ) = > { {
2019-01-18 01:25:05 +01:00
use wasmer_emscripten ::{
EmscriptenGlobals ,
generate_emscripten_env ,
} ;
2019-05-17 15:48:30 -07:00
use wasmer_dev_utils ::stdio ::StdioCapturer ;
2019-03-06 21:36:46 -06:00
2019-01-10 21:37:59 -08:00
let wasm_bytes = include_bytes! ( $file ) ;
2020-04-06 17:14:27 -07:00
let backend = $crate ::emtests ::_common ::get_backend ( ) . expect ( " Please set one of `WASMER_TEST_CRANELIFT`, `WASMER_TEST_LLVM`, or `WASMER_TEST_SINGELPASS` to `1`. " ) ;
2020-04-06 16:47:06 -07:00
let compiler = wasmer_runtime ::compiler_for_backend ( backend ) . expect ( " The desired compiler was not found! " ) ;
2019-01-18 01:25:05 +01:00
2020-04-06 16:47:06 -07:00
let module = wasmer_runtime ::compile_with_config_with ( & wasm_bytes [ .. ] , Default ::default ( ) , & * compiler ) . expect ( " WASM can't be compiled " ) ;
2019-01-17 23:55:44 -06:00
2019-08-16 09:57:36 -06:00
let mut emscripten_globals = EmscriptenGlobals ::new ( & module ) . expect ( " globals are valid " ) ;
2019-01-23 12:25:56 -08:00
let import_object = generate_emscripten_env ( & mut emscripten_globals ) ;
2019-01-18 01:25:05 +01:00
2019-02-04 18:46:10 -06:00
let mut instance = module . instantiate ( & import_object )
2019-01-19 00:28:41 -06: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 01:25:05 +01:00
2019-01-24 13:04:12 -08:00
let capturer = StdioCapturer ::new ( ) ;
2019-01-17 23:55:44 -06:00
2019-01-23 18:47:34 -06:00
wasmer_emscripten ::run_emscripten_instance (
& module ,
& mut instance ,
2019-07-06 22:05:45 -07:00
& mut emscripten_globals ,
2019-01-23 18:47:34 -06:00
$name ,
$args ,
2019-05-05 11:21:28 -07:00
None ,
2019-05-29 11:41:29 -07:00
vec! [ ] ,
2019-01-24 00:05:07 -06:00
) . expect ( " run_emscripten_instance finishes " ) ;
2019-01-23 18:47:34 -06:00
2019-02-07 10:45:48 -08: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-10 21:37:59 -08:00
} } ;
}
2019-03-01 10:53:40 -08:00
2019-04-09 18:33:29 -07:00
// pub fn assert_emscripten_output(wasm_bytes: &[u8], raw_expected_str: &str) {
// use wasmer_clif_backend::CraneliftCompiler;
// use wasmer_emscripten::{generate_emscripten_env, stdio::StdioCapturer, EmscriptenGlobals};
2019-03-01 10:53:40 -08:00
2019-04-09 18:33:29 -07:00
// let module = wasmer_runtime_core::compile_with(&wasm_bytes[..], &CraneliftCompiler::new())
// .expect("WASM can't be compiled");
2019-03-01 10:53:40 -08:00
2019-04-09 18:33:29 -07:00
// 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();
2019-03-01 10:53:40 -08:00
2019-04-09 18:33:29 -07:00
// let capturer = StdioCapturer::new();
2019-03-01 10:53:40 -08:00
2019-04-09 18:33:29 -07:00
// wasmer_emscripten::run_emscripten_instance(&module, &mut instance, "test", vec![])
// .expect("run_emscripten_instance finishes");
2019-03-01 10:53:40 -08:00
2019-04-09 18:33:29 -07:00
// let raw_output_string = capturer.end().unwrap().0;
2019-03-01 10:53:40 -08:00
2019-04-09 18:33:29 -07:00
// // 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();
2019-03-01 10:53:40 -08:00
2019-04-09 18:33:29 -07:00
// let contains_output = output.contains(expected_output);
2019-03-01 10:53:40 -08:00
2019-04-09 18:33:29 -07:00
// assert!(
// contains_output,
// "Output: `{}` does not contain expected output: `{}`",
// output, expected_output
// );
// }