forked from wasmerio/wasmer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
266 lines (207 loc) · 6.61 KB
/
function.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
package wasmer
// #include <wasmer_wasm.h>
//
// extern wasm_trap_t* function_trampoline(
// void *environment,
// /* const */ wasm_val_vec_t* arguments,
// wasm_val_vec_t* results
// );
//
// typedef void (*wasm_func_callback_env_finalizer_t)(void* environment);
//
// extern void function_environment_finalizer(void *environment);
import "C"
import (
"fmt"
"runtime"
"sync"
"unsafe"
)
type Function struct {
_inner *C.wasm_func_t
_ownedBy interface{}
environment *FunctionEnvironment
lazyNative func(...interface{}) (interface{}, error)
}
func newFunction(pointer *C.wasm_func_t, environment *FunctionEnvironment, ownedBy interface{}) *Function {
function := &Function{
_inner: pointer,
_ownedBy: ownedBy,
environment: environment,
lazyNative: nil,
}
if ownedBy == nil {
runtime.SetFinalizer(function, func(function *Function) {
if function.environment != nil {
hostFunctionStore.remove(function.environment.hostFunctionStoreIndex)
}
C.wasm_func_delete(function.inner())
})
}
return function
}
func NewFunction(store *Store, ty *FunctionType, function func([]Value) ([]Value, error)) *Function {
hostFunction := &hostFunction{
store: store,
function: function,
}
environment := &FunctionEnvironment{
hostFunctionStoreIndex: hostFunctionStore.store(hostFunction),
}
pointer := C.wasm_func_new_with_env(
store.inner(),
ty.inner(),
(C.wasm_func_callback_t)(C.function_trampoline),
unsafe.Pointer(environment),
(C.wasm_func_callback_env_finalizer_t)(C.function_environment_finalizer),
)
runtime.KeepAlive(environment)
return newFunction(pointer, environment, nil)
}
//export function_trampoline
func function_trampoline(env unsafe.Pointer, args *C.wasm_val_vec_t, res *C.wasm_val_vec_t) *C.wasm_trap_t {
environment := (*FunctionEnvironment)(env)
hostFunction, err := hostFunctionStore.load(environment.hostFunctionStoreIndex)
if err != nil {
panic(err)
}
arguments := toValueList(args)
results, err := (hostFunction.function)(arguments)
if err != nil {
trap := NewTrap(hostFunction.store, err.Error())
runtime.KeepAlive(trap)
return trap.inner()
}
toValueVec(results, res)
return nil
}
func (self *Function) inner() *C.wasm_func_t {
return self._inner
}
func (self *Function) ownedBy() interface{} {
if self._ownedBy == nil {
return self
}
return self._ownedBy
}
func (self *Function) IntoExtern() *Extern {
pointer := C.wasm_func_as_extern(self.inner())
return newExtern(pointer, self.ownedBy())
}
func (self *Function) Type() *FunctionType {
ty := C.wasm_func_type(self.inner())
runtime.KeepAlive(self)
return newFunctionType(ty, self.ownedBy())
}
func (self *Function) ParameterArity() uint {
return uint(C.wasm_func_param_arity(self.inner()))
}
func (self *Function) ResultArity() uint {
return uint(C.wasm_func_result_arity(self.inner()))
}
func (self *Function) Call(parameters ...interface{}) (interface{}, error) {
return self.Native()(parameters...)
}
func (self *Function) Native() func(...interface{}) (interface{}, error) {
if self.lazyNative != nil {
return self.lazyNative
}
self.lazyNative = func(receivedParameters ...interface{}) (interface{}, error) {
ty := self.Type()
expectedParameters := ty.Params()
numberOfReceivedParameters := len(receivedParameters)
numberOfExpectedParameters := len(expectedParameters)
diff := numberOfExpectedParameters - numberOfReceivedParameters
if diff > 0 {
return nil, newErrorWith(fmt.Sprintf("Missing %d argument(s) when calling the function; Expected %d argument(s), received %d", diff, numberOfExpectedParameters, numberOfReceivedParameters))
} else if diff < 0 {
return nil, newErrorWith(fmt.Sprintf("Given %d extra argument(s) when calling the function; Expected %d argument(s), received %d", -diff, numberOfExpectedParameters, numberOfReceivedParameters))
}
allArguments := make([]C.wasm_val_t, numberOfReceivedParameters)
for nth, receivedParameter := range receivedParameters {
argument, err := fromGoValue(receivedParameter, expectedParameters[nth].Kind())
if err != nil {
return nil, newErrorWith(fmt.Sprintf("Argument %d of the function must of of type `%s`, cannot cast value to this type.", nth+1, err))
}
allArguments[nth] = argument
}
results := C.wasm_val_vec_t{}
C.wasm_val_vec_new_uninitialized(&results, C.size_t(len(ty.Results())))
defer C.wasm_val_vec_delete(&results)
arguments := C.wasm_val_vec_t{}
defer C.wasm_val_vec_delete(&arguments)
if numberOfReceivedParameters > 0 {
C.wasm_val_vec_new(&arguments, C.size_t(numberOfReceivedParameters), (*C.wasm_val_t)(unsafe.Pointer(&allArguments[0])))
}
trap := C.wasm_func_call(self.inner(), &arguments, &results)
runtime.KeepAlive(arguments)
runtime.KeepAlive(results)
if trap != nil {
return nil, newErrorFromTrap(trap)
}
switch results.size {
case 0:
return nil, nil
case 1:
return toGoValue(results.data), nil
default:
numberOfValues := int(results.size)
allResults := make([]interface{}, numberOfValues)
firstValue := unsafe.Pointer(results.data)
sizeOfValuePointer := unsafe.Sizeof(C.wasm_val_t{})
var currentValuePointer *C.wasm_val_t
for nth := 0; nth < numberOfValues; nth++ {
currentValuePointer = (*C.wasm_val_t)(unsafe.Pointer(uintptr(firstValue) + uintptr(nth)*sizeOfValuePointer))
value := toGoValue(currentValuePointer)
allResults[nth] = value
}
return allResults, nil
}
}
return self.lazyNative
}
type FunctionEnvironment struct {
store *Store
hostFunctionStoreIndex uint
}
//export function_environment_finalizer
func function_environment_finalizer() {}
type hostFunction struct {
function func([]Value) ([]Value, error)
store *Store
}
type hostFunctions struct {
sync.RWMutex
functions map[uint]*hostFunction
}
func (self *hostFunctions) load(index uint) (*hostFunction, error) {
hostFunction, exists := self.functions[index]
if exists && hostFunction != nil {
return hostFunction, nil
}
return nil, newErrorWith(fmt.Sprintf("Host function `%d` does not exist", index))
}
func (self *hostFunctions) store(hostFunction *hostFunction) uint {
self.Lock()
// By default, the index is the size of the store.
index := uint(len(self.functions))
for nth, hostFunc := range self.functions {
// Find the first empty slot in the store.
if hostFunc == nil {
// Use that empty slot for the index.
index = nth
break
}
}
self.functions[index] = hostFunction
self.Unlock()
return index
}
func (self *hostFunctions) remove(index uint) {
self.Lock()
self.functions[index] = nil
self.Unlock()
}
var hostFunctionStore = hostFunctions{
functions: make(map[uint]*hostFunction),
}