Skip to content

Commit

Permalink
codegen: fix build crash
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Nov 9, 2023
1 parent 4601e10 commit ca3ff2c
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions cmd/codegen/gengo_typedefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ func new%[1]sFromC(cvalue *C.%[6]s) *%[1]s {
case IsCallbackTypedef(typedefs.data[k]):
glg.Infof("typedef %s is a callback. Not implemented yet", k)
case HasPrefix(typedefs.data[k], "struct"):
glg.Infof("typedef %s is an opaque struct.", k)
writeOpaqueStruct(k, callbacksGoSb)
isOpaque := !IsStructName(k, structs)
glg.Infof("typedef %s is a struct (is opaque? %v).", k, isOpaque)
writeOpaqueStruct(k, isOpaque, callbacksGoSb)
validTypeNames = append(validTypeNames, k)
}
}
Expand All @@ -200,7 +201,18 @@ func new%[1]sFromC(cvalue *C.%[6]s) *%[1]s {
return validTypeNames, nil
}

func writeOpaqueStruct(name CIdentifier, sb *strings.Builder) {
func writeOpaqueStruct(name CIdentifier, isOpaque bool, sb *strings.Builder) {
// this will be put only for structs that are NOT opaque (w can know the exact definition)
var toPlainValue string
if !isOpaque {
toPlainValue = fmt.Sprintf(`
func (self %[1]s) c() (C.%[2]s, func()) {
result, fn := self.handle()
return *result, fn
}
`, name.renameGoIdentifier(), name)
}

// we need to make it a struct, because we need to hide C type (otherwise it will duplicate methods)
fmt.Fprintf(sb, `
type %[1]s struct {
Expand All @@ -211,15 +223,12 @@ func (self *%[1]s) handle() (result *C.%[2]s, fin func()) {
return self.data, func() {}
}
func (self %[1]s) c() (C.%[2]s, func()) {
result, fn := self.handle()
return *result, fn
}
%[3]s
func new%[1]sFromC(cvalue *C.%[2]s) *%[1]s {
return &%[1]s{data: cvalue}
}
`, name.renameGoIdentifier(), name)
`, name.renameGoIdentifier(), name, toPlainValue)
}

func IsStructName(name CIdentifier, structs []StructDef) bool {
Expand Down

0 comments on commit ca3ff2c

Please sign in to comment.