hslua-classes-2.1.0: Type classes for HsLua

Copyright | © 2007–2012 Gracjan Polak 2012–2016 Ömer Sinan Ağacan 2017-2022 Albert Krewinkel
License | MIT
Maintainer | Albert Krewinkel <tarleb+hslua@zeitkraut.de>
Safe Haskell | None
Language | Haskell2010

HsLua.Class.Exposable

Description

Call Haskell functions from Lua.

Synopsis

- class LuaError e => Exposable e a where
  - partialApply :: StackIndex -> a -> Peek e NumResults
- toHaskellFunction :: forall e a. Exposable e a => a -> HaskellFunction e
- pushAsHaskellFunction :: forall e a. Exposable e a => a -> LuaE e ()
- registerHaskellFunction :: Exposable e a => Name -> a -> LuaE e ()

# Documentation

class LuaError e => Exposable e a where #

Operations and functions that can be pushed to the Lua stack. This is a helper function not intended to be used directly. Use the ``toHaskellFunction`` wrapper instead.

Methods

partialApply :: StackIndex -> a -> Peek e NumResults #

Helper function, called by ``toHaskellFunction``. Should do a partial application of the argument at the given index to the underlying function. Recurses if necessary, causing further partial applications until the operation is a easily exposable to Lua.

#### Instances

Instances details

LuaError e => Exposable e (HaskellFunction e) # |
Instance details Defined in HsLua.Class.Exposable Methods partialApply :: StackIndex -> HaskellFunction e -> Peek e NumResults #
(Peekable a, Exposable e b) => Exposable e (a -> b) # |
Instance details Defined in HsLua.Class.Exposable Methods partialApply :: StackIndex -> (a -> b) -> Peek e NumResults #
(LuaError e, Pushable a) => Exposable e (Peek e a) # |
Instance details Defined in HsLua.Class.Exposable Methods partialApply :: StackIndex -> Peek e a -> Peek e NumResults #
(LuaError e, Pushable a) => Exposable e (LuaE e a) # |
Instance details Defined in HsLua.Class.Exposable Methods partialApply :: StackIndex -> LuaE e a -> Peek e NumResults #

toHaskellFunction :: forall e a. Exposable e a => a -> HaskellFunction e #

Convert a Haskell function to a function type directly exposable to Lua. Any Haskell function can be converted provided that:

- all arguments are instances of ``Peekable``
- return type is `LuaE e a`, where `a` is an instance of ``Pushable``

Any exception of type `e` will be caught.

Important: this does not catch exceptions other than `e`; exception handling must be done by the Haskell function. Failure to do so will cause the program to crash.

E.g., the following code could be used to handle an Exception of type FooException, if that type is an instance of `MonadCatch` and `Pushable`:

    toHaskellFunction (myFun `catchM` (\e -> raiseError (e :: FooException)))

pushAsHaskellFunction :: forall e a. Exposable e a => a -> LuaE e () #

Pushes the given value as a function to the Lua stack.

See `toHaskellFunction` for details.

registerHaskellFunction :: Exposable e a => Name -> a -> LuaE e () #

Imports a Haskell function and registers it at global name.