增加 os.Exit 函数

This commit is contained in:
chai2010 2023-03-12 23:11:32 +08:00
parent e916cb0a13
commit 7f2f0983df
6 changed files with 24 additions and 1 deletions

View File

@ -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
}

View File

@ -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()
}

View File

@ -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) {}

View File

@ -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) {}

View File

@ -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)

View File

@ -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))