lua-2.3.2: Lua, an embeddable scripting language
Copyright© 2007–2012 Gracjan Polak;
© 2012–2016 Ömer Sinan Ağacan;
© 2017-2024 Albert Krewinkel
LicenseMIT
MaintainerAlbert Krewinkel <tarleb@hslua.org>
Stabilitybeta
Portabilitynon-portable (depends on GHC)
Safe HaskellSafe-Inferred
LanguageHaskell2010

Lua.Call

Description

Function to push Haskell functions as Lua C functions.

Haskell functions are converted into C functions in a two-step process. First, a function pointer to the Haskell function is stored in a Lua userdata object. The userdata gets a metatable which allows to invoke the object as a function. The userdata also ensures that the function pointer is freed when the object is garbage collected in Lua.

In a second step, the userdata is then wrapped into a C closure. The wrapping function calls the userdata object and implements the error protocol, converting special error values into proper Lua errors.

Synopsis

Documentation

hslua_pushhsfunction :: State -> PreCFunction -> IO () Source #

Pushes a Haskell operation as a Lua function. The Haskell operation is expected to follow the custom error protocol, i.e., it must signal errors with hslua_error.

Example

Export the function to calculate triangular numbers.

let triangular :: PreCFunction
    triangular l' = do
      n <- lua_tointegerx l' (nthBottom 1) nullPtr
      lua_pushinteger l' (sum [1..n])
      return (NumResults 1)

hslua_newhsfunction l triangular
withCString "triangular" (lua_setglobal l)