-
Notifications
You must be signed in to change notification settings - Fork 946
/
Copy pathruntime_tinygowasm.go
147 lines (120 loc) · 3.33 KB
/
runtime_tinygowasm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//go:build tinygo.wasm && !wasm_unknown && !wasip2
// This file is for wasm/wasip1 and for wasm/js, which both use much of the
// WASIp1 API.
package runtime
import (
"unsafe"
)
// Implements __wasi_iovec_t.
type __wasi_iovec_t struct {
buf unsafe.Pointer
bufLen uint
}
//go:wasmimport wasi_snapshot_preview1 fd_write
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:wasmimport wasi_snapshot_preview1 proc_exit
func proc_exit(exitcode uint32)
// Flush stdio on exit.
//
//export __stdio_exit
func __stdio_exit()
var args []string
//go:linkname os_runtime_args os.runtime_args
func os_runtime_args() []string {
if args == nil {
// Read the number of args (argc) and the buffer size required to store
// all these args (argv).
var argc, argv_buf_size uint32
args_sizes_get(&argc, &argv_buf_size)
if argc == 0 {
return nil
}
// Obtain the command line arguments
argsSlice := make([]unsafe.Pointer, argc)
buf := make([]byte, argv_buf_size)
args_get(&argsSlice[0], unsafe.Pointer(&buf[0]))
// Convert the array of C strings to an array of Go strings.
args = make([]string, argc)
for i, cstr := range argsSlice {
length := strlen(cstr)
argString := _string{
length: length,
ptr: (*byte)(cstr),
}
args[i] = *(*string)(unsafe.Pointer(&argString))
}
}
return args
}
//go:wasmimport wasi_snapshot_preview1 args_get
func args_get(argv *unsafe.Pointer, argv_buf unsafe.Pointer) (errno uint16)
//go:wasmimport wasi_snapshot_preview1 args_sizes_get
func args_sizes_get(argc *uint32, argv_buf_size *uint32) (errno uint16)
const (
putcharBufferSize = 120
stdout = 1
)
// Using global variables to avoid heap allocation.
var (
putcharBuffer = [putcharBufferSize]byte{}
putcharPosition uint = 0
putcharIOVec = __wasi_iovec_t{
buf: unsafe.Pointer(&putcharBuffer[0]),
}
putcharNWritten uint
)
func putchar(c byte) {
putcharBuffer[putcharPosition] = c
putcharPosition++
if c == '\n' || putcharPosition >= putcharBufferSize {
putcharIOVec.bufLen = putcharPosition
fd_write(stdout, &putcharIOVec, 1, &putcharNWritten)
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) {
mono = nanotime()
sec = mono / (1000 * 1000 * 1000)
nsec = int32(mono - sec*(1000*1000*1000))
return
}
// Abort executes the wasm 'unreachable' instruction.
func abort() {
trap()
}
//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?
proc_exit(uint32(code))
}
// 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) {
n |= uint64(libc_arc4random())
n |= uint64(libc_arc4random()) << 32
return n, true
}
// uint32_t arc4random(void);
//
//export arc4random
func libc_arc4random() uint32