diff options
author | Ayke van Laethem <[email protected]> | 2019-02-07 13:50:09 +0100 |
---|---|---|
committer | Ayke van Laethem <[email protected]> | 2019-02-08 13:19:02 +0100 |
commit | 95d895646a8005dc2afed425061865034a73450b (patch) | |
tree | e58fbd5cd10c14c580cdbed9cde617b90f953be8 /testdata | |
parent | 35fb594f8f75129a1839b25e96cf82fa3357a0e0 (diff) | |
download | tinygo-95d895646a8005dc2afed425061865034a73450b.tar.gz tinygo-95d895646a8005dc2afed425061865034a73450b.zip |
loader/cgo: add support for function pointers
Diffstat (limited to 'testdata')
-rw-r--r-- | testdata/cgo/main.c | 4 | ||||
-rw-r--r-- | testdata/cgo/main.go | 10 | ||||
-rw-r--r-- | testdata/cgo/main.h | 2 | ||||
-rw-r--r-- | testdata/cgo/out.txt | 2 |
4 files changed, 18 insertions, 0 deletions
diff --git a/testdata/cgo/main.c b/testdata/cgo/main.c index 8eb3d50ae..2f3269ea0 100644 --- a/testdata/cgo/main.c +++ b/testdata/cgo/main.c @@ -10,6 +10,10 @@ int add(int a, int b) { return a + b; } +int doCallback(int a, int b, binop_t callback) { + return callback(a, b); +} + void store(int value, int *ptr) { *ptr = value; } diff --git a/testdata/cgo/main.go b/testdata/cgo/main.go index 0416a1b88..0733d226e 100644 --- a/testdata/cgo/main.go +++ b/testdata/cgo/main.go @@ -3,6 +3,7 @@ package main /* int fortytwo(void); #include "main.h" +int mul(int, int); */ import "C" @@ -23,4 +24,13 @@ func main() { println("15:", *ptr) C.store(25, &n) println("25:", *ptr) + cb := C.binop_t(C.add) + println("callback 1:", C.doCallback(20, 30, cb)) + cb = C.binop_t(C.mul) + println("callback 2:", C.doCallback(20, 30, cb)) +} + +//export mul +func mul(a, b C.int) C.int { + return a * b } diff --git a/testdata/cgo/main.h b/testdata/cgo/main.h index d4d205b84..be529d4fb 100644 --- a/testdata/cgo/main.h +++ b/testdata/cgo/main.h @@ -1,5 +1,7 @@ typedef short myint; int add(int a, int b); +typedef int (*binop_t) (int, int); +int doCallback(int a, int b, binop_t cb); typedef int * intPointer; extern int global; void store(int value, int *ptr); diff --git a/testdata/cgo/out.txt b/testdata/cgo/out.txt index 6414dfd3e..8d6845ee7 100644 --- a/testdata/cgo/out.txt +++ b/testdata/cgo/out.txt @@ -6,3 +6,5 @@ longlong: -1099511627776 global: 3 15: 15 25: 25 +callback 1: 50 +callback 2: 600 |