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 // Then, we get the import object related to our WASI
// and attach it to the Wasm instance. // and attach it to the Wasm instance.
let import_object = wasi_env let import_object = wasi_env.import_object(&module).unwrap();
.import_object(&module)
.unwrap_or_else(|_| wasmer::imports!());
let instance = Instance::new(&module, &import_object).unwrap(); let instance = Instance::new(&module, &import_object).unwrap();
let start = instance.exports.get_function("_start").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 { 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 { fn run_with_wasmer(wasm_path: &std::path::Path, stdin: &[&str]) -> String {
use std::io::Write; use std::io::Write;
use wasmer::{Instance, Module, Store}; 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(); let start = instance.exports.get_function("_start").unwrap();
match start.call(&[]) { 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(); let mut state = wasi_env.state.lock().unwrap();
match state.fs.stdout_mut() { match state.fs.stdout_mut() {
@ -659,9 +674,4 @@ fn run_with_wasmer(wasm_path: &std::path::Path, stdin: &[&str]) -> String {
} }
_ => todo!(), _ => todo!(),
} }
}
Err(e) => {
panic!("Something went wrong running a wasm test:\n{:?}", e);
}
}
} }