-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathvaluetype.go
187 lines (153 loc) · 4.79 KB
/
valuetype.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
package wasmer
// #include <wasmer.h>
import "C"
import (
"runtime"
"unsafe"
)
// ValueKind represents the kind of a value.
type ValueKind C.wasm_valkind_t
const (
// A 32-bit integer. In WebAssembly, integers are
// sign-agnostic, i.E. this can either be signed or unsigned.
I32 = ValueKind(C.WASM_I32)
// A 64-bit integer. In WebAssembly, integers are
// sign-agnostic, i.E. this can either be signed or unsigned.
I64 = ValueKind(C.WASM_I64)
// A 32-bit float.
F32 = ValueKind(C.WASM_F32)
// A 64-bit float.
F64 = ValueKind(C.WASM_F64)
// An externref value which can hold opaque data to the
// WebAssembly instance itself.
AnyRef = ValueKind(C.WASM_ANYREF)
// A first-class reference to a WebAssembly function.
FuncRef = ValueKind(C.WASM_FUNCREF)
)
// String returns the ValueKind as a string.
//
// I32.String() // "i32"
// I64.String() // "i64"
// F32.String() // "f32"
// F64.String() // "f64"
// AnyRef.String() // "anyref"
// FuncRef.String() // "funcref"
func (self ValueKind) String() string {
switch self {
case I32:
return "i32"
case I64:
return "i64"
case F32:
return "f32"
case F64:
return "f64"
case AnyRef:
return "anyref"
case FuncRef:
return "funcref"
}
panic("Unknown value kind")
}
// IsNumber returns true if the ValueKind is a number type.
//
// I32.IsNumber() // true
// I64.IsNumber() // true
// F32.IsNumber() // true
// F64.IsNumber() // true
// AnyRef.IsNumber() // false
// FuncRef.IsNumber() // false
func (self ValueKind) IsNumber() bool {
return bool(C.wasm_valkind_is_num(C.wasm_valkind_t(self)))
}
// IsReference returns true if the ValueKind is a reference.
//
// I32.IsReference() // false
// I64.IsReference() // false
// F32.IsReference() // false
// F64.IsReference() // false
// AnyRef.IsReference() // true
// FuncRef.IsReference() // true
func (self ValueKind) IsReference() bool {
return bool(C.wasm_valkind_is_ref(C.wasm_valkind_t(self)))
}
func (self ValueKind) inner() C.wasm_valkind_t {
return C.wasm_valkind_t(self)
}
// ValueType classifies the individual values that WebAssembly code
// can compute with and the values that a variable accepts.
type ValueType struct {
_inner *C.wasm_valtype_t
_ownedBy interface{}
}
// NewValueType instantiates a new ValueType given a ValueKind.
//
// valueType := NewValueType(I32)
func NewValueType(kind ValueKind) *ValueType {
pointer := C.wasm_valtype_new(C.wasm_valkind_t(kind))
return newValueType(pointer, nil)
}
func newValueType(pointer *C.wasm_valtype_t, ownedBy interface{}) *ValueType {
valueType := &ValueType{_inner: pointer, _ownedBy: ownedBy}
if ownedBy == nil {
runtime.SetFinalizer(valueType, func(valueType *ValueType) {
C.wasm_valtype_delete(valueType.inner())
})
}
return valueType
}
func (self *ValueType) inner() *C.wasm_valtype_t {
return self._inner
}
// Kind returns the ValueType's ValueKind
//
// valueType := NewValueType(I32)
// _ = valueType.Kind()
func (self *ValueType) Kind() ValueKind {
kind := ValueKind(C.wasm_valtype_kind(self.inner()))
runtime.KeepAlive(self)
return kind
}
// NewValueTypes instantiates a new ValueType array from a list of
// ValueKind. Not that the list may be empty.
//
// valueTypes := NewValueTypes(I32, I64, F32)
//
// Note:️ NewValueTypes is specifically designed to help you declare
// function types, e.g. with NewFunctionType:
//
// functionType := NewFunctionType(
// NewValueTypes(), // arguments
// NewValueTypes(I32), // results
// )
func NewValueTypes(kinds ...ValueKind) []*ValueType {
valueTypes := make([]*ValueType, len(kinds))
for nth, kind := range kinds {
valueTypes[nth] = NewValueType(kind)
}
return valueTypes
}
func toValueTypeVec(valueTypes []*ValueType) C.wasm_valtype_vec_t {
vec := C.wasm_valtype_vec_t{}
C.wasm_valtype_vec_new_uninitialized(&vec, C.size_t(len(valueTypes)))
firstValueTypePointer := unsafe.Pointer(vec.data)
for nth, valueType := range valueTypes {
pointer := C.wasm_valtype_new(C.wasm_valtype_kind(valueType.inner()))
*(**C.wasm_valtype_t)(unsafe.Pointer(uintptr(firstValueTypePointer) + unsafe.Sizeof(pointer)*uintptr(nth))) = pointer
}
runtime.KeepAlive(valueTypes)
return vec
}
func toValueTypeList(valueTypes *C.wasm_valtype_vec_t, ownedBy interface{}) []*ValueType {
numberOfValueTypes := int(valueTypes.size)
list := make([]*ValueType, numberOfValueTypes)
firstValueType := unsafe.Pointer(valueTypes.data)
sizeOfValueTypePointer := unsafe.Sizeof(firstValueType)
var currentValueTypePointer *C.wasm_valtype_t
for nth := 0; nth < numberOfValueTypes; nth++ {
currentValueTypePointer = *(**C.wasm_valtype_t)(unsafe.Pointer(uintptr(firstValueType) + uintptr(nth)*sizeOfValueTypePointer))
valueType := newValueType(currentValueTypePointer, ownedBy)
list[nth] = valueType
}
return list
}