Source file
src/runtime/os_wasip1.go
1
2
3
4
5
6
7 package runtime
8
9 import (
10 "structs"
11 "unsafe"
12 )
13
14
15
16
17
18
19
20
21 type uintptr32 = uint32
22
23
24 type size = uint32
25
26
27 type errno = uint32
28
29
30 type filesize = uint64
31
32
33 type timestamp = uint64
34
35
36 type clockid = uint32
37
38 const (
39 clockRealtime clockid = 0
40 clockMonotonic clockid = 1
41 )
42
43
44 type iovec struct {
45 buf uintptr32
46 bufLen size
47 }
48
49
50 func exit(code int32)
51
52
53
54 func args_get(argv *uintptr32, argvBuf *byte) errno
55
56
57
58 func args_sizes_get(argc, argvBufLen *size) errno
59
60
61
62 func clock_time_get(clock_id clockid, precision timestamp, time *timestamp) errno
63
64
65
66 func environ_get(environ *uintptr32, environBuf *byte) errno
67
68
69
70 func environ_sizes_get(environCount, environBufLen *size) errno
71
72
73
74 func fd_write(fd int32, iovs unsafe.Pointer, iovsLen size, nwritten *size) errno
75
76
77
78 func random_get(buf *byte, bufLen size) errno
79
80 type eventtype = uint8
81
82 const (
83 eventtypeClock eventtype = iota
84 eventtypeFdRead
85 eventtypeFdWrite
86 )
87
88 type eventrwflags = uint16
89
90 const (
91 fdReadwriteHangup eventrwflags = 1 << iota
92 )
93
94 type userdata = uint64
95
96
97
98
99
100
101
102
103
104 type event struct {
105 _ structs.HostLayout
106 userdata userdata
107 error uint16
108 typ eventtype
109 fdReadwrite eventFdReadwrite
110 }
111
112 type eventFdReadwrite struct {
113 _ structs.HostLayout
114 nbytes filesize
115 flags eventrwflags
116 }
117
118 type subclockflags = uint16
119
120 const (
121 subscriptionClockAbstime subclockflags = 1 << iota
122 )
123
124 type subscriptionClock struct {
125 _ structs.HostLayout
126 id clockid
127 timeout timestamp
128 precision timestamp
129 flags subclockflags
130 }
131
132 type subscriptionFdReadwrite struct {
133 _ structs.HostLayout
134 fd int32
135 }
136
137 type subscription struct {
138 _ structs.HostLayout
139 userdata userdata
140 u subscriptionUnion
141 }
142
143 type subscriptionUnion [5]uint64
144
145 func (u *subscriptionUnion) eventtype() *eventtype {
146 return (*eventtype)(unsafe.Pointer(&u[0]))
147 }
148
149 func (u *subscriptionUnion) subscriptionClock() *subscriptionClock {
150 return (*subscriptionClock)(unsafe.Pointer(&u[1]))
151 }
152
153 func (u *subscriptionUnion) subscriptionFdReadwrite() *subscriptionFdReadwrite {
154 return (*subscriptionFdReadwrite)(unsafe.Pointer(&u[1]))
155 }
156
157
158
159 func poll_oneoff(in *subscription, out *event, nsubscriptions size, nevents *size) errno
160
161 func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
162 iov := iovec{
163 buf: uintptr32(uintptr(p)),
164 bufLen: size(n),
165 }
166 var nwritten size
167 if fd_write(int32(fd), unsafe.Pointer(&iov), 1, &nwritten) != 0 {
168 throw("fd_write failed")
169 }
170 return int32(nwritten)
171 }
172
173 func usleep(usec uint32) {
174 var in subscription
175 var out event
176 var nevents size
177
178 eventtype := in.u.eventtype()
179 *eventtype = eventtypeClock
180
181 subscription := in.u.subscriptionClock()
182 subscription.id = clockMonotonic
183 subscription.timeout = timestamp(usec) * 1e3
184 subscription.precision = 1e3
185
186 if poll_oneoff(&in, &out, 1, &nevents) != 0 {
187 throw("wasi_snapshot_preview1.poll_oneoff")
188 }
189 }
190
191 func readRandom(r []byte) int {
192 if random_get(&r[0], size(len(r))) != 0 {
193 return 0
194 }
195 return len(r)
196 }
197
198 func goenvs() {
199
200 var argc size
201 var argvBufLen size
202 if args_sizes_get(&argc, &argvBufLen) != 0 {
203 throw("args_sizes_get failed")
204 }
205
206 argslice = make([]string, argc)
207 if argc > 0 {
208 argv := make([]uintptr32, argc)
209 argvBuf := make([]byte, argvBufLen)
210 if args_get(&argv[0], &argvBuf[0]) != 0 {
211 throw("args_get failed")
212 }
213
214 for i := range argslice {
215 start := argv[i] - uintptr32(uintptr(unsafe.Pointer(&argvBuf[0])))
216 end := start
217 for argvBuf[end] != 0 {
218 end++
219 }
220 argslice[i] = string(argvBuf[start:end])
221 }
222 }
223
224
225 var environCount size
226 var environBufLen size
227 if environ_sizes_get(&environCount, &environBufLen) != 0 {
228 throw("environ_sizes_get failed")
229 }
230
231 envs = make([]string, environCount)
232 if environCount > 0 {
233 environ := make([]uintptr32, environCount)
234 environBuf := make([]byte, environBufLen)
235 if environ_get(&environ[0], &environBuf[0]) != 0 {
236 throw("environ_get failed")
237 }
238
239 for i := range envs {
240 start := environ[i] - uintptr32(uintptr(unsafe.Pointer(&environBuf[0])))
241 end := start
242 for environBuf[end] != 0 {
243 end++
244 }
245 envs[i] = string(environBuf[start:end])
246 }
247 }
248 }
249
250 func walltime() (sec int64, nsec int32) {
251 return walltime1()
252 }
253
254 func walltime1() (sec int64, nsec int32) {
255 var time timestamp
256 if clock_time_get(clockRealtime, 0, &time) != 0 {
257 throw("clock_time_get failed")
258 }
259 return int64(time / 1000000000), int32(time % 1000000000)
260 }
261
262 func nanotime1() int64 {
263 var time timestamp
264 if clock_time_get(clockMonotonic, 0, &time) != 0 {
265 throw("clock_time_get failed")
266 }
267 return int64(time)
268 }
269
View as plain text