mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-10 05:53:39 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -52,6 +52,3 @@ func sleepTicks(d timeUnit) {
|
||||
func ticks() timeUnit {
|
||||
return timeUnit(monotonicclock.Now())
|
||||
}
|
||||
|
||||
func beforeExit() {
|
||||
}
|
||||
|
||||
@@ -32,7 +32,3 @@ func sleepTicks(d timeUnit)
|
||||
|
||||
//go:wasmimport gojs runtime.ticks
|
||||
func ticks() timeUnit
|
||||
|
||||
func beforeExit() {
|
||||
__stdio_exit()
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user