diff options
author | Ayke van Laethem <[email protected]> | 2021-09-17 02:41:45 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2021-09-28 18:44:11 +0200 |
commit | e02727679fc9cc24e59e6d5378eabd313605cfc6 (patch) | |
tree | 33fa8df895c98a5e58b9b9fe70eecf1d5ed55e75 /testdata/cgo | |
parent | 138add2b96532fee1f132a69b706bbc5d10ed457 (diff) | |
download | tinygo-e02727679fc9cc24e59e6d5378eabd313605cfc6.tar.gz tinygo-e02727679fc9cc24e59e6d5378eabd313605cfc6.zip |
builder, cgo: support function definitions in CGo headers
For example, the following did not work before but does work with this
change:
// int add(int a, int b) {
// return a + b;
// }
import "C"
func main() {
println("add:", C.add(3, 5))
}
Even better, the functions in the header are compiled together with the
rest of the Go code and so they can be optimized together! Currently,
inlining is not yet allowed but const-propagation across functions
works. This should be improved in the future.
Diffstat (limited to 'testdata/cgo')
-rw-r--r-- | testdata/cgo/main.go | 4 | ||||
-rw-r--r-- | testdata/cgo/out.txt | 1 |
2 files changed, 5 insertions, 0 deletions
diff --git a/testdata/cgo/main.go b/testdata/cgo/main.go index 36ef2905c..113176df4 100644 --- a/testdata/cgo/main.go +++ b/testdata/cgo/main.go @@ -10,6 +10,7 @@ int mul(int, int); */ import "C" +// int headerfunc(int a) { return a + 1; } import "C" import "unsafe" @@ -43,6 +44,9 @@ func main() { println("variadic0:", C.variadic0()) println("variadic2:", C.variadic2(3, 5)) + // functions in the header C snippet + println("headerfunc:", C.headerfunc(5)) + // equivalent types var goInt8 int8 = 5 var _ C.int8_t = goInt8 diff --git a/testdata/cgo/out.txt b/testdata/cgo/out.txt index 5f4b5f411..00444cfa3 100644 --- a/testdata/cgo/out.txt +++ b/testdata/cgo/out.txt @@ -15,6 +15,7 @@ callback 1: 50 callback 2: 600 variadic0: 1 variadic2: 15 +headerfunc: 6 bool: true true float: +3.100000e+000 double: +3.200000e+000 |