mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-13 23:43:40 +00:00
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.
This commit is contained in:
committed by
Ron Evans
parent
536086988c
commit
d2b3a5486c
Vendored
+18
-1
@@ -8,8 +8,11 @@ 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};
|
||||
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;
|
||||
@@ -26,3 +29,17 @@ int doCallback(int a, int b, binop_t callback) {
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user