forked from go-qml/qml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.go
552 lines (502 loc) · 15.7 KB
/
bridge.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package qml
// #cgo CPPFLAGS: -I./cpp
// #cgo CPPFLAGS: -I/usr/include/qt5/QtCore/5.0.2/QtCore
// #cgo CPPFLAGS: -I/usr/include/qt5/QtCore/5.1.1/QtCore
// #cgo CPPFLAGS: -I/usr/include/qt5/QtCore/5.2.0/QtCore
// #cgo CPPFLAGS: -I/usr/include/qt/QtCore/5.1.1/QtCore
// #cgo CXXFLAGS: -std=c++0x -pedantic-errors -Wall -fno-strict-aliasing
// #cgo LDFLAGS: -lstdc++
// #cgo pkg-config: Qt5Core Qt5Widgets Qt5Quick glib-2.0
//
// #include <stdlib.h>
//
// #include "cpp/capi.h"
//
import "C"
import (
"fmt"
"github.com/niemeyer/qml/tref"
"reflect"
"runtime"
"strings"
"sync"
"sync/atomic"
"unsafe"
)
var hookWaiting C.int
// guiLoop runs the main GUI thread event loop in C++ land.
func guiLoop() {
runtime.LockOSThread()
guiLoopRef = tref.Ref()
C.newGuiApplication()
C.idleTimerInit(&hookWaiting)
guiLoopReady.Unlock()
C.applicationExec()
}
var (
guiFunc = make(chan func())
guiDone = make(chan struct{})
guiLock = 0
guiLoopReady sync.Mutex
guiLoopRef uintptr
)
// gui runs f in the main GUI thread and waits for f to return.
func gui(f func()) {
if tref.Ref() == guiLoopRef {
// Already within the GUI thread. Attempting to wait would deadlock.
f()
return
}
// Tell Qt we're waiting for the idle hook to be called.
if atomic.AddInt32((*int32)(unsafe.Pointer(&hookWaiting)), 1) == 1 {
C.idleTimerStart()
}
// Send f to be executed by the idle hook in the main GUI thread.
guiFunc <- f
// Wait until f is done executing.
<-guiDone
}
// Lock freezes all QML activity by blocking the main event loop.
// Locking is necessary before updating shared data structures
// without race conditions.
//
// It's safe to use qml functionality while holding a lock, as
// long as the requests made do not depend on follow up QML
// events to be processed before returning. If that happens, the
// problem will be observed as the application freezing.
//
// The Lock function is reentrant. That means it may be called
// multiple times, and QML activities will only be resumed after
// Unlock is called a matching number of times.
func Lock() {
// TODO Better testing for this.
gui(func() {
guiLock++
})
}
// Unlock releases the QML event loop. See Lock for details.
func Unlock() {
gui(func() {
if guiLock == 0 {
panic("qml.Unlock called without lock being held")
}
guiLock--
})
}
// Flush synchronously flushes all pending QML activities.
func Flush() {
// TODO Better testing for this.
gui(func() {
C.applicationFlushAll()
})
}
// Changed notifies all QML bindings that the given field value has changed.
//
// For example:
//
// qml.Changed(&value, &value.Field)
//
func Changed(value, fieldAddr interface{}) {
valuev := reflect.ValueOf(value)
fieldv := reflect.ValueOf(fieldAddr)
for valuev.Kind() == reflect.Ptr {
valuev = valuev.Elem()
}
for fieldv.Kind() == reflect.Ptr {
fieldv = fieldv.Elem()
}
if fieldv.Type().Size() == 0 {
panic("cannot report changes on zero-sized fields")
}
offset := fieldv.UnsafeAddr() - valuev.UnsafeAddr()
if !(0 <= offset && offset < valuev.Type().Size()) {
panic("provided field is not a member of the given value")
}
gui(func() {
tinfo := typeInfo(value)
for _, engine := range engines {
fold := engine.values[value]
for fold != nil {
C.goValueActivate(fold.cvalue, tinfo, C.int(offset))
fold = fold.next
}
// TODO typeNew might also be a linked list keyed by the gvalue.
// This would prevent the iteration and the deferrals.
for fold, _ = range typeNew {
if fold.gvalue == value {
// Activate these later so they don't get recursively moved
// out of typeNew while the iteration is still happening.
defer C.goValueActivate(fold.cvalue, tinfo, C.int(offset))
}
}
}
})
}
// hookIdleTimer is run once per iteration of the Qt event loop,
// within the main GUI thread, but only if at least one goroutine
// has atomically incremented hookWaiting.
//
//export hookIdleTimer
func hookIdleTimer() {
var f func()
for {
select {
case f = <-guiFunc:
default:
if guiLock > 0 {
f = <-guiFunc
} else {
return
}
}
f()
guiDone <- struct{}{}
atomic.AddInt32((*int32)(unsafe.Pointer(&hookWaiting)), -1)
}
}
type valueFold struct {
engine *Engine
gvalue interface{}
cvalue unsafe.Pointer
prev *valueFold
next *valueFold
owner valueOwner
}
func (fold *valueFold) gfield(reflectIndex int) reflect.Value {
v := reflect.ValueOf(fold.gvalue)
for v.Type().Kind() == reflect.Ptr {
v = v.Elem()
}
field := v.Field(reflectIndex)
for {
switch field.Kind() {
case reflect.Ptr, reflect.Interface:
field = field.Elem()
continue
}
return field
}
panic("cannot happen")
}
type valueOwner uint8
const (
cppOwner = 1 << iota
jsOwner
)
// wrapGoValue creates a new GoValue object in C++ land wrapping
// the Go value contained in the given interface.
//
// This must be run from the main GUI thread.
func wrapGoValue(engine *Engine, gvalue interface{}, owner valueOwner) (cvalue unsafe.Pointer) {
gvaluev := reflect.ValueOf(gvalue)
gvaluek := gvaluev.Kind()
if gvaluek == reflect.Struct && !hashable(gvalue) {
name := gvaluev.Type().Name()
if name != "" {
name = " (" + name + ")"
}
panic("cannot hand an unhashable struct value" + name + " to QML logic; use its address instead")
}
if gvaluek == reflect.Ptr && gvaluev.Elem().Kind() == reflect.Ptr {
panic("cannot hand pointer of pointer to QML logic; use a simple pointer instead")
}
prev, ok := engine.values[gvalue]
if ok && (prev.owner == owner || owner != cppOwner) {
return prev.cvalue
}
parent := nilPtr
if owner == cppOwner {
parent = engine.addr
}
fold := &valueFold{
engine: engine,
gvalue: gvalue,
owner: owner,
}
fold.cvalue = C.newGoValue(unsafe.Pointer(fold), typeInfo(gvalue), parent)
if prev != nil {
prev.next = fold
fold.prev = prev
} else {
engine.values[gvalue] = fold
}
stats.valuesAlive(+1)
C.engineSetContextForObject(engine.addr, fold.cvalue)
switch owner {
case cppOwner:
C.engineSetOwnershipCPP(engine.addr, fold.cvalue)
case jsOwner:
C.engineSetOwnershipJS(engine.addr, fold.cvalue)
}
return fold.cvalue
}
// typeNew holds fold values that are created by registered types.
// These values are special in two senses: first, they don't have a
// reference to an engine before they are used in a context that can
// set the reference; second, these values always hold a new cvalue,
// because they are created as a side-effect of the registered type
// being instantiated (it's too late to reuse an existent cvalue).
//
// For these reasons, typeNew holds the fold for these values until
// their engine is known, and once it's known they may have to be
// added to the linked list, since mulitple references for the same
// gvalue may occur.
var typeNew = make(map[*valueFold]bool)
//export hookGoValueTypeNew
func hookGoValueTypeNew(cvalue unsafe.Pointer, specp unsafe.Pointer) (foldp unsafe.Pointer) {
fold := &valueFold{
gvalue: (*TypeSpec)(specp).New(),
cvalue: cvalue,
owner: jsOwner,
}
typeNew[fold] = true
stats.valuesAlive(+1)
return unsafe.Pointer(fold)
}
//export hookGoValueDestroyed
func hookGoValueDestroyed(enginep unsafe.Pointer, foldp unsafe.Pointer) {
fold := (*valueFold)(foldp)
engine := fold.engine
if engine == nil {
before := len(typeNew)
delete(typeNew, fold)
if len(typeNew) == before {
panic("destroying value without an associated engine; who created the value?")
}
} else if engines[engine.addr] == nil {
// Must never do that. The engine holds memory references that C++ depends on.
panic(fmt.Sprintf("engine %p was released from global list while its values were still alive", engine.addr))
} else {
switch {
case fold.prev != nil:
fold.prev.next = fold.next
if fold.next != nil {
fold.next.prev = fold.prev
}
case fold.next != nil:
fold.next.prev = fold.prev
if fold.prev != nil {
fold.prev.next = fold.next
} else {
fold.engine.values[fold.gvalue] = fold.next
}
default:
before := len(engine.values)
delete(engine.values, fold.gvalue)
if len(engine.values) == before {
panic("destroying value that knows about the engine, but the engine doesn't know about the value; who cleared the engine?")
}
if engine.destroyed && len(engine.values) == 0 {
delete(engines, engine.addr)
}
}
}
stats.valuesAlive(-1)
}
//export hookGoValueReadField
func hookGoValueReadField(enginep, foldp unsafe.Pointer, reflectIndex, onChangedIndex C.int, resultdv *C.DataValue) {
fold := ensureEngine(enginep, foldp)
field := fold.gfield(int(reflectIndex))
// Cannot compare Type directly as field may be invalid (nil).
if field.Kind() == reflect.Slice && field.Type() == typeObjSlice {
// TODO Handle other GoValue slices (!= []qml.Object).
resultdv.dataType = C.DTListProperty
*(*unsafe.Pointer)(unsafe.Pointer(&resultdv.data)) = C.newListProperty(foldp, C.intptr_t(reflectIndex), C.intptr_t(onChangedIndex))
return
}
fieldk := field.Kind()
if fieldk == reflect.Slice || fieldk == reflect.Struct && field.Type() != typeRGBA {
if field.CanAddr() {
field = field.Addr()
} else if !hashable(field.Interface()) {
t := reflect.ValueOf(fold.gvalue).Type()
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
panic(fmt.Sprintf("cannot access unaddressable and unhashable struct value on interface field %s.%s; value: %#v", t.Name(), t.Field(int(reflectIndex)).Name, field.Interface()))
}
}
var gvalue interface{}
if field.IsValid() {
gvalue = field.Interface()
}
// TODO Strings are being passed in an unsafe manner here. There is a
// small chance that the field is changed and the garbage collector is run
// before C++ has a chance to look at the data. We can solve this problem
// by queuing up values in a stack, and cleaning the stack when the
// idle timer fires next.
packDataValue(gvalue, resultdv, fold.engine, jsOwner)
}
//export hookGoValueWriteField
func hookGoValueWriteField(enginep, foldp unsafe.Pointer, reflectIndex, onChangedIndex C.int, assigndv *C.DataValue) {
fold := ensureEngine(enginep, foldp)
v := reflect.ValueOf(fold.gvalue)
ve := v
for ve.Type().Kind() == reflect.Ptr {
ve = ve.Elem()
}
field := ve.Field(int(reflectIndex))
assign := unpackDataValue(assigndv, fold.engine)
// TODO Return false to the call site if it fails. That's how Qt seems to handle it internally.
convertAndSet(field, reflect.ValueOf(assign))
if onChangedIndex != -1 {
v.Method(int(onChangedIndex)).Call(nil)
}
}
var listType = reflect.TypeOf(&List{})
func convertAndSet(to, from reflect.Value) {
defer func() {
if v := recover(); v != nil {
// TODO This should be an error. Test and fix.
panic("FIXME attempted to set a field with the wrong type; this should be an error")
}
}()
toType := to.Type()
fromType := from.Type()
if toType == fromType {
to.Set(from)
} else if fromType == listType && to.Kind() == reflect.Slice {
list := from.Interface().(*List)
to.Set(reflect.MakeSlice(toType, len(list.data), len(list.data)))
elemType := toType.Elem()
for i, elem := range list.data {
to.Index(i).Set(reflect.ValueOf(elem).Convert(elemType))
}
} else {
to.Set(from.Convert(toType))
}
}
var (
dataValueSize = uintptr(unsafe.Sizeof(C.DataValue{}))
dataValueArray [C.MaxParams]C.DataValue
)
//export hookGoValueCallMethod
func hookGoValueCallMethod(enginep, foldp unsafe.Pointer, reflectIndex C.int, args *C.DataValue) {
fold := ensureEngine(enginep, foldp)
v := reflect.ValueOf(fold.gvalue)
// TODO Must assert that v is necessarily a pointer here, but we shouldn't have to manipulate
// gvalue here for that. This should happen in a sensible place in the wrapping functions
// that can still error out to the user in due time.
method := v.Method(int(reflectIndex))
methodt := method.Type()
methodName := v.Type().Method(int(reflectIndex)).Name
// TODO Ensure methods with more parameters than this are not registered.
var params [C.MaxParams]reflect.Value
var err error
numIn := methodt.NumIn()
for i := 0; i < numIn; i++ {
paramdv := (*C.DataValue)(unsafe.Pointer(uintptr(unsafe.Pointer(args)) + (uintptr(i)+1)*dataValueSize))
param := reflect.ValueOf(unpackDataValue(paramdv, fold.engine))
if argt := methodt.In(i); param.Type() != argt {
param, err = convertParam(methodName, i, param, argt)
if err != nil {
panic(err.Error())
}
}
params[i] = param
}
result := method.Call(params[:numIn])
if len(result) == 1 {
packDataValue(result[0].Interface(), args, fold.engine, jsOwner)
} else if len(result) > 1 {
if len(result) > len(dataValueArray) {
panic("function has too many results")
}
for i, v := range result {
packDataValue(v.Interface(), &dataValueArray[i], fold.engine, jsOwner)
}
args.dataType = C.DTVariantList
*(*unsafe.Pointer)(unsafe.Pointer(&args.data)) = C.newVariantList(&dataValueArray[0], C.int(len(result)))
}
}
func convertParam(methodName string, index int, param reflect.Value, argt reflect.Type) (newv reflect.Value, err error) {
defer func() {
if panicv := recover(); panicv != nil {
const prefix = "reflect.Value.Convert: "
if s, ok := panicv.(string); ok && strings.HasPrefix(s, prefix) {
err = fmt.Errorf("cannot convert parameter %d of method %s from %s to %s (got %#v)",
index, methodName, param.Type().Name(), argt.Name(), param.Interface())
} else {
panic(panicv)
}
}
}()
return param.Convert(argt), nil
}
func ensureEngine(enginep, foldp unsafe.Pointer) *valueFold {
fold := (*valueFold)(foldp)
if fold.engine != nil {
return fold
}
if enginep == nilPtr {
panic("accessing value without an engine pointer; who created the value?")
}
engine := engines[enginep]
if engine == nil {
panic("unknown engine pointer; who created the engine?")
}
fold.engine = engine
prev := engine.values[fold.gvalue]
if prev != nil {
for prev.next != nil {
prev = prev.next
}
prev.next = fold
fold.prev = prev
} else {
engine.values[fold.gvalue] = fold
}
before := len(typeNew)
delete(typeNew, fold)
if len(typeNew) == before {
panic("value had no engine, but was not created by a registered type; who created the value?")
}
return fold
}
//export hookPanic
func hookPanic(message *C.char) {
defer C.free(unsafe.Pointer(message))
panic(C.GoString(message))
}
//export hookListPropertyAt
func hookListPropertyAt(foldp unsafe.Pointer, reflectIndex, onChangedIndex C.intptr_t, index C.int) (objp unsafe.Pointer) {
fold := (*valueFold)(foldp)
field := fold.gfield(int(reflectIndex))
slice := field.Addr().Interface().(*[]Object)
return (*slice)[int(index)].Common().addr
}
//export hookListPropertyCount
func hookListPropertyCount(foldp unsafe.Pointer, reflectIndex, onChangedIndex C.intptr_t) C.int {
fold := (*valueFold)(foldp)
field := fold.gfield(int(reflectIndex))
slice := field.Addr().Interface().(*[]Object)
return C.int(len(*slice))
}
//export hookListPropertyAppend
func hookListPropertyAppend(foldp unsafe.Pointer, reflectIndex, onChangedIndex C.intptr_t, objp unsafe.Pointer) {
fold := (*valueFold)(foldp)
field := fold.gfield(int(reflectIndex))
slice := field.Addr().Interface().(*[]Object)
var objdv C.DataValue
objdv.dataType = C.DTObject
*(*unsafe.Pointer)(unsafe.Pointer(&objdv.data)) = objp
*slice = append(*slice, unpackDataValue(&objdv, fold.engine).(Object))
if onChangedIndex != -1 {
// TODO Must probably dereference the ptr here. Test it.
reflect.ValueOf(fold.gvalue).Method(int(onChangedIndex)).Call(nil)
}
}
//export hookListPropertyClear
func hookListPropertyClear(foldp unsafe.Pointer, reflectIndex, onChangedIndex C.intptr_t) {
fold := (*valueFold)(foldp)
field := fold.gfield(int(reflectIndex))
slice := field.Addr().Interface().(*[]Object)
for i := range (*slice) {
(*slice)[i] = nil
}
*slice = (*slice)[0:0]
if onChangedIndex != -1 {
// TODO Must probably dereference the ptr here. Test it.
reflect.ValueOf(fold.gvalue).Method(int(onChangedIndex)).Call(nil)
}
}