tinygo/testdata/cgo/main.c
Ayke van Laethem d2b3a5486c cgo: implement C unions
Unions are somewhat hard to implement in Go because they are not a
native type. But it is actually possible with some compiler magic.

This commit inserts a special "C union" field at the start of a struct
to indicate that it is a union. As such a field cannot be written
directly in Go, this is a useful to distinguish structs and unions.
2019-04-17 11:56:40 +02:00

45 строки
934 Б
C

#include "main.h"
int global = 3;
_Bool globalBool = 1;
_Bool globalBool2 = 10; // test narrowing
float globalFloat = 3.1;
double globalDouble = 3.2;
_Complex float globalComplexFloat = 4.1+3.3i;
_Complex double globalComplexDouble = 4.2+3.4i;
_Complex double globalComplexLongDouble = 4.3+3.5i;
collection_t globalStruct = {256, -123456, 3.14, 88};
int globalStructSize = sizeof(globalStruct);
short globalArray[3] = {5, 6, 7};
joined_t globalUnion;
int globalUnionSize = sizeof(globalUnion);
int fortytwo() {
return 42;
}
int add(int a, int b) {
return a + b;
}
int doCallback(int a, int b, binop_t callback) {
return callback(a, b);
}
void store(int value, int *ptr) {
*ptr = value;
}
void unionSetShort(short s) {
globalUnion.s = s;
}
void unionSetFloat(float f) {
globalUnion.f = f;
}
void unionSetData(short f0, short f1, short f2) {
globalUnion.data[0] = 5;
globalUnion.data[1] = 8;
globalUnion.data[2] = 1;
}