wasi preview 2 support (#4027)

* all: wasip2 support

Co-authored-by: Randy Reddig <randy.reddig@fastly.com>
This commit is contained in:
Damian Gryski
2024-07-02 07:02:03 -07:00
committed by GitHub
parent f18c6e342f
commit 9cb263479c
125 changed files with 7035 additions and 154 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
//go:build linux && !baremetal && !nintendoswitch && !wasip1 && !wasm_unknown
//go:build linux && !baremetal && !nintendoswitch && !wasip1 && !wasm_unknown && !wasip2
package runtime
+1 -1
View File
@@ -1,4 +1,4 @@
//go:build linux && (baremetal || nintendoswitch || wasi || wasm_unknown)
//go:build linux && (baremetal || nintendoswitch || wasm_unknown)
// Other systems that aren't operating systems supported by the Go toolchain
// need to pretend to be an existing operating system. Linux seems like a good
+5
View File
@@ -0,0 +1,5 @@
//go:build wasip2
package runtime
const GOOS = "wasip2"
+1 -1
View File
@@ -1,4 +1,4 @@
//go:build tinygo.wasm && !wasm_unknown
//go:build tinygo.wasm && !wasm_unknown && !wasip2
package runtime
+76
View File
@@ -0,0 +1,76 @@
//go:build wasip2
package runtime
import (
exit "internal/wasi/cli/v0.2.0/exit"
stdout "internal/wasi/cli/v0.2.0/stdout"
monotonicclock "internal/wasi/clocks/v0.2.0/monotonic-clock"
wallclock "internal/wasi/clocks/v0.2.0/wall-clock"
random "internal/wasi/random/v0.2.0/random"
"github.com/ydnar/wasm-tools-go/cm"
)
const putcharBufferSize = 120
// Using global variables to avoid heap allocation.
var (
putcharStdout = stdout.GetStdout()
putcharBuffer = [putcharBufferSize]byte{}
putcharPosition uint = 0
)
func putchar(c byte) {
putcharBuffer[putcharPosition] = c
putcharPosition++
if c == '\n' || putcharPosition >= putcharBufferSize {
list := cm.NewList(&putcharBuffer[0], putcharPosition)
putcharStdout.BlockingWriteAndFlush(list) // error return ignored; can't do anything anyways
putcharPosition = 0
}
}
func getchar() byte {
// dummy, TODO
return 0
}
func buffered() int {
// dummy, TODO
return 0
}
//go:linkname now time.now
func now() (sec int64, nsec int32, mono int64) {
now := wallclock.Now()
sec = int64(now.Seconds)
nsec = int32(now.Nanoseconds)
mono = int64(monotonicclock.Now())
return
}
// Abort executes the wasm 'unreachable' instruction.
func abort() {
trap()
}
//go:linkname syscall_Exit syscall.Exit
func syscall_Exit(code int) {
exit.Exit(code != 0)
}
// TinyGo does not yet support any form of parallelism on WebAssembly, so these
// can be left empty.
//go:linkname procPin sync/atomic.runtime_procPin
func procPin() {
}
//go:linkname procUnpin sync/atomic.runtime_procUnpin
func procUnpin() {
}
func hardwareRand() (n uint64, ok bool) {
return random.GetRandomU64(), true
}
+1 -1
View File
@@ -1,4 +1,4 @@
//go:build (darwin || (linux && !baremetal && !wasip1 && !wasm_unknown)) && !nintendoswitch
//go:build (darwin || (linux && !baremetal && !wasip1 && !wasm_unknown && !wasip2)) && !nintendoswitch
package runtime
+1 -1
View File
@@ -1,4 +1,4 @@
//go:build wasm && !wasi && !scheduler.none && !wasip1 && !wasm_unknown
//go:build wasm && !wasi && !scheduler.none && !wasip1 && !wasip2 && !wasm_unknown
package runtime
+61
View File
@@ -0,0 +1,61 @@
//go:build wasip2
package runtime
import (
"unsafe"
"internal/wasi/cli/v0.2.0/environment"
monotonicclock "internal/wasi/clocks/v0.2.0/monotonic-clock"
)
type timeUnit int64
//export wasi:cli/run@0.2.0#run
func __wasi_cli_run_run() uint32 {
_start()
return 0
}
//export _start
func _start() {
// These need to be initialized early so that the heap can be initialized.
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize)
run()
}
func init() {
}
var args []string
//go:linkname os_runtime_args os.runtime_args
func os_runtime_args() []string {
if args == nil {
args = environment.GetArguments().Slice()
}
return args
}
//export cabi_realloc
func cabi_realloc(ptr, oldsize, align, newsize unsafe.Pointer) unsafe.Pointer {
return realloc(ptr, uintptr(newsize))
}
func ticksToNanoseconds(ticks timeUnit) int64 {
return int64(ticks)
}
func nanosecondsToTicks(ns int64) timeUnit {
return timeUnit(ns)
}
func sleepTicks(d timeUnit) {
p := monotonicclock.SubscribeDuration(monotonicclock.Duration(d))
p.Block()
}
func ticks() timeUnit {
return timeUnit(monotonicclock.Now())
}