Exit(0) is success when running wasm executables

This commit is contained in:
Folkert 2021-09-10 09:50:13 +02:00
parent 3ace5c7660
commit 1d1d71be1d
2 changed files with 36 additions and 19 deletions

View file

@ -371,15 +371,22 @@ fn run_with_wasmer(wasm_path: &std::path::Path, args: &[String]) {
// Then, we get the import object related to our WASI
// and attach it to the Wasm instance.
let import_object = wasi_env
.import_object(&module)
.unwrap_or_else(|_| wasmer::imports!());
let import_object = wasi_env.import_object(&module).unwrap();
let instance = Instance::new(&module, &import_object).unwrap();
let start = instance.exports.get_function("_start").unwrap();
start.call(&[]).unwrap();
use wasmer_wasi::WasiError;
match start.call(&[]) {
Ok(_) => {}
Err(e) => match e.downcast::<WasiError>() {
Ok(WasiError::Exit(0)) => {
// we run the `_start` function, so exit(0) is expected
}
other => panic!("Wasmer error: {:?}", other),
},
}
}
enum Backend {

View file

@ -603,7 +603,7 @@ mod cli_run {
}
}
#[cfg(feature = "wasm32-cli-run")]
#[allow(dead_code)]
fn run_with_wasmer(wasm_path: &std::path::Path, stdin: &[&str]) -> String {
use std::io::Write;
use wasmer::{Instance, Module, Store};
@ -647,7 +647,22 @@ fn run_with_wasmer(wasm_path: &std::path::Path, stdin: &[&str]) -> String {
let start = instance.exports.get_function("_start").unwrap();
match start.call(&[]) {
Ok(_) => {
Ok(_) => read_wasi_stdout(wasi_env),
Err(e) => {
use wasmer_wasi::WasiError;
match e.downcast::<WasiError>() {
Ok(WasiError::Exit(0)) => {
// we run the `_start` function, so exit(0) is expected
read_wasi_stdout(wasi_env)
}
other => format!("Something went wrong running a wasm test: {:?}", other),
}
}
}
}
#[allow(dead_code)]
fn read_wasi_stdout(wasi_env: wasmer_wasi::WasiEnv) -> String {
let mut state = wasi_env.state.lock().unwrap();
match state.fs.stdout_mut() {
@ -659,9 +674,4 @@ fn run_with_wasmer(wasm_path: &std::path::Path, stdin: &[&str]) -> String {
}
_ => todo!(),
}
}
Err(e) => {
panic!("Something went wrong running a wasm test:\n{:?}", e);
}
}
}