-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.go
174 lines (140 loc) · 3.58 KB
/
templates.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
package main
const g_object_ref_unref = `extern GObject *g_object_ref_sink(GObject*);
extern void g_object_unref(GObject*);`
const g_error_free = `extern void g_error_free(GError*);`
const g_free = `extern void g_free(void*);`
const g_list_funcs = `
GList* g_list_append(GList*, void*);
void g_list_free(GList*);
`
var go_utils_template = must_template(`
const alot = 999999
type _GSList struct {
data unsafe.Pointer
next *_GSList
}
type _GList struct {
data unsafe.Pointer
next *_GList
prev *_GList
}
type _GError struct {
domain uint32
code int32
message *C.char
}
func (e _GError) ToGError() GError {
return GError{e.domain, e.code, C.GoString(e.message)}
}
type GError struct {
Domain uint32
Code int32
Message string
}
func (e GError) Error() string {
return e.Message
}
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))
}
[<if not .nocallbacks>]
//export _[<.namespace>]_go_callback_cleanup
func _[<.namespace>]_go_callback_cleanup(gofunc unsafe.Pointer) {
[<.gobjectns>]Holder.Release(gofunc)
}
[<end>]
`)
var object_template = must_template(`
type [<.name>]Like interface {
[<.parentlike>]
InheritedFrom[<.cprefix>][<.name>]() [<.cgotype>]
}
type [<.name>] struct {
[<.parent>]
[<.interfaces>]
}
func To[<.name>](objlike [<.gobjectns>]ObjectLike) *[<.name>] {
c := objlike.InheritedFromGObject()
if c == nil {
return nil
}
t := (*[<.name>])(nil).GetStaticType()
obj := [<.gobjectns>]ObjectGrabIfType(unsafe.Pointer(c), t)
if obj != nil {
return (*[<.name>])(obj)
}
panic("cannot cast to [<.name>]")
}
func (this0 *[<.name>]) InheritedFrom[<.cprefix>][<.name>]() [<.cgotype>] {
if this0 == nil {
return nil
}
return ([<.cgotype>])(this0.C)
}
func (this0 *[<.name>]) GetStaticType() [<.gobjectns>]Type {
return [<.gobjectns>]Type(C.[<.typeinit>]())
}
func [<.name>]GetType() [<.gobjectns>]Type {
return (*[<.name>])(nil).GetStaticType()
}
`)
// XXX: uses gc specific hack, expect problems on gccgo and/or ask developers
// about the address of an empty embedded struct
var interface_template = must_template(`
type [<.name>]Like interface {
Implements[<.cprefix>][<.name>]() [<.cgotype>]
}
type [<.name>] struct {
[<.gobjectns>]Object
[<.name>]Impl
}
func (*[<.name>]) GetStaticType() [<.gobjectns>]Type {
return [<.gobjectns>]Type(C.[<.typeinit>]())
}
type [<.name>]Impl struct {}
func To[<.name>](objlike [<.gobjectns>]ObjectLike) *[<.name>] {
c := objlike.InheritedFromGObject()
obj := [<.gobjectns>]ObjectGrabIfType(unsafe.Pointer(c), [<.gobjectns>]Type(C.[<.typeinit>]()))
if obj != nil {
return (*[<.name>])(obj)
}
panic("cannot cast to [<.name>]")
}
func (this0 *[<.name>]Impl) Implements[<.cprefix>][<.name>]() [<.cgotype>] {
obj := uintptr(unsafe.Pointer(this0)) - unsafe.Sizeof(uintptr(0))
return ([<.cgotype>])((*[<.gobjectns>]Object)(unsafe.Pointer(obj)).C)
}
`)
const c_header = `#pragma once
#include <stdlib.h>
#include <stdint.h>
typedef size_t GType;
typedef void *GVaClosureMarshal;
static unsigned int _array_length(void* _array)
{
void** array = (void**)_array;
unsigned int i=0;
while (array && array[i] != 0) i++;
return i;
}
`
var c_template = must_template(`
#include "[<.package>].gen.h"
static void _c_callback_cleanup(void *userdata)
{
_[<.namespace>]_go_callback_cleanup(userdata);
}
`)