forked from go-qml/qml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.go
407 lines (368 loc) · 11.1 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
package qml
// #cgo CPPFLAGS: -I/usr/include/qt5/QtCore/5.0.2/QtCore -I./cpp
// #cgo CXXFLAGS: -std=c++11
// #cgo pkg-config: Qt5Core Qt5Widgets Qt5Quick glib-2.0
// #cgo LDFLAGS: -lstdc++
//
// #include "cpp/capi.h"
//
import "C"
import (
"fmt"
"github.com/niemeyer/qml/tref"
"reflect"
"runtime"
"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()
guiLoopReady.Unlock()
C.newGuiApplication()
C.startIdleTimer(&hookWaiting)
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.
atomic.AddInt32((*int32)(unsafe.Pointer(&hookWaiting)), 1)
// 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")
}
found := false
gui(func() {
tinfo := typeInfo(value)
for _, engine := range engines {
fold := engine.values[value]
for fold != nil {
found = true
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 {
found = true
// 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))
}
}
}
})
if !found {
// TODO Perhaps return an error instead.
panic("value is not known")
}
}
// 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
}
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) {
// TODO Return an error if gvalue is a non-basic type and not a pointer.
// Pointer-to-pointer is also not okay.
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 C.int, resultdv *C.DataValue) {
fold := ensureEngine(enginep, foldp)
v := reflect.ValueOf(fold.gvalue)
for v.Type().Kind() == reflect.Ptr {
v = v.Elem()
}
field := v.Field(int(reflectIndex))
// 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(field.Interface(), resultdv, fold.engine, jsOwner)
}
//export hookGoValueWriteField
func hookGoValueWriteField(enginep, foldp unsafe.Pointer, reflectIndex C.int, assigndv *C.DataValue) {
fold := ensureEngine(enginep, foldp)
v := reflect.ValueOf(fold.gvalue)
for v.Type().Kind() == reflect.Ptr {
v = v.Elem()
}
field := v.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))
}
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")
}
}()
to.Set(from.Convert(to.Type()))
}
var (
dataValueSize = uintptr(unsafe.Sizeof(C.DataValue{}))
dataValueArray [C.MaximumParamCount - 1]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))
// TODO Ensure methods with more parameters than this are not registered.
var params [C.MaximumParamCount - 1]reflect.Value
numIn := uintptr(method.Type().NumIn())
for i := uintptr(0); i < numIn; i++ {
// TODO Convert the arguments when possible (int32 => int, etc).
// TODO Type checking to avoid explosions (or catch the explosion)
paramdv := (*C.DataValue)(unsafe.Pointer(uintptr(unsafe.Pointer(args)) + (i+1)*dataValueSize))
params[i] = reflect.ValueOf(unpackDataValue(paramdv, fold.engine))
}
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.DTList
*(*unsafe.Pointer)(unsafe.Pointer(&args.data)) = C.newVariantList(&dataValueArray[0], C.int(len(result)))
}
}
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
}