/
gobj.go
631 lines (537 loc) · 18.2 KB
/
gobj.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
package gtk
/*
#cgo pkg-config: gobject-2.0
#include <stdlib.h>
#include <stdint.h>
#include <glib-object.h>
#include <string.h>
#include "gobj.h"
extern uint32_t g_quark_from_string(const char*);
extern void g_object_set_qdata(GObject*, uint32_t, void*);
*/
import "C"
import "unsafe"
import "runtime"
import "reflect"
import "sync"
type Type C.GType
type _Value__data__union struct {
_data [8]byte
}
type Value struct {
GType Type
Data [2]_Value__data__union
}
type Object struct {
C unsafe.Pointer
}
type Quark uint32
// Every GObject generated by this generator implements this interface
// and it must work even if the receiver is a nil value
type StaticTyper interface {
GetStaticType() Type
}
// blacklisted: InterfaceInitFunc (callback)
type ObjectLike interface {
InheritedFromGObject() *C.GObject
}
//--------------------------------------------------------------
// FinalizerQueue
//--------------------------------------------------------------
type finalizer_item struct {
ptr unsafe.Pointer
finalizer func(unsafe.Pointer)
}
type fqueue_type struct {
sync.Mutex
queue []finalizer_item
exec_queue []finalizer_item
tid uint32
}
type holder_key [2]unsafe.Pointer
type holder_type map[holder_key]int
// we use this one to store Go's representation of the GObject
// as user data in that GObject once it was allocated. For the
// sake of avoiding allocations.
var go_repr Quark
var Holder holder_type
var FQueue fqueue_type
var (
Interface Type
Char Type
UChar Type
Boolean Type
Int Type
UInt Type
Long Type
ULong Type
Int64 Type
UInt64 Type
Enum Type
Flags Type
Float Type
Double Type
String Type
Pointer Type
Boxed Type
Param Type
GObject Type
GType Type
Variant Type
GoInterface Type
)
func init() {
// C.g_type_init()
Interface = Type(C._g_type_interface())
Char = Type(C._g_type_char())
UChar = Type(C._g_type_uchar())
Boolean = Type(C._g_type_boolean())
Int = Type(C._g_type_int())
UInt = Type(C._g_type_uint())
Long = Type(C._g_type_long())
ULong = Type(C._g_type_ulong())
Int64 = Type(C._g_type_int64())
UInt64 = Type(C._g_type_uint64())
Enum = Type(C._g_type_enum())
Flags = Type(C._g_type_flags())
Float = Type(C._g_type_float())
Double = Type(C._g_type_double())
String = Type(C._g_type_string())
Pointer = Type(C._g_type_pointer())
Boxed = Type(C._g_type_boxed())
Param = Type(C._g_type_param())
GObject = Type(C._g_type_object())
GType = Type(C._g_type_gtype())
Variant = Type(C._g_type_variant())
GoInterface = Type(C._g_type_go_interface())
// Holder------------------
Holder = holder_type(make(map[holder_key]int))
go_repr = NewQuarkFromString("go-representation")
}
const alot = 999999
func _GoStringToGString(x string) *C.char {
if x == "\x00" {
return nil
}
return C.CString(x)
}
func _GoBoolToCBool(x bool) C.int {
if x {
return 1
}
return 0
}
func _CInterfaceToGoInterface(iface [2]unsafe.Pointer) interface{} {
return *(*interface{})(unsafe.Pointer(&iface))
}
func _GoInterfaceToCInterface(iface interface{}) *unsafe.Pointer {
return (*unsafe.Pointer)(unsafe.Pointer(&iface))
}
//export _GObject_go_callback_cleanup
func _GObject_go_callback_cleanup(gofunc unsafe.Pointer) {
Holder.Release(gofunc)
}
//--------------------------------------------------------------
// Holder
//--------------------------------------------------------------
// holy crap, what am I doing here..
func (this holder_type) Grab(x interface{}) {
if x == nil {
return
}
key := *(*holder_key)(unsafe.Pointer(&x))
count := this[key]
this[key] = count + 1
}
func (this holder_type) Release(x interface{}) {
if x == nil {
return
}
key := *(*holder_key)(unsafe.Pointer(&x))
count := this[key]
if count <= 1 {
delete(this, key)
} else {
this[key] = count - 1
}
}
//--------------------------------------------------------------
// Closures
//--------------------------------------------------------------
//export gGoclosureFinalizeGo
func gGoclosureFinalizeGo(goclosure_up unsafe.Pointer) {
goclosure := (*C.GGoClosure)(goclosure_up)
clo := *(*interface{})(C.gGoclosureGetFunc(goclosure))
recv := *(*interface{})(C.gGoclosureGetRecv(goclosure))
Holder.Release(clo)
Holder.Release(recv)
}
//export gGoclosureMarshalGo
func gGoclosureMarshalGo(goclosure_up, ret_up unsafe.Pointer, nargs int32, args_up unsafe.Pointer) {
var callargs [20]reflect.Value
var recv reflect.Value
goclosure := (*C.GGoClosure)(goclosure_up)
ret := (*Value)(ret_up)
args := (*(*[alot]Value)(args_up))[:nargs]
f := reflect.ValueOf(*(*interface{})(C.gGoclosureGetFunc(goclosure)))
ft := f.Type()
callargsn := ft.NumIn()
recvi := *(*interface{})(C.gGoclosureGetRecv(goclosure))
if recvi != nil {
recv = reflect.ValueOf(recvi)
}
if callargsn >= 20 {
panic("too many arguments in a closure")
}
for i, n := 0, callargsn; i < n; i++ {
idx := i
if recvi != nil {
idx--
if i == 0 {
callargs[i] = recv
continue
}
}
in := ft.In(i)
// use default value, if there is not enough args
if len(args) <= idx {
callargs[i] = reflect.New(in).Elem()
continue
}
v := args[idx].GetGoValue(in)
callargs[i] = v
}
out := f.Call(callargs[:callargsn])
if len(out) == 1 {
ret.SetGoValue(out[0])
}
}
//--------------------------------------------------------------
// Value
//--------------------------------------------------------------
func (this *Value) asC() *C.GValue {
return (*C.GValue)(unsafe.Pointer(this))
}
// g_value_init
func (this *Value) Init(t Type) {
C.g_value_init(this.asC(), t.asC())
}
// g_value_copy
func (this *Value) Set(src *Value) {
C.g_value_copy(src.asC(), this.asC())
}
// g_value_reset
func (this *Value) Reset() {
C.g_value_reset(this.asC())
}
// g_value_unset
func (this *Value) Unset() {
C.g_value_unset(this.asC())
}
// G_VALUE_TYPE
func (this *Value) GetType() Type {
return Type(C._g_value_type(this.asC()))
}
// g_value_type_compatible
func ValueTypeCompatible(src, dst Type) bool {
return C.g_value_type_compatible(src.asC(), dst.asC()) != 0
}
// g_value_type_transformable
func ValueTypeTransformable(src, dst Type) bool {
return C.g_value_type_transformable(src.asC(), dst.asC()) != 0
}
// g_value_transform
func (this *Value) Transform(src *Value) bool {
return C.g_value_transform(src.asC(), this.asC()) != 0
}
// g_value_get_boolean
func (this *Value) GetBool() bool {
return C.g_value_get_boolean(this.asC()) != 0
}
// g_value_set_boolean
func (this *Value) SetBool(v bool) {
C.g_value_set_boolean(this.asC(), C.gboolean(_GoBoolToCBool(v)))
}
// g_value_get_int64
func (this *Value) GetInt() int64 {
return int64(C.g_value_get_int64(this.asC()))
}
// g_value_set_int64
func (this *Value) SetInt(v int64) {
C.g_value_set_int64(this.asC(), C.gint64(v))
}
// g_value_get_uint64
func (this *Value) GetUint() uint64 {
return uint64(C.g_value_get_uint64(this.asC()))
}
// g_value_set_uint64
func (this *Value) SetUint(v uint64) {
C.g_value_set_uint64(this.asC(), C.guint64(v))
}
// g_value_get_double
func (this *Value) GetFloat() float64 {
return float64(C.g_value_get_double(this.asC()))
}
// g_value_set_double
func (this *Value) SetFloat(v float64) {
C.g_value_set_double(this.asC(), C.gdouble(v))
}
// g_value_get_string
func (this *Value) GetString() string {
return C.GoString((*C.char)(C.g_value_get_string(this.asC())))
}
// g_value_take_string
func (this *Value) SetString(v string) {
cstr := _GString(v)
C.g_value_take_string(this.asC(), cstr)
// not freeing, because GValue takes the ownership
}
// g_value_get_object
func (this *Value) GetObject() unsafe.Pointer {
return unsafe.Pointer(C.g_value_get_object(this.asC()))
}
// g_value_set_object
func (this *Value) SetObject(x unsafe.Pointer) {
C.g_value_set_object(this.asC(), C.gpointer(x))
}
// g_value_get_boxed
func (this *Value) GetBoxed() unsafe.Pointer {
return unsafe.Pointer(C.g_value_get_boxed(this.asC()))
}
// g_value_take_boxed
func (this *Value) SetBoxed(x unsafe.Pointer) {
C.g_value_take_boxed(this.asC(), C.gconstpointer(x))
}
func (this *Value) GetBoxedInterface() interface{} {
return *(*interface{})(C.g_value_get_boxed(this.asC()))
}
func (this *Value) SetBoxedInterface(x interface{}) {
Holder.Grab(x)
newboxed := C.malloc(C.size_t(unsafe.Sizeof([2]unsafe.Pointer{})))
C.memcpy(newboxed, unsafe.Pointer(&x), C.size_t(unsafe.Sizeof([2]unsafe.Pointer{})))
C.g_value_take_boxed(this.asC(), C.gconstpointer(newboxed))
}
//--------------------------------------------------------------
// A giant glue for connecting GType and Go's reflection
//--------------------------------------------------------------
var statictyper = reflect.TypeOf((*StaticTyper)(nil)).Elem()
var objectlike = reflect.TypeOf((*ObjectLike)(nil)).Elem()
func (this *Value) SetGoValue(v reflect.Value) {
valuetype := this.GetType()
var src Value
if valuetype == GoInterface {
// special case
this.SetBoxedInterface(v.Interface())
return
}
transform := func() {
ok := this.Transform(&src)
if !ok {
panic("Go value (" + v.Type().String() + ") is not transformable to " + valuetype.String())
}
}
switch v.Kind() {
case reflect.Bool:
src.Init(Boolean)
src.SetBool(v.Bool())
transform()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
src.Init(Int64)
src.SetInt(v.Int())
transform()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
src.Init(UInt64)
src.SetUint(v.Uint())
transform()
case reflect.Float32, reflect.Float64:
src.Init(Double)
src.SetFloat(v.Float())
transform()
case reflect.String:
src.Init(String)
src.SetString(v.String())
transform()
src.Unset()
case reflect.Ptr:
gotype := v.Type()
src.Init(GObject)
if gotype.Implements(objectlike) {
obj, ok := v.Interface().(ObjectLike)
if !ok {
panic(gotype.String() + " is not transformable to GValue")
}
src.SetObject(unsafe.Pointer(obj.InheritedFromGObject()))
transform()
}
src.Unset()
}
}
var CairoMarshaler func(*Value, reflect.Type) (reflect.Value, bool)
func (this *Value) GetGoValue(t reflect.Type) reflect.Value {
var out reflect.Value
var dst Value
if this.GetType() == GoInterface {
return reflect.ValueOf(this.GetBoxedInterface())
}
transform := func() {
ok := dst.Transform(this)
if !ok {
panic("GValue is not transformable to " + t.String())
}
}
switch t.Kind() {
case reflect.Bool:
dst.Init(Boolean)
transform()
out = reflect.New(t).Elem()
out.SetBool(dst.GetBool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
dst.Init(Int64)
transform()
out = reflect.New(t).Elem()
out.SetInt(dst.GetInt())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
dst.Init(UInt64)
transform()
out = reflect.New(t).Elem()
out.SetUint(dst.GetUint())
case reflect.Float32, reflect.Float64:
dst.Init(Double)
transform()
out = reflect.New(t).Elem()
out.SetFloat(dst.GetFloat())
case reflect.String:
dst.Init(String)
transform()
out = reflect.New(t).Elem()
out.SetString(dst.GetString())
dst.Unset() // need to clean up in this case
case reflect.Ptr:
if t.Implements(objectlike) {
// at this point we're sure that this is a pointer to the ObjectLike
out = reflect.New(t)
st, ok := out.Elem().Interface().(StaticTyper)
if !ok {
panic("ObjectLike type must implement StaticTyper as well")
}
dst.Init(st.GetStaticType())
transform()
*(*unsafe.Pointer)(unsafe.Pointer(out.Pointer())) = ObjectWrap(dst.GetObject(), true)
dst.Unset()
out = out.Elem()
} else {
// cairo marshaler hook
if CairoMarshaler != nil {
var ok bool
out, ok = CairoMarshaler(this, t)
if ok {
break
}
}
// must be a struct then
out = reflect.New(t)
*(*unsafe.Pointer)(unsafe.Pointer(out.Pointer())) = this.GetBoxed()
out = out.Elem()
}
}
return out
}
func (this *Value) SetGoInterface(v interface{}) {
this.SetGoValue(reflect.ValueOf(v))
}
func (this *Value) GetGoInterface(v interface{}) {
vp := reflect.ValueOf(v)
if vp.Kind() != reflect.Ptr {
panic("a pointer to value is expected for Value.GetGoInterface")
}
vp.Elem().Set(this.GetGoValue(vp.Type().Elem()))
}
//--------------------------------------------------------------
// Type
//--------------------------------------------------------------
func (this Type) IsA(other Type) bool {
return C.g_type_is_a(C.GType(this), C.GType(other)) != 0
}
func (this Type) String() string {
cname := C.g_type_name(C.GType(this))
if cname == nil {
return ""
}
return C.GoString((*C.char)(cname))
}
func (this Type) asC() C.GType {
return C.GType(this)
}
//--------------------------------------------------------------
// Go Interface boxed type
//--------------------------------------------------------------
//export gGoInterfaceCopyGo
func gGoInterfaceCopyGo(boxed unsafe.Pointer) unsafe.Pointer {
Holder.Grab(*(*interface{})(boxed))
newboxed := C.malloc(C.size_t(unsafe.Sizeof([2]unsafe.Pointer{})))
C.memcpy(newboxed, boxed, C.size_t(unsafe.Sizeof([2]unsafe.Pointer{})))
return newboxed
}
//export gGoInterfaceFreeGo
func gGoInterfaceFreeGo(boxed unsafe.Pointer) {
Holder.Release(*(*interface{})(boxed))
C.free(boxed)
}
func ObjectWrap(c unsafe.Pointer, grab bool) unsafe.Pointer {
if c == nil {
return nil
}
obj := (*Object)(C.g_object_get_qdata((*C.GObject)(c), C.GQuark(go_repr)))
if obj != nil {
return unsafe.Pointer(obj)
}
obj = &Object{c}
if grab {
C.g_object_ref_sink(C.gpointer(obj.C))
}
setObjectFinalizer(obj)
C.g_object_set_qdata((*C.GObject)(obj.C),
C.GQuark(go_repr), unsafe.Pointer(obj))
return unsafe.Pointer(obj)
}
func setObjectFinalizer(obj *Object) {
runtime.SetFinalizer(obj, objectFinalizer)
}
//--------------------------------------------------------------
// Quark
//
// TODO: probably it's a temporary place for this, quarks are
// from glib
//--------------------------------------------------------------
func NewQuarkFromString(s string) Quark {
cs := _GString(s)
quark := C.g_quark_from_string(cs)
_GFree(unsafe.Pointer(cs))
return Quark(quark)
}
//--------------------------------------------------------------
// Object
//--------------------------------------------------------------
func objectFinalizer(obj *Object) {
if FQueue.Push(unsafe.Pointer(obj), objectFinalizer2) {
return
}
C.g_object_set_qdata((*C.GObject)(obj.C), C.GQuark(go_repr), nil)
C.g_object_unref(C.gpointer(obj.C))
}
func objectFinalizer2(objc unsafe.Pointer) {
obj := (*Object)(objc)
C.g_object_set_qdata((*C.GObject)(obj.C), C.GQuark(go_repr), nil)
C.g_object_unref(C.gpointer(obj.C))
}
// returns true if the item was enqueued, thread safe
func (this *fqueue_type) Push(ptr unsafe.Pointer, finalizer func(unsafe.Pointer)) bool {
this.Lock()
if this.queue != nil {
this.queue = append(this.queue, finalizer_item{ptr, finalizer})
this.Unlock()
return true
}
this.Unlock()
return false
}