It evokes me Python's ctypes module.<br>Nice job ! I think It will be useful.<br><br><br><div class="gmail_quote">2011/3/6 Remi Turk <span dir="ltr"><<a href="mailto:rturk@science.uva.nl">rturk@science.uva.nl</a>></span><br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">I am happy to finally announce cinvoke 0.1, a binding to the<br>
C library cinvoke[1], allowing functions to be loaded and called<br>
whose names and types are not known before run-time.<br>
<br>
Why?<br>
<br>
Sometimes you can't use the Haskell foreign function interface<br>
because you parse the type of the function from somewhere else,<br>
i.e. you're writing an interpreter for a language that has an FFI<br>
itself.<br>
<br>
What?<br>
<br>
The main function it exports is:<br>
<br>
cinvoke :: Symbol -> RetType b -> [Arg] -> IO b<br>
<br>
And because code is worth a thousand words, here's a small program<br>
that uses libc to write a 1Gb buffer of random garbage to a file:<br>
<br>
> module Main where<br>
><br>
> import Foreign.CInvoke<br>
><br>
> main = do<br>
> cxt <- newContext<br>
> libc <- loadLibrary cxt "libc.so.6"<br>
> malloc <- loadSymbol libc "malloc"<br>
> creat <- loadSymbol libc "creat"<br>
> write <- loadSymbol libc "write"<br>
> free <- loadSymbol libc "free"<br>
> let sz = 2^30<br>
> buf <- cinvoke malloc (retPtr retVoid)<br>
> [argCSize sz]<br>
> fd <- cinvoke creat retCInt [argString "/tmp/test", argCUInt 0o644]<br>
> n <- cinvoke write retCSize [argCInt fd, argPtr buf, argCSize sz]<br>
> cinvoke free (retPtr retVoid) [argPtr buf]<br>
<br>
It hopefully works on any machine on which cinvoke works,<br>
but has only been tested on linux x86_64.<br>
As the current version of cinvoke only installs a static library,<br>
it does not work from GHCi at the moment (without hacking cinvoke<br>
to build a shared library).<br>
More interesting examples are included in examples/ in the<br>
package.<br>
<br>
Where?<br>
Hackage: <a href="http://hackage.haskell.org/package/cinvoke" target="_blank">http://hackage.haskell.org/package/cinvoke</a><br>
<br>
Cheers, Remi<br>
<br>
[1] <a href="http://www.nongnu.org/cinvoke/" target="_blank">http://www.nongnu.org/cinvoke/</a><br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>