开发者

Call C Functions from Haskell at runtime

开发者 https://www.devze.com 2023-03-21 00:08 出处:网络
I\'m building an interpreter for a dynamic programming language in Haskell. I\'d like to add a simple mechanism to call C functions. In the past, I\'ve used the Haskell F开发者_JAVA技巧FI to call C fu

I'm building an interpreter for a dynamic programming language in Haskell. I'd like to add a simple mechanism to call C functions. In the past, I've used the Haskell F开发者_JAVA技巧FI to call C functions that I had explicitly declared the name and type of; this approach won't work here because the interpreter won't know the name or type of the C functions to be called until runtime.

Is it possible to declare and call C functions at runtime? Where should I begin?


Dynamic Importing

If you can list all possible types for the C functions that may be called, then you can use the FFI's dynamic import capability ( http://www.haskell.org/onlinereport/haskell2010/haskellch8.html). A dynamic import function wraps a C function at runtime. You'll need to declare an import function for each C function type that you may be calling. (Actually, only the ABI matters, so you can treat all C pointer types as equivalent.)

foreign import ccall "dynamic" mkPtrFun :: FunPtr (Ptr () -> IO (Ptr ())) -> Ptr () -> IO (Ptr ())

If you have a pointer to a C function, you can make it callable from Haskell using this wrapper function.

callWithNull :: FunPtr (Ptr a -> IO (Ptr ())) -> IO (Ptr ())
callWithNull f = mkPtrFun f nullPtr

If the types of the C functions are unknown when the Haskell code is compiled, then you cannot do this with the FFI.

Dynamic Loading

As for obtaining C function pointers dynamically, the FFI doesn't help you. You can use dynamic loading libraries in C such as libdl. See the man pages: http://linux.die.net/man/3/dlopen .

0

精彩评论

暂无评论...
验证码 换一张
取 消