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


-- | A Lua language interpreter embedding in Haskell
--   
--   The Foreign.Lua module is a wrapper of Lua language interpreter as
--   described on <a>lua.org</a>.
--   
--   This package contains a full Lua interpreter version 5.3.4. If you
--   want to link it with a system-wide Lua installation, use the
--   <tt>system-lua</tt> flag.
--   
--   <a>Example programs</a> are available in a separate repository.
@package hslua
@version 0.9.5


-- | The core Lua types, including mappings of Lua types to Haskell.
module Foreign.Lua.Api.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 LuaState
LuaState :: (Ptr ()) -> LuaState

-- | 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><tt>gettop</tt></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>gettop</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 (LuaState -> 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 LuaInteger
LuaInteger :: Int64 -> LuaInteger

-- | 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 LuaNumber
LuaNumber :: Double -> LuaNumber

-- | Boolean value returned by a Lua C API function. This is a
--   <tt><a>CInt</a></tt> and 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

-- | Convert a <tt><a>LuaBool</a></tt> to a Haskell <tt><a>Bool</a></tt>.
fromLuaBool :: LuaBool -> Bool

-- | Convert a Haskell <tt><a>Bool</a></tt> to a <tt><a>LuaBool</a></tt>.
toLuaBool :: Bool -> LuaBool

-- | Enumeration used as type tag. See <a>lua_type</a>.
data Type

-- | non-valid stack index
TypeNone :: Type

-- | type of lua's <tt>nil</tt> value
TypeNil :: Type

-- | type of lua booleans
TypeBoolean :: Type

-- | type of light userdata
TypeLightUserdata :: Type

-- | type of lua numbers. See <tt><a>LuaNumber</a></tt>
TypeNumber :: Type

-- | type of lua string values
TypeString :: Type

-- | type of lua tables
TypeTable :: Type

-- | type of functions, either normal or <tt><a>CFunction</a></tt>
TypeFunction :: Type

-- | type of full user data
TypeUserdata :: Type

-- | type of lua threads
TypeThread :: Type

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

-- | Convert a lua Type to a type code which can be passed to the C API.
fromType :: Type -> TypeCode

-- | Convert numerical code to lua type.
toType :: TypeCode -> Type

-- | Lua comparison operations.
data RelationalOperator

-- | Correponds to lua's equality (==) operator.
EQ :: RelationalOperator

-- | Correponds to lua's strictly-lesser-than (&lt;) operator
LT :: RelationalOperator

-- | Correponds to lua's lesser-or-equal (&lt;=) operator
LE :: RelationalOperator

-- | Convert relation operator to its C representation.
fromRelationalOperator :: RelationalOperator -> CInt

-- | Lua status values.
data Status

-- | success
OK :: Status

-- | yielding / suspended coroutine
Yield :: Status

-- | a runtime rror
ErrRun :: Status

-- | syntax error during precompilation
ErrSyntax :: Status

-- | memory allocation (out-of-memory) error.
ErrMem :: Status

-- | error while running the message handler.
ErrErr :: Status

-- | error while running a <tt>__gc</tt> metamethod.
ErrGcmm :: Status

-- | opening or reading a file failed.
ErrFile :: Status

-- | Convert C integer constant to <tt><tt>LuaStatus</tt></tt>.
toStatus :: StatusCode -> Status

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

-- | Value or an error, using the convention that value below zero indicate
--   an error. Values greater than zero are used verbatim. The phantom type
--   is currently used for documentation only and has no effect.
type Failable a = CInt

-- | Enumeration used by <tt>gc</tt> function.
data GCCONTROL
GCSTOP :: GCCONTROL
GCRESTART :: GCCONTROL
GCCOLLECT :: GCCONTROL
GCCOUNT :: GCCONTROL
GCCOUNTB :: GCCONTROL
GCSTEP :: GCCONTROL
GCSETPAUSE :: GCCONTROL
GCSETSTEPMUL :: GCCONTROL

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

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

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

-- | Top of the stack
stackTop :: StackIndex

-- | Bottom of the stack
stackBottom :: StackIndex

-- | The number of arguments expected a function.
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
instance GHC.Show.Show Foreign.Lua.Api.Types.NumResults
instance GHC.Classes.Ord Foreign.Lua.Api.Types.NumResults
instance GHC.Num.Num Foreign.Lua.Api.Types.NumResults
instance GHC.Classes.Eq Foreign.Lua.Api.Types.NumResults
instance GHC.Show.Show Foreign.Lua.Api.Types.NumArgs
instance GHC.Classes.Ord Foreign.Lua.Api.Types.NumArgs
instance GHC.Num.Num Foreign.Lua.Api.Types.NumArgs
instance GHC.Classes.Eq Foreign.Lua.Api.Types.NumArgs
instance GHC.Show.Show Foreign.Lua.Api.Types.StackIndex
instance GHC.Classes.Ord Foreign.Lua.Api.Types.StackIndex
instance GHC.Num.Num Foreign.Lua.Api.Types.StackIndex
instance GHC.Classes.Eq Foreign.Lua.Api.Types.StackIndex
instance GHC.Enum.Enum Foreign.Lua.Api.Types.StackIndex
instance GHC.Show.Show Foreign.Lua.Api.Types.GCCONTROL
instance GHC.Classes.Ord Foreign.Lua.Api.Types.GCCONTROL
instance GHC.Classes.Eq Foreign.Lua.Api.Types.GCCONTROL
instance GHC.Enum.Enum Foreign.Lua.Api.Types.GCCONTROL
instance GHC.Classes.Eq Foreign.Lua.Api.Types.StatusCode
instance GHC.Show.Show Foreign.Lua.Api.Types.Status
instance GHC.Classes.Eq Foreign.Lua.Api.Types.Status
instance GHC.Show.Show Foreign.Lua.Api.Types.RelationalOperator
instance GHC.Classes.Ord Foreign.Lua.Api.Types.RelationalOperator
instance GHC.Classes.Eq Foreign.Lua.Api.Types.RelationalOperator
instance GHC.Show.Show Foreign.Lua.Api.Types.TypeCode
instance GHC.Classes.Ord Foreign.Lua.Api.Types.TypeCode
instance GHC.Classes.Eq Foreign.Lua.Api.Types.TypeCode
instance GHC.Show.Show Foreign.Lua.Api.Types.Type
instance GHC.Classes.Ord Foreign.Lua.Api.Types.Type
instance GHC.Classes.Eq Foreign.Lua.Api.Types.Type
instance GHC.Enum.Bounded Foreign.Lua.Api.Types.Type
instance Foreign.Storable.Storable Foreign.Lua.Api.Types.LuaBool
instance GHC.Classes.Eq Foreign.Lua.Api.Types.LuaBool
instance GHC.Show.Show Foreign.Lua.Api.Types.LuaNumber
instance GHC.Real.RealFrac Foreign.Lua.Api.Types.LuaNumber
instance GHC.Float.RealFloat Foreign.Lua.Api.Types.LuaNumber
instance GHC.Real.Real Foreign.Lua.Api.Types.LuaNumber
instance GHC.Classes.Ord Foreign.Lua.Api.Types.LuaNumber
instance GHC.Num.Num Foreign.Lua.Api.Types.LuaNumber
instance GHC.Real.Fractional Foreign.Lua.Api.Types.LuaNumber
instance GHC.Float.Floating Foreign.Lua.Api.Types.LuaNumber
instance GHC.Classes.Eq Foreign.Lua.Api.Types.LuaNumber
instance GHC.Show.Show Foreign.Lua.Api.Types.LuaInteger
instance GHC.Real.Real Foreign.Lua.Api.Types.LuaInteger
instance GHC.Classes.Ord Foreign.Lua.Api.Types.LuaInteger
instance GHC.Num.Num Foreign.Lua.Api.Types.LuaInteger
instance GHC.Real.Integral Foreign.Lua.Api.Types.LuaInteger
instance GHC.Classes.Eq Foreign.Lua.Api.Types.LuaInteger
instance GHC.Enum.Enum Foreign.Lua.Api.Types.LuaInteger
instance GHC.Classes.Eq Foreign.Lua.Api.Types.LuaState
instance GHC.Enum.Enum Foreign.Lua.Api.Types.Type


-- | Haskell bindings to lua C API functions.
module Foreign.Lua.Api.RawBindings

-- | See <a>lua_close</a>
lua_close :: LuaState -> IO ()

-- | See <a>lua_absindex</a>
lua_absindex :: LuaState -> StackIndex -> IO StackIndex

-- | See <a>lua_gettop</a>
lua_gettop :: LuaState -> IO StackIndex

-- | See <a>lua_settop</a>
lua_settop :: LuaState -> StackIndex -> IO ()

-- | See <a>lua_pushvalue</a>
lua_pushvalue :: LuaState -> StackIndex -> IO ()

-- | See <a>lua_rotate</a>
lua_rotate :: LuaState -> StackIndex -> CInt -> IO ()

-- | See <a>lua_copy</a>
lua_copy :: LuaState -> StackIndex -> StackIndex -> IO ()

-- | See <a>lua_checkstack</a>
lua_checkstack :: LuaState -> StackIndex -> IO LuaBool

-- | See <a>lua_isnumber</a>
lua_isnumber :: LuaState -> StackIndex -> IO LuaBool

-- | See <a>lua_isstring</a>
lua_isstring :: LuaState -> StackIndex -> IO LuaBool

-- | See <a>lua_iscfunction</a>
lua_iscfunction :: LuaState -> StackIndex -> IO LuaBool

-- | See <a>lua_isuserdata</a>
lua_isuserdata :: LuaState -> StackIndex -> IO LuaBool

-- | See <a>lua_type</a>
lua_type :: LuaState -> StackIndex -> IO TypeCode

-- | See <a>lua_typename</a>
lua_typename :: LuaState -> TypeCode -> IO (Ptr CChar)

-- | Wrapper around <a>-- @lua_compare@</a> which catches any
--   <tt>longjmp</tt>s.
hslua_compare :: LuaState -> StackIndex -> StackIndex -> CInt -> IO (Failable LuaBool)

-- | See <a>lua_rawequal</a>
lua_rawequal :: LuaState -> StackIndex -> StackIndex -> IO LuaBool

-- | See <a>lua_toboolean</a>
lua_toboolean :: LuaState -> StackIndex -> IO StackIndex

-- | See <a>lua_tocfunction</a>
lua_tocfunction :: LuaState -> StackIndex -> IO CFunction

-- | See <a>lua_tointegerx</a>
lua_tointegerx :: LuaState -> StackIndex -> Ptr LuaBool -> IO LuaInteger

-- | See <a>lua_tonumberx</a>
lua_tonumberx :: LuaState -> StackIndex -> Ptr LuaBool -> IO LuaNumber

-- | See <a>lua_tolstring</a>
lua_tolstring :: LuaState -> StackIndex -> Ptr CSize -> IO (Ptr CChar)

-- | See <a>lua_topointer</a>
lua_topointer :: LuaState -> StackIndex -> IO (Ptr ())

-- | See <a>lua_tothread</a>
lua_tothread :: LuaState -> StackIndex -> IO LuaState

-- | See <a>lua_touserdata</a>
lua_touserdata :: LuaState -> StackIndex -> IO (Ptr a)

-- | See <a>lua_rawlen</a>
lua_rawlen :: LuaState -> StackIndex -> IO CSize

-- | See <a>lua_pushnil</a>
lua_pushnil :: LuaState -> IO ()

-- | See <a>lua_pushnumber</a>
lua_pushnumber :: LuaState -> LuaNumber -> IO ()

-- | See <a>lua_pushinteger</a>
lua_pushinteger :: LuaState -> LuaInteger -> IO ()

-- | See <a>lua_pushlstring</a>
lua_pushlstring :: LuaState -> Ptr CChar -> CSize -> IO ()

-- | See <a>lua_pushcclosure</a>
lua_pushcclosure :: LuaState -> CFunction -> NumArgs -> IO ()

-- | See <a>lua_pushboolean</a>
lua_pushboolean :: LuaState -> LuaBool -> IO ()

-- | See <a>lua_pushlightuserdata</a>
lua_pushlightuserdata :: LuaState -> Ptr a -> IO ()

-- | See <a>lua_pushthread</a>
lua_pushthread :: LuaState -> IO CInt

-- | Wrapper around <a>-- @lua_gettable@</a> which catches any
--   <tt>longjmp</tt>s.
hslua_gettable :: LuaState -> StackIndex -> IO CInt

-- | Wrapper around <a>-- @lua_getfield@</a> which catches any
--   <tt>longjmp</tt>s.
hslua_getfield :: LuaState -> StackIndex -> Ptr CChar -> IO CInt

-- | See <a>lua_rawget</a>
lua_rawget :: LuaState -> StackIndex -> IO ()

-- | See <a>lua_rawgeti</a>
lua_rawgeti :: LuaState -> StackIndex -> CInt -> IO ()

-- | See <a>lua_createtable</a>
lua_createtable :: LuaState -> CInt -> CInt -> IO ()

-- | See <a>lua_newuserdata</a>
lua_newuserdata :: LuaState -> CInt -> IO (Ptr ())

-- | See <a>lua_getmetatable</a>
lua_getmetatable :: LuaState -> StackIndex -> IO CInt

-- | Wrapper around <a>-- @lua_getglobal@</a> which catches any
--   <tt>longjmp</tt>s.
hslua_getglobal :: LuaState -> Ptr CChar -> IO CInt

-- | Wrapper around <a>-- @lua_settable@</a> which catches any
--   <tt>longjmp</tt>s.
hslua_settable :: LuaState -> StackIndex -> IO CInt

-- | Wrapper around <a>-- @lua_setfield@</a> which catches any
--   <tt>longjmp</tt>s.
hslua_setfield :: LuaState -> StackIndex -> Ptr CChar -> IO CInt

-- | See <a>lua_rawset</a>
lua_rawset :: LuaState -> StackIndex -> IO ()

-- | See <a>lua_rawseti</a>
lua_rawseti :: LuaState -> StackIndex -> CInt -> IO ()

-- | See <a>lua_setmetatable</a>
lua_setmetatable :: LuaState -> StackIndex -> IO ()

-- | Wrapper around <a>-- @lua_setglobal@</a> which catches any
--   <tt>longjmp</tt>s.
hslua_setglobal :: LuaState -> Ptr CChar -> IO CInt

-- | See <a>lua_pcallk</a>
lua_pcallk :: LuaState -> NumArgs -> NumResults -> StackIndex -> CInt -> Ptr () -> IO StatusCode

-- | See <a>lua_status</a>
lua_status :: LuaState -> IO StatusCode

-- | See <a>lua_gc</a>
lua_gc :: LuaState -> CInt -> CInt -> IO CInt

-- | Wrapper around <a>-- @lua_next@</a> which catches any
--   <tt>longjmp</tt>s.
hslua_next :: LuaState -> StackIndex -> IO (Failable LuaBool)

-- | Wrapper around <a>-- @lua_concat@</a> which catches any
--   <tt>longjmp</tt>s.
hslua_concat :: LuaState -> NumArgs -> IO (Failable LuaBool)

-- | See <a>luaL_openlibs</a>
luaL_openlibs :: LuaState -> IO ()

-- | Point to function opening the base library.
lua_open_base_ptr :: CFunction

-- | Point to function opening the table library.
lua_open_table_ptr :: CFunction

-- | Point to function opening the io library.
lua_open_io_ptr :: CFunction

-- | Point to function opening the os library.
lua_open_os_ptr :: CFunction

-- | Point to function opening the string library.
lua_open_string_ptr :: CFunction

-- | Point to function opening the math library.
lua_open_math_ptr :: CFunction

-- | Point to function opening the debug library.
lua_open_debug_ptr :: CFunction

-- | Point to function opening the package library.
lua_open_package_ptr :: CFunction

-- | See <a>luaL_newstate</a>
luaL_newstate :: IO LuaState

-- | See <a>luaL_newmetatable</a>
luaL_newmetatable :: LuaState -> Ptr CChar -> IO LuaBool

-- | See <a>luaL_ref</a>
luaL_ref :: LuaState -> StackIndex -> IO CInt

-- | See <a>luaL_unref</a>
luaL_unref :: LuaState -> StackIndex -> CInt -> IO ()

-- | See <a>luaL_loadfilex</a>
luaL_loadfilex :: LuaState -> Ptr CChar -> Ptr CChar -> IO StatusCode

-- | See <a>luaL_loadstring</a>
luaL_loadstring :: LuaState -> Ptr CChar -> IO StatusCode
hslua_call_hs_ptr :: CFunction


-- | Lua constants
module Foreign.Lua.Api.Constants

-- | Alias for C constant <tt>LUA_MULTRET</tt>. See <a>lua_call</a>.
multret :: NumResults

-- | Alias for C constant <tt>LUA_REGISTRYINDEX</tt>. See <a>Lua
--   registry</a>.
registryindex :: StackIndex

-- | Value signaling that no reference was created.
refnil :: Int

-- | Value signaling that no reference was found.
noref :: Int


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

-- | Lua computation
newtype Lua a
Lua :: ReaderT LuaState IO a -> Lua a
[unLua] :: Lua a -> ReaderT LuaState IO a

-- | Get the lua state of this lua computation.
luaState :: Lua LuaState

-- | Run lua computation with custom lua state. Errors are left unhandled,
--   the caller of this function is responsible to catch lua errors.
runLuaWith :: LuaState -> Lua a -> IO a

-- | Lift a computation from the <a>IO</a> monad.
liftIO :: MonadIO m => forall a. () => IO a -> m a

-- | Turn a function of typ <tt>LuaState -&gt; IO a</tt> into a monadic lua
--   operation.
liftLua :: (LuaState -> IO a) -> Lua a

-- | Turn a function of typ <tt>LuaState -&gt; a -&gt; IO b</tt> into a
--   monadic lua operation.
liftLua1 :: (LuaState -> a -> IO b) -> a -> Lua b
instance Control.Monad.Catch.MonadThrow Foreign.Lua.Types.Lua.Lua
instance Control.Monad.Reader.Class.MonadReader Foreign.Lua.Api.Types.LuaState Foreign.Lua.Types.Lua.Lua
instance Control.Monad.Catch.MonadMask Foreign.Lua.Types.Lua.Lua
instance Control.Monad.IO.Class.MonadIO Foreign.Lua.Types.Lua.Lua
instance Control.Monad.Catch.MonadCatch Foreign.Lua.Types.Lua.Lua
instance GHC.Base.Monad Foreign.Lua.Types.Lua.Lua
instance GHC.Base.Functor Foreign.Lua.Types.Lua.Lua
instance GHC.Base.Applicative Foreign.Lua.Types.Lua.Lua


-- | Lua exceptions and exception handling.
module Foreign.Lua.Types.Error

-- | Exceptions raised by Lua-related operations.
data LuaException
LuaException :: String -> LuaException

-- | Catch a <tt><a>LuaException</a></tt>.
catchLuaError :: Lua a -> (LuaException -> Lua a) -> Lua a

-- | Raise a <tt><a>LuaException</a></tt> containing the given error
--   message.
throwLuaError :: String -> Lua a

-- | Catch <tt><a>LuaException</a></tt>, alter the error message and
--   rethrow.
modifyLuaError :: Lua a -> (String -> String) -> Lua a

-- | Return either the result of a Lua computation or, if an exception was
--   thrown, the error.
tryLua :: Lua a -> Lua (Either LuaException a)
instance GHC.Classes.Eq Foreign.Lua.Types.Error.LuaException
instance GHC.Show.Show Foreign.Lua.Types.Error.LuaException
instance GHC.Exception.Exception Foreign.Lua.Types.Error.LuaException
instance GHC.Base.Alternative Foreign.Lua.Types.Lua.Lua


-- | Monadic functions which operate within the Lua type.
--   
--   The functions in this module are mostly just thin wrappers around the
--   respective C functions. However, C function which can throw an error
--   are wrapped such that the error is converted into a
--   <tt><a>LuaException</a></tt>. Memory allocation errors, however, are
--   not caught and will cause the host program to terminate.
module Foreign.Lua.Api

-- | 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><tt>gettop</tt></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>gettop</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 (LuaState -> 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>.
data LuaInteger

-- | 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>.
data LuaNumber

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

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

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

-- | Top of the stack
stackTop :: StackIndex

-- | Bottom of the stack
stackBottom :: StackIndex

-- | The number of arguments expected a function.
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

-- | Alias for C constant <tt>LUA_MULTRET</tt>. See <a>lua_call</a>.
multret :: NumResults

-- | Alias for C constant <tt>LUA_REGISTRYINDEX</tt>. See <a>Lua
--   registry</a>.
registryindex :: StackIndex

-- | Value signaling that no reference was found.
noref :: Int

-- | Value signaling that no reference was created.
refnil :: Int

-- | Returns the pseudo-index that represents the <tt>i</tt>-th upvalue of
--   the running function (see <a>§4.4</a> of the Lua 5.3 reference
--   manual).
--   
--   See also: <a>lua_upvalueindex</a>.
upvalueindex :: StackIndex -> StackIndex

-- | 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 LuaState
LuaState :: (Ptr ()) -> LuaState

-- | Creates a new Lua state. It calls <tt><tt>lua_newstate</tt></tt> with
--   an allocator based on the standard C <tt>realloc</tt> function and
--   then sets a panic function (see <a>§4.6</a> of the Lua 5.3 Reference
--   Manual) that prints an error message to the standard error output in
--   case of fatal errors.
--   
--   See also: <a>luaL_newstate</a>.
newstate :: IO LuaState

-- | 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. On 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.
--   
--   This is a wrapper function of <a>lua_close</a>.
close :: LuaState -> IO ()

-- | Converts the acceptable index <tt>idx</tt> into an equivalent absolute
--   index (that is, one that does not depend on the stack top).
absindex :: StackIndex -> Lua 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).
--   
--   See also: <a>lua_gettop</a>.
gettop :: Lua 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 index is 0, then all stack elements are removed.
--   
--   See also: <a>lua_settop</a>.
settop :: StackIndex -> Lua ()

-- | Pushes a copy of the element at the given index onto the stack.
--   
--   See <a>lua_pushvalue</a>.
pushvalue :: StackIndex -> Lua ()

-- | 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.
--   
--   See also <a>lua_copy</a> in the lua manual.
copy :: StackIndex -> StackIndex -> Lua ()

-- | 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.
--   
--   See also: <a>lua_insert</a>.
insert :: StackIndex -> Lua ()

-- | Pops <tt>n</tt> elements from the stack.
--   
--   See also: <a>lua_pop</a>.
pop :: StackIndex -> Lua ()

-- | 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.
--   
--   See <a>lua_remove</a>.
remove :: StackIndex -> Lua ()

-- | 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.
--   
--   See <a>lua_replace</a>.
replace :: StackIndex -> Lua ()

-- | 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.
--   
--   This is a wrapper function of <a>lua_checkstack</a>.
checkstack :: Int -> Lua Bool

-- | Enumeration used as type tag. See <a>lua_type</a>.
data Type

-- | non-valid stack index
TypeNone :: Type

-- | type of lua's <tt>nil</tt> value
TypeNil :: Type

-- | type of lua booleans
TypeBoolean :: Type

-- | type of light userdata
TypeLightUserdata :: Type

-- | type of lua numbers. See <tt><a>LuaNumber</a></tt>
TypeNumber :: Type

-- | type of lua string values
TypeString :: Type

-- | type of lua tables
TypeTable :: Type

-- | type of functions, either normal or <tt><a>CFunction</a></tt>
TypeFunction :: Type

-- | type of full user data
TypeUserdata :: Type

-- | type of lua threads
TypeThread :: Type

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

-- | Convert a lua Type to a type code which can be passed to the C API.
fromType :: Type -> TypeCode

-- | Convert numerical code to lua type.
toType :: TypeCode -> Type

-- | See <a>lua_type</a>.
ltype :: StackIndex -> Lua Type

-- | Returns the name of the type encoded by the value <tt>tp</tt>, which
--   must be one the values returned by <tt><a>ltype</a></tt>.
--   
--   See also: <a>lua_typename</a>.
typename :: Type -> Lua String

-- | Returns <tt>True</tt> if the value at the given index is a boolean,
--   and <tt>False</tt> otherwise.
--   
--   See also: <a>lua_isboolean</a>.
isboolean :: StackIndex -> Lua Bool

-- | Returns <tt>True</tt> if the value at the given index is a C function,
--   and <tt>False</tt> otherwise.
--   
--   See also: <a>lua_iscfunction</a>.
iscfunction :: StackIndex -> Lua Bool

-- | Returns <tt>True</tt> if the value at the given index is a function
--   (either C or Lua), and <tt>False</tt> otherwise.
--   
--   See also: <a>lua_isfunction</a>.
isfunction :: StackIndex -> Lua Bool

-- | Returns <tt>True</tt> if the value at the given index is a light
--   userdata, and <tt>False</tt> otherwise.
--   
--   See also: <a>-- lua_islightuserdata</a>.
islightuserdata :: StackIndex -> Lua Bool

-- | Returns <tt>True</tt> if the value at the given index is <tt>nil</tt>,
--   and <tt>False</tt> otherwise.
--   
--   See also: <a>lua_isnil</a>.
isnil :: StackIndex -> Lua Bool

-- | Returns <tt>True</tt> if the given index is not valid, and
--   <tt>False</tt> otherwise.
--   
--   See also: <a>lua_isnone</a>.
isnone :: StackIndex -> Lua Bool

-- | Returns <tt>True</tt> if the given index is not valid or if the value
--   at the given index is <tt>nil</tt>, and <tt>False</tt> otherwise.
--   
--   See also: <a>lua_isnoneornil</a>.
isnoneornil :: StackIndex -> Lua Bool

-- | Returns <tt>True</tt> if the value at the given index is a number or a
--   string convertible to a number, and <tt>False</tt> otherwise.
--   
--   See also: <a>lua_isnumber</a>.
isnumber :: StackIndex -> Lua Bool

-- | Returns <tt>True</tt> if the value at the given index is a string or a
--   number (which is always convertible to a string), and <tt>False</tt>
--   otherwise.
--   
--   See also: <a>lua_isstring</a>.
isstring :: StackIndex -> Lua Bool

-- | Returns <tt>True</tt> if the value at the given index is a table, and
--   <tt>False</tt> otherwise.
--   
--   See also: <a>lua_istable</a>.
istable :: StackIndex -> Lua Bool

-- | Returns <tt>True</tt> if the value at the given index is a thread, and
--   <tt>False</tt> otherwise.
--   
--   See also: <a>lua_isthread</a>.
isthread :: StackIndex -> Lua Bool

-- | Returns <tt>True</tt> if the value at the given index is a userdata
--   (either full or light), and <tt>False</tt> otherwise.
--   
--   See also: <a>lua_isuserdata</a>.
isuserdata :: StackIndex -> Lua Bool

-- | 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 <tt>false</tt> and <tt>nil</tt>;
--   otherwise it returns <tt>False</tt>. (If you want to accept only
--   actual boolean values, use <tt><a>isboolean</a></tt> to test the
--   value's type.)
--   
--   See also: <a>lua_toboolean</a>.
toboolean :: StackIndex -> Lua Bool

-- | Converts a value at the given index to a C function. That value must
--   be a C function; otherwise, returns NULL.
--   
--   See also: <a>lua_tocfunction</a>.
tocfunction :: StackIndex -> Lua CFunction

-- | Converts the Lua value at the given acceptable index to the signed
--   integral type <tt><tt>lua_Integer</tt></tt>. 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.3 Reference Manual); otherwise,
--   <tt>tointeger</tt> returns 0.
--   
--   If the number is not an integer, it is truncated in some non-specified
--   way.
--   
--   See also: <a>lua_tointeger</a>.
tointeger :: StackIndex -> Lua LuaInteger

-- | Like <tt><a>tointeger</a></tt>, but returns <tt>Nothing</tt> if the
--   conversion failed
tointegerx :: StackIndex -> Lua (Maybe LuaInteger)

-- | Converts the Lua value at the given index to the C type lua_Number.
--   The Lua value must be a number or a string convertible to a number;
--   otherwise, <tt>tonumber</tt> returns 0.
--   
--   See <a>lua_tonumber</a>.
tonumber :: StackIndex -> Lua LuaNumber

-- | Like <tt><a>tonumber</a></tt>, but returns <tt>Nothing</tt> if the
--   conversion failed
tonumberx :: StackIndex -> Lua (Maybe LuaNumber)

-- | Converts the value at the given index to a generic C pointer (void*).
--   The value can be a userdata, a table, a thread, or a function;
--   otherwise, lua_topointer returns NULL. 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.
--   
--   See also: <a>lua_topointer</a>.
topointer :: StackIndex -> Lua (Ptr ())

-- | See <a>lua_tostring</a>.
tostring :: StackIndex -> Lua ByteString

-- | Converts the value at the given index to a Lua thread (represented as
--   lua_State*). This value must be a thread; otherwise, the function
--   returns NULL.
--   
--   See also: <a>lua_tothread</a>.
tothread :: StackIndex -> Lua LuaState

-- | 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 NULL.
--   
--   See also: <a>lua_touserdata</a>.
touserdata :: StackIndex -> Lua (Ptr a)

-- | Obsolete alias for <tt><a>rawlen</a></tt>.

-- | <i>Deprecated: Use rawlen instead.</i>
objlen :: StackIndex -> Lua Int

-- | 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.
--   
--   See also: <a>lua_rawlen</a>.
rawlen :: StackIndex -> Lua Int

-- | Compatibility alias for rawlen

-- | <i>Deprecated: Use rawlen instead.</i>
strlen :: StackIndex -> Lua Int

-- | Lua comparison operations.
data RelationalOperator

-- | Correponds to lua's equality (==) operator.
EQ :: RelationalOperator

-- | Correponds to lua's strictly-lesser-than (&lt;) operator
LT :: RelationalOperator

-- | Correponds to lua's lesser-or-equal (&lt;=) operator
LE :: RelationalOperator

-- | Convert relation operator to its C representation.
fromRelationalOperator :: RelationalOperator -> CInt

-- | Compares two Lua values. Returns <tt>True</tt> if the value at index
--   <tt>idx1</tt> satisfies <tt>op</tt> when compared with the value at
--   index <tt>idx2</tt>, following the semantics of the corresponding Lua
--   operator (that is, it may call metamethods). Otherwise returns
--   <tt>False</tt>. Also returns <tt>False</tt> if any of the indices is
--   not valid.
--   
--   The value of op must be of type <tt><tt>LuaComparerOp</tt></tt>:
--   
--   OpEQ: compares for equality (==) OpLT: compares for less than (&lt;)
--   OpLE: compares for less or equal (&lt;=)
--   
--   This is a wrapper function of <a>lua_compare</a>.
compare :: StackIndex -> StackIndex -> RelationalOperator -> Lua Bool

-- | Returns <tt>True</tt> if the two values in acceptable indices index1
--   and index2 are equal, following the semantics of the Lua <tt>==</tt>
--   operator (that is, may call metamethods). Otherwise returns False.
--   Also returns False if any of the indices is non valid. Uses
--   <tt><a>compare</a></tt> internally.
equal :: StackIndex -> StackIndex -> Lua Bool

-- | Tests whether the object under the first index is smaller than that
--   under the second. Uses <tt><a>compare</a></tt> internally.
lessthan :: StackIndex -> StackIndex -> Lua Bool

-- | 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.
--   
--   See also: <a>lua_rawequal</a>.
rawequal :: StackIndex -> StackIndex -> Lua Bool

-- | Pushes a boolean value with the given value onto the stack.
--   
--   See also: <a>lua_pushboolean</a>.
pushboolean :: Bool -> Lua ()

-- | 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 function
--   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
--   <tt><a>CFunction</a></tt>)
--   
--   See also: <a>lua_pushcfunction</a>.
pushcfunction :: CFunction -> Lua ()

-- | 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.
--   
--   See also: <a>lua_pushcclosure</a>.
pushcclosure :: CFunction -> Int -> Lua ()

-- | Pushes an integer with with the given value onto the stack.
--   
--   See also: <a>lua_pushinteger</a>.
pushinteger :: LuaInteger -> Lua ()

-- | 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.
--   
--   See also: <a>lua_pushlightuserdata</a>.
pushlightuserdata :: Ptr a -> Lua ()

-- | Pushes a nil value onto the stack.
--   
--   See <a>lua_pushnil</a>.
pushnil :: Lua ()

-- | Pushes a float with the given value onto the stack.
--   
--   See <a>lua_pushnumber</a>.
pushnumber :: LuaNumber -> Lua ()

-- | Pushes the zero-terminated string pointed to by s 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.
--   
--   See also: <a>-- lua_pushstring</a>.
pushstring :: ByteString -> Lua ()

-- | Pushes the current thread onto the stack. Returns <tt>True</tt> if
--   this thread is the main thread of its state, <tt>False</tt> otherwise.
--   
--   See also: <a>lua_pushthread</a>.
pushthread :: Lua Bool

-- | Pushes onto the stack the value of the global <tt>name</tt>. Returns
--   the type of that value.
--   
--   Wrapper of <a>lua_getglobal</a>.
getglobal :: String -> Lua ()

-- | 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> of lua's manual).
--   
--   See also: <a>lua_gettable</a>.
gettable :: StackIndex -> Lua ()

-- | Pushes onto the stack the value <tt>t[k]</tt>, where <tt>t</tt> is the
--   value at the given stack index. As in Lua, this function may trigger a
--   metamethod for the "index" event (see <a>§2.4</a> of lua's manual).
--   
--   Returns the type of the pushed value.
--   
--   See also: <a>lua_getfield</a>.
getfield :: StackIndex -> String -> Lua ()

-- | Similar to <tt><a>gettable</a></tt>, but does a raw access (i.e.,
--   without metamethods).
--   
--   See also: <a>lua_rawget</a>.
rawget :: StackIndex -> Lua ()

-- | 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.
--   
--   See also: <a>lua_rawgeti</a>.
rawgeti :: StackIndex -> Int -> Lua ()

-- | Creates a new empty table and pushes it onto the stack. Parameter narr
--   is a hint for how many elements the table will have as a sequence;
--   parameter nrec 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 lua_newtable.
--   
--   This is a wrapper for function <a>lua_createtable</a>.
createtable :: Int -> Int -> Lua ()

-- | Creates a new empty table and pushes it onto the stack. It is
--   equivalent to <tt>createtable 0 0</tt>.
--   
--   See also: <a>lua_newtable</a>.
newtable :: Lua ()

-- | This function allocates a new block of memory with the given size,
--   pushes onto the stack a new full userdata with the block address, and
--   returns this address. The host program can freely use this memory.
--   
--   See also: <a>lua_newuserdata</a>.
newuserdata :: Int -> Lua (Ptr ())

-- | If the value at the given index has a metatable, the function pushes
--   that metatable onto the stack and returns <tt>True</tt>. Otherwise,
--   the function returns <tt>False</tt> and pushes nothing on the stack.
--   
--   See also: <a>lua_getmetatable</a>.
getmetatable :: StackIndex -> Lua Bool

-- | Pops a value from the stack and sets it as the new value of global
--   <tt>name</tt>.
--   
--   See also: <a>lua_setglobal</a>.
setglobal :: String -> Lua ()

-- | 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> of the Lua 5.3 Reference Manual).
--   
--   See also: <a>lua_settable</a>.
settable :: StackIndex -> Lua ()

-- | Does the equivalent to <tt>t[k] = v</tt>, where <tt>t</tt> is the
--   value 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. As in Lua, this function
--   may trigger a metamethod for the "newindex" event (see <a>§2.4</a> of
--   the Lua 5.3 Reference Manual).
--   
--   See also: <a>lua_setfield</a>.
setfield :: StackIndex -> String -> Lua ()

-- | Similar to <tt><a>settable</a></tt>, but does a raw assignment (i.e.,
--   without metamethods).
--   
--   See also: <a>lua_rawset</a>.
rawset :: StackIndex -> Lua ()

-- | 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.
--   
--   See also: <a>lua_rawseti</a>.
rawseti :: StackIndex -> Int -> Lua ()

-- | Pops a table from the stack and sets it as the new metatable for the
--   value at the given index.
--   
--   See also: <a>-- lua_setmetatable</a>.
setmetatable :: StackIndex -> Lua ()

-- | Calls a function.
--   
--   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>call</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>multret</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.
--   
--   Any error inside the called function cause a
--   <tt><a>LuaException</a></tt> to be thrown.
--   
--   The following example shows how the host program can do the equivalent
--   to this Lua code:
--   
--   <pre>
--   a = f("how", t.x, 14)
--   </pre>
--   
--   Here it is in Haskell (assuming the OverloadedStrings language
--   extension):
--   
--   <pre>
--   getglobal "f"         -- function to be called
--   pushstring  "how"     -- 1st argument
--   getglobal "t"         -- table to be indexed
--   getfield (-1) "x"     -- push result of t.x (2nd arg)
--   remove (-2)           -- remove 't' from the stack
--   pushinteger 14        -- 3rd argument
--   call 3 1              -- call 'f' with 3 arguments and 1 result
--   setglobal "a"         -- set global 'a'
--   </pre>
--   
--   Note that the code above is "balanced": at its end, the stack is back
--   to its original configuration. This is considered good programming
--   practice.
--   
--   See <a>lua_call</a>.
call :: NumArgs -> NumResults -> Lua ()

-- | Calls a function in protected mode.
--   
--   Both <tt>nargs</tt> and <tt>nresults</tt> have the same meaning as in
--   <tt><a>call</a></tt>. If there are no errors during the call,
--   <tt>pcall</tt> behaves exactly like <tt><a>call</a></tt>. However, if
--   there is any error, <tt>pcall</tt> catches it, pushes a single value
--   on the stack (the error message), and returns the error code. Like
--   <tt><a>call</a></tt>, <tt>pcall</tt> always removes the function and
--   its arguments from the stack.
--   
--   If <tt>msgh</tt> is <tt>Nothing</tt>, then the error object returned
--   on the stack is exactly the original error object. Otherwise, when
--   <tt>msgh</tt> is <tt>Just idx</tt>, the stack index <tt>idx</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>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>pcall</a></tt>, since by
--   then the stack has unwound.
--   
--   See <a>lua_pcall</a>.
pcall :: NumArgs -> NumResults -> Maybe StackIndex -> Lua Status

-- | See <a>luaL_loadfile</a>.
loadfile :: String -> Lua Status

-- | See <a>luaL_loadstring</a>.
loadstring :: String -> Lua Status

-- | Lua status values.
data Status

-- | success
OK :: Status

-- | yielding / suspended coroutine
Yield :: Status

-- | a runtime rror
ErrRun :: Status

-- | syntax error during precompilation
ErrSyntax :: Status

-- | memory allocation (out-of-memory) error.
ErrMem :: Status

-- | error while running the message handler.
ErrErr :: Status

-- | error while running a <tt>__gc</tt> metamethod.
ErrGcmm :: Status

-- | opening or reading a file failed.
ErrFile :: Status

-- | Convert C integer constant to <tt><tt>LuaStatus</tt></tt>.
toStatus :: StatusCode -> Status

-- | Returns the status of this Lua thread.
--   
--   The status can be <tt><a>OK</a></tt> for a normal thread, an error
--   value if the thread finished the execution of a
--   <tt><tt>lua_resume</tt></tt> with an error, or <tt><a>Yield</a></tt>
--   if the thread is suspended.
--   
--   You can only call functions in threads with status <tt><a>OK</a></tt>.
--   You can resume threads with status <tt><a>OK</a></tt> (to start a new
--   coroutine) or <tt><a>Yield</a></tt> (to resume a coroutine).
--   
--   See also: <a>lua_status</a>.
status :: Lua Status

-- | Enumeration used by <tt>gc</tt> function.
data GCCONTROL
GCSTOP :: GCCONTROL
GCRESTART :: GCCONTROL
GCCOLLECT :: GCCONTROL
GCCOUNT :: GCCONTROL
GCCOUNTB :: GCCONTROL
GCSTEP :: GCCONTROL
GCSETPAUSE :: GCCONTROL
GCSETSTEPMUL :: GCCONTROL

-- | Controls the garbage collector.
--   
--   This function performs several tasks, according to the value of the
--   parameter what:
--   
--   <ul>
--   <li><tt><a>GCSTOP</a></tt>: stops the garbage collector.</li>
--   <li><tt><a>GCRESTART</a></tt>: restarts the garbage collector.</li>
--   <li><tt><a>GCCOLLECT</a></tt>: performs a full garbage-collection
--   cycle.</li>
--   <li><tt><a>GCCOUNT</a></tt>: returns the current amount of memory (in
--   Kbytes) in use by Lua.</li>
--   <li><tt><a>GCCOUNTB</a></tt>: returns the remainder of dividing the
--   current amount of bytes of memory in use by Lua by 1024.</li>
--   <li><tt><a>GCSTEP</a></tt>: performs an incremental step of garbage
--   collection. The step "size" is controlled by data (larger values mean
--   more steps) in a non-specified way. If you want to control the step
--   size you must experimentally tune the value of data. The function
--   returns 1 if the step finished a garbage-collection cycle.</li>
--   <li><tt>'GCSETPAUSE</tt>': sets data as the new value for the pause of
--   the collector (see §2.10). The function returns the previous value of
--   the pause.</li>
--   <li><tt><a>GCSETSTEPMUL</a></tt>: sets data as the new value for the
--   step multiplier of the collector (see §2.10). The function returns the
--   previous value of the step multiplier.</li>
--   </ul>
--   
--   See <a>lua_gc</a>.
gc :: GCCONTROL -> Int -> Lua Int

-- | 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 <tt>next</tt> returns
--   <tt>False</tt> (and pushes nothing).
--   
--   See also: <a>lua_next</a>.
next :: StackIndex -> Lua Bool

-- | This is a convenience function to implement error propagation
--   convention described in <a>Error handling in hslua</a>. hslua doesn't
--   implement <tt>lua_error</tt> function from Lua C API because it's
--   never safe to use. (see <a>Error handling in hslua</a> for details)
lerror :: Lua Int

-- | 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).
--   
--   This is a wrapper function of <a>lua_concat</a>.
concat :: NumArgs -> Lua ()

-- | Sets the C function <tt>f</tt> as the new value of global
--   <tt>name</tt>.
--   
--   See <a>lua_register</a>.
register :: String -> CFunction -> Lua ()

-- | Opens all standard Lua libraries into the current state.
--   
--   | See <a>luaopen_base</a>.
openbase :: Lua ()

-- | Opens Lua's <i>debug</i> library into the current state.
--   
--   See also: <a>luaopen_debug</a>.
opendebug :: Lua ()

-- | Opens Lua's <i>io</i> library into the current state.
--   
--   See also: <a>luaopen_io</a>.
openio :: Lua ()

-- | Opens all standard Lua libraries into the current state.
--   
--   See also: <a>luaL_openlibs</a>.
openlibs :: Lua ()

-- | Opens Lua's <i>math</i> library into the current state.
--   
--   See also: <a>luaopen_math</a>.
openmath :: Lua ()

-- | Opens Lua's <i>package</i> library into the current state.
--   
--   See also: <a>luaopen_package</a>.
openpackage :: Lua ()

-- | Opens Lua's <i>os</i> library into the current state.
--   
--   See also: <a>luaopen_os</a>.
openos :: Lua ()

-- | Opens Lua's <i>string</i> library into the current state.
--   
--   See also: <a>luaopen_string</a>.
openstring :: Lua ()

-- | Opens Lua's <i>table</i> library into the current state.
--   
--   See also: <a>luaopen_table</a>.
opentable :: Lua ()

-- | Loads and runs the given string.
--   
--   Returns <tt><a>OK</a></tt> on success, or an error if either loading
--   of the string or calling of the thunk failed.
dostring :: String -> Lua Status

-- | Loads and runs the given file.
dofile :: FilePath -> Lua Status

-- | If the registry already has the key tname, returns <tt>False</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>True</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.
--   
--   See also: <a>luaL_newmetatable</a>.
newmetatable :: String -> Lua Bool

-- | 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>ref</tt> ensures the
--   uniqueness of the key it returns. You can retrieve an object referred
--   by reference <tt>r</tt> by calling <tt>rawgeti t r</tt>. Function
--   <tt><a>unref</a></tt> frees a reference and its associated object.
--   
--   If the object at the top of the stack is nil, <tt><a>ref</a></tt>
--   returns the constant <tt><a>refnil</a></tt>. The constant
--   <tt><a>noref</a></tt> is guaranteed to be different from any reference
--   returned by <tt><a>ref</a></tt>.
--   
--   See also: <a>luaL_ref</a>.
ref :: StackIndex -> Lua Int

-- | Releases reference <tt><a>ref</a></tt> from the table at index
--   <tt>idx</tt> (see <tt><a>ref</a></tt>). The entry is removed from the
--   table, so that the referred object can be collected. The reference
--   <tt><a>ref</a></tt> is also freed to be used again.
--   
--   See also: <a>luaL_unref</a>.
unref :: StackIndex -> Int -> Lua ()
throwTopMessageAsError :: Lua a
throwTopMessageAsError' :: (String -> String) -> Lua a

-- | Convert a Haskell function userdata object into a CFuntion. The
--   userdata object must be at the top of the stack. Errors signaled via
--   lerror are converted to lua errors.
wrapHaskellFunction :: Lua ()


-- | Sending haskell objects to the lua stack.
module Foreign.Lua.Types.FromLuaStack

-- | A value that can be read from the Lua stack.
class FromLuaStack a

-- | Check if at index <tt>n</tt> there is a convertible Lua value and if
--   so return it. Throws a <tt><a>LuaException</a></tt> otherwise.
peek :: FromLuaStack a => StackIndex -> Lua a

-- | Result returned when trying to get a value from the lua stack.
type Result a = Either String a

-- | Try to convert the value at the given stack index to a haskell value.
--   Returns <tt>Left</tt> with an error message on failure.
peekEither :: FromLuaStack a => StackIndex -> Lua (Either String a)

-- | Read a table into a list of pairs.
pairsFromTable :: (FromLuaStack a, FromLuaStack b) => StackIndex -> Lua [(a, b)]

-- | Read a table into a list
toList :: FromLuaStack a => StackIndex -> Lua [a]
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack ()
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack Foreign.Lua.Api.Types.LuaInteger
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack Foreign.Lua.Api.Types.LuaNumber
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack Data.ByteString.Internal.ByteString
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack GHC.Types.Bool
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack Foreign.Lua.Api.Types.CFunction
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack (GHC.Ptr.Ptr a)
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack Foreign.Lua.Api.Types.LuaState
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack Data.Text.Internal.Text
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack Data.ByteString.Lazy.Internal.ByteString
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack [GHC.Types.Char]
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack a => Foreign.Lua.Types.FromLuaStack.FromLuaStack [a]
instance (GHC.Classes.Ord a, Foreign.Lua.Types.FromLuaStack.FromLuaStack a, Foreign.Lua.Types.FromLuaStack.FromLuaStack b) => Foreign.Lua.Types.FromLuaStack.FromLuaStack (Data.Map.Internal.Map a b)
instance (Foreign.Lua.Types.FromLuaStack.FromLuaStack a, Foreign.Lua.Types.FromLuaStack.FromLuaStack b) => Foreign.Lua.Types.FromLuaStack.FromLuaStack (a, b)
instance (Foreign.Lua.Types.FromLuaStack.FromLuaStack a, Foreign.Lua.Types.FromLuaStack.FromLuaStack b, Foreign.Lua.Types.FromLuaStack.FromLuaStack c) => Foreign.Lua.Types.FromLuaStack.FromLuaStack (a, b, c)
instance (Foreign.Lua.Types.FromLuaStack.FromLuaStack a, Foreign.Lua.Types.FromLuaStack.FromLuaStack b, Foreign.Lua.Types.FromLuaStack.FromLuaStack c, Foreign.Lua.Types.FromLuaStack.FromLuaStack d) => Foreign.Lua.Types.FromLuaStack.FromLuaStack (a, b, c, d)
instance (Foreign.Lua.Types.FromLuaStack.FromLuaStack a, Foreign.Lua.Types.FromLuaStack.FromLuaStack b, Foreign.Lua.Types.FromLuaStack.FromLuaStack c, Foreign.Lua.Types.FromLuaStack.FromLuaStack d, Foreign.Lua.Types.FromLuaStack.FromLuaStack e) => Foreign.Lua.Types.FromLuaStack.FromLuaStack (a, b, c, d, e)
instance (Foreign.Lua.Types.FromLuaStack.FromLuaStack a, Foreign.Lua.Types.FromLuaStack.FromLuaStack b, Foreign.Lua.Types.FromLuaStack.FromLuaStack c, Foreign.Lua.Types.FromLuaStack.FromLuaStack d, Foreign.Lua.Types.FromLuaStack.FromLuaStack e, Foreign.Lua.Types.FromLuaStack.FromLuaStack f) => Foreign.Lua.Types.FromLuaStack.FromLuaStack (a, b, c, d, e, f)
instance (Foreign.Lua.Types.FromLuaStack.FromLuaStack a, Foreign.Lua.Types.FromLuaStack.FromLuaStack b, Foreign.Lua.Types.FromLuaStack.FromLuaStack c, Foreign.Lua.Types.FromLuaStack.FromLuaStack d, Foreign.Lua.Types.FromLuaStack.FromLuaStack e, Foreign.Lua.Types.FromLuaStack.FromLuaStack f, Foreign.Lua.Types.FromLuaStack.FromLuaStack g) => Foreign.Lua.Types.FromLuaStack.FromLuaStack (a, b, c, d, e, f, g)
instance (Foreign.Lua.Types.FromLuaStack.FromLuaStack a, Foreign.Lua.Types.FromLuaStack.FromLuaStack b, Foreign.Lua.Types.FromLuaStack.FromLuaStack c, Foreign.Lua.Types.FromLuaStack.FromLuaStack d, Foreign.Lua.Types.FromLuaStack.FromLuaStack e, Foreign.Lua.Types.FromLuaStack.FromLuaStack f, Foreign.Lua.Types.FromLuaStack.FromLuaStack g, Foreign.Lua.Types.FromLuaStack.FromLuaStack h) => Foreign.Lua.Types.FromLuaStack.FromLuaStack (a, b, c, d, e, f, g, h)


-- | Sending haskell objects to the lua stack.
module Foreign.Lua.Types.ToLuaStack

-- | A value that can be pushed to the Lua stack.
class ToLuaStack a

-- | Pushes a value onto Lua stack, casting it into meaningfully nearest
--   Lua type.
push :: ToLuaStack a => a -> Lua ()

-- | Push list as numerically indexed table.
pushList :: ToLuaStack a => [a] -> Lua ()
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack ()
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack Foreign.Lua.Api.Types.LuaInteger
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack Foreign.Lua.Api.Types.LuaNumber
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack Data.ByteString.Internal.ByteString
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack GHC.Types.Bool
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack Foreign.Lua.Api.Types.CFunction
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack (GHC.Ptr.Ptr a)
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack Data.Text.Internal.Text
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack Data.ByteString.Lazy.Internal.ByteString
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack [GHC.Types.Char]
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack a => Foreign.Lua.Types.ToLuaStack.ToLuaStack [a]
instance (Foreign.Lua.Types.ToLuaStack.ToLuaStack a, Foreign.Lua.Types.ToLuaStack.ToLuaStack b) => Foreign.Lua.Types.ToLuaStack.ToLuaStack (Data.Map.Internal.Map a b)
instance (Foreign.Lua.Types.ToLuaStack.ToLuaStack a, Foreign.Lua.Types.ToLuaStack.ToLuaStack b) => Foreign.Lua.Types.ToLuaStack.ToLuaStack (a, b)
instance (Foreign.Lua.Types.ToLuaStack.ToLuaStack a, Foreign.Lua.Types.ToLuaStack.ToLuaStack b, Foreign.Lua.Types.ToLuaStack.ToLuaStack c) => Foreign.Lua.Types.ToLuaStack.ToLuaStack (a, b, c)
instance (Foreign.Lua.Types.ToLuaStack.ToLuaStack a, Foreign.Lua.Types.ToLuaStack.ToLuaStack b, Foreign.Lua.Types.ToLuaStack.ToLuaStack c, Foreign.Lua.Types.ToLuaStack.ToLuaStack d) => Foreign.Lua.Types.ToLuaStack.ToLuaStack (a, b, c, d)
instance (Foreign.Lua.Types.ToLuaStack.ToLuaStack a, Foreign.Lua.Types.ToLuaStack.ToLuaStack b, Foreign.Lua.Types.ToLuaStack.ToLuaStack c, Foreign.Lua.Types.ToLuaStack.ToLuaStack d, Foreign.Lua.Types.ToLuaStack.ToLuaStack e) => Foreign.Lua.Types.ToLuaStack.ToLuaStack (a, b, c, d, e)
instance (Foreign.Lua.Types.ToLuaStack.ToLuaStack a, Foreign.Lua.Types.ToLuaStack.ToLuaStack b, Foreign.Lua.Types.ToLuaStack.ToLuaStack c, Foreign.Lua.Types.ToLuaStack.ToLuaStack d, Foreign.Lua.Types.ToLuaStack.ToLuaStack e, Foreign.Lua.Types.ToLuaStack.ToLuaStack f) => Foreign.Lua.Types.ToLuaStack.ToLuaStack (a, b, c, d, e, f)
instance (Foreign.Lua.Types.ToLuaStack.ToLuaStack a, Foreign.Lua.Types.ToLuaStack.ToLuaStack b, Foreign.Lua.Types.ToLuaStack.ToLuaStack c, Foreign.Lua.Types.ToLuaStack.ToLuaStack d, Foreign.Lua.Types.ToLuaStack.ToLuaStack e, Foreign.Lua.Types.ToLuaStack.ToLuaStack f, Foreign.Lua.Types.ToLuaStack.ToLuaStack g) => Foreign.Lua.Types.ToLuaStack.ToLuaStack (a, b, c, d, e, f, g)
instance (Foreign.Lua.Types.ToLuaStack.ToLuaStack a, Foreign.Lua.Types.ToLuaStack.ToLuaStack b, Foreign.Lua.Types.ToLuaStack.ToLuaStack c, Foreign.Lua.Types.ToLuaStack.ToLuaStack d, Foreign.Lua.Types.ToLuaStack.ToLuaStack e, Foreign.Lua.Types.ToLuaStack.ToLuaStack f, Foreign.Lua.Types.ToLuaStack.ToLuaStack g, Foreign.Lua.Types.ToLuaStack.ToLuaStack h) => Foreign.Lua.Types.ToLuaStack.ToLuaStack (a, b, c, d, e, f, g, h)


-- | Types for working with Lua.
module Foreign.Lua.Types


-- | HsLua utility functions.
module Foreign.Lua.Util

-- | Like <tt>getglobal</tt>, but knows about packages and nested tables.
--   E.g.
--   
--   <pre>
--   getglobal' "math.sin"
--   </pre>
--   
--   will return the function <tt>sin</tt> in package <tt>math</tt>.
getglobal' :: String -> Lua ()

-- | Like <tt>setglobal</tt>, but knows about packages and nested tables.
--   E.g.
--   
--   <pre>
--   pushstring "0.9.4"
--   setglobal' "mypackage.version"
--   </pre>
--   
--   All tables and fields, except for the last field, must exist.
setglobal' :: String -> Lua ()

-- | Run lua computation using the default HsLua state as starting point.
--   Raised exceptions are passed through; error handling is the
--   responsibility of the caller.
runLua :: Lua a -> IO a

-- | Run the given Lua computation; exceptions raised in haskell code are
--   caught, but other exceptions (user exceptions raised in haskell,
--   unchecked type errors, etc.) are passed through.
runLuaEither :: Lua a -> IO (Either LuaException a)

-- | Raise a Lua error, using the given value as the error object. This
--   must be the return value of a function which has been wrapped with
--   <tt><a>wrapHaskellFunction</a></tt>.
raiseError :: ToLuaStack a => a -> Lua NumResults

-- | Like <tt><a>Optional</a></tt>, but deprecated. Will be removed in the
--   next major release.
newtype OrNil a
OrNil :: Maybe a -> OrNil a
[toMaybe] :: OrNil a -> Maybe a

-- | Newtype wrapper intended to be used for optional Lua values. Nesting
--   this type is strongly discouraged as missing values on inner levels
--   are indistinguishable from missing values on an outer level; wrong
--   values would be the likely result.
newtype Optional a
Optional :: Maybe a -> Optional a
[fromOptional] :: Optional a -> Maybe a
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack a => Foreign.Lua.Types.FromLuaStack.FromLuaStack (Foreign.Lua.Util.OrNil a)
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack a => Foreign.Lua.Types.ToLuaStack.ToLuaStack (Foreign.Lua.Util.OrNil a)
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack a => Foreign.Lua.Types.FromLuaStack.FromLuaStack (Foreign.Lua.Util.Optional a)
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack a => Foreign.Lua.Types.ToLuaStack.ToLuaStack (Foreign.Lua.Util.Optional a)


-- | Call haskell functions from Lua, and vice versa.
module Foreign.Lua.FunctionCalling

-- | A value that can be read from the Lua stack.
class FromLuaStack a

-- | Check if at index <tt>n</tt> there is a convertible Lua value and if
--   so return it. Throws a <tt><a>LuaException</a></tt> otherwise.
peek :: FromLuaStack a => StackIndex -> Lua a

-- | Helper class used to make lua functions useable from haskell
class LuaCallFunc a
callFunc' :: LuaCallFunc a => String -> Lua () -> NumArgs -> a

-- | Operations and functions that can be pushed to the lua stack. This is
--   a helper function not intended to be used directly. Use the
--   <tt><a>toHaskellFunction</a></tt> wrapper instead.
class ToHaskellFunction a

-- | Helper function, called by <tt><a>toHaskellFunction</a></tt>
toHsFun :: ToHaskellFunction a => StackIndex -> a -> Lua NumResults

-- | Haskell function that can be called from Lua.
type HaskellFunction = Lua NumResults

-- | A value that can be pushed to the Lua stack.
class ToLuaStack a

-- | Pushes a value onto Lua stack, casting it into meaningfully nearest
--   Lua type.
push :: ToLuaStack a => a -> Lua ()

-- | Type of raw haskell functions that can be made into <a>CFunction</a>s.
type PreCFunction = LuaState -> IO NumResults

-- | Convert a Haskell function to Lua function. Any Haskell function can
--   be converted provided that:
--   
--   <ul>
--   <li>all arguments are instances of <tt><a>FromLuaStack</a></tt></li>
--   <li>return type is <tt>Lua a</tt>, where <tt>a</tt> is an instance of
--   <tt><a>ToLuaStack</a></tt></li>
--   </ul>
--   
--   Any Haskell exception will be converted to a string and returned as
--   Lua error.
toHaskellFunction :: ToHaskellFunction a => a -> HaskellFunction

-- | Call a Lua function. Use as:
--   
--   <pre>
--   v &lt;- callfunc "proc" "abc" (1::Int) (5.0::Double)
--   </pre>
callFunc :: (LuaCallFunc a) => String -> a

-- | Free function pointer created with <tt>newcfunction</tt>.
freeCFunction :: CFunction -> Lua ()

-- | Create new foreign Lua function. Function created can be called by Lua
--   engine. Remeber to free the pointer with <tt>freecfunction</tt>.
newCFunction :: ToHaskellFunction a => a -> Lua CFunction

-- | Pushes Haskell function as a callable userdata. All values created
--   will be garbage collected. Use as:
--   
--   <pre>
--   pushHaskellFunction myfun
--   setglobal "myfun"
--   </pre>
--   
--   You are not allowed to use <tt>lua_error</tt> anywhere, but use an
--   error code of (-1) to the same effect. Push error message as the sole
--   return value.
pushHaskellFunction :: ToHaskellFunction a => a -> Lua ()

-- | Imports a Haskell function and registers it at global name.
registerHaskellFunction :: ToHaskellFunction a => String -> a -> Lua ()
instance Foreign.Lua.Types.FromLuaStack.FromLuaStack a => Foreign.Lua.FunctionCalling.LuaCallFunc (Foreign.Lua.Types.Lua.Lua a)
instance (Foreign.Lua.Types.ToLuaStack.ToLuaStack a, Foreign.Lua.FunctionCalling.LuaCallFunc b) => Foreign.Lua.FunctionCalling.LuaCallFunc (a -> b)
instance Foreign.Lua.FunctionCalling.ToHaskellFunction Foreign.Lua.FunctionCalling.HaskellFunction
instance Foreign.Lua.Types.ToLuaStack.ToLuaStack a => Foreign.Lua.FunctionCalling.ToHaskellFunction (Foreign.Lua.Types.Lua.Lua a)
instance (Foreign.Lua.Types.FromLuaStack.FromLuaStack a, Foreign.Lua.FunctionCalling.ToHaskellFunction b) => Foreign.Lua.FunctionCalling.ToHaskellFunction (a -> b)


-- | Bindings, functions, and utilities enabling the integration of a lua
--   interpreter into a haskell project.
module Foreign.Lua

-- | Lua computation
newtype Lua a
Lua :: ReaderT LuaState IO a -> Lua a
[unLua] :: Lua a -> ReaderT LuaState IO a

-- | Get the lua state of this lua computation.
luaState :: Lua LuaState

-- | Run lua computation with custom lua state. Errors are left unhandled,
--   the caller of this function is responsible to catch lua errors.
runLuaWith :: LuaState -> Lua a -> IO a

-- | Lift a computation from the <a>IO</a> monad.
liftIO :: MonadIO m => forall a. () => IO a -> m a

-- | A value that can be read from the Lua stack.
class FromLuaStack a

-- | Check if at index <tt>n</tt> there is a convertible Lua value and if
--   so return it. Throws a <tt><a>LuaException</a></tt> otherwise.
peek :: FromLuaStack a => StackIndex -> Lua a

-- | Try to convert the value at the given stack index to a haskell value.
--   Returns <tt>Left</tt> with an error message on failure.
peekEither :: FromLuaStack a => StackIndex -> Lua (Either String a)

-- | Read a table into a list
toList :: FromLuaStack a => StackIndex -> Lua [a]

-- | Read a table into a list of pairs.
pairsFromTable :: (FromLuaStack a, FromLuaStack b) => StackIndex -> Lua [(a, b)]

-- | A value that can be pushed to the Lua stack.
class ToLuaStack a

-- | Pushes a value onto Lua stack, casting it into meaningfully nearest
--   Lua type.
push :: ToLuaStack a => a -> Lua ()

-- | Push list as numerically indexed table.
pushList :: ToLuaStack a => [a] -> Lua ()

-- | Type of raw haskell functions that can be made into <a>CFunction</a>s.
type PreCFunction = LuaState -> IO NumResults

-- | Haskell function that can be called from Lua.
type HaskellFunction = Lua NumResults

-- | Operations and functions that can be pushed to the lua stack. This is
--   a helper function not intended to be used directly. Use the
--   <tt><a>toHaskellFunction</a></tt> wrapper instead.
class ToHaskellFunction a

-- | Helper function, called by <tt><a>toHaskellFunction</a></tt>
toHsFun :: ToHaskellFunction a => StackIndex -> a -> Lua NumResults

-- | Convert a Haskell function to Lua function. Any Haskell function can
--   be converted provided that:
--   
--   <ul>
--   <li>all arguments are instances of <tt><a>FromLuaStack</a></tt></li>
--   <li>return type is <tt>Lua a</tt>, where <tt>a</tt> is an instance of
--   <tt><a>ToLuaStack</a></tt></li>
--   </ul>
--   
--   Any Haskell exception will be converted to a string and returned as
--   Lua error.
toHaskellFunction :: ToHaskellFunction a => a -> HaskellFunction

-- | Call a Lua function. Use as:
--   
--   <pre>
--   v &lt;- callfunc "proc" "abc" (1::Int) (5.0::Double)
--   </pre>
callFunc :: (LuaCallFunc a) => String -> a

-- | Create new foreign Lua function. Function created can be called by Lua
--   engine. Remeber to free the pointer with <tt>freecfunction</tt>.
newCFunction :: ToHaskellFunction a => a -> Lua CFunction

-- | Free function pointer created with <tt>newcfunction</tt>.
freeCFunction :: CFunction -> Lua ()

-- | Pushes Haskell function as a callable userdata. All values created
--   will be garbage collected. Use as:
--   
--   <pre>
--   pushHaskellFunction myfun
--   setglobal "myfun"
--   </pre>
--   
--   You are not allowed to use <tt>lua_error</tt> anywhere, but use an
--   error code of (-1) to the same effect. Push error message as the sole
--   return value.
pushHaskellFunction :: ToHaskellFunction a => a -> Lua ()

-- | Imports a Haskell function and registers it at global name.
registerHaskellFunction :: ToHaskellFunction a => String -> a -> Lua ()

-- | Run lua computation using the default HsLua state as starting point.
--   Raised exceptions are passed through; error handling is the
--   responsibility of the caller.
runLua :: Lua a -> IO a

-- | Run the given Lua computation; exceptions raised in haskell code are
--   caught, but other exceptions (user exceptions raised in haskell,
--   unchecked type errors, etc.) are passed through.
runLuaEither :: Lua a -> IO (Either LuaException a)

-- | Like <tt>getglobal</tt>, but knows about packages and nested tables.
--   E.g.
--   
--   <pre>
--   getglobal' "math.sin"
--   </pre>
--   
--   will return the function <tt>sin</tt> in package <tt>math</tt>.
getglobal' :: String -> Lua ()

-- | Like <tt>setglobal</tt>, but knows about packages and nested tables.
--   E.g.
--   
--   <pre>
--   pushstring "0.9.4"
--   setglobal' "mypackage.version"
--   </pre>
--   
--   All tables and fields, except for the last field, must exist.
setglobal' :: String -> Lua ()

-- | Raise a Lua error, using the given value as the error object. This
--   must be the return value of a function which has been wrapped with
--   <tt><a>wrapHaskellFunction</a></tt>.
raiseError :: ToLuaStack a => a -> Lua NumResults

-- | Newtype wrapper intended to be used for optional Lua values. Nesting
--   this type is strongly discouraged as missing values on inner levels
--   are indistinguishable from missing values on an outer level; wrong
--   values would be the likely result.
newtype Optional a
Optional :: Maybe a -> Optional a
[fromOptional] :: Optional a -> Maybe a

-- | Like <tt><a>Optional</a></tt>, but deprecated. Will be removed in the
--   next major release.
newtype OrNil a
OrNil :: Maybe a -> OrNil a
[toMaybe] :: OrNil a -> Maybe a

-- | Exceptions raised by Lua-related operations.
data LuaException
LuaException :: String -> LuaException

-- | Catch a <tt><a>LuaException</a></tt>.
catchLuaError :: Lua a -> (LuaException -> Lua a) -> Lua a

-- | Raise a <tt><a>LuaException</a></tt> containing the given error
--   message.
throwLuaError :: String -> Lua a

-- | Return either the result of a Lua computation or, if an exception was
--   thrown, the error.
tryLua :: Lua a -> Lua (Either LuaException a)

-- | Catch <tt><a>LuaException</a></tt>, alter the error message and
--   rethrow.
modifyLuaError :: Lua a -> (String -> String) -> Lua a
