wasm: correctly return from run() in wasm_exec.js

Instead of hanging forever, it should return the exit code from os.Exit.
This commit is contained in:
Ayke van Laethem
2024-10-30 12:24:21 +01:00
committed by Ayke
parent 04a7baec3e
commit ceb7891986
12 changed files with 173 additions and 30 deletions
+8 -3
View File
@@ -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.
@@ -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
+7
View File
@@ -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.
-4
View File
@@ -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
-3
View File
@@ -52,6 +52,3 @@ func sleepTicks(d timeUnit) {
func ticks() timeUnit {
return timeUnit(monotonicclock.Now())
}
func beforeExit() {
}
-4
View File
@@ -32,7 +32,3 @@ func sleepTicks(d timeUnit)
//go:wasmimport gojs runtime.ticks
func ticks() timeUnit
func beforeExit() {
__stdio_exit()
}
+4 -1
View File
@@ -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.
}
+2 -1
View File
@@ -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()
}
}