forked from go-qml/qml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatype.go
416 lines (385 loc) · 11.5 KB
/
datatype.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package qml
// #include <stdlib.h>
// #include "capi.h"
import "C"
import (
"bytes"
"fmt"
"image/color"
"reflect"
"strings"
"unicode"
"unsafe"
)
var (
intIs64 bool
intDT C.DataType
ptrSize = C.size_t(unsafe.Sizeof(uintptr(0)))
nilPtr = unsafe.Pointer(uintptr(0))
nilCharPtr = (*C.char)(nilPtr)
typeString = reflect.TypeOf("")
typeBool = reflect.TypeOf(false)
typeInt = reflect.TypeOf(int(0))
typeInt64 = reflect.TypeOf(int64(0))
typeInt32 = reflect.TypeOf(int32(0))
typeFloat64 = reflect.TypeOf(float64(0))
typeFloat32 = reflect.TypeOf(float32(0))
typeIface = reflect.TypeOf(new(interface{})).Elem()
typeRGBA = reflect.TypeOf(color.RGBA{})
typeObjSlice = reflect.TypeOf([]Object(nil))
)
func init() {
var i int = 1<<31 - 1
intIs64 = (i+1 > 0)
if intIs64 {
intDT = C.DTInt64
} else {
intDT = C.DTInt32
}
}
// packDataValue packs the provided Go value into a C.DataValue for
// shiping into C++ land.
//
// For simple types (bool, int, etc) value is converted into a
// native C++ value. For anything else, including cases when value
// has a type that has an underlying simple type, the Go value itself
// is encapsulated into a C++ wrapper so that field access and method
// calls work.
//
// This must be run from the main GUI thread due to the cases where
// calling wrapGoValue is necessary.
func packDataValue(value interface{}, dvalue *C.DataValue, engine *Engine, owner valueOwner) {
datap := unsafe.Pointer(&dvalue.data)
if value == nil {
dvalue.dataType = C.DTInvalid
return
}
switch value := value.(type) {
case string:
dvalue.dataType = C.DTString
cstr, cstrlen := unsafeStringData(value)
*(**C.char)(datap) = cstr
dvalue.len = cstrlen
case bool:
dvalue.dataType = C.DTBool
*(*bool)(datap) = value
case int:
if value > 1<<31-1 {
dvalue.dataType = C.DTInt64
*(*int64)(datap) = int64(value)
} else {
dvalue.dataType = C.DTInt32
*(*int32)(datap) = int32(value)
}
case int64:
dvalue.dataType = C.DTInt64
*(*int64)(datap) = value
case int32:
dvalue.dataType = C.DTInt32
*(*int32)(datap) = value
case float64:
dvalue.dataType = C.DTFloat64
*(*float64)(datap) = value
case float32:
dvalue.dataType = C.DTFloat32
*(*float32)(datap) = value
case *Common:
dvalue.dataType = C.DTObject
*(*unsafe.Pointer)(datap) = value.addr
case color.RGBA:
dvalue.dataType = C.DTColor
*(*uint32)(datap) = uint32(value.A)<<24 | uint32(value.R)<<16 | uint32(value.G)<<8 | uint32(value.B)
default:
dvalue.dataType = C.DTObject
if obj, ok := value.(Object); ok {
*(*unsafe.Pointer)(datap) = obj.Common().addr
} else {
*(*unsafe.Pointer)(datap) = wrapGoValue(engine, value, owner)
}
}
}
// TODO Handle byte slices.
// unpackDataValue converts a value shipped by C++ into a native Go value.
//
// HEADS UP: This is considered safe to be run out of the main GUI thread.
// If that changes, fix the call sites.
func unpackDataValue(dvalue *C.DataValue, engine *Engine) interface{} {
datap := unsafe.Pointer(&dvalue.data)
switch dvalue.dataType {
case C.DTString:
s := C.GoStringN(*(**C.char)(datap), dvalue.len)
// TODO If we move all unpackDataValue calls to the GUI thread,
// can we get rid of this allocation somehow?
C.free(unsafe.Pointer(*(**C.char)(datap)))
return s
case C.DTBool:
return *(*bool)(datap)
case C.DTInt64:
return *(*int64)(datap)
case C.DTInt32:
return int(*(*int32)(datap))
case C.DTFloat64:
return *(*float64)(datap)
case C.DTFloat32:
return *(*float32)(datap)
case C.DTColor:
var c uint32 = *(*uint32)(datap)
return color.RGBA{byte(c >> 16), byte(c >> 8), byte(c), byte(c >> 24)}
case C.DTGoAddr:
return (*(**valueFold)(datap)).gvalue
case C.DTInvalid:
return nil
case C.DTObject:
// TODO Would be good to preserve identity on the Go side.
return &Common{
engine: engine,
addr: *(*unsafe.Pointer)(datap),
}
case C.DTValueList:
var dvlist []C.DataValue
var dvlisth = (*reflect.SliceHeader)(unsafe.Pointer(&dvlist))
dvlisth.Data = uintptr(*(*unsafe.Pointer)(datap))
dvlisth.Len = int(dvalue.len)
dvlisth.Cap = int(dvalue.len)
result := make([]interface{}, len(dvlist))
for i := range result {
result[i] = unpackDataValue(&dvlist[i], engine)
}
C.free(*(*unsafe.Pointer)(datap))
return &List{result}
}
panic(fmt.Sprintf("unsupported data type: %d", dvalue.dataType))
}
func dataTypeOf(typ reflect.Type) C.DataType {
// Compare against the specific types rather than their kind.
// Custom types may have methods that must be supported.
switch typ {
case typeString:
return C.DTString
case typeBool:
return C.DTBool
case typeInt:
return intDT
case typeInt64:
return C.DTInt64
case typeInt32:
return C.DTInt32
case typeFloat32:
return C.DTFloat32
case typeFloat64:
return C.DTFloat64
case typeIface:
return C.DTAny
case typeRGBA:
return C.DTColor
case typeObjSlice:
return C.DTListProperty
}
return C.DTObject
}
var typeInfoSize = C.size_t(unsafe.Sizeof(C.GoTypeInfo{}))
var memberInfoSize = C.size_t(unsafe.Sizeof(C.GoMemberInfo{}))
var typeInfoCache = make(map[reflect.Type]*C.GoTypeInfo)
func typeInfo(v interface{}) *C.GoTypeInfo {
vt := reflect.TypeOf(v)
for vt.Kind() == reflect.Ptr {
vt = vt.Elem()
}
typeInfo := typeInfoCache[vt]
if typeInfo != nil {
return typeInfo
}
typeInfo = (*C.GoTypeInfo)(C.malloc(typeInfoSize))
typeInfo.typeName = C.CString(vt.Name())
typeInfo.metaObject = nilPtr
var onChanged map[string]int
// TODO Only do that if it's a struct?
vtptr := reflect.PtrTo(vt)
numField := vt.NumField()
prvField := 0
numMethod := vtptr.NumMethod()
// struct { FooBar T; Baz T } => "fooBar\0baz\0"
namesLen := 0
for i := 0; i < numField; i++ {
field := vt.Field(i)
if field.PkgPath != "" {
prvField++ // not exported
continue
}
namesLen += len(field.Name) + 1
}
for i := 0; i < numMethod; i++ {
namesLen += len(vtptr.Method(i).Name) + 1
}
names := make([]byte, 0, namesLen)
for i := 0; i < numField; i++ {
field := vt.Field(i)
if field.PkgPath != "" {
continue // not exported
}
name := field.Name
for i, rune := range name {
if i == 0 {
names = append(names, string(unicode.ToLower(rune))...)
} else {
names = append(names, name[i:]...)
break
}
}
names = append(names, 0)
}
for i := 0; i < numMethod; i++ {
name := vtptr.Method(i).Name
for i, rune := range name {
if i == 0 {
names = append(names, string(unicode.ToLower(rune))...)
} else {
names = append(names, name[i:]...)
break
}
}
names = append(names, 0)
// Track "On*Changed" notification methods.
if len(name) > 9 && name[0] == 'O' && name[1] == 'n' && strings.HasSuffix(name, "Changed") {
if onChanged == nil {
onChanged = make(map[string]int)
}
onChanged[name[2:len(name)-7]] = i
}
}
if len(names) != namesLen {
panic("pre-allocated buffer size was wrong")
}
typeInfo.memberNames = C.CString(string(names))
// Assemble information on members.
membersLen := numField - prvField + numMethod
membersi := uintptr(0)
mnamesi := uintptr(0)
members := uintptr(C.malloc(memberInfoSize * C.size_t(membersLen)))
mnames := uintptr(unsafe.Pointer(typeInfo.memberNames))
for i := 0; i < numField; i++ {
field := vt.Field(i)
if field.PkgPath != "" {
continue // not exported
}
memberInfo := (*C.GoMemberInfo)(unsafe.Pointer(members + uintptr(memberInfoSize)*membersi))
memberInfo.memberName = (*C.char)(unsafe.Pointer(mnames + mnamesi))
memberInfo.memberType = dataTypeOf(field.Type)
memberInfo.reflectIndex = C.int(i)
memberInfo.reflectChangedIndex = -1
memberInfo.addrOffset = C.int(field.Offset)
membersi += 1
mnamesi += uintptr(len(field.Name)) + 1
if methodIndex, ok := onChanged[field.Name]; ok {
memberInfo.reflectChangedIndex = C.int(methodIndex)
}
}
for i := 0; i < numMethod; i++ {
method := vtptr.Method(i)
memberInfo := (*C.GoMemberInfo)(unsafe.Pointer(members + uintptr(memberInfoSize)*membersi))
memberInfo.memberName = (*C.char)(unsafe.Pointer(mnames + mnamesi))
memberInfo.memberType = C.DTMethod
memberInfo.reflectIndex = C.int(i)
memberInfo.reflectChangedIndex = -1
memberInfo.addrOffset = 0
signature, result := methodQtSignature(method)
// TODO The signature data might be embedded in the same array as the member names.
memberInfo.methodSignature = C.CString(signature)
memberInfo.resultSignature = C.CString(result)
// TODO Sort out methods with a variable number of arguments.
// It's called while bound, so drop the receiver.
memberInfo.numIn = C.int(method.Type.NumIn() - 1)
memberInfo.numOut = C.int(method.Type.NumOut())
membersi += 1
mnamesi += uintptr(len(method.Name)) + 1
}
typeInfo.members = (*C.GoMemberInfo)(unsafe.Pointer(members))
typeInfo.membersLen = C.int(membersLen)
typeInfo.fields = typeInfo.members
typeInfo.fieldsLen = C.int(numField - prvField)
typeInfo.methods = (*C.GoMemberInfo)(unsafe.Pointer(members + uintptr(memberInfoSize)*uintptr(typeInfo.fieldsLen)))
typeInfo.methodsLen = C.int(numMethod)
if int(membersi) != membersLen {
panic("used more space than allocated for member names")
}
if int(mnamesi) != namesLen {
panic("allocated buffer doesn't match used space")
}
if typeInfo.fieldsLen+typeInfo.methodsLen != typeInfo.membersLen {
panic("lengths are inconsistent")
}
typeInfoCache[vt] = typeInfo
return typeInfo
}
func methodQtSignature(method reflect.Method) (signature, result string) {
var buf bytes.Buffer
for i, rune := range method.Name {
if i == 0 {
buf.WriteRune(unicode.ToLower(rune))
} else {
buf.WriteString(method.Name[i:])
break
}
}
buf.WriteByte('(')
n := method.Type.NumIn()
for i := 1; i < n; i++ {
if i > 1 {
buf.WriteByte(',')
}
buf.WriteString("QVariant")
}
buf.WriteByte(')')
signature = buf.String()
switch method.Type.NumOut() {
case 0:
// keep it as ""
case 1:
result = "QVariant"
default:
result = "QVariantList"
}
return
}
func hashable(value interface{}) (hashable bool) {
defer func() { recover() }()
return value == value
}
// unsafeString returns a Go string backed by C data.
//
// If the C data is deallocated or moved, the string will be
// invalid and will crash the program if used. As such, the
// resulting string must only be used inside the implementation
// of the qml package and while the life time of the C data
// is guaranteed.
func unsafeString(data *C.char, size C.int) string {
var s string
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
sh.Data = uintptr(unsafe.Pointer(data))
sh.Len = int(size)
return s
}
// unsafeStringData returns a C string backed by Go data. The C
// string is NOT null-terminated, so its length must be taken
// into account.
//
// If the s Go string is garbage collected, the returned C data
// will be invalid and will crash the program if used. As such,
// the resulting data must only be used inside the implementation
// of the qml package and while the life time of the Go string
// is guaranteed.
func unsafeStringData(s string) (*C.char, C.int) {
return *(**C.char)(unsafe.Pointer(&s)), C.int(len(s))
}
// unsafeBytesData returns a C string backed by Go data. The C
// string is NOT null-terminated, so its length must be taken
// into account.
//
// If the array backing the b Go slice is garbage collected, the
// returned C data will be invalid and will crash the program if
// used. As such, the resulting data must only be used inside the
// implementation of the qml package and while the life time of
// the Go array is guaranteed.
func unsafeBytesData(b []byte) (*C.char, C.int) {
return *(**C.char)(unsafe.Pointer(&b)), C.int(len(b))
}