Open timholy opened 11 years ago
I believe this can't be done portably except for functions taking a va_list, and even then it's tricky. In your example the metaprogramming is unnecessary; you can use a varargs julia function with an "if" case for various lengths, and pass args[1], args[2], etc. to the ccall.
I found a way to do what I wanted, although I'm sure I'm handling a much more specific case than you're envisioning. Would you please check https://github.com/timholy/julia_hdf5/blob/master/hdf5.jl and see whether the section marked ### Utilities for generating ccall wrapper functions programmatically ###
has anything of general interest? If not, it can continue to live in the HDF5 wrapper.
previously mentioned code now now lives at https://github.com/timholy/HDF5.jl/blob/master/src/plain.jl#L1443
I had a related problem: wanted to ccall
a varargs function with arguments (Int, Ptr{Void}...)
by splicing in the argument list.
There seems to be some disagreement about whether it is portable to assume that varargs are passed in the same way as ordinary arguments, although apparently LLVM assumes (assumed?) this: see here and here, for example.
Potentially relevant for this issue: I’ve created the Ccalls.jl package based on a discussion on Discourse that allows making FFI calls like ordinary function calls, i. e. including “splat” operations:
julia> using Ccalls
julia> args = (2, 10)
(2, 10)
julia> Ccall(:pow, Cdouble, (Cdouble, Cdouble), args...)
1024.0
So using current Julia, it is actually possible to do this!
Conceptually related to #1313.
If I try defining a function this way:
then I get:
One workaround is the following:
Perhaps there is a less-awkward syntax? If not, should we add a
ccallexpr
function to the library that would build those expressions?