-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample_test.go
147 lines (131 loc) · 2.92 KB
/
example_test.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
package bindgen_test
import (
"fmt"
"strings"
"github.com/gorgonia/bindgen"
"modernc.org/cc"
)
// GoSignature represents a Go signature
type GoSignature struct {
Receiver Param
Name string
Params []Param
Ret []Param
}
func (sig *GoSignature) Format(f fmt.State, c rune) {
f.Write([]byte("func "))
if sig.Receiver.Name != "" {
fmt.Fprintf(f, "(%v %v) ", sig.Receiver.Name, sig.Receiver.Type)
}
fmt.Fprintf(f, "%v(", sig.Name)
for i, p := range sig.Params {
fmt.Fprintf(f, "%v %v", p.Name, p.Type)
if i < len(sig.Params)-1 {
fmt.Fprint(f, ", ")
}
}
fmt.Fprint(f, ")")
switch len(sig.Ret) {
case 0:
return
default:
fmt.Fprint(f, " (")
for i, p := range sig.Ret {
fmt.Fprintf(f, "%v %v", p.Name, p.Type)
if i < len(sig.Ret)-1 {
fmt.Fprint(f, ", ")
}
}
fmt.Fprint(f, ")")
}
}
// Param represents the parameters in Go
type Param struct {
Name, Type string
}
// functions say we only want functions declared
func functions(t *cc.TranslationUnit) ([]bindgen.Declaration, error) {
filter := func(d *cc.Declarator) bool {
if !strings.HasPrefix(bindgen.NameOf(d), "func") {
return false
}
if d.Type.Kind() != cc.Function {
return false
}
return true
}
return bindgen.Get(t, filter)
}
func decl2GoSig(d *bindgen.CSignature) *GoSignature {
var params []Param
sig := new(GoSignature)
outer:
for _, p := range d.Parameters() {
// check if its a receiver
if ctxrec, ok := contextualFns[d.Name]; ok {
if ctxrec.Name == p.Name() {
sig.Receiver = ctxrec
continue
}
}
Type := cleanType(p.Type())
if retP, ok := retVals[d.Name]; ok {
for _, r := range retP {
if p.Name() == r {
sig.Ret = append(sig.Ret, Param{p.Name(), Type})
continue outer
}
}
}
params = append(params, Param{p.Name(), Type})
}
retType := cleanType(d.Return)
if !bindgen.IsVoid(d.Return) {
sig.Ret = append(sig.Ret, Param{"err", retType})
}
sig.Name = d.Name
sig.Params = params
return sig
}
func cleanType(t cc.Type) string {
Type := t.String()
if td := bindgen.TypeDefOf(t); td != "" {
Type = td
}
if bindgen.IsConstType(t) {
Type = strings.TrimPrefix(Type, "const ")
}
if bindgen.IsPointer(t) {
Type = strings.TrimSuffix(Type, "*")
}
return Type
}
var contextualFns = map[string]Param{
"funcCtx": Param{"ctx", "Ctx"},
}
var retVals = map[string][]string{
"funcErr": []string{"retVal"},
"funcCtx": []string{"retVal"},
}
func handleErr(err error) {
if err != nil {
panic(err)
}
}
func Example_simple() {
t, err := bindgen.Parse(bindgen.Model(), "testdata/dummy.h")
handleErr(err)
fns, err := functions(t)
handleErr(err)
for _, fn := range fns {
fmt.Println(decl2GoSig(fn.(*bindgen.CSignature)))
}
// Output:
// func func1i(a int)
// func func1f(a foo)
// func func1fp(a foo)
// func func2i(a int, b int)
// func func2f(a foo, b int)
// func funcErr(a int) (retVal foo, err error)
// func (ctx Ctx) funcCtx(a foo) (retVal foo, err error)
}