forked from bytecodealliance/wasmtime-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
val.go
238 lines (213 loc) · 5.78 KB
/
val.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
package wasmtime
// #include <wasm.h>
// #include "shims.h"
import "C"
import (
"runtime"
"sync"
"unsafe"
)
var gExternrefLock sync.Mutex
var gExternrefMap = make(map[int]interface{})
var gExternrefSlab slab
// Val is a primitive numeric value.
// Moreover, in the definition of programs, immutable sequences of values occur to represent more complex data, such as text strings or other vectors.
type Val struct {
_raw *C.wasmtime_val_t
}
// ValI32 converts a go int32 to a i32 Val
func ValI32(val int32) Val {
ret := Val{_raw: &C.wasmtime_val_t{kind: C.WASMTIME_I32}}
C.go_wasmtime_val_i32_set(ret.ptr(), C.int32_t(val))
return ret
}
// ValI64 converts a go int64 to a i64 Val
func ValI64(val int64) Val {
ret := Val{_raw: &C.wasmtime_val_t{kind: C.WASMTIME_I64}}
C.go_wasmtime_val_i64_set(ret.ptr(), C.int64_t(val))
return ret
}
// ValF32 converts a go float32 to a f32 Val
func ValF32(val float32) Val {
ret := Val{_raw: &C.wasmtime_val_t{kind: C.WASMTIME_F32}}
C.go_wasmtime_val_f32_set(ret.ptr(), C.float(val))
return ret
}
// ValF64 converts a go float64 to a f64 Val
func ValF64(val float64) Val {
ret := Val{_raw: &C.wasmtime_val_t{kind: C.WASMTIME_F64}}
C.go_wasmtime_val_f64_set(ret.ptr(), C.double(val))
return ret
}
// ValFuncref converts a Func to a funcref Val
//
// Note that `f` can be `nil` to represent a null `funcref`.
func ValFuncref(f *Func) Val {
ret := Val{_raw: &C.wasmtime_val_t{kind: C.WASMTIME_FUNCREF}}
if f != nil {
C.go_wasmtime_val_funcref_set(ret.ptr(), f.val)
}
return ret
}
// ValExternref converts a go value to a externref Val
//
// Using `externref` is a way to pass arbitrary Go data into a WebAssembly
// module for it to store. Later, when you get a `Val`, you can extract the type
// with the `Externref()` method.
func ValExternref(val interface{}) Val {
ret := Val{_raw: &C.wasmtime_val_t{kind: C.WASMTIME_EXTERNREF}}
// If we have a non-nil value then store it in our global map of all
// externref values. Otherwise there's nothing for us to do since the
// `ref` field will already be a nil pointer.
//
// Note that we add 1 so all non-null externref values are created with
// non-null pointers.
if val != nil {
gExternrefLock.Lock()
defer gExternrefLock.Unlock()
index := gExternrefSlab.allocate()
gExternrefMap[index] = val
ptr := C.go_externref_new(C.size_t(index + 1))
C.go_wasmtime_val_externref_set(ret.ptr(), ptr)
ret.setDtor()
}
return ret
}
//export goFinalizeExternref
func goFinalizeExternref(env unsafe.Pointer) {
idx := int(uintptr(env)) - 1
gExternrefLock.Lock()
defer gExternrefLock.Unlock()
delete(gExternrefMap, idx)
gExternrefSlab.deallocate(idx)
}
func mkVal(src *C.wasmtime_val_t) Val {
ret := Val{_raw: &C.wasmtime_val_t{}}
C.wasmtime_val_copy(ret.ptr(), src)
ret.setDtor()
return ret
}
func takeVal(src *C.wasmtime_val_t) Val {
ret := Val{_raw: &C.wasmtime_val_t{}}
*ret.ptr() = *src
ret.setDtor()
return ret
}
func (v Val) setDtor() {
runtime.SetFinalizer(v.ptr(), func(ptr *C.wasmtime_val_t) {
C.wasmtime_val_delete(ptr)
})
}
func (v Val) ptr() *C.wasmtime_val_t {
ret := v._raw
if ret == nil {
panic("object has been closed already")
}
maybeGC()
return ret
}
// Close will deallocate this value's state explicitly.
//
// For more information see the documentation for engine.Close()
func (v Val) Close() {
if v._raw == nil {
return
}
runtime.SetFinalizer(v._raw, nil)
C.wasmtime_val_delete(v._raw)
v._raw = nil
}
// Kind returns the kind of value that this `Val` contains.
func (v Val) Kind() ValKind {
switch v.ptr().kind {
case C.WASMTIME_I32:
return KindI32
case C.WASMTIME_I64:
return KindI64
case C.WASMTIME_F32:
return KindF32
case C.WASMTIME_F64:
return KindF64
case C.WASMTIME_FUNCREF:
return KindFuncref
case C.WASMTIME_EXTERNREF:
return KindExternref
}
panic("failed to get kind of `Val`")
}
// I32 returns the underlying 32-bit integer if this is an `i32`, or panics.
func (v Val) I32() int32 {
if v.Kind() != KindI32 {
panic("not an i32")
}
return int32(C.go_wasmtime_val_i32_get(v.ptr()))
}
// I64 returns the underlying 64-bit integer if this is an `i64`, or panics.
func (v Val) I64() int64 {
if v.Kind() != KindI64 {
panic("not an i64")
}
return int64(C.go_wasmtime_val_i64_get(v.ptr()))
}
// F32 returns the underlying 32-bit float if this is an `f32`, or panics.
func (v Val) F32() float32 {
if v.Kind() != KindF32 {
panic("not an f32")
}
return float32(C.go_wasmtime_val_f32_get(v.ptr()))
}
// F64 returns the underlying 64-bit float if this is an `f64`, or panics.
func (v Val) F64() float64 {
if v.Kind() != KindF64 {
panic("not an f64")
}
return float64(C.go_wasmtime_val_f64_get(v.ptr()))
}
// Funcref returns the underlying function if this is a `funcref`, or panics.
//
// Note that a null `funcref` is returned as `nil`.
func (v Val) Funcref() *Func {
if v.Kind() != KindFuncref {
panic("not a funcref")
}
val := C.go_wasmtime_val_funcref_get(v.ptr())
if val.store_id == 0 {
return nil
} else {
return mkFunc(val)
}
}
// Externref returns the underlying value if this is an `externref`, or panics.
//
// Note that a null `externref` is returned as `nil`.
func (v Val) Externref() interface{} {
if v.Kind() != KindExternref {
panic("not an externref")
}
val := C.go_wasmtime_val_externref_get(v.ptr())
if val == nil {
return nil
}
data := C.wasmtime_externref_data(val)
gExternrefLock.Lock()
defer gExternrefLock.Unlock()
return gExternrefMap[int(uintptr(data))-1]
}
// Get returns the underlying 64-bit float if this is an `f64`, or panics.
func (v Val) Get() interface{} {
switch v.Kind() {
case KindI32:
return v.I32()
case KindI64:
return v.I64()
case KindF32:
return v.F32()
case KindF64:
return v.F64()
case KindFuncref:
return v.Funcref()
case KindExternref:
return v.Externref()
}
panic("failed to get value of `Val`")
}