diff options
author | Ayke van Laethem <[email protected]> | 2021-09-24 03:05:10 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2021-09-29 13:38:33 +0200 |
commit | 5c0a337c4f82d011e1255fab6923cea7b25cbbc0 (patch) | |
tree | ce02a8186162233d70e4d98f224d1da4714e3175 /testdata/cgo | |
parent | 04e8f443706e442bd0052fe05121e7a3589190de (diff) | |
download | tinygo-5c0a337c4f82d011e1255fab6923cea7b25cbbc0.tar.gz tinygo-5c0a337c4f82d011e1255fab6923cea7b25cbbc0.zip |
cgo: implement rudimentary C array decaying
This is just a first step. It's not complete, but it gets some real
world C code to parse.
This signature, from the ESP-IDF:
esp_err_t esp_wifi_get_mac(wifi_interface_t ifx, uint8_t mac[6]);
Was previously converted to something like this (pseudocode):
C.esp_err_t esp_wifi_get_mac(ifx C.wifi_interface_t, mac [6]uint8)
But this is not correct. C array parameters will decay. The array is
passed by reference instead of by value. Instead, this would be the
correct signature:
C.esp_err_t esp_wifi_get_mac(ifx C.wifi_interface_t, mac *uint8)
So that it can be called like this (using CGo):
var mac [6]byte
errCode := C.esp_wifi_get_mac(C.ESP_IF_WIFI_AP, &mac[0])
This stores the result in the 6-element array mac.
Diffstat (limited to 'testdata/cgo')
-rw-r--r-- | testdata/cgo/main.c | 4 | ||||
-rw-r--r-- | testdata/cgo/main.go | 6 | ||||
-rw-r--r-- | testdata/cgo/main.h | 4 |
3 files changed, 14 insertions, 0 deletions
diff --git a/testdata/cgo/main.c b/testdata/cgo/main.c index 7954b91f6..d40833246 100644 --- a/testdata/cgo/main.c +++ b/testdata/cgo/main.c @@ -61,3 +61,7 @@ void unionSetData(short f0, short f1, short f2) { globalUnion.data[1] = 8; globalUnion.data[2] = 1; } + +void arraydecay(int buf1[5], int buf2[3][8], int buf3[4][7][2]) { + // Do nothing. +} diff --git a/testdata/cgo/main.go b/testdata/cgo/main.go index 113176df4..b1880c7b9 100644 --- a/testdata/cgo/main.go +++ b/testdata/cgo/main.go @@ -128,6 +128,12 @@ func main() { // Check whether CFLAGS are correctly passed on to compiled C files. println("CFLAGS value:", C.cflagsConstant) + // Check array-to-pointer decaying. This signature: + // void arraydecay(int buf1[5], int buf2[3][8], int buf3[4][7][2]); + // decays to: + // 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)) + // 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 ddd07efa9..09e1b4f09 100644 --- a/testdata/cgo/main.h +++ b/testdata/cgo/main.h @@ -144,3 +144,7 @@ extern int cflagsConstant; // test duplicate definitions int add(int a, int b); extern int global; + +// Test array decaying into a pointer. +typedef int arraydecay_buf3[4][7][2]; +void arraydecay(int buf1[5], int buf2[3][8], arraydecay_buf3 buf3); |