Safe Haskell | None |
---|---|
Language | Haskell2010 |
Easy FFI via MessagePack.
You can use this module to expose any Haskell function to other Programming languages.
It allows to convert functions that take multiple arguments
into functions that take one argument:
A ByteString
which contains all arguments encoded as a MessagePack array.
Common use cases:
- Write functions in fast native Haskell code, compile them into a dynamic.
library (
.so
/.dll
) and call them via C/Python/Ruby/whatever viadlopen()
or equivalents. - Expose Haskell functions via a socket / the web
- class MessagePackRec l where
- fromObjectRec :: Vector Object -> Maybe (TypeList l)
- getTypeListFromMsgpackArray :: forall l. (MessagePackRec l, ParamLength l) => Object -> Maybe (TypeList l)
- uncurryMsgpack :: (MessagePack (TypeList l), ToTypeList f l r, MessagePack r) => f -> ByteString -> ByteString
- tryUncurryMsgpack :: (MessagePack (TypeList l), ToTypeList f l r, MessagePack r) => f -> ByteString -> Maybe ByteString
- tryUncurryMsgpackIO :: (MessagePack (TypeList l), ToTypeList f l (IO r), MessagePack r) => f -> ByteString -> Maybe (IO ByteString)
- byteStringToCStringFun :: (ByteString -> ByteString) -> CString -> IO CString
- byteStringToCStringFunIO :: (ByteString -> IO ByteString) -> CString -> IO CString
- export :: (MessagePack (TypeList l), ToTypeList f l r, MessagePack r) => f -> CString -> IO CString
- exportIO :: (MessagePack (TypeList l), ToTypeList f l (IO r), MessagePack r) => f -> CString -> IO CString
Documentation
class MessagePackRec l where Source
Helper to allow writing a MessagePack
instance for TypeList
s.
We need this because we have to call parseArray
at the top-level
MessagePack
instance, but not at each function argument step.
MessagePackRec ([] *) Source | When no more types need to be unpacked, we are done. |
(MessagePack a, MessagePackRec l) => MessagePackRec ((:) * a l) Source | Unpack one type by just parsing the next element. |
getTypeListFromMsgpackArray :: forall l. (MessagePackRec l, ParamLength l) => Object -> Maybe (TypeList l) Source
Parses a tuple of arbitrary size (TypeList
s) from a MessagePack array.
uncurryMsgpack :: (MessagePack (TypeList l), ToTypeList f l r, MessagePack r) => f -> ByteString -> ByteString Source
Translates a function of type a -> b -> ... -> r
to
a function that:
- takes as a single argument a
ByteString
containing all arguments serialized in a MessagePack array - returns its result serialized in a
ByteString
via MessagePackpack
This function throws an error
if the de-serialization of the arguments fails!
It is recommended to use tryUncurryMsgpack
instead.
tryUncurryMsgpack :: (MessagePack (TypeList l), ToTypeList f l r, MessagePack r) => f -> ByteString -> Maybe ByteString Source
Like uncurryMsgpack
, but makes it clear when the ByteString
containing
the function arguments does not contain the right number/types of arguments.
tryUncurryMsgpackIO :: (MessagePack (TypeList l), ToTypeList f l (IO r), MessagePack r) => f -> ByteString -> Maybe (IO ByteString) Source
Like uncurryMsgpack
, but makes it clear when the ByteString
containing
the function arguments does not contain the right number/types of arguments.
byteStringToCStringFun :: (ByteString -> ByteString) -> CString -> IO CString Source
Transforms a ByteString
-mapping function to CString
-mapping function
for use in the FFI.
byteStringToCStringFunIO :: (ByteString -> IO ByteString) -> CString -> IO CString Source
Transforms a ByteString
-mapping IO
function to CString
-mapping function
for use in the FFI.
export :: (MessagePack (TypeList l), ToTypeList f l r, MessagePack r) => f -> CString -> IO CString Source
Exports a "pure" function to an FFI function that takes its arguments as a serialized MessagePack message.
Calling this function throws an error
if the de-serialization of the arguments fails!
Use tryExport
if you want to handle this case.
exportIO :: (MessagePack (TypeList l), ToTypeList f l (IO r), MessagePack r) => f -> CString -> IO CString Source