-
Notifications
You must be signed in to change notification settings - Fork 1
/
stub.go
122 lines (102 loc) · 2.84 KB
/
stub.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
/*
* libgoffi - libffi adapter library for Go
* Copyright 2019 clevabit GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libgoffi
/*
#include <ffi.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
const int _ptrSize = sizeof(uintptr_t);
const int _boolSize = sizeof(_Bool);
#if INT_MAX == 32767
const int _intSize = 2;
#elif INT_MAX == 2147483647
const int _intSize = 4;
#elif INT_MAX == 9223372036854775807
#error "64 bit base int is unsupported, please use int64 explicitly"
#endif
typedef void** argumentsPtr;
static void **argsArrayNew(int nargs) {
void** ptr = (void **)(malloc(nargs * _ptrSize));
memset(ptr, 0, nargs * _ptrSize);
return ptr;
}
static void argsArraySet(void **args, int index, void *ptr) {
args[index] = ptr;
}
static void argsArrayFree(void **args) {
free(args);
}
static void _ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **values) {
ffi_call(cif, fn, rvalue, values);
}
*/
import "C"
import (
"reflect"
"unsafe"
)
var (
ptrSize = int(C._ptrSize)
boolSize = int(C._boolSize)
intSize = int(C._intSize)
)
func makeStub(inFnType, outFnType reflect.Type, cif *C.ffi_cif, funcPtr functionPointer, outType ffiType,
inTypes []ffiType, returnsError bool) func(values []reflect.Value) []reflect.Value {
return func(values []reflect.Value) []reflect.Value {
nargs := len(values)
args := C.argsArrayNew(C.int(nargs))
finalizers := make([]finalizer, 0)
for i := 0; i < nargs; i++ {
if inFnType.In(i) != outFnType.In(i) {
values[i] = convertValue(values[i], outFnType.In(i))
}
arg, fin := wrapValue(values[i])
if fin != nil {
finalizers = append(finalizers, fin)
}
C.argsArraySet(args, C.int(i), arg)
}
var cargs C.argumentsPtr
if nargs > 0 {
cargs = args
}
ot := unwrapType(outType)
out := reflect.New(ot)
_, err := C._ffi_call(cif, funcPtr, unsafe.Pointer(out.Elem().UnsafeAddr()), cargs)
if err != nil {
if returnsError {
return []reflect.Value{valueNil, reflect.ValueOf(err)}
}
panic(err)
}
C.argsArrayFree(args)
for i := 0; i < len(finalizers); i++ {
finalizers[i]()
}
retValues := make([]reflect.Value, 0)
if inFnType.NumOut() > 0 {
rt := inFnType.Out(0)
out = convertValue(out, rt)
retValues = append(retValues, out)
}
if returnsError {
retValues = append(retValues, valueNilError)
}
return retValues
}
}