cgo: add support for C.CString and related functions

This commit is contained in:
Ayke van Laethem
2021-11-11 16:11:08 +01:00
committed by Ron Evans
parent 6bd18af5ef
commit c31aef06ba
14 changed files with 238 additions and 3 deletions
+2
View File
@@ -24,6 +24,8 @@ int cflagsConstant = SOME_CONSTANT;
int smallEnumWidth = sizeof(option2_t);
char globalChars[] = {2, 0, 4, 8};
int fortytwo() {
return 42;
}
+15
View File
@@ -135,6 +135,21 @@ func main() {
// void arraydecay(int *buf1, int *buf2[8], int *buf3[7][2]);
C.arraydecay((*C.int)(nil), (*[8]C.int)(nil), (*[7][2]C.int)(nil))
// Test CGo builtins like C.CString.
cstr := C.CString("string passed to C")
println("cstr length:", C.strlen(cstr))
gostr := C.GoString(cstr)
println("C.CString:", gostr)
charBuf := C.GoBytes(unsafe.Pointer(&C.globalChars[0]), 4)
println("C.charBuf:", charBuf[0], charBuf[1], charBuf[2], charBuf[3])
binaryString := C.GoStringN(&C.globalChars[0], 4)
println("C.CStringN:", len(binaryString), binaryString[0], binaryString[1], binaryString[2], binaryString[3])
// Test whether those builtins also work with zero length data.
println("C.GoString(nil):", C.GoString(nil))
println("len(C.GoStringN(nil, 0)):", len(C.GoStringN(nil, 0)))
println("len(C.GoBytes(nil, 0)):", len(C.GoBytes(nil, 0)))
// libc: test whether C functions work at all.
buf1 := []byte("foobar\x00")
buf2 := make([]byte, len(buf1))
+2
View File
@@ -141,6 +141,8 @@ extern int smallEnumWidth;
extern int cflagsConstant;
extern char globalChars[4];
// test duplicate definitions
int add(int a, int b);
extern int global;
+7
View File
@@ -61,5 +61,12 @@ option 2A: 20
option 3A: 21
enum width matches: true
CFLAGS value: 17
cstr length: 18
C.CString: string passed to C
C.charBuf: 2 0 4 8
C.CStringN: 4 2 0 4 8
C.GoString(nil):
len(C.GoStringN(nil, 0)): 0
len(C.GoBytes(nil, 0)): 0
copied string: foobar
line written using C puts