Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Implement command exit statuses. (#40)
Browse files Browse the repository at this point in the history
Add a `result` return type to `command` so that it can indicate success
or failure.

The idea here is that this isn't a full `i32` return value because the
meaning of return values isn't portable across platforms. Also, Typed Main
is a better long-term answer for users that want rich error return
values from commands.
  • Loading branch information
sunfishcode committed Dec 24, 2022
1 parent b66f92f commit 018ca43
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 16 deletions.
6 changes: 5 additions & 1 deletion host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ async fn main() -> Result<()> {

let (wasi, _instance) = Wasi::instantiate_async(&mut store, &component, &linker).await?;

wasi.command(&mut store, 0, 0, &[], &[], &[]).await?;
let result: Result<(), ()> = wasi.command(&mut store, 0, 0, &[], &[], &[]).await?;

if result.is_err() {
anyhow::bail!("command returned with failing exit status");
}

Ok(())
}
92 changes: 79 additions & 13 deletions host/tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ async fn run_hello_stdout(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
&[],
&[],
)
.await?;
Ok(())
.await?
.map_err(|()| anyhow::anyhow!("command returned with failing exit status"))
}

async fn run_panic(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
Expand Down Expand Up @@ -84,8 +84,8 @@ async fn run_args(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
&[],
&[],
)
.await?;
Ok(())
.await?
.map_err(|()| anyhow::anyhow!("command returned with failing exit status"))
}

async fn run_random(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
Expand Down Expand Up @@ -119,8 +119,8 @@ async fn run_random(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
&[],
&[],
)
.await?;
Ok(())
.await?
.map_err(|()| anyhow::anyhow!("command returned with failing exit status"))
}

async fn run_time(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
Expand Down Expand Up @@ -176,8 +176,8 @@ async fn run_time(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
&[],
&[],
)
.await?;
Ok(())
.await?
.map_err(|()| anyhow::anyhow!("command returned with failing exit status"))
}

async fn run_stdin(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
Expand All @@ -195,8 +195,8 @@ async fn run_stdin(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
&[],
&[],
)
.await?;
Ok(())
.await?
.map_err(|()| anyhow::anyhow!("command returned with failing exit status"))
}

async fn run_env(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
Expand All @@ -208,8 +208,8 @@ async fn run_env(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
&[("frabjous", "day"), ("callooh", "callay")],
&[],
)
.await?;
Ok(())
.await?
.map_err(|()| anyhow::anyhow!("command returned with failing exit status"))
}

async fn run_file_read(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
Expand All @@ -232,6 +232,72 @@ async fn run_file_read(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
&[],
&[(descriptor, "/")],
)
.await?;
.await?
.map_err(|()| anyhow::anyhow!("command returned with failing exit status"))
}

async fn run_exit_success(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
let r = wasi
.command(
&mut store,
0 as host::Descriptor,
1 as host::Descriptor,
&[],
&[("frabjous", "day"), ("callooh", "callay")],
&[],
)
.await;
let err = r.unwrap_err();
let status = err.downcast_ref::<wasi_common::I32Exit>().unwrap();
assert_eq!(status.0, 0);
Ok(())
}

async fn run_exit_default(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
let r = wasi
.command(
&mut store,
0 as host::Descriptor,
1 as host::Descriptor,
&[],
&[("frabjous", "day"), ("callooh", "callay")],
&[],
)
.await?;
assert!(r.is_ok());
Ok(())
}

async fn run_exit_failure(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
let r = wasi
.command(
&mut store,
0 as host::Descriptor,
1 as host::Descriptor,
&[],
&[("frabjous", "day"), ("callooh", "callay")],
&[],
)
.await;
let err = r.unwrap_err();
let status = err.downcast_ref::<wasi_common::I32Exit>().unwrap();
assert_eq!(status.0, 1);
Ok(())
}

async fn run_exit_panic(mut store: Store<WasiCtx>, wasi: Wasi) -> Result<()> {
let r = wasi
.command(
&mut store,
0 as host::Descriptor,
1 as host::Descriptor,
&[],
&[("frabjous", "day"), ("callooh", "callay")],
&[],
)
.await;
let err = r.unwrap_err();
// The panic should trap.
assert!(err.downcast_ref::<wasi_common::I32Exit>().is_none());
Ok(())
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub unsafe extern "C" fn command(
args_len: usize,
env_vars: StrTupleList,
preopens: PreopenList,
) {
) -> Result<(), ()> {
// TODO: ideally turning off `command` would remove this import and the
// `*.wit` metadata entirely but doing that ergonomically will likely
// require some form of `use` to avoid duplicating lots of `*.wit` bits.
Expand Down Expand Up @@ -86,6 +86,7 @@ pub unsafe extern "C" fn command(
fn _start();
}
_start();
Ok(())
}

// We're avoiding static initializers, so replace the standard assert macros
Expand Down
1 change: 1 addition & 0 deletions test-programs/src/bin/exit_default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
3 changes: 3 additions & 0 deletions test-programs/src/bin/exit_failure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
std::process::exit(1)
}
3 changes: 3 additions & 0 deletions test-programs/src/bin/exit_panic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
panic!("Curiouser and curiouser!")
}
3 changes: 3 additions & 0 deletions test-programs/src/bin/exit_success.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
std::process::exit(0)
}
2 changes: 1 addition & 1 deletion wit/wasi.wit
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface command {
args: list<string>,
env-vars: list<tuple<string, string>>,
preopens: list<tuple<descriptor, string>>
)
) -> result
}

interface wasi-exit {
Expand Down

0 comments on commit 018ca43

Please sign in to comment.