Lua.Ersatz.Functions

lua-2.3.1: Lua, an embeddable scripting language

Copyright | © 2007–2012 Gracjan Polak; © 2012–2016 Ömer Sinan Ağacan; © 2017-2023 Albert Krewinkel
License | MIT
Maintainer | Albert Krewinkel <tarleb@hslua.org>
Stability | beta
Portability | ForeignFunctionInterface, CPP
Safe Haskell | Safe-Inferred
Language | Haskell2010

Lua.Ersatz.Functions

Contents

- Get functions (Lua -> stack)
- Set functions (stack -> Lua)
- Misc

Description

Ersatz functions for Lua API items which may, directly or indirectly, throw a Lua error.

Synopsis

- hslua_arith :: State -> ArithOPCode -> Ptr StatusCode -> IO ()
- hslua_compare :: State -> StackIndex -> StackIndex -> OPCode -> Ptr StatusCode -> IO LuaBool
- hslua_gettable :: State -> StackIndex -> Ptr StatusCode -> IO TypeCode
- hslua_getglobal :: State -> CString -> CSize -> Ptr StatusCode -> IO TypeCode
- hslua_settable :: State -> StackIndex -> Ptr StatusCode -> IO ()
- hslua_setglobal :: State -> CString -> CSize -> Ptr StatusCode -> IO ()
- hslua_error :: State -> IO NumResults
- hslua_next :: State -> StackIndex -> Ptr StatusCode -> IO LuaBool
- hslua_concat :: State -> NumArgs -> Ptr StatusCode -> IO ()

# Documentation

hslua_arith #

Arguments

:: State |
-> ArithOPCode | op
-> Ptr StatusCode |
-> IO () |

Performs an arithmetic or bitwise operation over the two values (or one, in the case of negations) at the top of the stack, with the value at the top being the second operand, pops these values, and pushes the result of the operation. The function follows the semantics of the corresponding Lua operator (that is, it may call metamethods).

The value of `op` must be one of the following constants:

- `LUA_OPADD`: performs addition (`+`)
- `LUA_OPSUB`: performs subtraction (`-`)
- `LUA_OPMUL`: performs multiplication (`*`)
- `LUA_OPDIV`: performs float division (`/`)
- `LUA_OPIDIV`: performs floor division (`//`)
- `LUA_OPMOD`: performs modulo (`%`)
- `LUA_OPPOW`: performs exponentiation (`^`)
- `LUA_OPUNM`: performs mathematical negation (unary `-`)
- `LUA_OPBNOT`: performs bitwise NOT (`~`)
- `LUA_OPBAND`: performs bitwise AND (`&`)
- `LUA_OPBOR`: performs bitwise OR (`|`)
- `LUA_OPBXOR`: performs bitwise exclusive OR (`~`)
- `LUA_OPSHL`: performs left shift (`<<`)
- `LUA_OPSHR`: performs right shift (`>>`)

This function wraps `lua_arith` and takes an additional parameter `status`; if it is not `NULL`, then the return value is set to the status after calling `lua_arith`.

hslua_compare #

Arguments

:: State |
-> StackIndex | index 1
-> StackIndex | index 2
-> OPCode | operator
-> Ptr StatusCode | status
-> IO LuaBool |

Compares two Lua values. Returns `1` if the value at index `index1` satisfies op when compared with the value at index `index2`, following the semantics of the corresponding Lua operator (that is, it may call metamethods). Otherwise returns `0`. Also returns `0` if any of the indices is not valid.

The value of op must be one of the following constants:

- `LUA_OPEQ`: compares for equality (==)
- `LUA_OPLT`: compares for less than (<)
- `LUA_OPLE`: compares for less or equal (<=)

This function wraps `lua_compare` and takes an additional parameter `status`; if it is not `NULL`, then the return value is set to the status after calling `lua_compare`.

# Get functions (Lua -> stack)

hslua_gettable :: State -> StackIndex -> Ptr StatusCode -> IO TypeCode #

Behaves like ``lua_gettable``, but prevents unrecoverable program crashes by calling that function through ``lua_pcall``. Takes an additional status code pointer that is set to the status returned by `lua_pcall`.

hslua_getglobal :: State -> CString -> CSize -> Ptr StatusCode -> IO TypeCode #

Behaves like ``lua_getglobal``, but prevents unrecoverable program crashes by calling that function through ``lua_pcall``. Takes an additional status code pointer that is set to the status returned by `lua_pcall`.

# Set functions (stack -> Lua)

hslua_settable :: State -> StackIndex -> Ptr StatusCode -> IO () #

Behaves like ``lua_settable``, but prevents unrecoverable program crashes by calling that function through ``lua_pcall``. Takes an additional status code pointer that is set to the status returned by `lua_pcall`.

hslua_setglobal :: State -> CString -> CSize -> Ptr StatusCode -> IO () #

Behaves like ``lua_setglobal``, but prevents unrecoverable program crashes by calling that function through ``lua_pcall``. Takes an additional status code pointer that is set to the status returned by `lua_pcall`.

# Misc

hslua_error :: State -> IO NumResults #

Replacement for lua_error; it uses the HsLua error signaling convention instead of raw Lua errors.

hslua_next :: State -> StackIndex -> Ptr StatusCode -> IO LuaBool #

Wrapper around lua_next which catches any Lua errors.

hslua_concat :: State -> NumArgs -> Ptr StatusCode -> IO () #

Wrapper around lua_concat which catches any Lua errors.