mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
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).
This commit is contained in:
Vendored
+5
@@ -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;
|
||||
}
|
||||
|
||||
Vendored
+11
-1
@@ -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))
|
||||
|
||||
Vendored
+3
@@ -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);
|
||||
|
||||
Vendored
+2
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user