-
Notifications
You must be signed in to change notification settings - Fork 51
/
gengo_callbacks.go
478 lines (416 loc) · 11.3 KB
/
gengo_callbacks.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
package main
import (
"errors"
"fmt"
"os"
"regexp"
"sort"
"strings"
"github.com/kpango/glg"
)
type callbackType int
const (
callbackType1 callbackType = iota
callbackType2
)
var callbackNotGeneratedError = errors.New("callback was not generated")
type callbacksGenerator struct {
goSb, cgoSb *strings.Builder
ctx *Context
}
func GenerateCallbacks(callbacks []CIdentifier, context *Context) (validTypes []CIdentifier, err error) {
result := &callbacksGenerator{
goSb: &strings.Builder{},
cgoSb: &strings.Builder{},
ctx: context,
}
validTypes = make([]CIdentifier, 0)
// sort just in case
sort.Slice(callbacks, func(i, j int) bool {
return callbacks[i] < callbacks[j]
})
result.writeHeaer()
for _, typedefName := range callbacks {
if err := result.writeCallback(typedefName, result.ctx.typedefs.data[typedefName]); err != nil {
if errors.Is(err, callbackNotGeneratedError) {
if context.flags.showNotGenerated {
glg.Warnf("Callback \"%s\" was not generated: %v", typedefName, err)
}
continue
}
return nil, err
}
glg.Successf("Callback \"%s\" was generated", typedefName)
validTypes = append(validTypes, typedefName)
}
if err := result.saveToDisk(); err != nil {
return nil, fmt.Errorf("cannot save to disk: %w", err)
}
return validTypes, nil
}
// This is an underlying function for gengo_typedefs.go for now.
// May be reworked to be a separated "genertor" in the future.
// - includes logging
// - returns CallbackNotGeneratedError
func (g *callbacksGenerator) writeCallback(typedefName CIdentifier, def string) error {
// see https://github.com/AllenDang/cimgui-go/issues/224#issuecomment-2452156237
// 1: preprocessing - parse typedefs.data[k] to get return type and arguments
// now let me use a bit of regex.
// We have 2 possibilities:
// - returnType(*<funcName>)(args1 arg1Name, args2 arg2Name, args3 arg3Name);
// - returnType <funcName>(args1 arg1Name, args2 arg2Name, args3 arg3Name);
// NOTE: the second is uesed mainly in immarkdown
// NOTE: in the 1st, spaces does not matter so we'll trim them
expr1, err := regexp.Compile("([a-zA-Z0-9_]+\\*?)\\(\\*.*\\)\\((.*)\\);")
if err != nil {
panic(fmt.Sprintf("Cannot compile regex expr1!: %v", err))
}
expr2, err := regexp.Compile("([a-zA-Z0-9_]+)\\s+([a-zA-Z0-9_]+)\\((.*)\\);")
if err != nil {
panic(fmt.Sprintf("Cannot compile regex expr2!: %v", err))
}
// we need the following from them:
var returnTypeC CIdentifier
argsC := make([]ArgDef, 0)
var cbType callbackType
if ok := expr1.MatchString(def); ok {
cbType = callbackType1
glg.Debugf("callback typedef \"%s\" is in form 1", typedefName)
// now split by "("
// it should be something like this:
// ["returnType", "*<optional func name)", "args1 arg1Name, args2 arg2Name, args3 arg3Name);"]
parts := Split(def, "(")
if len(parts) != 3 {
panic("Cannot split by (, check implementation in cmd/codegen!")
}
returnTypeC = TrimSuffix(CIdentifier(parts[0]), " ")
argsStr := parts[2]
argsStr = TrimSuffix(argsStr, ");")
argsStr = ReplaceAll(argsStr, ", ", ",")
argsStr = ReplaceAll(argsStr, "&", "")
argsListStr := Split(argsStr, ",")
for a, argStr := range argsListStr {
// get name
argParts := Split(argStr, " ")
var name, typeName string
switch len(argParts) {
case 1:
name = fmt.Sprintf("arg%d", a)
typeName = strings.Join(argParts, " ")
case 2: // like "int arg1" or "const int"
if argParts[0] == "const" {
name = fmt.Sprintf("arg%d", a)
typeName = strings.Join(argParts, " ")
break
}
fallthrough
case 3: // something like "int" or "const int arg1"
name = argParts[len(argParts)-1]
typeName = strings.Join(argParts[:len(argParts)-1], " ")
}
argsC = append(argsC, ArgDef{
Name: CIdentifier(name),
Type: CIdentifier(typeName),
})
}
} else if ok := expr2.MatchString(def); ok {
cbType = callbackType2
parts := Split(def, "(")
if len(parts) != 2 {
panic("Cannot split by (, check implementation in cmd/codegen!")
}
returnTypeC = TrimSuffix(CIdentifier(parts[0]), " ")
returnTypeC = TrimSuffix(CIdentifier(parts[0]), fmt.Sprintf(" %s", typedefName))
argsStr := parts[1]
argsStr = TrimSuffix(argsStr, ");")
argsStr = ReplaceAll(argsStr, ", ", ",")
argsStr = ReplaceAll(argsStr, "&", "*")
argsListStr := Split(argsStr, ",")
for a, argStr := range argsListStr {
// get name
argParts := Split(argStr, " ")
var name, typeName string
switch len(argParts) {
case 1:
name = fmt.Sprintf("arg%d", a)
typeName = strings.Join(argParts, " ")
case 2: // like "int arg1" or "const int"
if argParts[0] == "const" {
name = fmt.Sprintf("arg%d", a)
typeName = strings.Join(argParts, " ")
break
}
fallthrough
case 3: // something like "int" or "const int arg1"
name = argParts[len(argParts)-1]
typeName = strings.Join(argParts[:len(argParts)-1], " ")
}
argsC = append(argsC, ArgDef{
Name: CIdentifier(name),
Type: CIdentifier(typeName),
})
}
} else {
if g.ctx.flags.showNotGenerated {
return fmt.Errorf("cannot parse callback typedef: %w", callbackNotGeneratedError)
}
}
// 2: Find wrappers:
// We need to figure out how to wrap returnType and args.
// In fact, we need to swap meaning of them, because we want to convert C argument type to Go argument type
// so we are supposed to use returnWrapper for that.
glg.Debugf("From %s got \"%s\" and \"%v\"", typedefName, returnTypeC, argsC)
// 2.1: get return wrapper
var returnType ArgumentWrapperData
if returnTypeC == "void" {
returnType = ArgumentWrapperData{}
} else {
_, returnType, err = getArgWrapper(
&ArgDef{
Name: "result",
Type: returnTypeC,
},
false, false,
g.ctx,
)
if err != nil {
return fmt.Errorf("unable to get return wrapper %w: %w", callbackNotGeneratedError, err)
}
}
// 2.1: get arg wrappers
args := make([]returnWrapper, len(argsC))
for i, arg := range argsC {
rw, err := getReturnWrapper(arg.Type, g.ctx)
if err != nil {
return fmt.Errorf("unable to get arg wrapper for \"%s\" - \"%s\" %w: %w", typedefName, arg.Type, callbackNotGeneratedError, err)
}
// fill rw return stmt
rw.returnStmt = fmt.Sprintf(rw.returnStmt, arg.Name)
args[i] = rw
}
// 3: Prepare call statement
cCallStmt := ""
goCallStmt := ""
valuePassStmt := ""
for i, arg := range args {
cCallStmt += fmt.Sprintf("%s %s, ", argsC[i].Name, arg.CType)
goCallStmt += fmt.Sprintf("%s %s, ", argsC[i].Name, arg.returnType)
valuePassStmt += fmt.Sprintf("%s, ", argsC[i].Name)
}
cCallStmt = TrimSuffix(cCallStmt, ", ")
goCallStmt = TrimSuffix(goCallStmt, ", ")
valuePassStmt = TrimSuffix(valuePassStmt, ", ")
// 4: Write code
switch cbType {
case callbackType1:
fmt.Fprintf(g.goSb, `
type %[1]s func(%[4]s) %[2]s
type c%[1]s func(%[5]s) %[3]s
func New%[1]sFromC(cvalue *C.%[6]s) *%[1]s {
result := pool%[1]s.Find(*cvalue)
return &result
}
func (c %[1]s) C() (C.%[6]s, func()) {
return pool%[1]s.Allocate(c), func() { }
}
`,
typedefName.renameGoIdentifier(g.ctx),
returnType.ArgType,
returnType.CType,
goCallStmt,
cCallStmt,
typedefName,
)
case callbackType2:
fmt.Fprintf(g.goSb, `
type %[1]s func(%[4]s) %[2]s
type c%[1]s func(%[5]s) %[3]s
func New%[1]sFromC(cvalue *C.%[6]s) *%[1]s {
result := pool%[1]s.Find(cvalue)
return &result
}
func (c %[1]s) Handle() (*C.%[6]s, func()) {
result := pool%[1]s.Allocate(c)
return result, func() {}
}
`,
typedefName.renameGoIdentifier(g.ctx),
returnType.ArgType,
returnType.CType,
goCallStmt,
cCallStmt,
typedefName,
)
}
cCallStmt2 := cCallStmt
if cCallStmt2 != "" {
cCallStmt2 = ", " + cCallStmt2
}
if returnType.ArgType == "" {
fmt.Fprintf(g.goSb, `
func wrap%[1]s(cb %[1]s %[3]s) %[2]s {
cb(%[4]s)
}
`,
typedefName.renameGoIdentifier(g.ctx),
returnType.CType,
cCallStmt2,
func() string {
result := ""
for _, a := range args {
result += a.returnStmt + ", "
}
result = TrimSuffix(result, ", ")
return result
}(),
)
} else {
fmt.Fprintf(g.goSb, `
func wrap%[1]s(cb %[1]s %[5]s) %[2]s {
result := cb(%[6]s)
%[3]s
defer func() {
%[7]s
}()
return %[4]s
}
`,
typedefName.renameGoIdentifier(g.ctx),
returnType.CType,
returnType.ArgDef,
returnType.VarName,
cCallStmt2,
func() string {
result := ""
for _, a := range args {
result += a.returnStmt + ", "
}
result = TrimSuffix(result, ", ")
return result
}(),
returnType.Finalizer,
)
}
size := g.ctx.preset.TypedefsPoolSize
if h, ok := g.ctx.preset.TypedefsCustomPoolSizes[typedefName]; ok {
size = h
}
poolNames := make([]string, size)
// now write N functions
for i := 0; i < size; i++ {
switch cbType {
case callbackType1:
fmt.Fprintf(g.goSb,
`//export callback%[1]s%[2]d
func callback%[1]s%[2]d(%[5]s) %[3]s { %[4]s wrap%[1]s(pool%[1]s.Get(%[2]d), %[6]s) }
`,
typedefName.renameGoIdentifier(g.ctx),
i,
returnType.CType,
func() string {
if returnType.ArgType != "" {
return "return"
}
return ""
}(),
cCallStmt,
valuePassStmt,
)
case callbackType2:
fmt.Fprintf(g.goSb,
`//export callback%[1]s%[2]d
func callback%[1]s%[2]d(%[5]s) %[3]s {%[4]s wrap%[1]s(pool%[1]s.Get(%[2]d), %[6]s) }
`,
typedefName.renameGoIdentifier(g.ctx),
i,
returnType.CType,
func() string {
if returnType.ArgType != "" {
return "return"
}
return ""
}(),
cCallStmt,
valuePassStmt,
)
}
fmt.Fprintf(g.cgoSb,
`// extern %[1]s callback%[2]s%[3]d(%[4]s);
`,
returnTypeC,
typedefName.renameGoIdentifier(g.ctx),
i,
func() string {
result := ""
for _, a := range argsC {
result += TrimPrefix(string(a.Type), "const ") + ", "
}
return TrimSuffix(result, ", ")
return result
}(),
)
switch cbType {
case callbackType1:
poolNames[i] = fmt.Sprintf("C.%[3]s(C.callback%[1]s%[2]d)", typedefName.renameGoIdentifier(g.ctx), i, typedefName)
case callbackType2:
poolNames[i] = fmt.Sprintf("(*C.%[3]s)(C.callback%[1]s%[2]d)", typedefName.renameGoIdentifier(g.ctx), i, typedefName)
}
}
switch cbType {
case callbackType1:
fmt.Fprintf(g.goSb, `
var pool%[1]s *internal.Pool[%[1]s, C.%[3]s]
func init() {
pool%[1]s = internal.NewPool[%[1]s, C.%[3]s](
%[2]s
)
}
func Clear%[1]sPool() {
pool%[1]s.Clear()
}
`,
typedefName.renameGoIdentifier(g.ctx),
strings.Join(poolNames, ",\n")+",\n",
typedefName,
)
case callbackType2:
fmt.Fprintf(g.goSb, `
var pool%[1]s *internal.Pool[%[1]s, *C.%[3]s]
func init() {
pool%[1]s = internal.NewPool[%[1]s, *C.%[3]s](
%[2]s
)
}
func Clear%[1]sPool() {
pool%[1]s.Clear()
}
`,
typedefName.renameGoIdentifier(g.ctx),
strings.Join(poolNames, ",\n")+",\n",
typedefName,
)
}
return nil
}
func (g *callbacksGenerator) writeHeaer() {
}
func (g *callbacksGenerator) saveToDisk() error {
result := &strings.Builder{}
result.WriteString(getGoPackageHeader(g.ctx))
fmt.Fprintf(result,
`// #include <stdlib.h>
// #include <memory.h>
// #include "wrapper.h"
// #include "typedefs.h"
%s
`, g.ctx.preset.MergeCGoPreamble())
fmt.Fprintf(result, g.cgoSb.String())
fmt.Fprintf(result, `import "C"
import "unsafe"
`)
fmt.Fprintf(result, g.goSb.String())
if err := os.WriteFile("callbacks.go", []byte(result.String()), 0644); err != nil {
return fmt.Errorf("cannot write typedefs.go: %w", err)
}
return nil
}