-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/


-- | Lua, an embeddable scripting language
--   
--   This package provides bindings and types to bridge Haskell and
--   <a>Lua</a>.
--   
--   The full Lua interpreter version 5.4.8 is included. Alternatively, a
--   system-wide Lua installation can be linked instead.
@package lua
@version 2.3.4


-- | The core Lua types, including mappings of Lua types to Haskell.
module Lua.Types

-- | An opaque structure that points to a thread and indirectly (through
--   the thread) to the whole state of a Lua interpreter. The Lua library
--   is fully reentrant: it has no global variables. All information about
--   a state is accessible through this structure.
--   
--   Synonym for <tt>lua_State *</tt>. See <a>lua_State</a>.
newtype State
State :: Ptr () -> State

-- | The reader function used by <tt><a>load</a></tt>. Every time it needs
--   another piece of the chunk, lua_load calls the reader, passing along
--   its data parameter. The reader must return a pointer to a block of
--   memory with a new piece of the chunk and set size to the block size.
--   The block must exist until the reader function is called again. To
--   signal the end of the chunk, the reader must return <tt>NULL</tt> or
--   set size to zero. The reader function may return pieces of any size
--   greater than zero.
--   
--   See <a>lua_Reader</a>.
type Reader = FunPtr State -> Ptr () -> Ptr CSize -> IO Ptr CChar

-- | Integer code used to encode the type of a Lua value.
newtype TypeCode
TypeCode :: CInt -> TypeCode
[fromTypeCode] :: TypeCode -> CInt

-- | Type for C functions.
--   
--   In order to communicate properly with Lua, a C function must use the
--   following protocol, which defines the way parameters and results are
--   passed: a C function receives its arguments from Lua in its stack in
--   direct order (the first argument is pushed first). So, when the
--   function starts, <tt><a>lua_gettop</a></tt> returns the number of
--   arguments received by the function. The first argument (if any) is at
--   index 1 and its last argument is at index <tt><a>lua_gettop</a></tt>.
--   To return values to Lua, a C function just pushes them onto the stack,
--   in direct order (the first result is pushed first), and returns the
--   number of results. Any other value in the stack below the results will
--   be properly discarded by Lua. Like a Lua function, a C function called
--   by Lua can also return many results.
--   
--   See <a>lua_CFunction</a>.
type CFunction = FunPtr PreCFunction

-- | Type of Haskell functions that can be turned into C functions.
--   
--   This is the same as a dereferenced <a>CFunction</a>.
type PreCFunction = State -> IO NumResults

-- | The type of warning functions, called by Lua to emit warnings. The
--   first parameter is an opaque pointer set by <tt>lua_setwarnf</tt>. The
--   second parameter is the warning message. The third parameter is a
--   boolean that indicates whether the message is to be continued by the
--   message in the next call.
--   
--   See <a>warn</a> for more details about warnings.
type WarnFunction = FunPtr Ptr () -> CString -> LuaBool -> IO ()

-- | Type of Haskell functions that can be turned into a WarnFunction.
--   
--   This is the same as a dereferenced <a>WarnFunction</a>.
type PreWarnFunction = Ptr () -> CString -> LuaBool -> IO ()

-- | Boolean value returned by a Lua C API function. This is a
--   <tt><a>CInt</a></tt> and should be interpreted as
--   <tt><a>False</a></tt> iff the value is <tt>0</tt>,
--   <tt><a>True</a></tt> otherwise.
newtype LuaBool
LuaBool :: CInt -> LuaBool

-- | The type of integers in Lua.
--   
--   By default this type is <tt><a>Int64</a></tt>, but that can be changed
--   to different values in Lua. (See <tt>LUA_INT_TYPE</tt> in
--   <tt>luaconf.h</tt>.)
--   
--   See <a>lua_Integer</a>.
newtype Integer
Integer :: Int64 -> Integer

-- | The type of floats in Lua.
--   
--   By default this type is <tt><a>Double</a></tt>, but that can be
--   changed in Lua to a single float or a long double. (See
--   <tt>LUA_FLOAT_TYPE</tt> in <tt>luaconf.h</tt>.)
--   
--   See <a>lua_Number</a>.
newtype Number
Number :: Double -> Number

-- | A stack index
newtype StackIndex
StackIndex :: CInt -> StackIndex
[fromStackIndex] :: StackIndex -> CInt

-- | The number of arguments consumed curing a function call.
newtype NumArgs
NumArgs :: CInt -> NumArgs
[fromNumArgs] :: NumArgs -> CInt

-- | The number of results returned by a function call.
newtype NumResults
NumResults :: CInt -> NumResults
[fromNumResults] :: NumResults -> CInt

-- | Relational operator code.
newtype OPCode
OPCode :: CInt -> OPCode

-- | Arithmetic operator code.
newtype ArithOPCode
ArithOPCode :: CInt -> ArithOPCode

-- | Integer code used to signal the status of a thread or computation.
newtype StatusCode
StatusCode :: CInt -> StatusCode

-- | Garbage-collection options.
newtype GCCode
GCCode :: CInt -> GCCode
instance GHC.Internal.Enum.Bounded Lua.Types.Integer
instance GHC.Internal.Enum.Enum Lua.Types.Integer
instance GHC.Internal.Enum.Enum Lua.Types.StackIndex
instance GHC.Classes.Eq Lua.Types.ArithOPCode
instance GHC.Classes.Eq Lua.Types.GCCode
instance GHC.Classes.Eq Lua.Types.Integer
instance GHC.Classes.Eq Lua.Types.LuaBool
instance GHC.Classes.Eq Lua.Types.NumArgs
instance GHC.Classes.Eq Lua.Types.NumResults
instance GHC.Classes.Eq Lua.Types.Number
instance GHC.Classes.Eq Lua.Types.OPCode
instance GHC.Classes.Eq Lua.Types.StackIndex
instance GHC.Classes.Eq Lua.Types.State
instance GHC.Classes.Eq Lua.Types.StatusCode
instance GHC.Classes.Eq Lua.Types.TypeCode
instance GHC.Internal.Float.Floating Lua.Types.Number
instance GHC.Internal.Real.Fractional Lua.Types.Number
instance GHC.Internal.Generics.Generic Lua.Types.State
instance GHC.Internal.Real.Integral Lua.Types.Integer
instance GHC.Internal.Num.Num Lua.Types.Integer
instance GHC.Internal.Num.Num Lua.Types.NumArgs
instance GHC.Internal.Num.Num Lua.Types.NumResults
instance GHC.Internal.Num.Num Lua.Types.Number
instance GHC.Internal.Num.Num Lua.Types.StackIndex
instance GHC.Classes.Ord Lua.Types.Integer
instance GHC.Classes.Ord Lua.Types.NumArgs
instance GHC.Classes.Ord Lua.Types.NumResults
instance GHC.Classes.Ord Lua.Types.Number
instance GHC.Classes.Ord Lua.Types.StackIndex
instance GHC.Classes.Ord Lua.Types.TypeCode
instance GHC.Internal.Read.Read Lua.Types.Integer
instance GHC.Internal.Read.Read Lua.Types.Number
instance GHC.Internal.Float.RealFloat Lua.Types.Number
instance GHC.Internal.Real.RealFrac Lua.Types.Number
instance GHC.Internal.Real.Real Lua.Types.Integer
instance GHC.Internal.Real.Real Lua.Types.Number
instance GHC.Internal.Show.Show Lua.Types.ArithOPCode
instance GHC.Internal.Show.Show Lua.Types.GCCode
instance GHC.Internal.Show.Show Lua.Types.Integer
instance GHC.Internal.Show.Show Lua.Types.LuaBool
instance GHC.Internal.Show.Show Lua.Types.NumArgs
instance GHC.Internal.Show.Show Lua.Types.NumResults
instance GHC.Internal.Show.Show Lua.Types.Number
instance GHC.Internal.Show.Show Lua.Types.OPCode
instance GHC.Internal.Show.Show Lua.Types.StackIndex
instance GHC.Internal.Show.Show Lua.Types.StatusCode
instance GHC.Internal.Show.Show Lua.Types.TypeCode
instance GHC.Internal.Foreign.Storable.Storable Lua.Types.ArithOPCode
instance GHC.Internal.Foreign.Storable.Storable Lua.Types.GCCode
instance GHC.Internal.Foreign.Storable.Storable Lua.Types.LuaBool
instance GHC.Internal.Foreign.Storable.Storable Lua.Types.OPCode
instance GHC.Internal.Foreign.Storable.Storable Lua.Types.StatusCode


-- | Lua standard libraries.
module Lua.Lib

-- | Pointer to function opening the base library.
luaopen_base :: CFunction

-- | Pointer to function opening the table library.
luaopen_table :: CFunction

-- | Pointer to function opening the io library.
luaopen_io :: CFunction

-- | Pointer to function opening the os library.
luaopen_os :: CFunction

-- | Pointer to function opening the string library.
luaopen_string :: CFunction

-- | Pointer to function opening the math library.
luaopen_math :: CFunction

-- | Pointer to function opening the debug library.
luaopen_debug :: CFunction

-- | Pointer to function opening the package library.
luaopen_package :: CFunction


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

-- | 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 <tt>op</tt> must be one of the following constants:
--   
--   <ul>
--   <li><b><tt>LUA_OPADD</tt>:</b> performs addition (<tt>+</tt>)</li>
--   <li><b><tt>LUA_OPSUB</tt>:</b> performs subtraction (<tt>-</tt>)</li>
--   <li><b><tt>LUA_OPMUL</tt>:</b> performs multiplication
--   (<tt>*</tt>)</li>
--   <li><b><tt>LUA_OPDIV</tt>:</b> performs float division
--   (<tt>/</tt>)</li>
--   <li><b><tt>LUA_OPIDIV</tt>:</b> performs floor division
--   (<tt>//</tt>)</li>
--   <li><b><tt>LUA_OPMOD</tt>:</b> performs modulo (<tt>%</tt>)</li>
--   <li><b><tt>LUA_OPPOW</tt>:</b> performs exponentiation
--   (<tt>^</tt>)</li>
--   <li><b><tt>LUA_OPUNM</tt>:</b> performs mathematical negation (unary
--   <tt>-</tt>)</li>
--   <li><b><tt>LUA_OPBNOT</tt>:</b> performs bitwise NOT (<tt>~</tt>)</li>
--   <li><b><tt>LUA_OPBAND</tt>:</b> performs bitwise AND
--   (<tt>&amp;</tt>)</li>
--   <li><b><tt>LUA_OPBOR</tt>:</b> performs bitwise OR (<tt>|</tt>)</li>
--   <li><b><tt>LUA_OPBXOR</tt>:</b> performs bitwise exclusive OR
--   (<tt>~</tt>)</li>
--   <li><b><tt>LUA_OPSHL</tt>:</b> performs left shift
--   (<tt>&lt;&lt;</tt>)</li>
--   <li><b><tt>LUA_OPSHR</tt>:</b> performs right shift
--   (<tt>&gt;&gt;</tt>)</li>
--   </ul>
--   
--   This function wraps <tt>lua_arith</tt> and takes an additional
--   parameter <tt>status</tt>; if it is not <tt>NULL</tt>, then the return
--   value is set to the status after calling <tt>lua_arith</tt>.
hslua_arith :: State -> ArithOPCode -> Ptr StatusCode -> IO ()

-- | Compares two Lua values. Returns <tt>1</tt> if the value at index
--   <tt>index1</tt> satisfies op when compared with the value at index
--   <tt>index2</tt>, following the semantics of the corresponding Lua
--   operator (that is, it may call metamethods). Otherwise returns
--   <tt>0</tt>. Also returns <tt>0</tt> if any of the indices is not
--   valid.
--   
--   The value of op must be one of the following constants:
--   
--   <ul>
--   <li><a>LUA_OPEQ</a>: compares for equality (==)</li>
--   <li><a>LUA_OPLT</a>: compares for less than (&lt;)</li>
--   <li><a>LUA_OPLE</a>: compares for less or equal (&lt;=)</li>
--   </ul>
--   
--   This function wraps <tt>lua_compare</tt> and takes an additional
--   parameter <tt>status</tt>; if it is not <tt>NULL</tt>, then the return
--   value is set to the status after calling <tt>lua_compare</tt>.
hslua_compare :: State -> StackIndex -> StackIndex -> OPCode -> Ptr StatusCode -> IO LuaBool

-- | Behaves like <tt><a>lua_gettable</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_gettable :: State -> StackIndex -> Ptr StatusCode -> IO TypeCode

-- | Behaves like <tt><a>lua_getglobal</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_getglobal :: State -> CString -> CSize -> Ptr StatusCode -> IO TypeCode

-- | Behaves like <tt><a>lua_settable</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_settable :: State -> StackIndex -> Ptr StatusCode -> IO ()

-- | Behaves like <tt><a>lua_setglobal</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_setglobal :: State -> CString -> CSize -> Ptr StatusCode -> IO ()

-- | Replacement for <a>lua_error</a>; it uses the HsLua error signaling
--   convention instead of raw Lua errors.
hslua_error :: State -> IO NumResults

-- | Wrapper around <a>lua_next</a> which catches any Lua errors.
hslua_next :: State -> StackIndex -> Ptr StatusCode -> IO LuaBool

-- | Wrapper around <a>lua_concat</a> which catches any Lua errors.
hslua_concat :: State -> NumArgs -> Ptr StatusCode -> IO ()


-- | Raw bindings to ersatz functions of the auxiliary library.
module Lua.Ersatz.Auxiliary

-- | Creates a new Lua state and set extra registry values for error
--   bookkeeping.
hsluaL_newstate :: IO State

-- | If <tt>modname</tt> is not already present in <tt>package.loaded</tt>.
--   calls function <tt>openf</tt> with string <tt>modname</tt> as an
--   argument and sets the call result in <tt>package.loaded[modname]</tt>,
--   as if that function has been called through <a>require</a>.
--   
--   If <tt>glb</tt> is true, also stores the module into global
--   <tt>modname</tt>.
--   
--   Leaves a copy of the module on the stack.
hsluaL_requiref :: State -> Ptr CChar -> CFunction -> LuaBool -> Ptr StatusCode -> IO ()

-- | Converts object to string, respecting any metamethods; returns
--   <tt>NULL</tt> if an error occurs.
hsluaL_tolstring :: State -> StackIndex -> Ptr CSize -> IO (Ptr CChar)


-- | Haskell bindings to Lua C API functions.
--   
--   The exposed functions correspond closely to the respective C Lua API
--   functions. However, C API functions which can throw Lua errors are not
--   exported directly, as any errors would crash the program. Non-error
--   throwing <tt>hslua_</tt> versions are provided instead. The
--   <tt>hslua</tt> ersatz functions have worse performance than the
--   original.
--   
--   Some of the Lua functions may, directly or indirectly, call a Haskell
--   function, and trigger garbage collection, rescheduling etc. These
--   functions are always imported safely (i.e., with the <tt>safe</tt>
--   keyword).
--   
--   However, all function can trigger garbage collection. If that can lead
--   to problems, then the package should be configured without flag
--   <tt>allow-unsafe-gc</tt>.
module Lua.Primary

-- | Converts the acceptable index <tt>idx</tt> into an equivalent absolute
--   index (that is, one that does not depend on the stack top).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_absindex</a>
lua_absindex :: State -> StackIndex -> IO StackIndex

-- | 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 <tt>op</tt> must be one of the following constants:
--   
--   <ul>
--   <li><b><tt>LUA_OPADD</tt>:</b> performs addition (<tt>+</tt>)</li>
--   <li><b><tt>LUA_OPSUB</tt>:</b> performs subtraction (<tt>-</tt>)</li>
--   <li><b><tt>LUA_OPMUL</tt>:</b> performs multiplication
--   (<tt>*</tt>)</li>
--   <li><b><tt>LUA_OPDIV</tt>:</b> performs float division
--   (<tt>/</tt>)</li>
--   <li><b><tt>LUA_OPIDIV</tt>:</b> performs floor division
--   (<tt>//</tt>)</li>
--   <li><b><tt>LUA_OPMOD</tt>:</b> performs modulo (<tt>%</tt>)</li>
--   <li><b><tt>LUA_OPPOW</tt>:</b> performs exponentiation
--   (<tt>^</tt>)</li>
--   <li><b><tt>LUA_OPUNM</tt>:</b> performs mathematical negation (unary
--   <tt>-</tt>)</li>
--   <li><b><tt>LUA_OPBNOT</tt>:</b> performs bitwise NOT (<tt>~</tt>)</li>
--   <li><b><tt>LUA_OPBAND</tt>:</b> performs bitwise AND
--   (<tt>&amp;</tt>)</li>
--   <li><b><tt>LUA_OPBOR</tt>:</b> performs bitwise OR (<tt>|</tt>)</li>
--   <li><b><tt>LUA_OPBXOR</tt>:</b> performs bitwise exclusive OR
--   (<tt>~</tt>)</li>
--   <li><b><tt>LUA_OPSHL</tt>:</b> performs left shift
--   (<tt>&lt;&lt;</tt>)</li>
--   <li><b><tt>LUA_OPSHR</tt>:</b> performs right shift
--   (<tt>&gt;&gt;</tt>)</li>
--   </ul>
--   
--   <b>WARNING</b>: <tt>lua_arith</tt> is unsafe in Haskell: if the call
--   to a metamethod triggers an error, then that error cannot be handled
--   and will lead to an unrecoverable program crash. Consider using the
--   <tt><a>hslua_arith</a></tt> ersatz function instead. Likewise, the
--   metamethod may not call a Haskell function unless the library was
--   compiled without <tt>allow-unsafe-gc</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_arith</a>.

-- | <i>Warning: This is an unsafe function, errors will lead to a program
--   crash;consider using hslua_arith instead.</i>
lua_arith :: State -> ArithOPCode -> IO ()

-- | Ensures that the stack has space for at least <tt>n</tt> extra slots
--   (that is, that you can safely push up to <tt>n</tt> values into it).
--   It returns false if it cannot fulfill the request, either because it
--   would cause the stack to be larger than a fixed maximum size
--   (typically at least several thousand elements) or because it cannot
--   allocate memory for the extra space. This function never shrinks the
--   stack; if the stack already has space for the extra slots, it is left
--   unchanged.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_checkstack</a>
lua_checkstack :: State -> CInt -> IO LuaBool

-- | Destroys all objects in the given Lua state (calling the corresponding
--   garbage-collection metamethods, if any) and frees all dynamic memory
--   used by this state. In several platforms, you may not need to call
--   this function, because all resources are naturally released when the
--   host program ends. On the other hand, long-running programs that
--   create multiple states, such as daemons or web servers, will probably
--   need to close states as soon as they are not needed.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_close</a>
lua_close :: State -> IO ()

-- | Function pointer to function <a>lua_close</a>; intended to be used
--   with <a>newForeignPtr</a>.
lua_close_ptr :: FunPtr (Ptr () -> IO ())

-- | Concatenates the <tt>n</tt> values at the top of the stack, pops them,
--   and leaves the result at the top. If <tt>n</tt> is 1, the result is
--   the single value on the stack (that is, the function does nothing); if
--   <tt>n</tt> is 0, the result is the empty string. Concatenation is
--   performed following the usual semantics of Lua (see <a>§3.4.6</a> of
--   the Lua manual).
--   
--   <b>WARNING</b>: <tt>lua_concat</tt> is unsafe in Haskell: This
--   function will cause an unrecoverable crash an error if any of the
--   concatenated values causes an error when executing a metamethod.
--   Consider using the <tt><a>hslua_concat</a></tt> ersatz function
--   instead.

-- | <i>Warning: This is an unsafe function, it will cause a program crash
--   ifa metamethod throws an error.Consider using hslua_concat
--   instead.</i>
lua_concat :: State -> CInt -> IO ()

-- | Copies the element at index <tt>fromidx</tt> into the valid index
--   <tt>toidx</tt>, replacing the value at that position. Values at other
--   positions are not affected.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_copy</a>
lua_copy :: State -> StackIndex -> StackIndex -> IO ()

-- | Creates a new empty table and pushes it onto the stack. Parameter
--   <tt>narr</tt> is a hint for how many elements the table will have as a
--   sequence; parameter <tt>nrec</tt> is a hint for how many other
--   elements the table will have. Lua may use these hints to preallocate
--   memory for the new table. This preallocation is useful for performance
--   when you know in advance how many elements the table will have.
--   Otherwise you can use the function <tt>lua_newtable</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_createtable</a>.
lua_createtable :: State -> CInt -> CInt -> IO ()

-- | Controls the garbage collector.
--   
--   See the Lua docs at
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_gc</a>. Unused dataN
--   values should be set to 0, but can be anything.
lua_gc :: State -> GCCode -> CInt -> CInt -> CInt -> IO CInt

-- | Pushes onto the stack the value of the global name. Returns the type
--   of that value.
--   
--   <b>WARNING</b>: <tt>lua_getglobal</tt> is unsafe in Haskell: if the
--   call to a metamethod triggers an error, then that error cannot be
--   handled and will lead to an unrecoverable program crash. Consider
--   using the <tt><a>hslua_getglobal</a></tt> ersatz function instead.
--   Likewise, the metamethod may not call a Haskell function unless the
--   library was compiled without <tt>allow-unsafe-gc</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_getglobal</a>.

-- | <i>Warning: This is an unsafe function, errors will lead to a program
--   crash;consider using hslua_getglobal instead.</i>
lua_getglobal :: State -> CString -> IO TypeCode

-- | If the value at the given index has a metatable, the function pushes
--   that metatable onto the stack and returns <tt>1</tt>. Otherwise, the
--   function returns <tt>0</tt> and pushes nothing on the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_getmetatable</a>.
lua_getmetatable :: State -> StackIndex -> IO LuaBool

-- | Pushes onto the stack the value <tt>t[k]</tt>, where <tt>t</tt> is the
--   value at the given index and <tt>k</tt> is the value at the top of the
--   stack.
--   
--   This function pops the key from the stack, pushing the resulting value
--   in its place. As in Lua, this function may trigger a metamethod for
--   the "index" event (see <a>§2.4</a>).
--   
--   Returns the type of the pushed value.
--   
--   <b>WARNING</b>: <tt>lua_gettable</tt> is unsafe in Haskell: if the
--   call to a metamethod triggers an error, then that error cannot be
--   handled and will lead to an unrecoverable program crash. Consider
--   using the <tt><a>hslua_gettable</a></tt> ersatz function instead.
--   Likewise, the metamethod may not call a Haskell function unless the
--   library was compiled without <tt>allow-unsafe-gc</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_gettable</a>.

-- | <i>Warning: This is an unsafe function, errors will lead to a program
--   crash;consider using hslua_gettable instead.</i>
lua_gettable :: State -> StackIndex -> IO TypeCode

-- | Returns the index of the top element in the stack. Because indices
--   start at 1, this result is equal to the number of elements in the
--   stack (and so 0 means an empty stack).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_gettop</a>
lua_gettop :: State -> IO StackIndex

-- | Pushes onto the stack the <tt>n</tt>-th user value associated with the
--   full userdata at the given index and returns the type of the pushed
--   value.
--   
--   If the userdata does not have that value, pushes <b>nil</b> and
--   returns <tt>LUA_TNONE</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_getiuservalue</a>
lua_getiuservalue :: State -> StackIndex -> CInt -> IO TypeCode

-- | Moves the top element into the given valid index, shifting up the
--   elements above this index to open space. This function cannot be
--   called with a pseudo-index, because a pseudo-index is not an actual
--   stack position.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_insert</a>
lua_insert :: State -> StackIndex -> IO ()

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   boolean, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isboolean</a>
lua_isboolean :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a C
--   function, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_iscfunction</a>
lua_iscfunction :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   function (either C or Lua), and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isfunction</a>
lua_isfunction :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is an
--   integer (that is, the value is a number and is represented as an
--   integer), and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isinteger</a>
lua_isinteger :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   light userdata, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_islightuserdata</a>
lua_islightuserdata :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is
--   <b>nil</b>, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isnil</a>
lua_isnil :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the given index is not valid, and
--   <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isnone</a>
lua_isnone :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the given index is not valid or if the
--   value at the given index is <b>nil</b>, and <tt><a>FALSE</a></tt>
--   otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isnoneornil</a>
lua_isnoneornil :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   number or a string convertible to a number, and <tt><a>FALSE</a></tt>
--   otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isnumber</a>
lua_isnumber :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   string or a number (which is always convertible to a string), and
--   <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isstring</a>
lua_isstring :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   table, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_istable</a>
lua_istable :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   thread, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isthread</a>
lua_isthread :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   userdata (either full or light), and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isuserdata</a>
lua_isuserdata :: State -> StackIndex -> IO LuaBool

-- | Loads a Lua chunk (without running it). If there are no errors,
--   <tt>lua_load</tt> pushes the compiled chunk as a Lua function on top
--   of the stack. Otherwise, it pushes an error message.
--   
--   The return values of <tt>lua_load</tt> are:
--   
--   <ul>
--   <li><tt><a>LUA_OK</a></tt>: no errors;</li>
--   <li><tt><a>LUA_ERRSYNTAX</a></tt>: syntax error during
--   pre-compilation;</li>
--   <li><tt><a>LUA_ERRMEM</a></tt>: memory allocation error;</li>
--   <li><tt><a>LUA_ERRGCMM</a></tt>: error while running a <tt>__gc</tt>
--   metamethod. (This error has no relation with the chunk being loaded.
--   It is generated by the garbage collector.)</li>
--   </ul>
--   
--   This function only loads a chunk; it does not run it.
--   
--   <tt>lua_load</tt> automatically detects whether the chunk is text or
--   binary, and loads it accordingly (see program luac).
--   
--   The <tt>lua_load</tt> function uses a user-supplied reader function to
--   read the chunk (see <tt><a>Reader</a></tt>). The data argument is an
--   opaque value passed to the reader function.
--   
--   The <tt>chunkname</tt> argument gives a name to the chunk, which is
--   used for error messages and in debug information (see <a>§4.7</a>).
--   
--   <tt>lua_load</tt> automatically detects whether the chunk is text or
--   binary and loads it accordingly (see program <tt>luac</tt>). The
--   string mode works as in function <tt>load</tt>, with the addition that
--   a <tt>NULL</tt> value is equivalent to the string "bt".
--   
--   <tt>lua_load</tt> uses the stack internally, so the reader function
--   must always leave the stack unmodified when returning.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_load</a>.
lua_load :: State -> Reader -> Ptr () -> CString -> CString -> IO StatusCode

-- | Creates a new thread, pushes it on the stack, and returns a
--   <a>State</a> that represents this new thread. The new thread returned
--   by this function shares with the original thread its global
--   environment, but has an independent execution stack.
--   
--   There is no explicit function to close or to destroy a thread. Threads
--   are subject to garbage collection, like any Lua object.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_newthread</a>
lua_newthread :: State -> IO State

-- | This function creates and pushes on the stack a new full userdata,
--   with <tt>nuvalue</tt> associated Lua values, called <tt>user
--   values</tt>, plus an associated block of raw memory with <tt>size</tt>
--   bytes. (The user values can be set and read with the functions
--   <a>lua_setiuservalue</a> and <a>lua_getiuservalue</a>.)
--   
--   The function returns the address of the block of memory. Lua ensures
--   that this address is valid as long as the corresponding userdata is
--   alive (see <a>§2.5</a>). Moreover, if the userdata is marked for
--   finalization (see <a>§2.5.3</a>), its address is valid at least until
--   the call to its finalizer.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_newuserdatauv</a>.
lua_newuserdatauv :: State -> CSize -> CInt -> IO (Ptr ())

-- | Pops a key from the stack, and pushes a key–value pair from the table
--   at the given index (the "next" pair after the given key). If there are
--   no more elements in the table, then <a>lua_next</a> returns
--   <a>FALSE</a> (and pushes nothing).
--   
--   A typical traversal looks like this:
--   
--   <pre>
--   -- table is in the stack at index 't'
--   lua_pushnil l    -- first key
--   let loop = lua_next l t &gt;&gt;= \case
--         FALSE -&gt; return ()
--         _ -&gt; do
--           lua_type l (-2) &gt;&gt;= lua_typename l &gt;&gt;= peekCString &gt;&gt;= putStrLn
--           lua_type l (-1) &gt;&gt;= lua_typename l &gt;&gt;= peekCString &gt;&gt;= putStrLn
--           -- removes 'value'; keeps 'key' for next iteration
--           lua_pop l 1
--           loop
--   loop
--   </pre>
--   
--   While traversing a table, do not call <a>lua_tolstring</a> directly on
--   a key, unless you know that the key is actually a string. Recall that
--   <a>lua_tolstring</a> may change the value at the given index; this
--   confuses the next call to <a>lua_next</a>.
--   
--   See function <a>next</a> for the caveats of modifying the table during
--   its traversal.
--   
--   <b>WARNING</b>: <tt>lua_next</tt> is unsafe in Haskell: This function
--   will cause an unrecoverable crash an error if the given key is neither
--   <tt>nil</tt> nor present in the table. Consider using the
--   <tt><a>hslua_next</a></tt> ersatz function instead.

-- | <i>Warning: This is an unsafe function, it will cause a program crash
--   ifthe given key is neither nil nor present in the table.Consider using
--   hslua_next instead.</i>
lua_next :: State -> StackIndex -> IO LuaBool

-- | Calls a function in protected mode.
--   
--   To call a function you must use the following protocol: first, the
--   function to be called is pushed onto the stack; then, the arguments to
--   the function are pushed in direct order; that is, the first argument
--   is pushed first. Finally you call <tt>lua_pcall</tt>; <tt>nargs</tt>
--   is the number of arguments that you pushed onto the stack. All
--   arguments and the function value are popped from the stack when the
--   function is called. The function results are pushed onto the stack
--   when the function returns. The number of results is adjusted to
--   <tt>nresults</tt>, unless <tt>nresults</tt> is
--   <tt><a>LUA_MULTRET</a></tt>. In this case, all results from the
--   function are pushed. Lua takes care that the returned values fit into
--   the stack space. The function results are pushed onto the stack in
--   direct order (the first result is pushed first), so that after the
--   call the last result is on the top of the stack.
--   
--   If there is any error, <tt>lua_pcall</tt> catches it, pushes a single
--   value on the stack (the error message), and returns the error code.
--   <tt>lua_pcall</tt> always removes the function and its arguments from
--   the stack.
--   
--   If <tt>msgh</tt> is <tt>0</tt>, then the error object returned on the
--   stack is exactly the original error object. Otherwise, <tt>msgh</tt>
--   is the location of a message handler. (This index cannot be a
--   pseudo-index.) In case of runtime errors, this function will be called
--   with the error object and its return value will be the object returned
--   on the stack by <tt><a>lua_pcall</a></tt>.
--   
--   Typically, the message handler is used to add more debug information
--   to the error object, such as a stack traceback. Such information
--   cannot be gathered after the return of <tt><a>lua_pcall</a></tt>,
--   since by then the stack has unwound.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pcall</a>.
lua_pcall :: State -> NumArgs -> NumResults -> StackIndex -> IO StatusCode

-- | Pops <tt>n</tt> elements from the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pop</a>
lua_pop :: State -> CInt -> IO ()

-- | Pushes a boolean value with the given value onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushboolean</a>.
lua_pushboolean :: State -> LuaBool -> IO ()

-- | Pushes a new C closure onto the stack.
--   
--   When a C function is created, it is possible to associate some values
--   with it, thus creating a C closure (see <a>§3.4</a>); these values are
--   then accessible to the function whenever it is called. To associate
--   values with a C function, first these values should be pushed onto the
--   stack (when there are multiple values, the first value is pushed
--   first). Then lua_pushcclosure is called to create and push the C
--   function onto the stack, with the argument <tt>n</tt> telling how many
--   values should be associated with the function. lua_pushcclosure also
--   pops these values from the stack.
--   
--   The maximum value for <tt>n</tt> is 255.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushcclosure</a>.
lua_pushcclosure :: State -> CFunction -> NumArgs -> IO ()

-- | Pushes a C function onto the stack. This function receives a pointer
--   to a C function and pushes onto the stack a Lua value of type
--   <tt>function</tt> that, when called, invokes the corresponding C
--   function.
--   
--   Any function to be callable by Lua must follow the correct protocol to
--   receive its parameters and return its results (see <a>CFunction</a>).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushcfunction</a>.
lua_pushcfunction :: State -> CFunction -> IO ()

-- | Pushes the global environment onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushglobaltable</a>
lua_pushglobaltable :: State -> IO ()

-- | Pushes an integer with with the given value onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushinteger</a>.
lua_pushinteger :: State -> Integer -> IO ()

-- | Pushes a light userdata onto the stack.
--   
--   Userdata represent C values in Lua. A light userdata represents a
--   pointer, a <tt>Ptr ()</tt> (i.e., <tt>void*</tt> in C lingo). It is a
--   value (like a number): you do not create it, it has no individual
--   metatable, and it is not collected (as it was never created). A light
--   userdata is equal to "any" light userdata with the same C address.
--   
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushlightuserdata</a>.
lua_pushlightuserdata :: State -> Ptr a -> IO ()

-- | Pushes the string pointed to by <tt>s</tt> with size <tt>len</tt> onto
--   the stack. Lua makes (or reuses) an internal copy of the given string,
--   so the memory at s can be freed or reused immediately after the
--   function returns. The string can contain any binary data, including
--   embedded zeros.
--   
--   Returns a pointer to the internal copy of the string.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushlstring</a>.
lua_pushlstring :: State -> Ptr CChar -> CSize -> IO ()

-- | Pushes a nil value onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushnil</a>.
lua_pushnil :: State -> IO ()

-- | Pushes a float with the given value onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushnumber</a>.
lua_pushnumber :: State -> Number -> IO ()

-- | Pushes the zero-terminated string pointed to by <tt>s</tt> onto the
--   stack. Lua makes (or reuses) an internal copy of the given string, so
--   the memory at <tt>s</tt> can be freed or reused immediately after the
--   function returns.
--   
--   Returns a pointer to the internal copy of the string.
--   
--   If s is NULL, pushes nil and returns NULL.
lua_pushstring :: State -> CString -> IO CString

-- | Pushes the current thread onto the stack. Returns <tt>1</tt> iff this
--   thread is the main thread of its state.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushthread</a>.
lua_pushthread :: State -> IO CInt

-- | Pushes a copy of the element at the given index onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushvalue</a>
lua_pushvalue :: State -> StackIndex -> IO ()

-- | Returns <tt>True</tt> if the two values in indices <tt>idx1</tt> and
--   <tt>idx2</tt> are primitively equal (that is, without calling the
--   <tt>__eq</tt> metamethod). Otherwise returns <tt>False</tt>. Also
--   returns <tt>False</tt> if any of the indices are not valid.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawequal</a>
lua_rawequal :: State -> StackIndex -> StackIndex -> IO LuaBool

-- | Similar to <tt><a>lua_gettable</a></tt>, but does a raw access (i.e.,
--   without metamethods).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawget</a>.
lua_rawget :: State -> StackIndex -> IO TypeCode

-- | Pushes onto the stack the value <tt>t[n]</tt>, where <tt>t</tt> is the
--   table at the given index. The access is raw, that is, it does not
--   invoke the <tt>__index</tt> metamethod.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawgeti</a>.
lua_rawgeti :: State -> StackIndex -> Integer -> IO TypeCode

-- | Returns the raw "length" of the value at the given index: for strings,
--   this is the string length; for tables, this is the result of the
--   length operator (<tt>#</tt>) with no metamethods; for userdata, this
--   is the size of the block of memory allocated for the userdata; for
--   other values, it is 0.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawlen</a>.
lua_rawlen :: State -> StackIndex -> IO CSize

-- | Similar to <tt><a>lua_settable</a></tt>, but does a raw assignment
--   (i.e., without metamethods).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawset</a>.
lua_rawset :: State -> StackIndex -> IO ()

-- | Does the equivalent of <tt>t[i] = v</tt>, where <tt>t</tt> is the
--   table at the given index and <tt>v</tt> is the value at the top of the
--   stack.
--   
--   This function pops the value from the stack. The assignment is raw,
--   that is, it does not invoke the <tt>__newindex</tt> metamethod.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawseti</a>.
lua_rawseti :: State -> StackIndex -> Integer -> IO ()

-- | Removes the element at the given valid index, shifting down the
--   elements above this index to fill the gap. This function cannot be
--   called with a pseudo-index, because a pseudo-index is not an actual
--   stack position.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_remove</a>
lua_remove :: State -> StackIndex -> IO ()

-- | Moves the top element into the given valid index without shifting any
--   element (therefore replacing the value at that given index), and then
--   pops the top element.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_replace</a>
lua_replace :: State -> StackIndex -> IO ()

-- | Rotates the stack elements between the valid index <tt>idx</tt> and
--   the top of the stack. The elements are rotated <tt>n</tt> positions in
--   the direction of the top, for a positive <tt>n</tt>, or <tt>-n</tt>
--   positions in the direction of the bottom, for a negative <tt>n</tt>.
--   The absolute value of <tt>n</tt> must not be greater than the size of
--   the slice being rotated. This function cannot be called with a
--   pseudo-index, because a pseudo-index is not an actual stack position.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rotate</a>
lua_rotate :: State -> StackIndex -> CInt -> IO ()

-- | Pops a value from the stack and sets it as the new value of global
--   <tt>name</tt>.
--   
--   <b>WARNING</b>: <tt>lua_setglobal</tt> is unsafe in Haskell: if the
--   call to a metamethod triggers an error, then that error cannot be
--   handled and will lead to an unrecoverable program crash. Consider
--   using the <tt><a>hslua_setglobal</a></tt> ersatz function instead.
--   Likewise, the global metamethod may not call a Haskell function unless
--   the library was compiled without <tt>allow-unsafe-gc</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_setglobal</a>.

-- | <i>Warning: This is an unsafe function, errors will lead to a program
--   crash;consider using hslua_getglobal instead.</i>
lua_setglobal :: State -> CString -> IO ()

-- | Pops a table from the stack and sets it as the new metatable for the
--   value at the given index.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_setmetatable</a>.
lua_setmetatable :: State -> StackIndex -> IO ()

-- | Does the equivalent to <tt>t[k] = v</tt>, where <tt>t</tt> is the
--   value at the given index, <tt>v</tt> is the value at the top of the
--   stack, and <tt>k</tt> is the value just below the top.
--   
--   This function pops both the key and the value from the stack. As in
--   Lua, this function may trigger a metamethod for the "newindex" event
--   (see <a>§2.4</a>).
--   
--   <b>WARNING</b>: <tt>lua_settable</tt> is unsafe in Haskell: if the
--   call to a metamethod triggers an error, then that error cannot be
--   handled and will lead to an unrecoverable program crash. Consider
--   using the <tt><a>hslua_settable</a></tt> ersatz function instead.
--   Likewise, the metamethod may not call a Haskell function unless the
--   library was compiled without <tt>allow-unsafe-gc</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_settable</a>

-- | <i>Warning: This is an unsafe function, errors will lead to a program
--   crash;consider using hslua_settable instead.</i>
lua_settable :: State -> StackIndex -> IO ()

-- | Accepts any index, or 0, and sets the stack top to this index. If the
--   new top is larger than the old one, then the new elements are filled
--   with *nil*. If <tt>index</tt> is 0, then all stack elements are
--   removed.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_settop</a>
lua_settop :: State -> StackIndex -> IO ()

-- | Pops a value from the stack and sets it as the new <tt>n</tt>-th user
--   value associated to the full userdata at the given index. Returns 0 if
--   the userdata does not have that value.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_setiuservalue</a>
lua_setiuservalue :: State -> StackIndex -> CInt -> IO LuaBool

-- | Sets the warning function to be used by Lua to emit warnings (see
--   <a>WarnFunction</a>). The <tt>ud</tt> parameter sets the value
--   <tt>ud</tt> passed to the warning function.
lua_setwarnf :: State -> WarnFunction -> Ptr () -> IO ()

-- | Returns the status of this Lua thread.
--   
--   The status can be <tt><a>LUA_OK</a></tt> for a normal thread, an error
--   value if the thread finished the execution of a <tt>lua_resume</tt>
--   with an error, or <tt><a>LUA_YIELD</a></tt> if the thread is
--   suspended.
--   
--   You can only call functions in threads with status
--   <tt><a>LUA_OK</a></tt>. You can resume threads with status
--   <tt><a>LUA_OK</a></tt> (to start a new coroutine) or
--   <tt><a>LUA_YIELD</a></tt> (to resume a coroutine).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_status</a>.
lua_status :: State -> IO StatusCode

-- | Converts the zero-terminated string <tt>s</tt> to a number, pushes
--   that number into the stack, and returns the total size of the string,
--   that is, its length plus one. The conversion can result in an integer
--   or a float, according to the lexical conventions of Lua (see
--   <a>§3.1</a>). The string may have leading and trailing spaces and a
--   sign. If the string is not a valid numeral, returns 0 and pushes
--   nothing. (Note that the result can be used as a boolean, true if the
--   conversion succeeds.)
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_stringtonumber</a>.
lua_stringtonumber :: State -> CString -> IO CSize

-- | Converts the Lua value at the given index to a haskell boolean value.
--   Like all tests in Lua, <tt>toboolean</tt> returns <tt>True</tt> for
--   any Lua value different from *false* and *nil*; otherwise it returns
--   <tt>False</tt>. (If you want to accept only actual boolean values, use
--   <tt><a>lua_isboolean</a></tt> to test the value's type.)
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_toboolean</a>
lua_toboolean :: State -> StackIndex -> IO LuaBool

-- | Converts a value at the given index to a C function. That value must
--   be a C function; otherwise, returns <tt>Nothing</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_tocfunction</a>
lua_tocfunction :: State -> StackIndex -> IO CFunction

-- | Converts the Lua value at the given acceptable index to the signed
--   integral type <a>Integer</a>. The Lua value must be an integer, a
--   number, or a string convertible to an integer (see <a>§3.4.3</a> of
--   the Lua 5.4 Reference Manual); otherwise, <tt>lua_tointegerx</tt>
--   returns <tt>0</tt>.
--   
--   If the number is not an integer, it is truncated in some non-specified
--   way.
--   
--   If <tt>isnum</tt> is not <tt>NULL</tt>, its referent is assigned a
--   boolean value that indicates whether the operation succeeded.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_tointegerx</a>
lua_tointegerx :: State -> StackIndex -> Ptr LuaBool -> IO Integer

-- | Converts the Lua value at the given index to a C string. If
--   <tt>len</tt> is not <tt>NULL</tt>, it sets the referent with the
--   string length. The Lua value must be a string or a number; otherwise,
--   the function returns <tt>NULL</tt>. If the value is a number, then
--   <tt>lua_tolstring</tt> also changes the actual value in the stack to a
--   string. (This change confuses <tt>lua_next</tt> when
--   <tt>lua_tolstring</tt> is applied to keys during a table traversal.)
--   
--   <tt>lua_tolstring</tt> returns a pointer to a string inside the Lua
--   state. This string always has a zero ('0') after its last character
--   (as in C), but can contain other zeros in its body.
--   
--   Because Lua has garbage collection, there is no guarantee that the
--   pointer returned by <tt>lua_tolstring</tt> will be valid after the
--   corresponding Lua value is removed from the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_tolstring</a>
lua_tolstring :: State -> StackIndex -> Ptr CSize -> IO (Ptr CChar)

-- | Converts the Lua value at the given index to the C type lua_Number
--   (see lua_Number). The Lua value must be a number or a string
--   convertible to a number (see §3.4.3); otherwise, lua_tonumberx returns
--   0.
--   
--   If <tt>isnum</tt> is not <tt>NULL</tt>, its referent is assigned a
--   boolean value that indicates whether the operation succeeded.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_tonumberx</a>
lua_tonumberx :: State -> StackIndex -> Ptr LuaBool -> IO Number

-- | Converts the value at the given index to a generic C pointer
--   (<tt>void*</tt>). The value can be a userdata, a table, a thread, or a
--   function; otherwise, <tt>lua_topointer</tt> returns
--   <tt><a>nullPtr</a></tt>. Different objects will give different
--   pointers. There is no way to convert the pointer back to its original
--   value.
--   
--   Typically this function is used only for hashing and debug
--   information.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_topointer</a>
lua_topointer :: State -> StackIndex -> IO (Ptr ())

-- | Converts the value at the given index to a Lua thread (represented as
--   <tt><a>State</a></tt>). This value must be a thread; otherwise, the
--   function returns <tt><a>nullPtr</a></tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_tothread</a>
lua_tothread :: State -> StackIndex -> IO State

-- | If the value at the given index is a full userdata, returns its block
--   address. If the value is a light userdata, returns its pointer.
--   Otherwise, returns <tt><a>nullPtr</a></tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_touserdata</a>
lua_touserdata :: State -> StackIndex -> IO (Ptr a)

-- | Returns the type of the value in the given valid index, or
--   <tt><a>LUA_TNONE</a></tt> for a non-valid (but acceptable) index.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_type</a>
lua_type :: State -> StackIndex -> IO TypeCode

-- | Returns the name of the type encoded by the value <tt>tp</tt>, which
--   must be one the values returned by <tt><a>lua_type</a></tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_typename</a>
lua_typename :: State -> TypeCode -> IO CString

-- | Returns the address of the version number (a C static variable) stored
--   in the Lua core. When called with a valid <a>State</a>, returns the
--   address of the version used to create that state. When called with
--   <tt>NULL</tt>, returns the address of the version running the call.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_version</a>
lua_version :: State -> IO (Ptr Number)

-- | Emits a warning with the given message. A message in a call with
--   <tt>tocont</tt> true should be continued in another call to this
--   function.
--   
--   See <a>warn</a> for more details about warnings.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_warning</a>
lua_warning :: State -> CString -> LuaBool -> IO ()


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

-- | Behaves like <tt><a>lua_gettable</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_gettable :: State -> StackIndex -> Ptr StatusCode -> IO TypeCode

-- | Behaves like <tt><a>lua_getglobal</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_getglobal :: State -> CString -> CSize -> Ptr StatusCode -> IO TypeCode

-- | Behaves like <tt><a>lua_settable</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_settable :: State -> StackIndex -> Ptr StatusCode -> IO ()

-- | Behaves like <tt><a>lua_setglobal</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_setglobal :: State -> CString -> CSize -> Ptr StatusCode -> IO ()

-- | Replacement for <a>lua_error</a>; it uses the HsLua error signaling
--   convention instead of raw Lua errors.
hslua_error :: State -> IO NumResults

-- | Wrapper around <a>lua_next</a> which catches any Lua errors.
hslua_next :: State -> StackIndex -> Ptr StatusCode -> IO LuaBool

-- | Wrapper around <a>lua_concat</a> which catches any Lua errors.
hslua_concat :: State -> NumArgs -> Ptr StatusCode -> IO ()

-- | Compares two Lua values. Returns <tt>1</tt> if the value at index
--   <tt>index1</tt> satisfies op when compared with the value at index
--   <tt>index2</tt>, following the semantics of the corresponding Lua
--   operator (that is, it may call metamethods). Otherwise returns
--   <tt>0</tt>. Also returns <tt>0</tt> if any of the indices is not
--   valid.
--   
--   The value of op must be one of the following constants:
--   
--   <ul>
--   <li><a>LUA_OPEQ</a>: compares for equality (==)</li>
--   <li><a>LUA_OPLT</a>: compares for less than (&lt;)</li>
--   <li><a>LUA_OPLE</a>: compares for less or equal (&lt;=)</li>
--   </ul>
--   
--   This function wraps <tt>lua_compare</tt> and takes an additional
--   parameter <tt>status</tt>; if it is not <tt>NULL</tt>, then the return
--   value is set to the status after calling <tt>lua_compare</tt>.
hslua_compare :: State -> StackIndex -> StackIndex -> OPCode -> Ptr StatusCode -> IO LuaBool

-- | Creates a new Lua state and set extra registry values for error
--   bookkeeping.
hsluaL_newstate :: IO State

-- | Converts object to string, respecting any metamethods; returns
--   <tt>NULL</tt> if an error occurs.
hsluaL_tolstring :: State -> StackIndex -> Ptr CSize -> IO (Ptr CChar)


-- | Haskell bindings to Lua's debug interface.
module Lua.Debug

-- | Gets information about the <tt>n</tt>-th upvalue of the closure at
--   index <tt>funcindex</tt>. It pushes the upvalue's value onto the stack
--   and returns its name. Returns <tt>NULL</tt> (and pushes nothing) when
--   the index <tt>n</tt> is greater than the number of upvalues.
--   
--   See <a>debug.getupvalue</a> for more information about upvalues.
--   
--   <pre>
--   [0, +(0|1), -]
--   </pre>
--   
--   <a>lua_getupvalue</a>.
lua_getupvalue :: State -> StackIndex -> CInt -> IO CString

-- | Sets the value of a closure’s upvalue. It assigns the value on the top
--   of the stack to the upvalue and returns its name. It also pops the
--   value from the stack.
--   
--   Returns <tt>NULL</tt> (and pops nothing) when the index <tt>n</tt> is
--   greater than the number of upvalues.
--   
--   Parameters <tt>funcindex</tt> and <tt>n</tt> are as in the function
--   <a>lua_getupvalue</a>.
--   
--   <pre>
--   [-(0|1), +0, -]
--   </pre>
--   
--   <a>lua_setupvalue</a>.
lua_setupvalue :: State -> StackIndex -> CInt -> IO CString


-- | Lua constants
module Lua.Constants

-- | Lua version information in the form "<tt>Lua MAJOR.MINOR</tt>".
pattern LUA_VERSION :: String

-- | Lua version information in the form "<tt>Lua
--   MAJOR.MINOR.RELEASE</tt>".
pattern LUA_RELEASE :: String

-- | Lua copyright information; includes the Lua release
pattern LUA_COPYRIGHT :: String

-- | Option for multiple returns in <tt><a>lua_pcall</a></tt>.
pattern LUA_MULTRET :: NumResults

-- | Stack index of the Lua registry.
pattern LUA_REGISTRYINDEX :: StackIndex

-- | Non-valid stack index
pattern LUA_TNONE :: TypeCode

-- | Type of Lua's <tt>nil</tt> value
pattern LUA_TNIL :: TypeCode

-- | Type of Lua booleans
pattern LUA_TBOOLEAN :: TypeCode

-- | Type of light userdata
pattern LUA_TLIGHTUSERDATA :: TypeCode

-- | Type of Lua numbers. See <tt><a>Number</a></tt>
pattern LUA_TNUMBER :: TypeCode

-- | Type of Lua string values
pattern LUA_TSTRING :: TypeCode

-- | Type of Lua tables
pattern LUA_TTABLE :: TypeCode

-- | Type of functions, either normal or <tt><a>CFunction</a></tt>
pattern LUA_TFUNCTION :: TypeCode

-- | Type of full user data
pattern LUA_TUSERDATA :: TypeCode

-- | Type of Lua threads
pattern LUA_TTHREAD :: TypeCode

-- | Success.
pattern LUA_OK :: StatusCode

-- | Yielding / suspended coroutine.
pattern LUA_YIELD :: StatusCode

-- | A runtime error.
pattern LUA_ERRRUN :: StatusCode

-- | A syntax error.
pattern LUA_ERRSYNTAX :: StatusCode

-- | Memory allocation error. For such errors, Lua does not call the
--   message handler.
pattern LUA_ERRMEM :: StatusCode

-- | Error while running the message handler.
pattern LUA_ERRERR :: StatusCode

-- | File related error (e.g., the file cannot be opened or read).
pattern LUA_ERRFILE :: StatusCode

-- | Compares for equality (==)
pattern LUA_OPEQ :: OPCode

-- | Compares for less than (&lt;)
pattern LUA_OPLT :: OPCode

-- | Compares for less or equal (&lt;=)
pattern LUA_OPLE :: OPCode

-- | Performs addition (<tt>+</tt>).
pattern LUA_OPADD :: ArithOPCode

-- | Performs subtraction (<tt>-</tt>)
pattern LUA_OPSUB :: ArithOPCode

-- | Performs multiplication (<tt>*</tt>)
pattern LUA_OPMUL :: ArithOPCode

-- | Performs float division (<tt>/</tt>)
pattern LUA_OPDIV :: ArithOPCode

-- | Performs floor division (<tt>//</tt>)
pattern LUA_OPIDIV :: ArithOPCode

-- | Performs modulo (<tt>%</tt>)
pattern LUA_OPMOD :: ArithOPCode

-- | Performs exponentiation (<tt>^</tt>)
pattern LUA_OPPOW :: ArithOPCode

-- | Performs mathematical negation (unary <tt>-</tt>)
pattern LUA_OPUNM :: ArithOPCode

-- | Performs bitwise NOT (<tt>~</tt>)
pattern LUA_OPBNOT :: ArithOPCode

-- | Performs bitwise AND (<tt>&amp;</tt>)
pattern LUA_OPBAND :: ArithOPCode

-- | Performs bitwise OR (<tt>|</tt>)
pattern LUA_OPBOR :: ArithOPCode

-- | Performs bitwise exclusive OR (<tt>~</tt>)
pattern LUA_OPBXOR :: ArithOPCode

-- | Performs left shift (<tt>&lt;&lt;</tt>)
pattern LUA_OPSHL :: ArithOPCode

-- | Performs right shift (<tt>&gt;&gt;</tt>)
pattern LUA_OPSHR :: ArithOPCode

-- | Stops the garbage collector.
pattern LUA_GCSTOP :: GCCode

-- | Restarts the garbage collector.
pattern LUA_GCRESTART :: GCCode

-- | Performs a full garbage-collection cycle.
pattern LUA_GCCOLLECT :: GCCode

-- | Returns the current amount of memory (in Kbytes) in use by Lua.
pattern LUA_GCCOUNT :: GCCode

-- | Returns the remainder of dividing the current amount of bytes of
--   memory in use by Lua by 1024.
pattern LUA_GCCOUNTB :: GCCode

-- | Performs an incremental step of garbage collection.
pattern LUA_GCSTEP :: GCCode

-- | Sets data as the new value for the pause of the collector (see §2.5)
--   and returns the previous value of the pause.
pattern LUA_GCSETPAUSE :: GCCode

-- | Sets data as the new value for the step multiplier of the collector
--   (see §2.5) and returns the previous value of the step multiplier.
pattern LUA_GCSETSTEPMUL :: GCCode

-- | Returns a boolean that tells whether the collector is running (i.e.,
--   not stopped).
pattern LUA_GCISRUNNING :: GCCode

-- | Changes the collector to generational mode.
pattern LUA_GCGEN :: GCCode

-- | Changes the collector to incremental mode.
pattern LUA_GCINC :: GCCode

-- | Value signaling that no reference was created.
pattern LUA_REFNIL :: CInt

-- | Value signaling that no reference was found.
pattern LUA_NOREF :: CInt

-- | The registry index that holds the global environment.
pattern LUA_RIDX_GLOBALS :: Integer

-- | Value which Lua usually uses as <a>True</a>.
pattern TRUE :: LuaBool

-- | Value which Lua usually uses as <a>False</a>.
pattern FALSE :: LuaBool


-- | 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.
module Lua.Call

-- | 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 <tt><a>hslua_error</a></tt>.
--   
--   <h3>Example</h3>
--   
--   Export the function to calculate triangular numbers.
--   
--   <pre>
--   let triangular :: PreCFunction
--       triangular l' = do
--         n &lt;- lua_tointegerx l' (nthBottom 1) nullPtr
--         lua_pushinteger l' (sum [1..n])
--         return (NumResults 1)
--   
--   hslua_newhsfunction l triangular
--   withCString "triangular" (lua_setglobal l)
--   </pre>
hslua_pushhsfunction :: State -> PreCFunction -> IO ()


-- | Raw bindings to functions and constants of the auxiliary library.
module Lua.Auxiliary

-- | Pushes onto the stack the field <tt>e</tt> from the metatable of the
--   object at index <tt>obj</tt> and returns the type of the pushed value.
--   If the object does not have a metatable, or if the metatable does not
--   have this field, pushes nothing and returns <tt><a>LUA_TNIL</a></tt>.
luaL_getmetafield :: State -> StackIndex -> CString -> IO TypeCode

-- | Pushes onto the stack the metatable associated with name tname in the
--   registry (see <tt><a>luaL_newmetatable</a></tt>) (<b>nil</b> if there
--   is no metatable associated with that name). Returns the type of the
--   pushed value.
luaL_getmetatable :: State -> CString -> IO TypeCode

-- | Loads a buffer as a Lua chunk. This function uses <tt>lua_load</tt> to
--   load the chunk in the buffer pointed to by <tt>buff</tt> with size
--   <tt>sz</tt>.
--   
--   This function returns the same results as <tt>lua_load</tt>.
--   <tt>name</tt> is the chunk name, used for debug information and error
--   messages.
luaL_loadbuffer :: State -> Ptr CChar -> CSize -> CString -> IO StatusCode

-- | Equivalent to luaL_loadfilex with mode equal to <tt>NULL</tt>.
luaL_loadfile :: State -> Ptr CChar -> IO StatusCode

-- | Loads a file as a Lua chunk. This function uses <tt>lua_load</tt> to
--   load the chunk in the file named filename. If filename is
--   <tt>NULL</tt>, then it loads from the standard input. The first line
--   in the file is ignored if it starts with a <tt>#</tt>.
--   
--   The string mode works as in function <tt>lua_load</tt>.
--   
--   This function returns the same results as <tt>lua_load</tt>, but it
--   has an extra error code LUA_ERRFILE for file-related errors (e.g., it
--   cannot open or read the file).
--   
--   As <tt>lua_load</tt>, this function only loads the chunk; it does not
--   run it.
luaL_loadfilex :: State -> Ptr CChar -> Ptr CChar -> IO StatusCode

-- | Opens all standard Lua libraries into the given state.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#luaL_openlibs</a>
luaL_openlibs :: State -> IO ()

-- | If the registry already has the key <tt>tname</tt>, returns
--   <tt>0</tt>. Otherwise, creates a new table to be used as a metatable
--   for userdata, adds to this new table the pair <tt>__name = tname</tt>,
--   adds to the registry the pair <tt>[tname] = new table</tt>, and
--   returns <tt>1</tt>. (The entry <tt>__name</tt> is used by some
--   error-reporting functions.)
--   
--   In both cases pushes onto the stack the final value associated with
--   <tt>tname</tt> in the registry.
luaL_newmetatable :: State -> CString -> IO LuaBool

-- | Creates and returns a reference, in the table at index <tt>t</tt>, for
--   the object at the top of the stack (and pops the object).
--   
--   A reference is a unique integer key. As long as you do not manually
--   add integer keys into table <tt>t</tt>, <tt>luaL_ref</tt> ensures the
--   uniqueness of the key it returns. You can retrieve an object referred
--   by reference <tt>r</tt> by calling <tt>lua_rawgeti l t r</tt>.
--   Function <tt><a>luaL_unref</a></tt> frees a reference and its
--   associated object.
--   
--   If the object at the top of the stack is nil, <tt>luaL_ref</tt>
--   returns the constant <tt><a>LUA_REFNIL</a></tt>. The constant
--   <tt><a>LUA_NOREF</a></tt> is guaranteed to be different from any
--   reference returned by <tt>luaL_ref</tt>.
luaL_ref :: State -> StackIndex -> IO CInt

-- | Checks whether the function argument <tt>arg</tt> is a userdata of the
--   type <tt>tname</tt> (see <tt><a>luaL_newmetatable</a></tt>) and
--   returns the userdata address (see <tt><a>lua_touserdata</a></tt>).
--   Returns <tt>NULL</tt> if the test fails.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#luaL_testudata</a>
luaL_testudata :: State -> StackIndex -> CString -> IO (Ptr ())

-- | Creates and pushes a traceback of the stack <tt>l1</tt>. If
--   <tt>msg</tt> is not <tt>NULL</tt> it is appended at the beginning of
--   the traceback. The level parameter tells at which level to start the
--   traceback.
luaL_traceback :: State -> State -> CString -> CInt -> IO ()

-- | Releases reference <tt>ref</tt> from the table at index <tt>t</tt>
--   (see <tt><a>luaL_ref</a></tt>). The entry is removed from the table,
--   so that the referred object can be collected. The reference
--   <tt>ref</tt> is also freed to be used again.
luaL_unref :: State -> StackIndex -> CInt -> IO ()

-- | Pushes onto the stack a string identifying the current position of the
--   control at level <tt>lvl</tt> in the call stack. Typically this string
--   has the following format:
--   
--   <pre>
--   chunkname:currentline:
--   </pre>
--   
--   Level 0 is the running function, level 1 is the function that called
--   the running function, etc.
--   
--   This function is used to build a prefix for error messages.
luaL_where :: State -> CInt -> IO ()

-- | Key, in the registry, for table of loaded modules.
loadedTableRegistryField :: String

-- | Key, in the registry, for table of preloaded loaders.
preloadTableRegistryField :: String

-- | Reference to a stored value.
data Reference

-- | Reference to a stored value
Reference :: CInt -> Reference

-- | Reference to a nil value
RefNil :: Reference

-- | Convert a reference to its C representation.
fromReference :: Reference -> CInt

-- | Create a reference from its C representation.
toReference :: CInt -> Reference
instance GHC.Classes.Eq Lua.Auxiliary.Reference
instance GHC.Internal.Show.Show Lua.Auxiliary.Reference


-- | Bindings to HsLua-specific functions used to push Haskell values as
--   userdata.
module Lua.Userdata

-- | Retrieves a Haskell object from userdata at the given index. The
--   userdata <i>must</i> have the given name.
hslua_fromuserdata :: State -> StackIndex -> CString -> IO (Maybe a)

-- | Creates a new userdata wrapping the given Haskell object, with
--   <tt>nuvalue</tt> associated Lua values (uservalues).
hslua_newhsuserdatauv :: State -> a -> CInt -> IO ()

-- | Creates and registers a new metatable for a userdata-wrapped Haskell
--   value; checks whether a metatable of that name has been registered yet
--   and uses the registered table if possible.
hslua_newudmetatable :: State -> CString -> IO LuaBool

-- | Replaces the Haskell value contained in the userdata value at
--   <tt>index</tt>. Checks that the userdata is of type <tt>name</tt> and
--   returns <a>True</a> on success, or <a>False</a> otherwise.
hslua_putuserdata :: State -> StackIndex -> CString -> a -> IO Bool


-- | Simpler interface to the Lua warnings system.
module Lua.Warn

-- | Sets a warning function. This is a simplified version of
--   <a>lua_setwarnf</a>. The function at the top of the stack is set as
--   the "warning hook", i.e., it is called with the concatenated warning
--   components as the single argument.
--   
--   The hook function is popped of the stack.
--   
--   The control messages <tt>@on</tt> and <tt>@off</tt> are still
--   supported; as with the default warning function, these commands can
--   switch error reporting to stderr on and off. The given Haskell
--   function will be called in either case, even when the error is not
--   written to stderr.
hsluaL_setwarnf :: State -> IO ()


-- | Extend Haskell programs with a Lua interpreter.
--   
--   This package provides the basic building blocks to integrate Lua into
--   a Haskell program. The library is kept very close to the C Lua API,
--   and users familiar with the C API should have no problem using it.
--   
--   However, there are important differences of which users must be aware:
--   The method for error signaling used in Lua, based on <tt>setjmp</tt>
--   and <tt>longjmp</tt>, is incompatible with the Haskell FFI. All errors
--   <i>must</i> be handled at language boundaries, as failure to do so
--   will lead to unrecoverable crashes. C API functions that can throw Lua
--   errors are still exported, but non-error throwing <tt>hslua_</tt>
--   versions are provided as safer alternatives. . The <tt>hslua</tt>
--   ersatz functions have worse performance than the original versions,
--   but should be fast enough for most use cases.
--   
--   The Haskell FFI requires all C function that can call back into
--   Haskell to be imported <tt>safe</tt>ly. Some of the Lua functions may,
--   directly or indirectly, call a Haskell function, so they are always
--   imported with the <tt>safe</tt> keyword.
--   
--   Many API functions can trigger garbage collection. This will lead to
--   problems if Haskell functions are used as part of finalizers (i.e.,
--   <tt>__gc</tt> metamethods). Haskell in finalizers is not supported by
--   default, but can be enabled by unsetting the <tt>allow-unsafe-gc</tt>
--   flag.
module Lua

-- | Runs operations on a new Lua <tt><a>State</a></tt>. The state is
--   created when the function is called and closed on return. The state,
--   and all pointers to values within it, <b>must not</b> be used after
--   the function returns.
--   
--   <h3>Example</h3>
--   
--   Run a small Lua operation (prints the major version of Lua).
--   
--   <pre>
--   withNewState $ \l -&gt; do
--     luaL_openlibs l
--     withCString "print" (lua_getglobal l)
--     withCString "_VERSION" (lua_getglobal l)
--     lua_pcall l (NumArgs 1) (NumResults 1) (StackIndex 0)
--   </pre>
withNewState :: (State -> IO a) -> IO a

-- | An opaque structure that points to a thread and indirectly (through
--   the thread) to the whole state of a Lua interpreter. The Lua library
--   is fully reentrant: it has no global variables. All information about
--   a state is accessible through this structure.
--   
--   Synonym for <tt>lua_State *</tt>. See <a>lua_State</a>.
newtype State
State :: Ptr () -> State

-- | The reader function used by <tt><a>load</a></tt>. Every time it needs
--   another piece of the chunk, lua_load calls the reader, passing along
--   its data parameter. The reader must return a pointer to a block of
--   memory with a new piece of the chunk and set size to the block size.
--   The block must exist until the reader function is called again. To
--   signal the end of the chunk, the reader must return <tt>NULL</tt> or
--   set size to zero. The reader function may return pieces of any size
--   greater than zero.
--   
--   See <a>lua_Reader</a>.
type Reader = FunPtr State -> Ptr () -> Ptr CSize -> IO Ptr CChar

-- | Type for C functions.
--   
--   In order to communicate properly with Lua, a C function must use the
--   following protocol, which defines the way parameters and results are
--   passed: a C function receives its arguments from Lua in its stack in
--   direct order (the first argument is pushed first). So, when the
--   function starts, <tt><a>lua_gettop</a></tt> returns the number of
--   arguments received by the function. The first argument (if any) is at
--   index 1 and its last argument is at index <tt><a>lua_gettop</a></tt>.
--   To return values to Lua, a C function just pushes them onto the stack,
--   in direct order (the first result is pushed first), and returns the
--   number of results. Any other value in the stack below the results will
--   be properly discarded by Lua. Like a Lua function, a C function called
--   by Lua can also return many results.
--   
--   See <a>lua_CFunction</a>.
type CFunction = FunPtr PreCFunction

-- | Type of Haskell functions that can be turned into C functions.
--   
--   This is the same as a dereferenced <a>CFunction</a>.
type PreCFunction = State -> IO NumResults

-- | The type of integers in Lua.
--   
--   By default this type is <tt><a>Int64</a></tt>, but that can be changed
--   to different values in Lua. (See <tt>LUA_INT_TYPE</tt> in
--   <tt>luaconf.h</tt>.)
--   
--   See <a>lua_Integer</a>.
newtype Integer
Integer :: Int64 -> Integer

-- | The type of floats in Lua.
--   
--   By default this type is <tt><a>Double</a></tt>, but that can be
--   changed in Lua to a single float or a long double. (See
--   <tt>LUA_FLOAT_TYPE</tt> in <tt>luaconf.h</tt>.)
--   
--   See <a>lua_Number</a>.
newtype Number
Number :: Double -> Number

-- | Boolean value returned by a Lua C API function. This is a
--   <tt><a>CInt</a></tt> and should be interpreted as
--   <tt><a>False</a></tt> iff the value is <tt>0</tt>,
--   <tt><a>True</a></tt> otherwise.
newtype LuaBool
LuaBool :: CInt -> LuaBool

-- | Value which Lua usually uses as <a>True</a>.
pattern TRUE :: LuaBool

-- | Value which Lua usually uses as <a>False</a>.
pattern FALSE :: LuaBool

-- | A stack index
newtype StackIndex
StackIndex :: CInt -> StackIndex
[fromStackIndex] :: StackIndex -> CInt

-- | Stack index of the Lua registry.
pattern LUA_REGISTRYINDEX :: StackIndex

-- | The number of arguments consumed curing a function call.
newtype NumArgs
NumArgs :: CInt -> NumArgs
[fromNumArgs] :: NumArgs -> CInt

-- | The number of results returned by a function call.
newtype NumResults
NumResults :: CInt -> NumResults
[fromNumResults] :: NumResults -> CInt

-- | Option for multiple returns in <tt><a>lua_pcall</a></tt>.
pattern LUA_MULTRET :: NumResults

-- | Integer code used to encode the type of a Lua value.
newtype TypeCode
TypeCode :: CInt -> TypeCode
[fromTypeCode] :: TypeCode -> CInt

-- | Non-valid stack index
pattern LUA_TNONE :: TypeCode

-- | Type of Lua's <tt>nil</tt> value
pattern LUA_TNIL :: TypeCode

-- | Type of Lua booleans
pattern LUA_TBOOLEAN :: TypeCode

-- | Type of light userdata
pattern LUA_TLIGHTUSERDATA :: TypeCode

-- | Type of Lua numbers. See <tt><a>Number</a></tt>
pattern LUA_TNUMBER :: TypeCode

-- | Type of Lua string values
pattern LUA_TSTRING :: TypeCode

-- | Type of Lua tables
pattern LUA_TTABLE :: TypeCode

-- | Type of functions, either normal or <tt><a>CFunction</a></tt>
pattern LUA_TFUNCTION :: TypeCode

-- | Type of full user data
pattern LUA_TUSERDATA :: TypeCode

-- | Type of Lua threads
pattern LUA_TTHREAD :: TypeCode

-- | Relational operator code.
newtype OPCode
OPCode :: CInt -> OPCode

-- | Compares for equality (==)
pattern LUA_OPEQ :: OPCode

-- | Compares for less than (&lt;)
pattern LUA_OPLT :: OPCode

-- | Compares for less or equal (&lt;=)
pattern LUA_OPLE :: OPCode

-- | Performs addition (<tt>+</tt>).
pattern LUA_OPADD :: ArithOPCode

-- | Performs subtraction (<tt>-</tt>)
pattern LUA_OPSUB :: ArithOPCode

-- | Performs multiplication (<tt>*</tt>)
pattern LUA_OPMUL :: ArithOPCode

-- | Performs float division (<tt>/</tt>)
pattern LUA_OPDIV :: ArithOPCode

-- | Performs floor division (<tt>//</tt>)
pattern LUA_OPIDIV :: ArithOPCode

-- | Performs modulo (<tt>%</tt>)
pattern LUA_OPMOD :: ArithOPCode

-- | Performs exponentiation (<tt>^</tt>)
pattern LUA_OPPOW :: ArithOPCode

-- | Performs mathematical negation (unary <tt>-</tt>)
pattern LUA_OPUNM :: ArithOPCode

-- | Performs bitwise NOT (<tt>~</tt>)
pattern LUA_OPBNOT :: ArithOPCode

-- | Performs bitwise AND (<tt>&amp;</tt>)
pattern LUA_OPBAND :: ArithOPCode

-- | Performs bitwise OR (<tt>|</tt>)
pattern LUA_OPBOR :: ArithOPCode

-- | Performs bitwise exclusive OR (<tt>~</tt>)
pattern LUA_OPBXOR :: ArithOPCode

-- | Performs left shift (<tt>&lt;&lt;</tt>)
pattern LUA_OPSHL :: ArithOPCode

-- | Performs right shift (<tt>&gt;&gt;</tt>)
pattern LUA_OPSHR :: ArithOPCode

-- | Integer code used to signal the status of a thread or computation.
newtype StatusCode
StatusCode :: CInt -> StatusCode

-- | Success.
pattern LUA_OK :: StatusCode

-- | Yielding / suspended coroutine.
pattern LUA_YIELD :: StatusCode

-- | A runtime error.
pattern LUA_ERRRUN :: StatusCode

-- | A syntax error.
pattern LUA_ERRSYNTAX :: StatusCode

-- | Memory allocation error. For such errors, Lua does not call the
--   message handler.
pattern LUA_ERRMEM :: StatusCode

-- | Error while running the message handler.
pattern LUA_ERRERR :: StatusCode

-- | File related error (e.g., the file cannot be opened or read).
pattern LUA_ERRFILE :: StatusCode

-- | Stack index of the nth element from the top of the stack.
nthTop :: CInt -> StackIndex

-- | Stack index of the nth element from the bottom of the stack.
nthBottom :: CInt -> StackIndex

-- | Alias for <a>nthTop</a>.
nth :: CInt -> StackIndex

-- | Index of the topmost stack element.
top :: StackIndex

-- | Destroys all objects in the given Lua state (calling the corresponding
--   garbage-collection metamethods, if any) and frees all dynamic memory
--   used by this state. In several platforms, you may not need to call
--   this function, because all resources are naturally released when the
--   host program ends. On the other hand, long-running programs that
--   create multiple states, such as daemons or web servers, will probably
--   need to close states as soon as they are not needed.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_close</a>
lua_close :: State -> IO ()

-- | Creates a new thread, pushes it on the stack, and returns a
--   <a>State</a> that represents this new thread. The new thread returned
--   by this function shares with the original thread its global
--   environment, but has an independent execution stack.
--   
--   There is no explicit function to close or to destroy a thread. Threads
--   are subject to garbage collection, like any Lua object.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_newthread</a>
lua_newthread :: State -> IO State

-- | Returns the address of the version number (a C static variable) stored
--   in the Lua core. When called with a valid <a>State</a>, returns the
--   address of the version used to create that state. When called with
--   <tt>NULL</tt>, returns the address of the version running the call.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_version</a>
lua_version :: State -> IO (Ptr Number)

-- | Converts the acceptable index <tt>idx</tt> into an equivalent absolute
--   index (that is, one that does not depend on the stack top).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_absindex</a>
lua_absindex :: State -> StackIndex -> IO StackIndex

-- | Returns the index of the top element in the stack. Because indices
--   start at 1, this result is equal to the number of elements in the
--   stack (and so 0 means an empty stack).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_gettop</a>
lua_gettop :: State -> IO StackIndex

-- | Accepts any index, or 0, and sets the stack top to this index. If the
--   new top is larger than the old one, then the new elements are filled
--   with *nil*. If <tt>index</tt> is 0, then all stack elements are
--   removed.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_settop</a>
lua_settop :: State -> StackIndex -> IO ()

-- | Pushes a copy of the element at the given index onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushvalue</a>
lua_pushvalue :: State -> StackIndex -> IO ()

-- | Pops <tt>n</tt> elements from the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pop</a>
lua_pop :: State -> CInt -> IO ()

-- | Copies the element at index <tt>fromidx</tt> into the valid index
--   <tt>toidx</tt>, replacing the value at that position. Values at other
--   positions are not affected.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_copy</a>
lua_copy :: State -> StackIndex -> StackIndex -> IO ()

-- | Removes the element at the given valid index, shifting down the
--   elements above this index to fill the gap. This function cannot be
--   called with a pseudo-index, because a pseudo-index is not an actual
--   stack position.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_remove</a>
lua_remove :: State -> StackIndex -> IO ()

-- | Moves the top element into the given valid index, shifting up the
--   elements above this index to open space. This function cannot be
--   called with a pseudo-index, because a pseudo-index is not an actual
--   stack position.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_insert</a>
lua_insert :: State -> StackIndex -> IO ()

-- | Moves the top element into the given valid index without shifting any
--   element (therefore replacing the value at that given index), and then
--   pops the top element.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_replace</a>
lua_replace :: State -> StackIndex -> IO ()

-- | Rotates the stack elements between the valid index <tt>idx</tt> and
--   the top of the stack. The elements are rotated <tt>n</tt> positions in
--   the direction of the top, for a positive <tt>n</tt>, or <tt>-n</tt>
--   positions in the direction of the bottom, for a negative <tt>n</tt>.
--   The absolute value of <tt>n</tt> must not be greater than the size of
--   the slice being rotated. This function cannot be called with a
--   pseudo-index, because a pseudo-index is not an actual stack position.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rotate</a>
lua_rotate :: State -> StackIndex -> CInt -> IO ()

-- | Ensures that the stack has space for at least <tt>n</tt> extra slots
--   (that is, that you can safely push up to <tt>n</tt> values into it).
--   It returns false if it cannot fulfill the request, either because it
--   would cause the stack to be larger than a fixed maximum size
--   (typically at least several thousand elements) or because it cannot
--   allocate memory for the extra space. This function never shrinks the
--   stack; if the stack already has space for the extra slots, it is left
--   unchanged.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_checkstack</a>
lua_checkstack :: State -> CInt -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is
--   <b>nil</b>, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isnil</a>
lua_isnil :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   boolean, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isboolean</a>
lua_isboolean :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   number or a string convertible to a number, and <tt><a>FALSE</a></tt>
--   otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isnumber</a>
lua_isnumber :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is an
--   integer (that is, the value is a number and is represented as an
--   integer), and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isinteger</a>
lua_isinteger :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   string or a number (which is always convertible to a string), and
--   <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isstring</a>
lua_isstring :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   function (either C or Lua), and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isfunction</a>
lua_isfunction :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   table, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_istable</a>
lua_istable :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a C
--   function, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_iscfunction</a>
lua_iscfunction :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   userdata (either full or light), and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isuserdata</a>
lua_isuserdata :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   light userdata, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_islightuserdata</a>
lua_islightuserdata :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the value at the given index is a
--   thread, and <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isthread</a>
lua_isthread :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the given index is not valid, and
--   <tt><a>FALSE</a></tt> otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isnone</a>
lua_isnone :: State -> StackIndex -> IO LuaBool

-- | Returns <tt><a>TRUE</a></tt> if the given index is not valid or if the
--   value at the given index is <b>nil</b>, and <tt><a>FALSE</a></tt>
--   otherwise.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_isnoneornil</a>
lua_isnoneornil :: State -> StackIndex -> IO LuaBool

-- | Returns the type of the value in the given valid index, or
--   <tt><a>LUA_TNONE</a></tt> for a non-valid (but acceptable) index.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_type</a>
lua_type :: State -> StackIndex -> IO TypeCode

-- | Returns the name of the type encoded by the value <tt>tp</tt>, which
--   must be one the values returned by <tt><a>lua_type</a></tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_typename</a>
lua_typename :: State -> TypeCode -> IO CString

-- | Returns <tt>True</tt> if the two values in indices <tt>idx1</tt> and
--   <tt>idx2</tt> are primitively equal (that is, without calling the
--   <tt>__eq</tt> metamethod). Otherwise returns <tt>False</tt>. Also
--   returns <tt>False</tt> if any of the indices are not valid.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawequal</a>
lua_rawequal :: State -> StackIndex -> StackIndex -> IO LuaBool

-- | Converts the Lua value at the given index to a haskell boolean value.
--   Like all tests in Lua, <tt>toboolean</tt> returns <tt>True</tt> for
--   any Lua value different from *false* and *nil*; otherwise it returns
--   <tt>False</tt>. (If you want to accept only actual boolean values, use
--   <tt><a>lua_isboolean</a></tt> to test the value's type.)
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_toboolean</a>
lua_toboolean :: State -> StackIndex -> IO LuaBool

-- | Converts a value at the given index to a C function. That value must
--   be a C function; otherwise, returns <tt>Nothing</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_tocfunction</a>
lua_tocfunction :: State -> StackIndex -> IO CFunction

-- | Converts the Lua value at the given acceptable index to the signed
--   integral type <a>Integer</a>. The Lua value must be an integer, a
--   number, or a string convertible to an integer (see <a>§3.4.3</a> of
--   the Lua 5.4 Reference Manual); otherwise, <tt>lua_tointegerx</tt>
--   returns <tt>0</tt>.
--   
--   If the number is not an integer, it is truncated in some non-specified
--   way.
--   
--   If <tt>isnum</tt> is not <tt>NULL</tt>, its referent is assigned a
--   boolean value that indicates whether the operation succeeded.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_tointegerx</a>
lua_tointegerx :: State -> StackIndex -> Ptr LuaBool -> IO Integer

-- | Converts the Lua value at the given index to the C type lua_Number
--   (see lua_Number). The Lua value must be a number or a string
--   convertible to a number (see §3.4.3); otherwise, lua_tonumberx returns
--   0.
--   
--   If <tt>isnum</tt> is not <tt>NULL</tt>, its referent is assigned a
--   boolean value that indicates whether the operation succeeded.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_tonumberx</a>
lua_tonumberx :: State -> StackIndex -> Ptr LuaBool -> IO Number

-- | Converts the Lua value at the given index to a C string. If
--   <tt>len</tt> is not <tt>NULL</tt>, it sets the referent with the
--   string length. The Lua value must be a string or a number; otherwise,
--   the function returns <tt>NULL</tt>. If the value is a number, then
--   <tt>lua_tolstring</tt> also changes the actual value in the stack to a
--   string. (This change confuses <tt>lua_next</tt> when
--   <tt>lua_tolstring</tt> is applied to keys during a table traversal.)
--   
--   <tt>lua_tolstring</tt> returns a pointer to a string inside the Lua
--   state. This string always has a zero ('0') after its last character
--   (as in C), but can contain other zeros in its body.
--   
--   Because Lua has garbage collection, there is no guarantee that the
--   pointer returned by <tt>lua_tolstring</tt> will be valid after the
--   corresponding Lua value is removed from the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_tolstring</a>
lua_tolstring :: State -> StackIndex -> Ptr CSize -> IO (Ptr CChar)

-- | Converts the value at the given index to a generic C pointer
--   (<tt>void*</tt>). The value can be a userdata, a table, a thread, or a
--   function; otherwise, <tt>lua_topointer</tt> returns
--   <tt><a>nullPtr</a></tt>. Different objects will give different
--   pointers. There is no way to convert the pointer back to its original
--   value.
--   
--   Typically this function is used only for hashing and debug
--   information.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_topointer</a>
lua_topointer :: State -> StackIndex -> IO (Ptr ())

-- | Converts the value at the given index to a Lua thread (represented as
--   <tt><a>State</a></tt>). This value must be a thread; otherwise, the
--   function returns <tt><a>nullPtr</a></tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_tothread</a>
lua_tothread :: State -> StackIndex -> IO State

-- | If the value at the given index is a full userdata, returns its block
--   address. If the value is a light userdata, returns its pointer.
--   Otherwise, returns <tt><a>nullPtr</a></tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_touserdata</a>
lua_touserdata :: State -> StackIndex -> IO (Ptr a)

-- | Returns the raw "length" of the value at the given index: for strings,
--   this is the string length; for tables, this is the result of the
--   length operator (<tt>#</tt>) with no metamethods; for userdata, this
--   is the size of the block of memory allocated for the userdata; for
--   other values, it is 0.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawlen</a>.
lua_rawlen :: State -> StackIndex -> IO CSize

-- | Pushes a nil value onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushnil</a>.
lua_pushnil :: State -> IO ()

-- | Pushes a float with the given value onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushnumber</a>.
lua_pushnumber :: State -> Number -> IO ()

-- | Pushes an integer with with the given value onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushinteger</a>.
lua_pushinteger :: State -> Integer -> IO ()

-- | Pushes the string pointed to by <tt>s</tt> with size <tt>len</tt> onto
--   the stack. Lua makes (or reuses) an internal copy of the given string,
--   so the memory at s can be freed or reused immediately after the
--   function returns. The string can contain any binary data, including
--   embedded zeros.
--   
--   Returns a pointer to the internal copy of the string.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushlstring</a>.
lua_pushlstring :: State -> Ptr CChar -> CSize -> IO ()

-- | Pushes the zero-terminated string pointed to by <tt>s</tt> onto the
--   stack. Lua makes (or reuses) an internal copy of the given string, so
--   the memory at <tt>s</tt> can be freed or reused immediately after the
--   function returns.
--   
--   Returns a pointer to the internal copy of the string.
--   
--   If s is NULL, pushes nil and returns NULL.
lua_pushstring :: State -> CString -> IO CString

-- | Pushes a new C closure onto the stack.
--   
--   When a C function is created, it is possible to associate some values
--   with it, thus creating a C closure (see <a>§3.4</a>); these values are
--   then accessible to the function whenever it is called. To associate
--   values with a C function, first these values should be pushed onto the
--   stack (when there are multiple values, the first value is pushed
--   first). Then lua_pushcclosure is called to create and push the C
--   function onto the stack, with the argument <tt>n</tt> telling how many
--   values should be associated with the function. lua_pushcclosure also
--   pops these values from the stack.
--   
--   The maximum value for <tt>n</tt> is 255.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushcclosure</a>.
lua_pushcclosure :: State -> CFunction -> NumArgs -> IO ()

-- | Pushes a C function onto the stack. This function receives a pointer
--   to a C function and pushes onto the stack a Lua value of type
--   <tt>function</tt> that, when called, invokes the corresponding C
--   function.
--   
--   Any function to be callable by Lua must follow the correct protocol to
--   receive its parameters and return its results (see <a>CFunction</a>).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushcfunction</a>.
lua_pushcfunction :: State -> CFunction -> IO ()

-- | Pushes a boolean value with the given value onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushboolean</a>.
lua_pushboolean :: State -> LuaBool -> IO ()

-- | Pushes a light userdata onto the stack.
--   
--   Userdata represent C values in Lua. A light userdata represents a
--   pointer, a <tt>Ptr ()</tt> (i.e., <tt>void*</tt> in C lingo). It is a
--   value (like a number): you do not create it, it has no individual
--   metatable, and it is not collected (as it was never created). A light
--   userdata is equal to "any" light userdata with the same C address.
--   
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushlightuserdata</a>.
lua_pushlightuserdata :: State -> Ptr a -> IO ()

-- | Pushes the current thread onto the stack. Returns <tt>1</tt> iff this
--   thread is the main thread of its state.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushthread</a>.
lua_pushthread :: State -> IO CInt

-- | Similar to <tt><a>lua_gettable</a></tt>, but does a raw access (i.e.,
--   without metamethods).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawget</a>.
lua_rawget :: State -> StackIndex -> IO TypeCode

-- | Pushes onto the stack the value <tt>t[n]</tt>, where <tt>t</tt> is the
--   table at the given index. The access is raw, that is, it does not
--   invoke the <tt>__index</tt> metamethod.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawgeti</a>.
lua_rawgeti :: State -> StackIndex -> Integer -> IO TypeCode

-- | Creates a new empty table and pushes it onto the stack. Parameter
--   <tt>narr</tt> is a hint for how many elements the table will have as a
--   sequence; parameter <tt>nrec</tt> is a hint for how many other
--   elements the table will have. Lua may use these hints to preallocate
--   memory for the new table. This preallocation is useful for performance
--   when you know in advance how many elements the table will have.
--   Otherwise you can use the function <tt>lua_newtable</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_createtable</a>.
lua_createtable :: State -> CInt -> CInt -> IO ()

-- | This function creates and pushes on the stack a new full userdata,
--   with <tt>nuvalue</tt> associated Lua values, called <tt>user
--   values</tt>, plus an associated block of raw memory with <tt>size</tt>
--   bytes. (The user values can be set and read with the functions
--   <a>lua_setiuservalue</a> and <a>lua_getiuservalue</a>.)
--   
--   The function returns the address of the block of memory. Lua ensures
--   that this address is valid as long as the corresponding userdata is
--   alive (see <a>§2.5</a>). Moreover, if the userdata is marked for
--   finalization (see <a>§2.5.3</a>), its address is valid at least until
--   the call to its finalizer.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_newuserdatauv</a>.
lua_newuserdatauv :: State -> CSize -> CInt -> IO (Ptr ())

-- | If the value at the given index has a metatable, the function pushes
--   that metatable onto the stack and returns <tt>1</tt>. Otherwise, the
--   function returns <tt>0</tt> and pushes nothing on the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_getmetatable</a>.
lua_getmetatable :: State -> StackIndex -> IO LuaBool

-- | Pushes onto the stack the <tt>n</tt>-th user value associated with the
--   full userdata at the given index and returns the type of the pushed
--   value.
--   
--   If the userdata does not have that value, pushes <b>nil</b> and
--   returns <tt>LUA_TNONE</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_getiuservalue</a>
lua_getiuservalue :: State -> StackIndex -> CInt -> IO TypeCode

-- | Pushes onto the stack the value of the global name. Returns the type
--   of that value.
--   
--   <b>WARNING</b>: <tt>lua_getglobal</tt> is unsafe in Haskell: if the
--   call to a metamethod triggers an error, then that error cannot be
--   handled and will lead to an unrecoverable program crash. Consider
--   using the <tt><a>hslua_getglobal</a></tt> ersatz function instead.
--   Likewise, the metamethod may not call a Haskell function unless the
--   library was compiled without <tt>allow-unsafe-gc</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_getglobal</a>.

-- | <i>Warning: This is an unsafe function, errors will lead to a program
--   crash;consider using hslua_getglobal instead.</i>
lua_getglobal :: State -> CString -> IO TypeCode

-- | Pushes onto the stack the value <tt>t[k]</tt>, where <tt>t</tt> is the
--   value at the given index and <tt>k</tt> is the value at the top of the
--   stack.
--   
--   This function pops the key from the stack, pushing the resulting value
--   in its place. As in Lua, this function may trigger a metamethod for
--   the "index" event (see <a>§2.4</a>).
--   
--   Returns the type of the pushed value.
--   
--   <b>WARNING</b>: <tt>lua_gettable</tt> is unsafe in Haskell: if the
--   call to a metamethod triggers an error, then that error cannot be
--   handled and will lead to an unrecoverable program crash. Consider
--   using the <tt><a>hslua_gettable</a></tt> ersatz function instead.
--   Likewise, the metamethod may not call a Haskell function unless the
--   library was compiled without <tt>allow-unsafe-gc</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_gettable</a>.

-- | <i>Warning: This is an unsafe function, errors will lead to a program
--   crash;consider using hslua_gettable instead.</i>
lua_gettable :: State -> StackIndex -> IO TypeCode

-- | Similar to <tt><a>lua_settable</a></tt>, but does a raw assignment
--   (i.e., without metamethods).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawset</a>.
lua_rawset :: State -> StackIndex -> IO ()

-- | Does the equivalent of <tt>t[i] = v</tt>, where <tt>t</tt> is the
--   table at the given index and <tt>v</tt> is the value at the top of the
--   stack.
--   
--   This function pops the value from the stack. The assignment is raw,
--   that is, it does not invoke the <tt>__newindex</tt> metamethod.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_rawseti</a>.
lua_rawseti :: State -> StackIndex -> Integer -> IO ()

-- | Pops a table from the stack and sets it as the new metatable for the
--   value at the given index.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_setmetatable</a>.
lua_setmetatable :: State -> StackIndex -> IO ()

-- | Pops a value from the stack and sets it as the new <tt>n</tt>-th user
--   value associated to the full userdata at the given index. Returns 0 if
--   the userdata does not have that value.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_setiuservalue</a>
lua_setiuservalue :: State -> StackIndex -> CInt -> IO LuaBool

-- | Pops a value from the stack and sets it as the new value of global
--   <tt>name</tt>.
--   
--   <b>WARNING</b>: <tt>lua_setglobal</tt> is unsafe in Haskell: if the
--   call to a metamethod triggers an error, then that error cannot be
--   handled and will lead to an unrecoverable program crash. Consider
--   using the <tt><a>hslua_setglobal</a></tt> ersatz function instead.
--   Likewise, the global metamethod may not call a Haskell function unless
--   the library was compiled without <tt>allow-unsafe-gc</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_setglobal</a>.

-- | <i>Warning: This is an unsafe function, errors will lead to a program
--   crash;consider using hslua_getglobal instead.</i>
lua_setglobal :: State -> CString -> IO ()

-- | Does the equivalent to <tt>t[k] = v</tt>, where <tt>t</tt> is the
--   value at the given index, <tt>v</tt> is the value at the top of the
--   stack, and <tt>k</tt> is the value just below the top.
--   
--   This function pops both the key and the value from the stack. As in
--   Lua, this function may trigger a metamethod for the "newindex" event
--   (see <a>§2.4</a>).
--   
--   <b>WARNING</b>: <tt>lua_settable</tt> is unsafe in Haskell: if the
--   call to a metamethod triggers an error, then that error cannot be
--   handled and will lead to an unrecoverable program crash. Consider
--   using the <tt><a>hslua_settable</a></tt> ersatz function instead.
--   Likewise, the metamethod may not call a Haskell function unless the
--   library was compiled without <tt>allow-unsafe-gc</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_settable</a>

-- | <i>Warning: This is an unsafe function, errors will lead to a program
--   crash;consider using hslua_settable instead.</i>
lua_settable :: State -> StackIndex -> IO ()

-- | Converts the zero-terminated string <tt>s</tt> to a number, pushes
--   that number into the stack, and returns the total size of the string,
--   that is, its length plus one. The conversion can result in an integer
--   or a float, according to the lexical conventions of Lua (see
--   <a>§3.1</a>). The string may have leading and trailing spaces and a
--   sign. If the string is not a valid numeral, returns 0 and pushes
--   nothing. (Note that the result can be used as a boolean, true if the
--   conversion succeeds.)
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_stringtonumber</a>.
lua_stringtonumber :: State -> CString -> IO CSize

-- | 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 <tt>op</tt> must be one of the following constants:
--   
--   <ul>
--   <li><b><tt>LUA_OPADD</tt>:</b> performs addition (<tt>+</tt>)</li>
--   <li><b><tt>LUA_OPSUB</tt>:</b> performs subtraction (<tt>-</tt>)</li>
--   <li><b><tt>LUA_OPMUL</tt>:</b> performs multiplication
--   (<tt>*</tt>)</li>
--   <li><b><tt>LUA_OPDIV</tt>:</b> performs float division
--   (<tt>/</tt>)</li>
--   <li><b><tt>LUA_OPIDIV</tt>:</b> performs floor division
--   (<tt>//</tt>)</li>
--   <li><b><tt>LUA_OPMOD</tt>:</b> performs modulo (<tt>%</tt>)</li>
--   <li><b><tt>LUA_OPPOW</tt>:</b> performs exponentiation
--   (<tt>^</tt>)</li>
--   <li><b><tt>LUA_OPUNM</tt>:</b> performs mathematical negation (unary
--   <tt>-</tt>)</li>
--   <li><b><tt>LUA_OPBNOT</tt>:</b> performs bitwise NOT (<tt>~</tt>)</li>
--   <li><b><tt>LUA_OPBAND</tt>:</b> performs bitwise AND
--   (<tt>&amp;</tt>)</li>
--   <li><b><tt>LUA_OPBOR</tt>:</b> performs bitwise OR (<tt>|</tt>)</li>
--   <li><b><tt>LUA_OPBXOR</tt>:</b> performs bitwise exclusive OR
--   (<tt>~</tt>)</li>
--   <li><b><tt>LUA_OPSHL</tt>:</b> performs left shift
--   (<tt>&lt;&lt;</tt>)</li>
--   <li><b><tt>LUA_OPSHR</tt>:</b> performs right shift
--   (<tt>&gt;&gt;</tt>)</li>
--   </ul>
--   
--   <b>WARNING</b>: <tt>lua_arith</tt> is unsafe in Haskell: if the call
--   to a metamethod triggers an error, then that error cannot be handled
--   and will lead to an unrecoverable program crash. Consider using the
--   <tt><a>hslua_arith</a></tt> ersatz function instead. Likewise, the
--   metamethod may not call a Haskell function unless the library was
--   compiled without <tt>allow-unsafe-gc</tt>.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_arith</a>.

-- | <i>Warning: This is an unsafe function, errors will lead to a program
--   crash;consider using hslua_arith instead.</i>
lua_arith :: State -> ArithOPCode -> IO ()

-- | Concatenates the <tt>n</tt> values at the top of the stack, pops them,
--   and leaves the result at the top. If <tt>n</tt> is 1, the result is
--   the single value on the stack (that is, the function does nothing); if
--   <tt>n</tt> is 0, the result is the empty string. Concatenation is
--   performed following the usual semantics of Lua (see <a>§3.4.6</a> of
--   the Lua manual).
--   
--   <b>WARNING</b>: <tt>lua_concat</tt> is unsafe in Haskell: This
--   function will cause an unrecoverable crash an error if any of the
--   concatenated values causes an error when executing a metamethod.
--   Consider using the <tt><a>hslua_concat</a></tt> ersatz function
--   instead.

-- | <i>Warning: This is an unsafe function, it will cause a program crash
--   ifa metamethod throws an error.Consider using hslua_concat
--   instead.</i>
lua_concat :: State -> CInt -> IO ()

-- | Pops a key from the stack, and pushes a key–value pair from the table
--   at the given index (the "next" pair after the given key). If there are
--   no more elements in the table, then <a>lua_next</a> returns
--   <a>FALSE</a> (and pushes nothing).
--   
--   A typical traversal looks like this:
--   
--   <pre>
--   -- table is in the stack at index 't'
--   lua_pushnil l    -- first key
--   let loop = lua_next l t &gt;&gt;= \case
--         FALSE -&gt; return ()
--         _ -&gt; do
--           lua_type l (-2) &gt;&gt;= lua_typename l &gt;&gt;= peekCString &gt;&gt;= putStrLn
--           lua_type l (-1) &gt;&gt;= lua_typename l &gt;&gt;= peekCString &gt;&gt;= putStrLn
--           -- removes 'value'; keeps 'key' for next iteration
--           lua_pop l 1
--           loop
--   loop
--   </pre>
--   
--   While traversing a table, do not call <a>lua_tolstring</a> directly on
--   a key, unless you know that the key is actually a string. Recall that
--   <a>lua_tolstring</a> may change the value at the given index; this
--   confuses the next call to <a>lua_next</a>.
--   
--   See function <a>next</a> for the caveats of modifying the table during
--   its traversal.
--   
--   <b>WARNING</b>: <tt>lua_next</tt> is unsafe in Haskell: This function
--   will cause an unrecoverable crash an error if the given key is neither
--   <tt>nil</tt> nor present in the table. Consider using the
--   <tt><a>hslua_next</a></tt> ersatz function instead.

-- | <i>Warning: This is an unsafe function, it will cause a program crash
--   ifthe given key is neither nil nor present in the table.Consider using
--   hslua_next instead.</i>
lua_next :: State -> StackIndex -> IO LuaBool

-- | Calls a function in protected mode.
--   
--   To call a function you must use the following protocol: first, the
--   function to be called is pushed onto the stack; then, the arguments to
--   the function are pushed in direct order; that is, the first argument
--   is pushed first. Finally you call <tt>lua_pcall</tt>; <tt>nargs</tt>
--   is the number of arguments that you pushed onto the stack. All
--   arguments and the function value are popped from the stack when the
--   function is called. The function results are pushed onto the stack
--   when the function returns. The number of results is adjusted to
--   <tt>nresults</tt>, unless <tt>nresults</tt> is
--   <tt><a>LUA_MULTRET</a></tt>. In this case, all results from the
--   function are pushed. Lua takes care that the returned values fit into
--   the stack space. The function results are pushed onto the stack in
--   direct order (the first result is pushed first), so that after the
--   call the last result is on the top of the stack.
--   
--   If there is any error, <tt>lua_pcall</tt> catches it, pushes a single
--   value on the stack (the error message), and returns the error code.
--   <tt>lua_pcall</tt> always removes the function and its arguments from
--   the stack.
--   
--   If <tt>msgh</tt> is <tt>0</tt>, then the error object returned on the
--   stack is exactly the original error object. Otherwise, <tt>msgh</tt>
--   is the location of a message handler. (This index cannot be a
--   pseudo-index.) In case of runtime errors, this function will be called
--   with the error object and its return value will be the object returned
--   on the stack by <tt><a>lua_pcall</a></tt>.
--   
--   Typically, the message handler is used to add more debug information
--   to the error object, such as a stack traceback. Such information
--   cannot be gathered after the return of <tt><a>lua_pcall</a></tt>,
--   since by then the stack has unwound.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pcall</a>.
lua_pcall :: State -> NumArgs -> NumResults -> StackIndex -> IO StatusCode

-- | Loads a Lua chunk (without running it). If there are no errors,
--   <tt>lua_load</tt> pushes the compiled chunk as a Lua function on top
--   of the stack. Otherwise, it pushes an error message.
--   
--   The return values of <tt>lua_load</tt> are:
--   
--   <ul>
--   <li><tt><a>LUA_OK</a></tt>: no errors;</li>
--   <li><tt><a>LUA_ERRSYNTAX</a></tt>: syntax error during
--   pre-compilation;</li>
--   <li><tt><a>LUA_ERRMEM</a></tt>: memory allocation error;</li>
--   <li><tt><a>LUA_ERRGCMM</a></tt>: error while running a <tt>__gc</tt>
--   metamethod. (This error has no relation with the chunk being loaded.
--   It is generated by the garbage collector.)</li>
--   </ul>
--   
--   This function only loads a chunk; it does not run it.
--   
--   <tt>lua_load</tt> automatically detects whether the chunk is text or
--   binary, and loads it accordingly (see program luac).
--   
--   The <tt>lua_load</tt> function uses a user-supplied reader function to
--   read the chunk (see <tt><a>Reader</a></tt>). The data argument is an
--   opaque value passed to the reader function.
--   
--   The <tt>chunkname</tt> argument gives a name to the chunk, which is
--   used for error messages and in debug information (see <a>§4.7</a>).
--   
--   <tt>lua_load</tt> automatically detects whether the chunk is text or
--   binary and loads it accordingly (see program <tt>luac</tt>). The
--   string mode works as in function <tt>load</tt>, with the addition that
--   a <tt>NULL</tt> value is equivalent to the string "bt".
--   
--   <tt>lua_load</tt> uses the stack internally, so the reader function
--   must always leave the stack unmodified when returning.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_load</a>.
lua_load :: State -> Reader -> Ptr () -> CString -> CString -> IO StatusCode

-- | Returns the status of this Lua thread.
--   
--   The status can be <tt><a>LUA_OK</a></tt> for a normal thread, an error
--   value if the thread finished the execution of a <tt>lua_resume</tt>
--   with an error, or <tt><a>LUA_YIELD</a></tt> if the thread is
--   suspended.
--   
--   You can only call functions in threads with status
--   <tt><a>LUA_OK</a></tt>. You can resume threads with status
--   <tt><a>LUA_OK</a></tt> (to start a new coroutine) or
--   <tt><a>LUA_YIELD</a></tt> (to resume a coroutine).
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_status</a>.
lua_status :: State -> IO StatusCode

-- | Controls the garbage collector.
--   
--   See the Lua docs at
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_gc</a>. Unused dataN
--   values should be set to 0, but can be anything.
lua_gc :: State -> GCCode -> CInt -> CInt -> CInt -> IO CInt

-- | Garbage-collection options.
newtype GCCode
GCCode :: CInt -> GCCode

-- | Stops the garbage collector.
pattern LUA_GCSTOP :: GCCode

-- | Restarts the garbage collector.
pattern LUA_GCRESTART :: GCCode

-- | Performs a full garbage-collection cycle.
pattern LUA_GCCOLLECT :: GCCode

-- | Returns the current amount of memory (in Kbytes) in use by Lua.
pattern LUA_GCCOUNT :: GCCode

-- | Returns the remainder of dividing the current amount of bytes of
--   memory in use by Lua by 1024.
pattern LUA_GCCOUNTB :: GCCode

-- | Performs an incremental step of garbage collection.
pattern LUA_GCSTEP :: GCCode

-- | Sets data as the new value for the pause of the collector (see §2.5)
--   and returns the previous value of the pause.
pattern LUA_GCSETPAUSE :: GCCode

-- | Sets data as the new value for the step multiplier of the collector
--   (see §2.5) and returns the previous value of the step multiplier.
pattern LUA_GCSETSTEPMUL :: GCCode

-- | Returns a boolean that tells whether the collector is running (i.e.,
--   not stopped).
pattern LUA_GCISRUNNING :: GCCode

-- | Changes the collector to generational mode.
pattern LUA_GCGEN :: GCCode

-- | Changes the collector to incremental mode.
pattern LUA_GCINC :: GCCode

-- | Emits a warning with the given message. A message in a call with
--   <tt>tocont</tt> true should be continued in another call to this
--   function.
--   
--   See <a>warn</a> for more details about warnings.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_warning</a>
lua_warning :: State -> CString -> LuaBool -> IO ()

-- | Sets the warning function to be used by Lua to emit warnings (see
--   <a>WarnFunction</a>). The <tt>ud</tt> parameter sets the value
--   <tt>ud</tt> passed to the warning function.
lua_setwarnf :: State -> WarnFunction -> Ptr () -> IO ()

-- | The type of warning functions, called by Lua to emit warnings. The
--   first parameter is an opaque pointer set by <tt>lua_setwarnf</tt>. The
--   second parameter is the warning message. The third parameter is a
--   boolean that indicates whether the message is to be continued by the
--   message in the next call.
--   
--   See <a>warn</a> for more details about warnings.
type WarnFunction = FunPtr Ptr () -> CString -> LuaBool -> IO ()

-- | Pushes the global environment onto the stack.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#lua_pushglobaltable</a>
lua_pushglobaltable :: State -> IO ()

-- | Pushes onto the stack the field <tt>e</tt> from the metatable of the
--   object at index <tt>obj</tt> and returns the type of the pushed value.
--   If the object does not have a metatable, or if the metatable does not
--   have this field, pushes nothing and returns <tt><a>LUA_TNIL</a></tt>.
luaL_getmetafield :: State -> StackIndex -> CString -> IO TypeCode

-- | Pushes onto the stack the metatable associated with name tname in the
--   registry (see <tt><a>luaL_newmetatable</a></tt>) (<b>nil</b> if there
--   is no metatable associated with that name). Returns the type of the
--   pushed value.
luaL_getmetatable :: State -> CString -> IO TypeCode

-- | Loads a buffer as a Lua chunk. This function uses <tt>lua_load</tt> to
--   load the chunk in the buffer pointed to by <tt>buff</tt> with size
--   <tt>sz</tt>.
--   
--   This function returns the same results as <tt>lua_load</tt>.
--   <tt>name</tt> is the chunk name, used for debug information and error
--   messages.
luaL_loadbuffer :: State -> Ptr CChar -> CSize -> CString -> IO StatusCode

-- | Opens all standard Lua libraries into the given state.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#luaL_openlibs</a>
luaL_openlibs :: State -> IO ()

-- | If the registry already has the key <tt>tname</tt>, returns
--   <tt>0</tt>. Otherwise, creates a new table to be used as a metatable
--   for userdata, adds to this new table the pair <tt>__name = tname</tt>,
--   adds to the registry the pair <tt>[tname] = new table</tt>, and
--   returns <tt>1</tt>. (The entry <tt>__name</tt> is used by some
--   error-reporting functions.)
--   
--   In both cases pushes onto the stack the final value associated with
--   <tt>tname</tt> in the registry.
luaL_newmetatable :: State -> CString -> IO LuaBool

-- | Creates and returns a reference, in the table at index <tt>t</tt>, for
--   the object at the top of the stack (and pops the object).
--   
--   A reference is a unique integer key. As long as you do not manually
--   add integer keys into table <tt>t</tt>, <tt>luaL_ref</tt> ensures the
--   uniqueness of the key it returns. You can retrieve an object referred
--   by reference <tt>r</tt> by calling <tt>lua_rawgeti l t r</tt>.
--   Function <tt><a>luaL_unref</a></tt> frees a reference and its
--   associated object.
--   
--   If the object at the top of the stack is nil, <tt>luaL_ref</tt>
--   returns the constant <tt><a>LUA_REFNIL</a></tt>. The constant
--   <tt><a>LUA_NOREF</a></tt> is guaranteed to be different from any
--   reference returned by <tt>luaL_ref</tt>.
luaL_ref :: State -> StackIndex -> IO CInt

-- | Checks whether the function argument <tt>arg</tt> is a userdata of the
--   type <tt>tname</tt> (see <tt><a>luaL_newmetatable</a></tt>) and
--   returns the userdata address (see <tt><a>lua_touserdata</a></tt>).
--   Returns <tt>NULL</tt> if the test fails.
--   
--   <a>https://www.lua.org/manual/5.4/manual.html#luaL_testudata</a>
luaL_testudata :: State -> StackIndex -> CString -> IO (Ptr ())

-- | Creates and pushes a traceback of the stack <tt>l1</tt>. If
--   <tt>msg</tt> is not <tt>NULL</tt> it is appended at the beginning of
--   the traceback. The level parameter tells at which level to start the
--   traceback.
luaL_traceback :: State -> State -> CString -> CInt -> IO ()

-- | Releases reference <tt>ref</tt> from the table at index <tt>t</tt>
--   (see <tt><a>luaL_ref</a></tt>). The entry is removed from the table,
--   so that the referred object can be collected. The reference
--   <tt>ref</tt> is also freed to be used again.
luaL_unref :: State -> StackIndex -> CInt -> IO ()

-- | Key, in the registry, for table of loaded modules.
loadedTableRegistryField :: String

-- | Key, in the registry, for table of preloaded loaders.
preloadTableRegistryField :: String

-- | Reference to a stored value.
data Reference

-- | Reference to a stored value
Reference :: CInt -> Reference

-- | Reference to a nil value
RefNil :: Reference

-- | Value signaling that no reference was created.
pattern LUA_REFNIL :: CInt

-- | Value signaling that no reference was found.
pattern LUA_NOREF :: CInt

-- | Convert a reference to its C representation.
fromReference :: Reference -> CInt

-- | Create a reference from its C representation.
toReference :: CInt -> Reference

-- | Gets information about the <tt>n</tt>-th upvalue of the closure at
--   index <tt>funcindex</tt>. It pushes the upvalue's value onto the stack
--   and returns its name. Returns <tt>NULL</tt> (and pushes nothing) when
--   the index <tt>n</tt> is greater than the number of upvalues.
--   
--   See <a>debug.getupvalue</a> for more information about upvalues.
--   
--   <pre>
--   [0, +(0|1), -]
--   </pre>
--   
--   <a>lua_getupvalue</a>.
lua_getupvalue :: State -> StackIndex -> CInt -> IO CString

-- | Sets the value of a closure’s upvalue. It assigns the value on the top
--   of the stack to the upvalue and returns its name. It also pops the
--   value from the stack.
--   
--   Returns <tt>NULL</tt> (and pops nothing) when the index <tt>n</tt> is
--   greater than the number of upvalues.
--   
--   Parameters <tt>funcindex</tt> and <tt>n</tt> are as in the function
--   <a>lua_getupvalue</a>.
--   
--   <pre>
--   [-(0|1), +0, -]
--   </pre>
--   
--   <a>lua_setupvalue</a>.
lua_setupvalue :: State -> StackIndex -> CInt -> IO CString

-- | Creates a new Lua state and set extra registry values for error
--   bookkeeping.
hsluaL_newstate :: IO State

-- | Converts object to string, respecting any metamethods; returns
--   <tt>NULL</tt> if an error occurs.
hsluaL_tolstring :: State -> StackIndex -> Ptr CSize -> IO (Ptr CChar)

-- | If <tt>modname</tt> is not already present in <tt>package.loaded</tt>.
--   calls function <tt>openf</tt> with string <tt>modname</tt> as an
--   argument and sets the call result in <tt>package.loaded[modname]</tt>,
--   as if that function has been called through <a>require</a>.
--   
--   If <tt>glb</tt> is true, also stores the module into global
--   <tt>modname</tt>.
--   
--   Leaves a copy of the module on the stack.
hsluaL_requiref :: State -> Ptr CChar -> CFunction -> LuaBool -> Ptr StatusCode -> IO ()

-- | Behaves like <tt><a>lua_gettable</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_gettable :: State -> StackIndex -> Ptr StatusCode -> IO TypeCode

-- | Behaves like <tt><a>lua_getglobal</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_getglobal :: State -> CString -> CSize -> Ptr StatusCode -> IO TypeCode

-- | Behaves like <tt><a>lua_settable</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_settable :: State -> StackIndex -> Ptr StatusCode -> IO ()

-- | Behaves like <tt><a>lua_setglobal</a></tt>, but prevents unrecoverable
--   program crashes by calling that function through
--   <tt><a>lua_pcall</a></tt>. Takes an additional status code pointer
--   that is set to the status returned by <tt>lua_pcall</tt>.
hslua_setglobal :: State -> CString -> CSize -> Ptr StatusCode -> IO ()

-- | Replacement for <a>lua_error</a>; it uses the HsLua error signaling
--   convention instead of raw Lua errors.
hslua_error :: State -> IO NumResults

-- | Wrapper around <a>lua_next</a> which catches any Lua errors.
hslua_next :: State -> StackIndex -> Ptr StatusCode -> IO LuaBool

-- | Wrapper around <a>lua_concat</a> which catches any Lua errors.
hslua_concat :: State -> NumArgs -> 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 <tt>op</tt> must be one of the following constants:
--   
--   <ul>
--   <li><b><tt>LUA_OPADD</tt>:</b> performs addition (<tt>+</tt>)</li>
--   <li><b><tt>LUA_OPSUB</tt>:</b> performs subtraction (<tt>-</tt>)</li>
--   <li><b><tt>LUA_OPMUL</tt>:</b> performs multiplication
--   (<tt>*</tt>)</li>
--   <li><b><tt>LUA_OPDIV</tt>:</b> performs float division
--   (<tt>/</tt>)</li>
--   <li><b><tt>LUA_OPIDIV</tt>:</b> performs floor division
--   (<tt>//</tt>)</li>
--   <li><b><tt>LUA_OPMOD</tt>:</b> performs modulo (<tt>%</tt>)</li>
--   <li><b><tt>LUA_OPPOW</tt>:</b> performs exponentiation
--   (<tt>^</tt>)</li>
--   <li><b><tt>LUA_OPUNM</tt>:</b> performs mathematical negation (unary
--   <tt>-</tt>)</li>
--   <li><b><tt>LUA_OPBNOT</tt>:</b> performs bitwise NOT (<tt>~</tt>)</li>
--   <li><b><tt>LUA_OPBAND</tt>:</b> performs bitwise AND
--   (<tt>&amp;</tt>)</li>
--   <li><b><tt>LUA_OPBOR</tt>:</b> performs bitwise OR (<tt>|</tt>)</li>
--   <li><b><tt>LUA_OPBXOR</tt>:</b> performs bitwise exclusive OR
--   (<tt>~</tt>)</li>
--   <li><b><tt>LUA_OPSHL</tt>:</b> performs left shift
--   (<tt>&lt;&lt;</tt>)</li>
--   <li><b><tt>LUA_OPSHR</tt>:</b> performs right shift
--   (<tt>&gt;&gt;</tt>)</li>
--   </ul>
--   
--   This function wraps <tt>lua_arith</tt> and takes an additional
--   parameter <tt>status</tt>; if it is not <tt>NULL</tt>, then the return
--   value is set to the status after calling <tt>lua_arith</tt>.
hslua_arith :: State -> ArithOPCode -> Ptr StatusCode -> IO ()

-- | Compares two Lua values. Returns <tt>1</tt> if the value at index
--   <tt>index1</tt> satisfies op when compared with the value at index
--   <tt>index2</tt>, following the semantics of the corresponding Lua
--   operator (that is, it may call metamethods). Otherwise returns
--   <tt>0</tt>. Also returns <tt>0</tt> if any of the indices is not
--   valid.
--   
--   The value of op must be one of the following constants:
--   
--   <ul>
--   <li><a>LUA_OPEQ</a>: compares for equality (==)</li>
--   <li><a>LUA_OPLT</a>: compares for less than (&lt;)</li>
--   <li><a>LUA_OPLE</a>: compares for less or equal (&lt;=)</li>
--   </ul>
--   
--   This function wraps <tt>lua_compare</tt> and takes an additional
--   parameter <tt>status</tt>; if it is not <tt>NULL</tt>, then the return
--   value is set to the status after calling <tt>lua_compare</tt>.
hslua_compare :: State -> StackIndex -> StackIndex -> OPCode -> Ptr StatusCode -> IO LuaBool

-- | Sets a warning function. This is a simplified version of
--   <a>lua_setwarnf</a>. The function at the top of the stack is set as
--   the "warning hook", i.e., it is called with the concatenated warning
--   components as the single argument.
--   
--   The hook function is popped of the stack.
--   
--   The control messages <tt>@on</tt> and <tt>@off</tt> are still
--   supported; as with the default warning function, these commands can
--   switch error reporting to stderr on and off. The given Haskell
--   function will be called in either case, even when the error is not
--   written to stderr.
hsluaL_setwarnf :: State -> IO ()

-- | Pointer to function opening the base library.
luaopen_base :: CFunction

-- | Pointer to function opening the table library.
luaopen_table :: CFunction

-- | Pointer to function opening the io library.
luaopen_io :: CFunction

-- | Pointer to function opening the os library.
luaopen_os :: CFunction

-- | Pointer to function opening the string library.
luaopen_string :: CFunction

-- | Pointer to function opening the math library.
luaopen_math :: CFunction

-- | Pointer to function opening the debug library.
luaopen_debug :: CFunction

-- | Pointer to function opening the package library.
luaopen_package :: CFunction

-- | 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 <tt><a>hslua_error</a></tt>.
--   
--   <h3>Example</h3>
--   
--   Export the function to calculate triangular numbers.
--   
--   <pre>
--   let triangular :: PreCFunction
--       triangular l' = do
--         n &lt;- lua_tointegerx l' (nthBottom 1) nullPtr
--         lua_pushinteger l' (sum [1..n])
--         return (NumResults 1)
--   
--   hslua_newhsfunction l triangular
--   withCString "triangular" (lua_setglobal l)
--   </pre>
hslua_pushhsfunction :: State -> PreCFunction -> IO ()

-- | The registry index that holds the global environment.
pattern LUA_RIDX_GLOBALS :: Integer

-- | Lua version information in the form "<tt>Lua MAJOR.MINOR</tt>".
pattern LUA_VERSION :: String

-- | Lua version information in the form "<tt>Lua
--   MAJOR.MINOR.RELEASE</tt>".
pattern LUA_RELEASE :: String

-- | Lua copyright information; includes the Lua release
pattern LUA_COPYRIGHT :: String
