-
Notifications
You must be signed in to change notification settings - Fork 2
/
types_misc.go
404 lines (335 loc) · 9.06 KB
/
types_misc.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package wypes
import (
"context"
"encoding/binary"
"math"
"time"
)
// Bool wraps [bool].
type Bool bool
const BoolSize = 1
// Unwrap returns the wrapped value.
func (v Bool) Unwrap() bool {
return bool(v)
}
// ValueTypes implements [Value] interface.
func (Bool) ValueTypes() []ValueType {
return []ValueType{ValueTypeI32}
}
// Lift implements [Lift] interface.
func (Bool) Lift(s *Store) Bool {
return s.Stack.Pop() != 0
}
// Lower implements [Lower] interface.
func (v Bool) Lower(s *Store) {
res := 0
if v {
res = 1
}
s.Stack.Push(Raw(res))
}
// MemoryLift implements [MemoryLift] interface.
func (Bool) MemoryLift(s *Store, offset uint32) (Bool, uint32) {
raw, ok := s.Memory.Read(offset, BoolSize)
if !ok {
s.Error = ErrMemRead
return Bool(false), 0
}
return Bool(raw[0] > 0), BoolSize
}
// MemoryLower implements [MemoryLower] interface.
func (v Bool) MemoryLower(s *Store, offset uint32) (length uint32) {
res := byte(0)
if v {
res = 1
}
ok := s.Memory.Write(offset, []byte{res})
if !ok {
s.Error = ErrMemRead
return 0
}
return BoolSize
}
// Float32 wraps [float32].
type Float32 float32
const Float32Size = 4
// Unwrap returns the wrapped value.
func (v Float32) Unwrap() float32 {
return float32(v)
}
// ValueTypes implements [Value] interface.
func (Float32) ValueTypes() []ValueType {
return []ValueType{ValueTypeF32}
}
// Lift implements [Lift] interface.
func (Float32) Lift(s *Store) Float32 {
f := math.Float32frombits(uint32(s.Stack.Pop()))
return Float32(f)
}
// Lower implements [Lower] interface.
func (v Float32) Lower(s *Store) {
r := math.Float32bits(float32(v))
s.Stack.Push(Raw(r))
}
// MemoryLift implements [MemoryLift] interface.
func (Float32) MemoryLift(s *Store, offset uint32) (Float32, uint32) {
raw, ok := s.Memory.Read(offset, Float32Size)
if !ok {
s.Error = ErrMemRead
return Float32(0), 0
}
return Float32(math.Float32frombits(binary.LittleEndian.Uint32(raw))), Float32Size
}
// MemoryLower implements [MemoryLower] interface.
func (v Float32) MemoryLower(s *Store, offset uint32) (length uint32) {
data := make([]byte, Float32Size)
binary.LittleEndian.PutUint32(data, math.Float32bits(float32(v)))
ok := s.Memory.Write(offset, data)
if !ok {
s.Error = ErrMemRead
return 0
}
return Float32Size
}
// Float64 wraps [float64].
type Float64 float64
const Float64Size = 8
// Unwrap returns the wrapped value.
func (v Float64) Unwrap() float64 {
return float64(v)
}
// ValueTypes implements [Value] interface.
func (Float64) ValueTypes() []ValueType {
return []ValueType{ValueTypeF64}
}
// Lift implements [Lift] interface.
func (Float64) Lift(s *Store) Float64 {
f := math.Float64frombits(s.Stack.Pop())
return Float64(f)
}
// Lower implements [Lower] interface.
func (v Float64) Lower(s *Store) {
res := math.Float64bits(float64(v))
s.Stack.Push(Raw(res))
}
// MemoryLift implements [MemoryLift] interface.
func (Float64) MemoryLift(s *Store, offset uint32) (Float64, uint32) {
raw, ok := s.Memory.Read(offset, Float64Size)
if !ok {
s.Error = ErrMemRead
return Float64(0), 0
}
return Float64(math.Float32frombits(binary.LittleEndian.Uint32(raw))), Float64Size
}
// MemoryLower implements [MemoryLower] interface.
func (v Float64) MemoryLower(s *Store, offset uint32) (length uint32) {
data := make([]byte, Float64Size)
binary.LittleEndian.PutUint64(data, math.Float64bits(float64(v)))
ok := s.Memory.Write(offset, data)
if !ok {
s.Error = ErrMemRead
return 0
}
return Float64Size
}
// Complex64 wraps [complex64].
type Complex64 complex64
// Unwrap returns the wrapped value.
func (v Complex64) Unwrap() complex64 {
return complex64(v)
}
// ValueTypes implements [Value] interface.
func (Complex64) ValueTypes() []ValueType {
return []ValueType{ValueTypeF32, ValueTypeF32}
}
// Lift implements [Lift] interface.
func (Complex64) Lift(s *Store) Complex64 {
c := complex(
math.Float32frombits(uint32(s.Stack.Pop())),
math.Float32frombits(uint32(s.Stack.Pop())),
)
return Complex64(c)
}
// Lower implements [Lower] interface.
func (v Complex64) Lower(s *Store) {
vReal := math.Float32bits(real(v))
vImag := math.Float32bits(imag(v))
s.Stack.Push(Raw(vReal))
s.Stack.Push(Raw(vImag))
}
// Complex128 wraps [complex128].
type Complex128 complex128
// Unwrap returns the wrapped value.
func (v Complex128) Unwrap() complex128 {
return complex128(v)
}
// ValueTypes implements [Value] interface.
func (Complex128) ValueTypes() []ValueType {
return []ValueType{ValueTypeF64, ValueTypeF64}
}
// Lift implements [Lift] interface.
func (Complex128) Lift(s *Store) Complex128 {
c := complex(
math.Float64frombits(uint64(s.Stack.Pop())),
math.Float64frombits(uint64(s.Stack.Pop())),
)
return Complex128(c)
}
// Lower implements [Lower] interface.
func (v Complex128) Lower(s *Store) {
vReal := math.Float64bits(real(v))
vImag := math.Float64bits(imag(v))
s.Stack.Push(Raw(vReal))
s.Stack.Push(Raw(vImag))
}
// Duration wraps [time.Duration].
type Duration time.Duration
// Unwrap returns the wrapped value.
func (v Duration) Unwrap() time.Duration {
return time.Duration(v)
}
// ValueTypes implements [Value] interface.
func (Duration) ValueTypes() []ValueType {
return []ValueType{ValueTypeI64}
}
// Lift implements [Lift] interface.
func (Duration) Lift(s *Store) Duration {
return Duration(s.Stack.Pop())
}
// Lower implements [Lower] interface.
func (v Duration) Lower(s *Store) {
s.Stack.Push(Raw(v))
}
// Time wraps [time.Time].
type Time time.Time
// Unwrap returns the wrapped value.
func (v Time) Unwrap() time.Time {
return time.Time(v)
}
// ValueTypes implements [Value] interface.
func (Time) ValueTypes() []ValueType {
return []ValueType{ValueTypeI64}
}
// Lift implements [Lift] interface.
func (Time) Lift(s *Store) Time {
return Time(time.Unix(int64(s.Stack.Pop()), 0))
}
// Lower implements [Lower] interface.
func (v Time) Lower(s *Store) {
s.Stack.Push(Raw(time.Time(v).Unix()))
}
// Context wraps [context.Context].
type Context struct{ ctx context.Context }
// Unwrap returns the wrapped value.
func (v Context) Unwrap() context.Context {
return v.ctx
}
// ValueTypes implements [Value] interface.
func (Context) ValueTypes() []ValueType {
return []ValueType{}
}
// Lift implements [Lift] interface.
func (Context) Lift(s Store) Context {
return Context{ctx: s.Context}
}
// Void is a return type of a function that returns nothing.
type Void struct{}
// ValueTypes implements [Value] interface.
func (Void) ValueTypes() []ValueType {
return []ValueType{}
}
// Lower implements [Lower] interface.
func (Void) Lower(s *Store) {}
// Pair wraps two values of arbitrary types.
//
// You can combine multiple pairs to pass more than 2 values at once.
// All values are passed through the stack, not memory.
type Pair[L LiftLower[L], R LiftLower[R]] struct {
Left L
Right R
}
// ValueTypes implements [Value] interface.
func (v Pair[L, R]) ValueTypes() []ValueType {
types := make([]ValueType, 0, 2)
types = append(types, v.Left.ValueTypes()...)
types = append(types, v.Right.ValueTypes()...)
return types
}
// Lift implements [Lift] interface.
func (Pair[L, R]) Lift(s *Store) Pair[L, R] {
var left L
var right R
return Pair[L, R]{
Left: left.Lift(s),
Right: right.Lift(s),
}
}
// Lower implements [Lower] interface.
func (v Pair[L, R]) Lower(s *Store) {
v.Left.Lower(s)
v.Right.Lower(s)
}
// HostRef is a reference to a Go object stored on the host side in [Refs].
//
// References created this way are never collected by GC because there is no way
// to know if the wasm module still needs it. So it is important to explicitly clean
// references by calling [HostRef.Drop].
//
// A common usage pattern is to create a reference in one host-defined function,
// return it into the wasm module, and then clean it up in another host-defined function
// caled from wasm when the guest doesn't need the value anymore.
// In this scenario, the latter function accepts HostRef as an argument and calls its
// [HostRef.Drop] method. After that, the reference is removed from [Refs] in the [Store]
// and will be eventually collected by GC.
type HostRef[T any] struct {
Raw T
index uint32
refs Refs
}
// Unwrap returns the wrapped value.
func (v HostRef[T]) Unwrap() T {
return v.Raw
}
// Drop remove the reference from [Refs] in [Store].
//
// Can be called only on lifted references
// (passed as an argument into a host-defined function).
func (v HostRef[T]) Drop() {
if v.refs != nil {
v.refs.Drop(v.index)
}
}
// ValueTypes implements [Value] interface.
func (HostRef[T]) ValueTypes() []ValueType {
return []ValueType{ValueTypeI32}
}
// Lift implements [Lift] interface.
func (HostRef[T]) Lift(s *Store) HostRef[T] {
index := uint32(s.Stack.Pop())
var def T
raw, found := s.Refs.Get(index, def)
if !found {
s.Error = ErrRefNotFound
}
cast, ok := raw.(T)
if found && !ok {
s.Error = ErrRefCast
cast = def
}
return HostRef[T]{
Raw: cast,
index: index,
refs: s.Refs,
}
}
// Lower implements [Lower] interface.
func (v HostRef[T]) Lower(s *Store) {
var index uint32
if v.index == 0 {
index = s.Refs.Put(v.Raw)
} else {
index = v.index
s.Refs.Set(v.index, v.Raw)
}
s.Stack.Push(Raw(index))
}