Moved the process exit to after the WASI cleanup and added some flushing of stdout and stderr

This commit is contained in:
Johnathan Sharratt
2023-03-17 00:45:33 +11:00
parent 7afc8691e6
commit 461c208c15
2 changed files with 14 additions and 9 deletions

View File

@@ -193,7 +193,7 @@ impl RunWithPathBuf {
} }
} }
fn inner_module_run(&self, store: &mut Store, instance: Instance) -> Result<()> { fn inner_module_run(&self, store: &mut Store, instance: Instance) -> Result<i32> {
// If this module exports an _initialize function, run that first. // If this module exports an _initialize function, run that first.
if let Ok(initialize) = instance.exports.get_function("_initialize") { if let Ok(initialize) = instance.exports.get_function("_initialize") {
initialize initialize
@@ -216,12 +216,12 @@ impl RunWithPathBuf {
let start: Function = self.try_find_function(&instance, "_start", &[])?; let start: Function = self.try_find_function(&instance, "_start", &[])?;
let result = start.call(store, &[]); let result = start.call(store, &[]);
#[cfg(feature = "wasi")] #[cfg(feature = "wasi")]
self.wasi.handle_result(result)?; return self.wasi.handle_result(result);
#[cfg(not(feature = "wasi"))] #[cfg(not(feature = "wasi"))]
result?; return Ok(result?);
} }
Ok(()) Ok(0)
} }
fn inner_execute(&self) -> Result<()> { fn inner_execute(&self) -> Result<()> {
@@ -326,6 +326,7 @@ impl RunWithPathBuf {
let res = self.inner_module_run(&mut store, instance); let res = self.inner_module_run(&mut store, instance);
ctx.cleanup(&mut store, None); ctx.cleanup(&mut store, None);
res res
} }
// not WASI // not WASI
@@ -334,7 +335,12 @@ impl RunWithPathBuf {
self.inner_module_run(&mut store, instance) self.inner_module_run(&mut store, instance)
} }
} }
}; }.map(|exit_code| {
std::io::stdout().flush().ok();
std::io::stderr().flush().ok();
std::process::exit(exit_code);
});
#[cfg(not(feature = "wasi"))] #[cfg(not(feature = "wasi"))]
let ret = { let ret = {
let instance = Instance::new(&module, &imports! {})?; let instance = Instance::new(&module, &imports! {})?;

View File

@@ -198,14 +198,13 @@ impl Wasi {
} }
/// Helper function for handling the result of a Wasi _start function. /// Helper function for handling the result of a Wasi _start function.
pub fn handle_result(&self, result: Result<Box<[Value]>, RuntimeError>) -> Result<()> { pub fn handle_result(&self, result: Result<Box<[Value]>, RuntimeError>) -> Result<i32> {
match result { match result {
Ok(_) => Ok(()), Ok(_) => Ok(0),
Err(err) => { Err(err) => {
let err: anyhow::Error = match err.downcast::<WasiError>() { let err: anyhow::Error = match err.downcast::<WasiError>() {
Ok(WasiError::Exit(exit_code)) => { Ok(WasiError::Exit(exit_code)) => {
// We should exit with the provided exit code return Ok(exit_code.raw());
std::process::exit(exit_code.into());
} }
Ok(err) => err.into(), Ok(err) => err.into(),
Err(err) => err.into(), Err(err) => err.into(),