[Haskell-cafe] FunPtr to C function with #arguments determined atruntime?
Donn Cave
donn at avvanta.com
Mon Feb 18 00:18:28 CET 2013
Quoth Ryan Newton <rrnewton at gmail.com>,
> The scenario is pretty simple. I generate C code at runtime. I compile it
> to a .so. I know how many arguments it expects (but only at runtime), and
> I get a FunPtr back from 'dlsym'. How do I call it?
>
> I was hoping there would be some "Raw" lower level FFI layer that I could
> use to invoke a C function without automagic marshaling and all the other
> goodness provided by the normal "foreign import" mechanism.
I feel that I might be confused about the problem, but since I don't
see anyone direct answers -- in order to call a FunPtr, you can use
foreign import ccall "dynamic", to create a regular function. As
described in the library documentation for Foreign.Ptr, which I bet
you've seen, so you know this.
You can cast the FunPtr to whatever type you like, so you can call the
function with an argument list different from its initial declaration.
There are two types of support you don't get, as I see it: 1) Haskell
isn't going to make it really super convenient to use a variably typed
function, and 2) there's the argument promotion issue mentioned in the
document you cited. On that second point, however you go about this,
you'll need to thoroughly understand what that means, but it won't
strictly prevent you from calling this function, just puts you on
notice that the FFI is blind to the argument types and will make
default assumptions, so your C function needs to support that.
On the first point, of course that's how Haskell is - and from a
design point of view, if you're writing C code to be called from
Haskell, wouldn't there be better ways to accommodate variable
inputs than the argument list?
Donn
More information about the Haskell-Cafe
mailing list