API reference for Julia Embedding in C

So assume you don’t have any problem creating the type (unless you have a too big type you can just create a big enough buffer to pass in the tuple element types). With that you shouldn’t have any problem allocating the object either so I assume you only have problem with accessing elements.

C does not allow unknown type during compile time so you cannot declare the corresponding C type unless you know it at C code compile time in general. In this particular case though, the three methods I mentioned in the first post still all apply.

  1. You can use the getfield function. It’s accessible in C as jl_get_field. It should be pretty obvious to use. This does not need to rely on any C functions in your code, all offset calculation is down in julia runtime, it’s also the slowest.
  2. You can do offset computation yourself. You can easily predict the layout of the object with n fields. You can just do the offset computation and loads yourself. For NTuple it’s simple enough that you can just cast the pointer to ssize_t* and index it.
  3. It turns out that this is a very special case where it is actually possible to declare a compatible C struct. This require C99 and you can do it as typedef struct { ssize_t a[]; } ntupleint;
2 Likes