[Haskell-cafe] Anonymous FFI calls
Sylvain Henry
hsyl20 at gmail.com
Wed Feb 11 17:26:10 UTC 2015
2015-02-11 16:18 GMT+01:00 Francesco Mazzoli <f at mazzo.li>:
>
> Relatedly, if I have some function pointer known at runtime that
> addresses a C function that takes some arguments, I have no way to
> invoke it from Haskell, since all FFI imports must be declared at
> compile-time, and I don't know the address of the symbol I want to
> execute at compile time.
>
You can use FunPtr and wrapper imports to convert a FunPtr into a Haskell
function.
https://hackage.haskell.org/package/base-4.7.0.2/docs/Foreign-Ptr.html#g:2
You may be interested in my dynamic-linker-template package [1] to avoid
having to write boilerplate wrappers. For now it only works with dynamic
linking from System.Posix.DynamicLinker, but it could be easily extended to
support other platforms. It automatically generates wrappers for all the
functions in a record as well as the code to load symbol addresses and to
convert them into Haskell functions (examples [2,3]).
Sylvain
[1] https://hackage.haskell.org/package/dynamic-linker-template
[2]
https://github.com/hsyl20/dynamic-linker-template/blob/master/Tests/Test.hs
[3]
https://github.com/hsyl20/ViperVM/blob/master/src/lib/ViperVM/Arch/OpenCL/Library.hs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/ghc-devs/attachments/20150211/e5a7aa8c/attachment.html>
More information about the ghc-devs
mailing list