HsLua.Core.Closures

hslua-core-2.1.0: Bindings to Lua, an embeddable scripting language

Copyright | © 2007–2012 Gracjan Polak; © 2012–2016 Ömer Sinan Ağacan; © 2017-2022 Albert Krewinkel
License | MIT
Maintainer | Albert Krewinkel <tarleb+hslua@zeitkraut.de>
Stability | beta
Portability | non-portable (depends on GHC)
Safe Haskell | Safe-Inferred
Language | Haskell2010

HsLua.Core.Closures

Description

Expose Haskell functions as Lua closures.

Synopsis

- pushPreCFunction :: PreCFunction -> LuaE e ()
- pushHaskellFunction :: LuaError e => HaskellFunction e -> LuaE e ()

# Documentation

pushPreCFunction :: PreCFunction -> LuaE e () #

Converts a pre C function to a Lua function and pushes it to the stack.

Pre C functions collect parameters from the stack and return a `CInt` that represents number of return values left on the stack. See `CFunction` for more info.

pushHaskellFunction :: LuaError e => HaskellFunction e -> LuaE e () #

Pushes Haskell function as a callable userdata. All values created will be garbage collected. The function should behave similar to a `CFunction`.

Error conditions should be indicated by raising a catchable exception or by returning the result of ``error``.

Example:

    mod23 :: Lua NumResults
    mod23 = do
      mn <- tointeger (nthBottom 1)
      case mn of
        Nothing -> pushstring "expected an integer" *> error
        Just n  -> pushinteger (n `mod` 23)
    pushHaskellFunction mod23
    setglobal "mod23"