Skip to content

Commit

Permalink
cgo: fix calling CGo callback inside generic function
Browse files Browse the repository at this point in the history
The package of the generic function might not be set, so we have to call
Origin() for it.

Found while porting mcufont to TinyGo.
  • Loading branch information
aykevl authored and deadprogram committed Jan 12, 2024
1 parent a40b11f commit d0445d6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3261,7 +3261,11 @@ func (b *builder) createUnOp(unop *ssa.UnOp) (llvm.Value, error) {
// Instead of a load from the global, create a bitcast of the
// function pointer itself.
name := strings.TrimSuffix(unop.X.(*ssa.Global).Name(), "$funcaddr")
_, fn := b.getFunction(b.fn.Pkg.Members[name].(*ssa.Function))
pkg := b.fn.Pkg
if pkg == nil {
pkg = b.fn.Origin().Pkg
}
_, fn := b.getFunction(pkg.Members[name].(*ssa.Function))
if fn.IsNil() {
return llvm.Value{}, b.makeError(unop.Pos(), "cgo function not found: "+name)
}
Expand Down
9 changes: 9 additions & 0 deletions testdata/cgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func main() {
println("callback 1:", C.doCallback(20, 30, cb))
cb = C.binop_t(C.mul)
println("callback 2:", C.doCallback(20, 30, cb))
genericCallbackCall[int]()

// variadic functions
println("variadic0:", C.variadic0())
Expand Down Expand Up @@ -197,3 +198,11 @@ func printBitfield(bitfield *C.bitfield_t) {
println("bitfield d:", bitfield.d)
println("bitfield e:", bitfield.e)
}

type Int interface {
int | uint
}

func genericCallbackCall[T Int]() {
println("callback inside generic function:", C.doCallback(20, 30, C.binop_t(C.add)))
}
1 change: 1 addition & 0 deletions testdata/cgo/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defined expr: 9
25: 25
callback 1: 50
callback 2: 600
callback inside generic function: 50
variadic0: 1
variadic2: 15
headerfunc: 6
Expand Down

0 comments on commit d0445d6

Please sign in to comment.