Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,9 @@ func RegisterFunc(fptr any, cfn uintptr) {
v = reflect.NewAt(outType, unsafe.Pointer(&a1)).Elem()
case reflect.Func:
// wrap this C function in a nicely typed Go function
v = reflect.New(outType)
RegisterFunc(v.Interface(), syscall.a1)
if syscall.a1 != 0 {
RegisterFunc(v.Addr().Interface(), syscall.a1)
}
case reflect.String:
v.SetString(strings.GoString(syscall.a1))
case reflect.Float32:
Expand Down
18 changes: 18 additions & 0 deletions func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,24 @@ func TestABI(t *testing.T) {
t.Fatalf("%s: got %q, want %q", cName, res, want)
}
}
{
const cName = "return_func_ptr"
var fn func() func(a, b int32) int32
purego.RegisterLibFunc(&fn, lib, cName)
add := fn()
const expect = 5
if res := add(2, 3); res != expect {
t.Fatalf("%s: got %d, want %d", cName, res, expect)
}
}
{
const cName = "return_null_func_ptr"
var fn func() func(a, b int32) int32
purego.RegisterLibFunc(&fn, lib, cName)
if fn() != nil {
t.Fatalf("%s: got a non-nil func, want nil", cName)
}
}
}

func TestABI_ArgumentPassing(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions testdata/abitest/abi_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,17 @@ double arm_float64_unaligned_on_stack(uintptr_t a1, uintptr_t a2, uintptr_t a3,
return (double)a1 * 1 + (double)a2 * 2 + (double)a3 * 3 + (double)a4 * 4 +
(double)a5 * 5 + a6;
}

typedef int32_t (*AddFunc)(int32_t, int32_t);

int32_t returned_add(int32_t a, int32_t b) {
return a + b;
}

AddFunc return_func_ptr(void) {
return returned_add;
}

AddFunc return_null_func_ptr(void) {
return NULL;
}