wasm: use wasi ABI for basic startup/stdout

This allows TinyGo-built binaries to run under wasmtime, for example:

    tinygo build -o test.wasm -no-debug -target=wasm examples/test
    wasmtime run test.wasm 0
This commit is contained in:
Ayke van Laethem
2020-01-25 23:22:27 +01:00
committed by Ron Evans
parent eb9c2c276e
commit 24a0f237d8
2 changed files with 50 additions and 36 deletions
+25 -17
View File
@@ -2,35 +2,43 @@
package runtime
import "unsafe"
type timeUnit float64 // time in milliseconds, just like Date.now() in JavaScript
const tickMicros = 1000000
//go:export io_get_stdout
func io_get_stdout() int32
//go:export resource_write
func resource_write(id int32, ptr *uint8, len int32) int32
var stdout int32
func init() {
stdout = io_get_stdout()
// Implements __wasi_ciovec_t and __wasi_iovec_t.
type wasiIOVec struct {
buf unsafe.Pointer
bufLen uint
}
//go:export _start
//go:wasm-module wasi_unstable
//export fd_write
func fd_write(id uint32, iovs *wasiIOVec, iovs_len uint, nwritten *uint) (errno uint)
//export _start
func _start() {
initAll()
}
//go:export cwa_main
func cwa_main() {
initAll() // _start is not called by olin/cwa so has to be called here
callMain()
}
// Using global variables to avoid heap allocation.
var (
putcharBuf = byte(0)
putcharIOVec = wasiIOVec{
buf: unsafe.Pointer(&putcharBuf),
bufLen: 1,
}
)
func putchar(c byte) {
resource_write(stdout, &c, 1)
// write to stdout
const stdout = 1
var nwritten uint
putcharBuf = c
fd_write(stdout, &putcharIOVec, 1, &nwritten)
}
var handleEvent func()