diff options
author | Ayke van Laethem <[email protected]> | 2024-10-30 12:24:21 +0100 |
---|---|---|
committer | Ayke <[email protected]> | 2024-11-08 11:55:38 +0100 |
commit | ceb789198643d372cceb162581f059940b601578 (patch) | |
tree | 145cce369fd81890a5f950b768d58e7dea25055c /src | |
parent | 04a7baec3ede3d91243cfc73916b0b237a93e3fe (diff) | |
download | tinygo-ceb789198643d372cceb162581f059940b601578.tar.gz tinygo-ceb789198643d372cceb162581f059940b601578.zip |
wasm: correctly return from run() in wasm_exec.js
Instead of hanging forever, it should return the exit code from os.Exit.
Diffstat (limited to 'src')
-rw-r--r-- | src/runtime/runtime_tinygowasm.go | 11 | ||||
-rw-r--r-- | src/runtime/runtime_tinygowasm_unknown.go | 4 | ||||
-rw-r--r-- | src/runtime/runtime_tinygowasmp2.go | 7 | ||||
-rw-r--r-- | src/runtime/runtime_wasip1.go | 4 | ||||
-rw-r--r-- | src/runtime/runtime_wasip2.go | 3 | ||||
-rw-r--r-- | src/runtime/runtime_wasm_js.go | 4 | ||||
-rw-r--r-- | src/runtime/runtime_wasm_unknown.go | 5 | ||||
-rw-r--r-- | src/runtime/runtime_wasmentry.go | 3 |
8 files changed, 25 insertions, 16 deletions
diff --git a/src/runtime/runtime_tinygowasm.go b/src/runtime/runtime_tinygowasm.go index f791ffacd..7bc65e9c4 100644 --- a/src/runtime/runtime_tinygowasm.go +++ b/src/runtime/runtime_tinygowasm.go @@ -80,12 +80,17 @@ func abort() { //go:linkname syscall_Exit syscall.Exit func syscall_Exit(code int) { - // TODO: should we call __stdio_exit here? - // It's a low-level exit (syscall.Exit) so doing any libc stuff seems - // unexpected, but then where else should stdio buffers be flushed? + // Flush stdio buffers. + __stdio_exit() + + // Exit the program. proc_exit(uint32(code)) } +func mainReturnExit() { + syscall_Exit(0) +} + // TinyGo does not yet support any form of parallelism on WebAssembly, so these // can be left empty. diff --git a/src/runtime/runtime_tinygowasm_unknown.go b/src/runtime/runtime_tinygowasm_unknown.go index 39caa245a..e426f36ff 100644 --- a/src/runtime/runtime_tinygowasm_unknown.go +++ b/src/runtime/runtime_tinygowasm_unknown.go @@ -31,6 +31,10 @@ func abort() { //go:linkname syscall_Exit syscall.Exit func syscall_Exit(code int) { + // Because this is the "unknown" target we can't call an exit function. + // But we also can't just return since the program will likely expect this + // function to never return. So we panic instead. + runtimePanic("unsupported: syscall.Exit") } // There is not yet any support for any form of parallelism on WebAssembly, so these diff --git a/src/runtime/runtime_tinygowasmp2.go b/src/runtime/runtime_tinygowasmp2.go index eb3c507fd..70b5a6d11 100644 --- a/src/runtime/runtime_tinygowasmp2.go +++ b/src/runtime/runtime_tinygowasmp2.go @@ -60,6 +60,13 @@ func syscall_Exit(code int) { exit.Exit(code != 0) } +func mainReturnExit() { + // WASIp2 does not use _start, instead it uses _initialize and a custom + // WASIp2-specific main function. So this should never be called in + // practice. + runtimePanic("unreachable: _start was called") +} + // TinyGo does not yet support any form of parallelism on WebAssembly, so these // can be left empty. diff --git a/src/runtime/runtime_wasip1.go b/src/runtime/runtime_wasip1.go index ad66b0d86..92adb9bef 100644 --- a/src/runtime/runtime_wasip1.go +++ b/src/runtime/runtime_wasip1.go @@ -91,10 +91,6 @@ func ticks() timeUnit { return timeUnit(nano) } -func beforeExit() { - __stdio_exit() -} - // Implementations of WASI APIs //go:wasmimport wasi_snapshot_preview1 args_get diff --git a/src/runtime/runtime_wasip2.go b/src/runtime/runtime_wasip2.go index ba8f52100..296f4a45b 100644 --- a/src/runtime/runtime_wasip2.go +++ b/src/runtime/runtime_wasip2.go @@ -52,6 +52,3 @@ func sleepTicks(d timeUnit) { func ticks() timeUnit { return timeUnit(monotonicclock.Now()) } - -func beforeExit() { -} diff --git a/src/runtime/runtime_wasm_js.go b/src/runtime/runtime_wasm_js.go index b49ffd15d..21a0bc105 100644 --- a/src/runtime/runtime_wasm_js.go +++ b/src/runtime/runtime_wasm_js.go @@ -32,7 +32,3 @@ func sleepTicks(d timeUnit) //go:wasmimport gojs runtime.ticks func ticks() timeUnit - -func beforeExit() { - __stdio_exit() -} diff --git a/src/runtime/runtime_wasm_unknown.go b/src/runtime/runtime_wasm_unknown.go index 846b95d2a..27e248579 100644 --- a/src/runtime/runtime_wasm_unknown.go +++ b/src/runtime/runtime_wasm_unknown.go @@ -34,5 +34,8 @@ func ticks() timeUnit { return timeUnit(0) } -func beforeExit() { +func mainReturnExit() { + // Don't exit explicitly here. We can't (there is no environment with an + // exit call) but also it's not needed. We can just let _start and main.main + // return to the caller. } diff --git a/src/runtime/runtime_wasmentry.go b/src/runtime/runtime_wasmentry.go index 756db5095..1d2cec6ca 100644 --- a/src/runtime/runtime_wasmentry.go +++ b/src/runtime/runtime_wasmentry.go @@ -19,7 +19,8 @@ func wasmEntryCommand() { heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize) run() if mainExited { - beforeExit() + // To make sure wasm_exec.js knows that we've exited, exit explicitly. + mainReturnExit() } } |