call-haskell-from-anything-1.0.0.0: Call Haskell functions from other languages via serialization and dynamic libraries

Safe HaskellNone
LanguageHaskell2010

FFI.Anything.TypeUncurry.Msgpack

Description

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 via dlopen() or equivalents.
  • Expose Haskell functions via a socket / the web

Synopsis

Documentation

class MessagePackRec l where Source

Helper to allow writing a MessagePack instance for TypeLists.

We need this because we have to call parseArray at the top-level MessagePack instance, but not at each function argument step.

Instances

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 (TypeLists) 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 MessagePack pack

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

Exports an IO 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 tryExportIO if you want to handle this case.