diff --git a/internal/app/apputil/wabt_wat2wasm.go b/internal/app/apputil/wabt_wat2wasm.go index 1306ec7c6844fb8c1dbc3f093a6b438b4a496205..ed59b59a4ebc33d88acc8aa70bc720e5f92d7ad4 100644 --- a/internal/app/apputil/wabt_wat2wasm.go +++ b/internal/app/apputil/wabt_wat2wasm.go @@ -15,6 +15,7 @@ import ( "sync" "github.com/tetratelabs/wazero" + "github.com/tetratelabs/wazero/sys" "wa-lang.org/wa/internal/app/waruntime" "wa-lang.org/wa/internal/config" @@ -78,6 +79,11 @@ func RunWasm(cfg *config.Config, filename string, wasmArgs ...string) (stdoutStd _, err = r.InstantiateModule(ctx, code, conf) if err != nil { + if exitErr, ok := err.(*sys.ExitError); ok { + if exitErr.ExitCode() == 0 { + return outputBuffer.Bytes(), nil + } + } return outputBuffer.Bytes(), err } diff --git a/internal/waroot/_waroot/src/os/os.wa b/internal/waroot/_waroot/src/os/os.wa index f188a27a54e494c847256dcb9cc2bf615942299e..2f0102685a7c7ebc5f98d305526fb7466eaffc2a 100644 --- a/internal/waroot/_waroot/src/os/os.wa +++ b/internal/waroot/_waroot/src/os/os.wa @@ -5,6 +5,9 @@ var Args []string #wa:linkname runtime.os_get_args func os_get_args() => []string +#wa:linkname $runtime.procExit +func Exit(code: i32) + func init { Args = os_get_args() } diff --git a/internal/waroot/_waroot/src/runtime/runtime_arduino.wa b/internal/waroot/_waroot/src/runtime/runtime_arduino.wa index 3a1ae8919396b2a8edcf80a2b32d78353a06619a..ab4bdf6712fc875f58a45852d041a179702565eb 100644 --- a/internal/waroot/_waroot/src/runtime/runtime_arduino.wa +++ b/internal/waroot/_waroot/src/runtime/runtime_arduino.wa @@ -19,6 +19,9 @@ func fdWrite(fd: i32, io: i32, iovs_len: i32, nwritten: i32) => (written: i32) { return } +#wa:linkname $runtime.procExit +func procExit(code: i32) {} + #wa:linkname $runtime.waPrintI32 func waPrintI32(i: i32) {} diff --git a/internal/waroot/_waroot/src/runtime/runtime_chrome.wa b/internal/waroot/_waroot/src/runtime/runtime_chrome.wa index 1ee4184c6ccb3f256365b39e421570fc39143c01..6e801f834c92f9f1f9a71b3143c6768a0630940c 100644 --- a/internal/waroot/_waroot/src/runtime/runtime_chrome.wa +++ b/internal/waroot/_waroot/src/runtime/runtime_chrome.wa @@ -17,6 +17,9 @@ func fdWrite(fd: i32, io: i32, iovs_len: i32, nwritten: i32) => (written: i32) { return } +#wa:linkname $runtime.procExit +func procExit(code: i32) {} + #wa:linkname $runtime.waPrintI32 func waPrintI32(i: i32) {} diff --git a/internal/waroot/_waroot/src/runtime/runtime_wasi.wa b/internal/waroot/_waroot/src/runtime/runtime_wasi.wa index c2c88007e94116fd3abb05f1233a9de95de92e48..39a085cee1d2da47528cf3b73867ab0a97c1278d 100644 --- a/internal/waroot/_waroot/src/runtime/runtime_wasi.wa +++ b/internal/waroot/_waroot/src/runtime/runtime_wasi.wa @@ -14,6 +14,10 @@ func argsGet(result_argv: i32, result_argv_buf: i32) => (errno: i32) #wa:linkname $runtime.fdWrite func fdWrite(fd: i32, io: i32, iovs_len: i32, nwritten: i32) => (errno: i32) +#wa:import wasi_snapshot_preview1 proc_exit +#wa:linkname $runtime.procExit +func procExit(code: i32) + #wa:linkname puts func puts(ptr: i32, len: i32) diff --git a/main.go b/main.go index 813dd0d960d3ffd7550d72bd190a137e3afab98f..25960cbf90ab6fabb9e317bff8f525d55a46cc99 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,8 @@ import ( "strings" "time" + "github.com/tetratelabs/wazero/sys" + "wa-lang.org/wa/api" "wa-lang.org/wa/internal/3rdparty/cli" "wa-lang.org/wa/internal/app" @@ -550,8 +552,10 @@ func cliRun(c *cli.Context) { if len(stdoutStderr) > 0 { fmt.Println(string(stdoutStderr)) } + if exitErr, ok := err.(*sys.ExitError); ok { + os.Exit(int(exitErr.ExitCode())) + } fmt.Println(err) - os.Exit(1) } if len(stdoutStderr) > 0 { fmt.Println(string(stdoutStderr))