-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes.go
More file actions
366 lines (311 loc) · 8.47 KB
/
types.go
File metadata and controls
366 lines (311 loc) · 8.47 KB
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
/*
* 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 <stdlib.h>
#include <stdint.h>
typedef void* _ptr;
*/
import "C"
import (
"fmt"
"reflect"
"unsafe"
)
type ffiType = *C.ffi_type
var (
typeVoid ffiType = &C.ffi_type_void
typeUint8 ffiType = &C.ffi_type_uint8
typeUint16 ffiType = &C.ffi_type_uint16
typeUint32 ffiType = &C.ffi_type_uint32
typeUint64 ffiType = &C.ffi_type_uint64
typeInt8 ffiType = &C.ffi_type_sint8
typeInt16 ffiType = &C.ffi_type_sint16
typeInt32 ffiType = &C.ffi_type_sint32
typeInt64 ffiType = &C.ffi_type_sint64
typeFloat ffiType = &C.ffi_type_float
typeDouble ffiType = &C.ffi_type_double
typePointer ffiType = &C.ffi_type_pointer
)
var (
// TypeError represents a Go error. This type is not
// translated into a C type
TypeError = reflect.TypeOf((*error)(nil)).Elem()
// TypeUint represents a Go uint. This type is
// translated into the basic unsigned int type in C, which
// can be 2 or 4 bytes.
TypeUint = reflect.TypeOf(uint(0))
// TypeUint8 represents a Go uint8. This type is
// translated into a uint8_t (or similar) type in C.
TypeUint8 = reflect.TypeOf(uint8(0))
// TypeUint16 represents a Go uint16. This type is
// translated into a uint16_t (or similar) type in C.
TypeUint16 = reflect.TypeOf(uint16(0))
// TypeUint32 represents a Go uint32. This type is
// translated into a uint32_t (or similar) type in C.
TypeUint32 = reflect.TypeOf(uint32(0))
// TypeUint64 represents a Go uint64. This type is
// translated into a uint64_t (or similar) type in C.
TypeUint64 = reflect.TypeOf(uint64(0))
// TypeInt represents a Go int. This type is
// translated into the basic signed int type in C, which
// can be 2 or 4 bytes.
TypeInt = reflect.TypeOf(int(0))
// TypeInt8 represents a Go int8. This type is
// translated into a int8_t (or similar) type in C.
TypeInt8 = reflect.TypeOf(int8(0))
// TypeInt16 represents a Go int16. This type is
// translated into a int16_t (or similar) type in C.
TypeInt16 = reflect.TypeOf(int16(0))
// TypeInt32 represents a Go int32. This type is
// translated into a int32_t (or similar) type in C.
TypeInt32 = reflect.TypeOf(int32(0))
// TypeInt64 represents a Go int64. This type is
// translated into a int64_t (or similar) type in C.
TypeInt64 = reflect.TypeOf(int64(0))
// TypeFloat32 represents a Go float32. This type is
// translated into a float type in C.
TypeFloat32 = reflect.TypeOf(float32(0))
// TypeFloat64 represents a Go float64. This type is
// translated into a double type in C.
TypeFloat64 = reflect.TypeOf(float64(0))
// TypeBool represents a Go bool. This type is
// translated into a _Bool / bool (or similar) type in C.
TypeBool = reflect.TypeOf(true)
// TypeUintptr represents a Go uintptr. This type is
// translated into a intptr_t (or similar) type in C.
TypeUintptr = reflect.TypeOf(uintptr(0))
// TypeUnsafePointer represents a Go unsafe.Pointer. This type is
// translated into a void* type in C.
TypeUnsafePointer = reflect.TypeOf(unsafe.Pointer(uintptr(0)))
// TypeVoid represents a virtual Go void type, which means
// no type at all. This type is "translated" into a void
// type in C.
TypeVoid = reflect.TypeOf(&struct{}{})
)
var valueNil = reflect.ValueOf(nil)
var valueNilError = reflect.Zero(TypeError)
func wrapType(t reflect.Type) ffiType {
// Unhandled for now
// - map
// - slices (maybe only pointer slices?)
// - func
// - interface
// - array
// - struct
// - complex64
// - complex128
// - chan
switch t.Kind() {
case reflect.String:
fallthrough
case reflect.Ptr:
fallthrough
case reflect.UnsafePointer:
fallthrough
case reflect.Uintptr:
return typePointer
case reflect.Uint:
if intSize == 2 {
return typeUint16
}
return typeUint32
case reflect.Uint8:
return typeUint8
case reflect.Uint16:
return typeUint16
case reflect.Uint32:
return typeUint32
case reflect.Uint64:
return typeUint64
case reflect.Int:
if intSize == 2 {
return typeInt16
}
return typeInt32
case reflect.Int8:
return typeInt8
case reflect.Int16:
return typeInt16
case reflect.Int32:
return typeInt32
case reflect.Int64:
return typeInt64
case reflect.Float32:
return typeFloat
case reflect.Float64:
return typeDouble
case reflect.Bool:
if boolSize == 1 {
return typeInt8
}
return typeInt16
}
panic(fmt.Errorf("unhandled data type: %s", t.Kind().String()))
}
func unwrapType(t ffiType) reflect.Type {
switch t {
case typeUint8:
return TypeUint8
case typeUint16:
return TypeUint16
case typeUint32:
return TypeUint32
case typeUint64:
return TypeUint64
case typeInt8:
return TypeInt8
case typeInt16:
return TypeInt16
case typeInt32:
return TypeInt32
case typeInt64:
return TypeInt64
case typeFloat:
return TypeFloat32
case typeDouble:
return TypeFloat64
case typePointer:
return TypeUintptr
}
panic(fmt.Errorf("unhandled data type: %d", t))
}
type finalizer = func()
func wrapValue(value reflect.Value) (unsafe.Pointer, finalizer) {
t := value.Type()
v := value.Interface()
switch t.Kind() {
case reflect.String:
cs := C.CString(v.(string))
fin := func() {
C.free(unsafe.Pointer(cs))
}
return unsafe.Pointer(&cs), fin
case reflect.UnsafePointer:
return v.(unsafe.Pointer), nil
case reflect.Uintptr:
return unsafe.Pointer(v.(uintptr)), nil
case reflect.Uint:
val := value.Uint()
if intSize == 2 {
v := C.uint16_t(val)
return unsafe.Pointer(&v), nil
}
v := C.uint32_t(val)
return unsafe.Pointer(&v), nil
case reflect.Uint8:
val := C.uint8_t(value.Uint())
return unsafe.Pointer(&val), nil
case reflect.Uint16:
val := C.uint16_t(value.Uint())
return unsafe.Pointer(&val), nil
case reflect.Uint32:
val := C.uint32_t(value.Uint())
return unsafe.Pointer(&val), nil
case reflect.Uint64:
val := C.uint64_t(value.Uint())
return unsafe.Pointer(&val), nil
case reflect.Int:
val := value.Int()
if intSize == 2 {
v := C.int16_t(val)
return unsafe.Pointer(&v), nil
}
v := C.int32_t(val)
return unsafe.Pointer(&v), nil
case reflect.Int8:
val := C.int8_t(value.Int())
return unsafe.Pointer(&val), nil
case reflect.Int16:
val := C.int16_t(value.Int())
return unsafe.Pointer(&val), nil
case reflect.Int32:
val := C.int32_t(value.Int())
return unsafe.Pointer(&val), nil
case reflect.Int64:
val := C.int64_t(value.Int())
return unsafe.Pointer(&val), nil
case reflect.Float32:
val := C.float(value.Float())
return unsafe.Pointer(&val), nil
case reflect.Float64:
val := C.double(value.Float())
return unsafe.Pointer(&val), nil
case reflect.Ptr:
ptr := (*C.int)(C.malloc(C.size_t(ptrSize)))
*ptr = (C.int)(value.Pointer())
fin := func() {
C.free(unsafe.Pointer(ptr))
}
return unsafe.Pointer(&ptr), fin
case reflect.Bool:
b := 0
if v.(bool) {
b = 1
}
if boolSize == 1 {
val := C.int8_t(b)
return unsafe.Pointer(&val), nil
}
val := C.int16_t(b)
return unsafe.Pointer(&val), nil
}
panic(fmt.Errorf("unhandled data type: %s", t.Kind().String()))
}
func convertValue(value reflect.Value, t reflect.Type) reflect.Value {
vt := value.Type()
if vt.Kind() == reflect.Ptr {
ivt := reflect.Indirect(value)
if t.Kind() == reflect.Ptr {
it := t.Elem()
if ivt.Kind() == it.Kind() {
return value
}
}
value = ivt
vt = value.Type()
}
switch t.Kind() {
case reflect.Uintptr:
fallthrough
case reflect.UnsafePointer:
fallthrough
case reflect.Ptr:
it := t.Elem()
reflect.ValueOf(value.Convert(it).Interface())
pv := reflect.New(it)
pv.Elem().Set(value)
value = pv
case reflect.String:
if vt.Kind() == reflect.Uintptr {
ptr := value.Interface().(uintptr)
value = reflect.ValueOf(C.GoString((*C.char)(unsafe.Pointer(ptr))))
C.free(unsafe.Pointer(ptr))
}
case reflect.Bool:
v := value.Int()
b := false
if v != 0 {
b = true
}
value = reflect.ValueOf(b)
default:
value = reflect.ValueOf(value.Convert(t).Interface())
}
return value
}