Goal: I have a simple interpreter for a language that I've written,
and I wish to implement an FFI. My ideal api would be like 'ctypes'
in python. Here's an example:
from ctypes import *
libc = CDLL("libc.so.6")
libc.printf("An int %d, a double %f\n", 1234, c_double(3.14))
Prints out:
An int 1234, a double 3.140000
Implementation: I am currently trying to use the dynamic linker in
order to specify the library string, for example the following seems
to work:
import Foreign
import Foreign.C.Types
import Foreign.C.String
import Monad (liftM)
import System.Posix.DynamicLinker
type Fun = CString -> IO CInt
foreign import ccall unsafe "dynamic" c_printf :: FunPtr Fun -> Fun
printf :: String -> IO Int
printf str = do
withDL "libc.so.6" [RTLD_NOW] $ \dl -> do
printf_ptr <- dlsym dl "printf"
let fun = c_printf printf_ptr
liftM fromIntegral $ withCString str fun
main = printf "hello world"
Problem: I don't understand how I can generate the foreign import
statements at runtime. I am currently considering generating strings
of such expressions, and using hs-plugins in order to compile them.
This seems like a complete hack though - I'm dynamically loading a
haskell library in order to dynamically load a foreign library.
Another option is to write a dynamic loader in C, and then call this
from haskell, but how would I type the loading function - obviously I
can give it a CString for the library, and another for the function in
question - but what would the return type be?
regards,
Richard Warburton
_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Thanks a lot. Though the haskell wiki [0] claims that libffi works on x64.
regards,
Richard
[0] http://www.haskell.org/haskellwiki/Library/libffi#Does_it_work.3F
C/Invoke is another library whose name i forget.
> Thanks a lot. Though the haskell wiki [0] claims that libffi works on x64.
i don't know, just read yesterday on Lua list:
> A question for Fabio: what are the issues with Alien for 64-bit Windows?
I can answer part of that. Libffi [1] (the C library on top of which
Alien is built) has no support for 64-bit windows. More specifically
there is a need for some runtime generated glue code (mainly for
callbacks), and assembler support for x64 is very poor so far (many
Visual C++ compilers come without assembler and/or lack support for
inline asm).
Python and Java (in the JNA package) have their own port of libffi
specifically for x64. However I never managed to compile any of these
two for Alien (usually because of the lack of assembler).
I tried all this about a year ago, so things may have changed since then.
[1] http://sourceware.org/libffi/
--
Best regards,
Bulat mailto:Bulat.Z...@gmail.com