aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2024-11-06 14:57:56 +0100
committerAyke <[email protected]>2024-11-20 07:53:59 +0100
commit6593cf22fad6fab2a201ed00c2cc20c76e29161a (patch)
tree929fba3e751e9665844c2e7c8b2783a872ccb1dd /testdata
parent548fba82e691b125bdac71f6c393f67fcf4d769f (diff)
downloadtinygo-6593cf22fad6fab2a201ed00c2cc20c76e29161a.tar.gz
tinygo-6593cf22fad6fab2a201ed00c2cc20c76e29161a.zip
cgo: support errno value as second return parameter
Making this work on all targets was interesting but there's now a test in place to make sure this works on all targets that have the CGo test enabled (which is almost all targets).
Diffstat (limited to 'testdata')
-rw-r--r--testdata/cgo/main.c5
-rw-r--r--testdata/cgo/main.go12
-rw-r--r--testdata/cgo/main.h3
-rw-r--r--testdata/cgo/out.txt2
4 files changed, 21 insertions, 1 deletions
diff --git a/testdata/cgo/main.c b/testdata/cgo/main.c
index 4a5bd6b9c..94b338dda 100644
--- a/testdata/cgo/main.c
+++ b/testdata/cgo/main.c
@@ -77,3 +77,8 @@ double doSqrt(double x) {
void printf_single_int(char *format, int arg) {
printf(format, arg);
}
+
+int set_errno(int err) {
+ errno = err;
+ return -1;
+}
diff --git a/testdata/cgo/main.go b/testdata/cgo/main.go
index 00e0ba01d..38d11386a 100644
--- a/testdata/cgo/main.go
+++ b/testdata/cgo/main.go
@@ -18,7 +18,10 @@ import "C"
// static int headerfunc_static(int a) { return a - 1; }
import "C"
-import "unsafe"
+import (
+ "syscall"
+ "unsafe"
+)
func main() {
println("fortytwo:", C.fortytwo())
@@ -168,6 +171,13 @@ func main() {
println("len(C.GoBytes(C.CBytes(nil),0)):", len(C.GoBytes(C.CBytes(nil), 0)))
println(`rountrip CBytes:`, C.GoString((*C.char)(C.CBytes([]byte("hello\000")))))
+ // Check that errno is returned from the second return value, and that it
+ // matches the errno value that was just set.
+ _, errno := C.set_errno(C.EINVAL)
+ println("EINVAL:", errno == syscall.EINVAL)
+ _, errno = C.set_errno(C.EAGAIN)
+ println("EAGAIN:", errno == syscall.EAGAIN)
+
// libc: test whether C functions work at all.
buf1 := []byte("foobar\x00")
buf2 := make([]byte, len(buf1))
diff --git a/testdata/cgo/main.h b/testdata/cgo/main.h
index e7c64ffc3..3942497f2 100644
--- a/testdata/cgo/main.h
+++ b/testdata/cgo/main.h
@@ -1,5 +1,6 @@
#include <stdbool.h>
#include <stdint.h>
+#include <errno.h>
typedef short myint;
typedef short unusedTypedef;
@@ -154,3 +155,5 @@ void arraydecay(int buf1[5], int buf2[3][8], arraydecay_buf3 buf3);
double doSqrt(double);
void printf_single_int(char *format, int arg);
+
+int set_errno(int err);
diff --git a/testdata/cgo/out.txt b/testdata/cgo/out.txt
index 3088d4cb4..1d63f5e82 100644
--- a/testdata/cgo/out.txt
+++ b/testdata/cgo/out.txt
@@ -75,6 +75,8 @@ len(C.GoStringN(nil, 0)): 0
len(C.GoBytes(nil, 0)): 0
len(C.GoBytes(C.CBytes(nil),0)): 0
rountrip CBytes: hello
+EINVAL: true
+EAGAIN: true
copied string: foobar
CGo sqrt(3): +1.732051e+000
C sqrt(3): +1.732051e+000