JuliaLang/julia — pull request #1831 by vtjnash (2012-12-27 19:24:59+00:00)
Title: RFC: ccall with julia types as structs

This adds syntax and support for passing julia types to ccall functions, either as StructTypes (by-value) or Pointers to StructTypes (by-reference).

Some example/test code:
```julia
ccall((:cptest,"ztest"), Void, (StructPtr{ComplexPair{Int}},), ComplexPair(122,3))
ccall((:cptest,"ztest"), Void, (StructPtr{ComplexPair{Int}},), &ComplexPair(122,3))
ccall((:cgptest,"ztest"), Void, (StructPtr{ComplexPair{Float64}},), &ComplexPair(2.,3))

ccall((:cgtest,"ztest"), Void, (Struct{ComplexPair{Float64}},), ComplexPair(2.,3))
ccall((:ctest,"ztest"), Void, (Struct{ComplexPair{Int}},), ComplexPair(2,3))

ccall((:sptest,"ztest"), Void, (Struct{ASCIIString},), "asdf")
```

compile the following library with `gcc ztest.c -shared -o ztest.so -g -Wall` or `gcc ztest.c -shared -o ztest.dylib -g -Wall` depending on your platform:
```c
#include
#include
#include
#include
typedef struct {
long real;
long imag;
} complex_t;
void ctest(complex_t a) {
//Unpack a ComplexPair{Int} struct
printf("%ld + %ld i\n", a.real, a.imag);
return;
}
void cgtest(complex double a) {
//Unpack a ComplexPair{Float64} struct
printf("%g + %g i\n", creal(a), cimag(a));
return;
}
void cgptest(complex double *a) {
//Unpack a ComplexPair{Float64} struct
printf("%g + %g i\n", creal(*a), cimag(*a));
return;
}
void cptest(complex_t *a) {
//Unpack a ComplexPair{Int} struct pointer
printf("%ld + %ld i\n", a->real, a->imag);
return;
}
void stest(char *x) {
//Print an Array
printf("%s\n", x);
}
void sptest(struct { struct { void* t; char* x } *x } x ) {
//Unpack an ASCIIString
printf("%s\n", x.x->x);
}
```