mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-08 04:53:42 +00:00
loader/cgo: add support for function pointers
This commit is contained in:
Vendored
+4
@@ -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;
|
||||
}
|
||||
|
||||
Vendored
+10
@@ -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
|
||||
}
|
||||
|
||||
Vendored
+2
@@ -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);
|
||||
|
||||
Vendored
+2
@@ -6,3 +6,5 @@ longlong: -1099511627776
|
||||
global: 3
|
||||
15: 15
|
||||
25: 25
|
||||
callback 1: 50
|
||||
callback 2: 600
|
||||
|
||||
Reference in New Issue
Block a user