wasm: use WASI ABI for exit function

This improves compatibility between the regular browser target
(-target=wasm) and the WASI target (-target=wasi). Specifically, it
allows running WASI tests like this:

    tinygo test -target=wasi encoding/base32
Этот коммит содержится в:
Ayke van Laethem 2021-04-14 23:10:42 +02:00 коммит произвёл Ron Evans
родитель f145663464
коммит 6dd5666ed1
3 изменённых файлов: 23 добавлений и 11 удалений

Просмотреть файл

@ -16,6 +16,12 @@ type __wasi_iovec_t struct {
//export fd_write //export fd_write
func fd_write(id uint32, iovs *__wasi_iovec_t, iovs_len uint, nwritten *uint) (errno uint) func fd_write(id uint32, iovs *__wasi_iovec_t, iovs_len uint, nwritten *uint) (errno uint)
// See:
// https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#-proc_exitrval-exitcode
//go:wasm-module wasi_snapshot_preview1
//export proc_exit
func proc_exit(exitcode uint32)
func postinit() {} func postinit() {}
const ( const (
@ -49,6 +55,11 @@ func abort() {
trap() trap()
} }
//go:linkname syscall_Exit syscall.Exit
func syscall_Exit(code int) {
proc_exit(uint32(code))
}
// TinyGo does not yet support any form of parallelism on WebAssembly, so these // TinyGo does not yet support any form of parallelism on WebAssembly, so these
// can be left empty. // can be left empty.

Просмотреть файл

@ -276,6 +276,15 @@
mem().setUint32(nwritten_ptr, nwritten, true); mem().setUint32(nwritten_ptr, nwritten, true);
return 0; return 0;
}, },
"proc_exit": (code) => {
if (global.process) {
// Node.js
process.exit(code);
} else {
// Can't exit in a browser.
throw 'trying to exit with code ' + code;
}
},
}, },
env: { env: {
// func ticks() float64 // func ticks() float64
@ -289,17 +298,6 @@
setTimeout(this._inst.exports.go_scheduler, timeout); setTimeout(this._inst.exports.go_scheduler, timeout);
}, },
// func Exit(code int)
"syscall.Exit": (code) => {
if (global.process) {
// Node.js
process.exit(code);
} else {
// Can't exit in a browser.
throw 'trying to exit with code ' + code;
}
},
// func finalizeRef(v ref) // func finalizeRef(v ref)
"syscall/js.finalizeRef": (sp) => { "syscall/js.finalizeRef": (sp) => {
// Note: TinyGo does not support finalizers so this should never be // Note: TinyGo does not support finalizers so this should never be

3
testdata/stdlib.go предоставленный
Просмотреть файл

@ -19,4 +19,7 @@ func main() {
// package strings // package strings
fmt.Println("strings.IndexByte:", strings.IndexByte("asdf", 'd')) fmt.Println("strings.IndexByte:", strings.IndexByte("asdf", 'd'))
fmt.Println("strings.Replace:", strings.Replace("An example string", " ", "-", -1)) fmt.Println("strings.Replace:", strings.Replace("An example string", " ", "-", -1))
// Exit the program normally.
os.Exit(0)
} }