-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Bindings to Lua, an embeddable scripting language -- -- Wrappers and helpers to bridge Haskell and Lua. -- -- It builds upon the lua package, which allows to bundle a Lua -- interpreter with a Haskell program. @package hslua-core @version 2.0.0 -- | Encoding and decoding of String to and from UTF8. module HsLua.Core.Utf8 -- | Decode ByteString to String using -- UTF-8. Invalid input bytes are replaced with the Unicode replacement -- character U+FFFD. toString :: ByteString -> String -- | Decode ByteString to Text using UTF-8. -- Invalid input bytes are replaced with the Unicode replacement -- character U+FFFD. toText :: ByteString -> Text -- | Encode String to ByteString using -- UTF-8. fromString :: String -> ByteString -- | Encode Text to ByteString using UTF-8. fromText :: Text -> ByteString -- | The core Lua types, including mappings of Lua types to Haskell. -- -- This module has mostly been moved to Types and -- currently re-exports that module. This module might be removed in the -- future. module HsLua.Core.Types -- | A Lua computation. This is the base type used to run Lua programs of -- any kind. The Lua state is handled automatically, but can be retrieved -- via state. newtype LuaE e a Lua :: ReaderT LuaEnvironment IO a -> LuaE e a [unLua] :: LuaE e a -> ReaderT LuaEnvironment IO a -- | Environment in which Lua computations are evaluated. newtype LuaEnvironment LuaEnvironment :: State -> LuaEnvironment -- | Lua interpreter state [luaEnvState] :: LuaEnvironment -> State -- | 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 lua_State *. See lua_State. newtype State State :: Ptr () -> State -- | The reader function used by load. 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 NULL or -- set size to zero. The reader function may return pieces of any size -- greater than zero. -- -- See lua_Reader. type Reader = FunPtr State -> Ptr () -> Ptr CSize -> IO Ptr CChar -- | Turn a function of typ Lua.State -> IO a into a monadic -- Lua operation. liftLua :: (State -> IO a) -> LuaE e a -- | Turn a function of typ Lua.State -> a -> IO b into a -- monadic Lua operation. liftLua1 :: (State -> a -> IO b) -> a -> LuaE e b -- | Get the Lua state of this Lua computation. state :: LuaE e State -- | Run Lua computation with the given Lua state. Exception handling is -- left to the caller; resulting exceptions are left unhandled. runWith :: State -> LuaE e a -> IO a -- | Run the given operation, but crash if any Haskell exceptions occur. unsafeRunWith :: State -> LuaE e a -> IO a -- | Commands to control the garbage collector. data GCControl -- | stops the garbage collector. GCStop :: GCControl -- | restarts the garbage collector GCRestart :: GCControl -- | performs a full garbage-collection cycle. GCCollect :: GCControl -- | returns the current amount of memory (in Kbytes) in use by Lua. GCCount :: GCControl -- | returns the remainder of dividing the current amount of bytes of -- memory in use by Lua by 1024. GCCountb :: GCControl -- | performs an incremental step of garbage collection. GCStep :: GCControl -- | sets data as the new value for the pause of the collector (see -- <https://www.lua.org/manual/5.3/manual.html#2.5 §2.5> of -- the Lua reference manual) and returns the previous value of the pause. GCSetPause :: CInt -> GCControl -- | sets data as the new value for the step multiplier of the collector -- (see <https://www.lua.org/manual/5.3/manual.html#2.5 -- §2.5> of the Lua reference manual) and returns the previous value -- of the step multiplier. GCSetStepMul :: CInt -> GCControl -- | returns a boolean that tells whether the collector is running (i.e., -- not stopped). GCIsRunning :: GCControl -- | Converts a GCControl command to its corresponding code. toGCcode :: GCControl -> GCCode -- | Returns the data value associated with a GCControl command. toGCdata :: GCControl -> CInt -- | Enumeration used as type tag. See lua_type. data Type -- | non-valid stack index TypeNone :: Type -- | type of Lua's nil value TypeNil :: Type -- | type of Lua booleans TypeBoolean :: Type -- | type of light userdata TypeLightUserdata :: Type -- | type of Lua numbers. See Number TypeNumber :: Type -- | type of Lua string values TypeString :: Type -- | type of Lua tables TypeTable :: Type -- | type of functions, either normal or CFunction TypeFunction :: Type -- | type of full user data TypeUserdata :: Type -- | type of Lua threads TypeThread :: Type -- | 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 -- | Lift a computation from the IO monad. liftIO :: MonadIO m => IO a -> m a -- | 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, lua_gettop 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 lua_gettop. -- 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 lua_CFunction. type CFunction = FunPtr PreCFunction -- | Type of Haskell functions that can be turned into C functions. -- -- This is the same as a dereferenced CFunction. type PreCFunction = State -> IO NumResults -- | Haskell function that can be called from Lua. The HsLua equivallent of -- a PreCFunction. type HaskellFunction e = LuaE e NumResults -- | Boolean value returned by a Lua C API function. This is a -- CInt and should be interpreted as -- False iff the value is 0, -- True otherwise. newtype LuaBool LuaBool :: CInt -> LuaBool -- | Convert a LuaBool to a Haskell Bool. fromLuaBool :: LuaBool -> Bool -- | Convert a Haskell Bool to a LuaBool. toLuaBool :: Bool -> LuaBool -- | The type of integers in Lua. -- -- By default this type is Int64, but that can be changed -- to different values in lua. (See LUA_INT_TYPE in -- luaconf.h.) -- -- See lua_Integer. newtype Integer Integer :: Int64 -> Integer -- | The type of floats in Lua. -- -- By default this type is Double, but that can be -- changed in Lua to a single float or a long double. (See -- LUA_FLOAT_TYPE in luaconf.h.) -- -- See lua_Number. newtype Number Number :: Double -> Number -- | A stack index newtype StackIndex StackIndex :: CInt -> StackIndex [fromStackIndex] :: StackIndex -> CInt -- | Pseudo stack index of the Lua registry. 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 pcall. multret :: NumResults -- | Lua comparison operations. data RelationalOperator -- | Correponds to Lua's equality (==) operator. EQ :: RelationalOperator -- | Correponds to Lua's strictly-lesser-than (<) operator LT :: RelationalOperator -- | Correponds to Lua's lesser-or-equal (<=) operator LE :: RelationalOperator -- | Convert relation operator to its C representation. fromRelationalOperator :: RelationalOperator -> OPCode -- | 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 __gc metamethod. ErrGcmm :: Status -- | opening or reading a file failed. ErrFile :: Status -- | Convert C integer constant to Status. toStatus :: StatusCode -> Status -- | 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 -- | Value signaling that no reference was found. noref :: Int -- | Value signaling that no reference was created. refnil :: Int -- | 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 nthTop. nth :: CInt -> StackIndex -- | Index of the topmost stack element. top :: StackIndex -- | Name of a function, table field, or chunk; the name must be valid -- UTF-8 and may not contain any nul characters. -- -- Implementation note: this is a newtype instead of a simple -- type Name = ByteString alias so we can define a UTF-8 based -- IsString instance. Non-ASCII users would have a bad time -- otherwise. newtype Name Name :: ByteString -> Name [fromName] :: Name -> ByteString instance Control.Monad.Catch.MonadThrow (HsLua.Core.Types.LuaE e) instance Control.Monad.Reader.Class.MonadReader HsLua.Core.Types.LuaEnvironment (HsLua.Core.Types.LuaE e) instance Control.Monad.Catch.MonadMask (HsLua.Core.Types.LuaE e) instance Control.Monad.IO.Class.MonadIO (HsLua.Core.Types.LuaE e) instance Control.Monad.Catch.MonadCatch (HsLua.Core.Types.LuaE e) instance GHC.Base.Monad (HsLua.Core.Types.LuaE e) instance GHC.Base.Functor (HsLua.Core.Types.LuaE e) instance GHC.Base.Applicative (HsLua.Core.Types.LuaE e) instance GHC.Show.Show HsLua.Core.Types.Type instance GHC.Classes.Ord HsLua.Core.Types.Type instance GHC.Classes.Eq HsLua.Core.Types.Type instance GHC.Enum.Bounded HsLua.Core.Types.Type instance GHC.Show.Show HsLua.Core.Types.Status instance GHC.Classes.Eq HsLua.Core.Types.Status instance GHC.Show.Show HsLua.Core.Types.RelationalOperator instance GHC.Classes.Ord HsLua.Core.Types.RelationalOperator instance GHC.Classes.Eq HsLua.Core.Types.RelationalOperator instance GHC.Show.Show HsLua.Core.Types.GCControl instance GHC.Classes.Ord HsLua.Core.Types.GCControl instance GHC.Classes.Eq HsLua.Core.Types.GCControl instance GHC.Show.Show HsLua.Core.Types.Name instance GHC.Base.Semigroup HsLua.Core.Types.Name instance GHC.Classes.Ord HsLua.Core.Types.Name instance GHC.Classes.Eq HsLua.Core.Types.Name instance Data.String.IsString HsLua.Core.Types.Name instance GHC.Enum.Enum HsLua.Core.Types.Type -- | Convenience functions to convert Haskell values into Lua userdata. module HsLua.Core.Userdata -- | Creates a new userdata wrapping the given Haskell object. The userdata -- is pushed to the top of the stack. newhsuserdata :: forall a e. a -> LuaE e () -- | 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. -- -- Using a metatable created by this functions ensures that the pointer -- to the Haskell value will be freed when the userdata object is garbage -- collected in Lua. -- -- The name may not contain a nul character. newudmetatable :: Name -> LuaE e Bool -- | Retrieves a Haskell object from userdata at the given index. The -- userdata must have the given name. fromuserdata :: forall a e. StackIndex -> Name -> LuaE e (Maybe a) -- | Replaces the Haskell value contained in the userdata value at -- index. Checks that the userdata is of type name and -- returns True on success, or False otherwise. putuserdata :: forall a e. StackIndex -> Name -> a -> LuaE e Bool -- | Unsafe Lua functions. -- -- This module exports functions which conflict with those in -- Core. It is intended to be imported qualified. module HsLua.Core.Unsafe -- | Wrapper for lua_next. -- -- WARNING: lua_next is unsafe in Haskell: This function -- will cause an unrecoverable crash an error if the given key is neither -- nil nor present in the table. Consider using the safe -- next function in HsLua.Core instead. next :: StackIndex -> LuaE e Bool -- | Lua exceptions and exception handling. module HsLua.Core.Error -- | Default Lua error type. Exceptions raised by Lua-related operations. newtype Exception Exception :: String -> Exception [exceptionMessage] :: Exception -> String -- | Any type that you wish to use for error handling in HsLua must be an -- instance of the LuaError class. class Exception e => LuaError e -- | Converts the error at the top of the stack into an exception and pops -- the error off the stack. -- -- This function is expected to produce a valid result for any Lua value; -- neither a Haskell exception nor a Lua error may result when this is -- called. popException :: LuaError e => LuaE e e -- | Pushes an exception to the top of the Lua stack. The pushed Lua object -- is used as an error object, and it is recommended that calling -- tostring() on the object produces an informative message. pushException :: LuaError e => e -> LuaE e () -- | Creates a new exception with the given message. luaException :: LuaError e => String -> e -- | A Lua operation. -- -- This type is suitable for most users. It uses a default exception for -- error handling. Users who need more control over error handling can -- use LuaE with a custom error type instead. type Lua a = LuaE Exception a -- | Return either the result of a Lua computation or, if an exception was -- thrown, the error. try :: Exception e => LuaE e a -> LuaE e (Either e a) -- | Raises an exception in the Lua monad. failLua :: forall e a. LuaError e => String -> LuaE e a -- | Converts a Lua error at the top of the stack into a Haskell exception -- and throws it. throwErrorAsException :: LuaError e => LuaE e a -- | Raises an exception that's appropriate when the type of a Lua object -- at the given index did not match the expected type. The name or -- description of the expected type is taken as an argument. throwTypeMismatchError :: forall e a. LuaError e => ByteString -> StackIndex -> LuaE e a -- | Change the error type of a computation. changeErrorType :: forall old new a. LuaE old a -> LuaE new a -- | Takes a failable HsLua function and transforms it into a monadic -- Lua operation. Throws an exception if an error occured. liftLuaThrow :: forall e a. LuaError e => (State -> Ptr StatusCode -> IO a) -> LuaE e a -- | Retrieve and pop the top object as an error message. This is very -- similar to tostring', but ensures that we don't recurse if getting the -- message failed. -- -- This helpful as a "last resort" method when implementing -- peekException. popErrorMessage :: State -> IO ByteString -- | Creates an error to notify about a Lua type mismatch and pushes it to -- the stack. pushTypeMismatchError :: ByteString -> StackIndex -> LuaE e () instance GHC.Classes.Eq HsLua.Core.Error.Exception instance GHC.Show.Show HsLua.Core.Error.Exception instance GHC.Exception.Type.Exception HsLua.Core.Error.Exception instance HsLua.Core.Error.LuaError HsLua.Core.Error.Exception instance HsLua.Core.Error.LuaError e => GHC.Base.Alternative (HsLua.Core.Types.LuaE e) instance HsLua.Core.Error.LuaError e => Control.Monad.Fail.MonadFail (HsLua.Core.Types.LuaE e) -- | Expose Haskell functions as Lua closures. module HsLua.Core.Closures -- | Converts a pre C function to a Lua function and pushes it to the -- stack. -- -- Pre C functions collect parameters from the stack and return a -- CInt that represents number of return values left on the -- stack. See CFunction for more info. pushPreCFunction :: PreCFunction -> LuaE e () -- | Pushes Haskell function as a callable userdata. All values created -- will be garbage collected. The function should behave similar to a -- CFunction. -- -- Error conditions should be indicated by raising a catchable exception -- or by returning the result of error. -- -- Example: -- --
-- mod23 :: Lua NumResults -- mod23 = do -- mn <- tointeger (nthBottom 1) -- case mn of -- Nothing -> pushstring "expected an integer" *> error -- Just n -> pushinteger (n `mod` 23) -- pushHaskellFunction mod23 -- setglobal "mod23" --pushHaskellFunction :: LuaError e => HaskellFunction e -> LuaE e () -- | Helper functions to run Lua computations. module HsLua.Core.Run -- | Run Lua computation using the default HsLua state as starting point. -- Exceptions are masked, thus avoiding some issues when using multiple -- threads. All exceptions are passed through; error handling is the -- responsibility of the caller. run :: LuaE e 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. runEither :: Exception e => LuaE e a -> IO (Either e a) -- | Run Lua computation with the given Lua state. Exception handling is -- left to the caller; resulting exceptions are left unhandled. runWith :: State -> LuaE e a -> IO a -- | Utility functions for HsLua modules. module HsLua.Core.Package -- | Load a module, defined by a Haskell action, under the given name. -- -- Similar to luaL_required: After checking "loaded" table, -- calls pushMod to push a module to the stack, and registers -- the result in package.loaded table. -- -- The pushMod function must push exactly one element to the top -- of the stack. This is not checked, but failure to do so will lead to -- problems. Lua's package module must have been loaded by the -- time this function is invoked. -- -- Leaves a copy of the module on the stack. requirehs :: LuaError e => Name -> LuaE e () -> LuaE e () -- | Registers a preloading function. Takes an module name and the Lua -- operation which produces the package. preloadhs :: LuaError e => Name -> LuaE e NumResults -> LuaE e () -- | Core Lua API. This module provides thin wrappers around the respective -- functions of the Lua C API. C functions which can throw an error are -- wrapped such that the error is converted into an -- Exception. However, memory allocation errors are not -- caught and will cause the host program to terminate. module HsLua.Core -- | Run Lua computation using the default HsLua state as starting point. -- Exceptions are masked, thus avoiding some issues when using multiple -- threads. All exceptions are passed through; error handling is the -- responsibility of the caller. run :: LuaE e a -> IO a -- | Run Lua computation with the given Lua state. Exception handling is -- left to the caller; resulting exceptions are left unhandled. runWith :: State -> LuaE e 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. runEither :: Exception e => LuaE e a -> IO (Either e a) -- | A Lua computation. This is the base type used to run Lua programs of -- any kind. The Lua state is handled automatically, but can be retrieved -- via state. newtype LuaE e a Lua :: ReaderT LuaEnvironment IO a -> LuaE e a [unLua] :: LuaE e a -> ReaderT LuaEnvironment IO a -- | A Lua operation. -- -- This type is suitable for most users. It uses a default exception for -- error handling. Users who need more control over error handling can -- use LuaE with a custom error type instead. type Lua a = LuaE Exception a -- | Run the given operation, but crash if any Haskell exceptions occur. unsafeRunWith :: State -> LuaE e a -> IO a -- | Lift a computation from the IO monad. liftIO :: MonadIO m => IO a -> m a -- | Get the Lua state of this Lua computation. state :: LuaE e State -- | Environment in which Lua computations are evaluated. newtype LuaEnvironment LuaEnvironment :: State -> LuaEnvironment -- | Lua interpreter state [luaEnvState] :: LuaEnvironment -> State -- | 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, lua_gettop 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 lua_gettop. -- 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 lua_CFunction. type CFunction = FunPtr PreCFunction -- | Type of Haskell functions that can be turned into C functions. -- -- This is the same as a dereferenced CFunction. type PreCFunction = State -> IO NumResults -- | The type of integers in Lua. -- -- By default this type is Int64, but that can be changed -- to different values in lua. (See LUA_INT_TYPE in -- luaconf.h.) -- -- See lua_Integer. newtype Integer Integer :: Int64 -> Integer -- | The type of floats in Lua. -- -- By default this type is Double, but that can be -- changed in Lua to a single float or a long double. (See -- LUA_FLOAT_TYPE in luaconf.h.) -- -- See lua_Number. newtype Number Number :: Double -> Number -- | A stack index newtype StackIndex StackIndex :: CInt -> StackIndex [fromStackIndex] :: StackIndex -> CInt -- | 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 nthTop. nth :: CInt -> StackIndex -- | Index of the topmost stack element. top :: 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 -- | Name of a function, table field, or chunk; the name must be valid -- UTF-8 and may not contain any nul characters. -- -- Implementation note: this is a newtype instead of a simple -- type Name = ByteString alias so we can define a UTF-8 based -- IsString instance. Non-ASCII users would have a bad time -- otherwise. newtype Name Name :: ByteString -> Name [fromName] :: Name -> ByteString -- | Option for multiple returns in pcall. multret :: NumResults -- | Pseudo stack index of the Lua registry. registryindex :: StackIndex -- | Returns the pseudo-index that represents the i-th upvalue of -- the running function (see -- <https://www.lua.org/manual/5.3/manual.html#4.4 §4.4> of -- the Lua 5.3 reference manual). -- -- See also: lua_upvalueindex. 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 lua_State *. See lua_State. newtype State State :: Ptr () -> State -- | Creates a new Lua state. It calls lua_newstate with an -- allocator based on the standard C realloc function and then -- sets a panic function (see §4.6 of the Lua 5.3 Reference -- Manual) that prints an error message to the standard error output in -- case of fatal errors. -- -- Wraps hsluaL_newstate. See also: luaL_newstate. newstate :: IO State -- | 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. -- -- Same as lua_close. close :: State -> IO () -- | Converts the acceptable index idx into an equivalent absolute -- index (that is, one that does not depend on the stack top). -- -- Wraps lua_absindex. absindex :: StackIndex -> LuaE e 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). -- -- Wraps lua_gettop. gettop :: LuaE e 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. -- -- Wraps lua_settop. settop :: StackIndex -> LuaE e () -- | Pushes a copy of the element at the given index onto the stack. -- -- Wraps lua_pushvalue. pushvalue :: StackIndex -> LuaE e () -- | Copies the element at index fromidx into the valid index -- toidx, replacing the value at that position. Values at other -- positions are not affected. -- -- Wraps lua_copy. copy :: StackIndex -> StackIndex -> LuaE e () -- | 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. -- -- Wraps lua_insert. insert :: StackIndex -> LuaE e () -- | Pops n elements from the stack. -- -- See also: lua_pop. pop :: Int -> LuaE e () -- | 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. -- -- Wraps lua_remove. remove :: StackIndex -> LuaE e () -- | 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. -- -- Wraps lua_replace. replace :: StackIndex -> LuaE e () -- | Ensures that the stack has space for at least n extra slots -- (that is, that you can safely push up to n 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. -- -- Wraps lua_checkstack. checkstack :: Int -> LuaE e Bool -- | Enumeration used as type tag. See lua_type. data Type -- | non-valid stack index TypeNone :: Type -- | type of Lua's nil value TypeNil :: Type -- | type of Lua booleans TypeBoolean :: Type -- | type of light userdata TypeLightUserdata :: Type -- | type of Lua numbers. See Number TypeNumber :: Type -- | type of Lua string values TypeString :: Type -- | type of Lua tables TypeTable :: Type -- | type of functions, either normal or CFunction TypeFunction :: Type -- | type of full user data TypeUserdata :: Type -- | type of Lua threads TypeThread :: Type -- | Returns the type of the value in the given valid index, or -- TypeNone for a non-valid (but acceptable) index. -- -- This function wraps lua_type. ltype :: StackIndex -> LuaE e Type -- | Returns the name of the type encoded by the value tp, which -- must be one the values returned by ltype. -- -- Wraps lua_typename. typename :: Type -> LuaE e ByteString -- | Returns True if the value at the given index is a boolean, and -- False otherwise. -- -- Wraps lua_isboolean. isboolean :: StackIndex -> LuaE e Bool -- | Returns True if the value at the given index is a C function, -- and False otherwise. -- -- Wraps lua_iscfunction. iscfunction :: StackIndex -> LuaE e Bool -- | Returns True if the value at the given index is a function -- (either C or Lua), and False otherwise. -- -- Wraps lua_isfunction. isfunction :: StackIndex -> LuaE e Bool -- | Returns True if the value at the given index is an integer -- (that is, the value is a number and is represented as an integer), and -- False otherwise. -- -- Wraps lua_isinteger. isinteger :: StackIndex -> LuaE e Bool -- | Returns True if the value at the given index is a light -- userdata, and False otherwise. -- -- Wraps lua_islightuserdata. islightuserdata :: StackIndex -> LuaE e Bool -- | Returns True if the value at the given index is *nil*, and -- False otherwise. -- -- Wraps lua_isnil. isnil :: StackIndex -> LuaE e Bool -- | Returns True if the given index is not valid, and False -- otherwise. -- -- Wraps lua_isnone. isnone :: StackIndex -> LuaE e Bool -- | Returns True if the given index is not valid or if the value at -- the given index is *nil*, and False otherwise. -- -- Wraps lua_isnoneornil. isnoneornil :: StackIndex -> LuaE e Bool -- | Returns True if the value at the given index is a number or a -- string convertible to a number, and False otherwise. -- -- Wraps lua_isnumber. isnumber :: StackIndex -> LuaE e Bool -- | Returns True if the value at the given index is a string or a -- number (which is always convertible to a string), and False -- otherwise. -- -- Wraps lua_isstring. isstring :: StackIndex -> LuaE e Bool -- | Returns True if the value at the given index is a table, and -- False otherwise. -- -- Wraps lua_istable. istable :: StackIndex -> LuaE e Bool -- | Returns True if the value at the given index is a thread, and -- False otherwise. -- -- Wraps lua_isthread. isthread :: StackIndex -> LuaE e Bool -- | Returns True if the value at the given index is a userdata -- (either full or light), and False otherwise. -- -- Wraps lua_isuserdata. isuserdata :: StackIndex -> LuaE e Bool -- | Converts the Lua value at the given index to a haskell boolean value. -- Like all tests in Lua, toboolean returns True for -- any Lua value different from false and nil; -- otherwise it returns False. (If you want to accept only -- actual boolean values, use isboolean to test the -- value's type.) -- -- Wraps lua_toboolean. toboolean :: StackIndex -> LuaE e Bool -- | Converts a value at the given index to a C function. That value must -- be a C function; otherwise, returns Nothing. -- -- Wraps lua_tocfunction. tocfunction :: StackIndex -> LuaE e (Maybe CFunction) -- | Converts the Lua value at the given acceptable index to the signed -- integral type Integer. The Lua value must be an integer, a -- number or a string convertible to an integer (see §3.4.3 of the -- Lua 5.3 Reference Manual); otherwise, tointeger returns -- Nothing. -- -- If the number is not an integer, it is truncated in some non-specified -- way. -- -- Wraps lua_tointegerx. See also: lua_tointeger. tointeger :: StackIndex -> LuaE e (Maybe Integer) -- | Converts the Lua value at the given index to a Number. The Lua -- value must be a number or a string convertible to a number; otherwise, -- tonumber returns Nothing. -- -- Wraps lua_tonumberx. See also lua_tonumber. tonumber :: StackIndex -> LuaE e (Maybe Number) -- | 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 nullPtr. 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. -- -- Wraps lua_topointer. topointer :: StackIndex -> LuaE e (Ptr ()) -- | Converts the Lua value at the given index to a ByteString. The -- Lua value must be a string or a number; otherwise, the function -- returns Nothing. If the value is a number, then tostring -- also changes the actual value in the stack to a string. (This change -- confuses next when tostring is applied to keys during a -- table traversal.) -- -- Wraps lua_tolstring. tostring :: StackIndex -> LuaE e (Maybe ByteString) -- | Converts the value at the given index to a Lua thread (represented as -- State). This value must be a thread; otherwise, the function -- returns Nothing. -- -- Wraps lua_tothread. tothread :: StackIndex -> LuaE e (Maybe 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 Nothing.. -- -- Wraps lua_touserdata. touserdata :: StackIndex -> LuaE e (Maybe (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 (#) with no metamethods; for userdata, this -- is the size of the block of memory allocated for the userdata; for -- other values, it is 0. -- -- Wraps lua_rawlen. rawlen :: StackIndex -> LuaE e Int -- | Lua comparison operations. data RelationalOperator -- | Correponds to Lua's equality (==) operator. EQ :: RelationalOperator -- | Correponds to Lua's strictly-lesser-than (<) operator LT :: RelationalOperator -- | Correponds to Lua's lesser-or-equal (<=) operator LE :: RelationalOperator -- | Compares two Lua values. Returns True if the value at index -- idx1 satisfies op when compared with the value at -- index idx2, following the semantics of the corresponding Lua -- operator (that is, it may call metamethods). Otherwise returns -- False. Also returns False if any of the indices is -- not valid. -- -- The value of op must be of type RelationalOperator: -- -- EQ: compares for equality (==) LT: compares for less than (<) LE: -- compares for less or equal (<=) -- -- Wraps hslua_compare. See also lua_compare. compare :: LuaError e => StackIndex -> StackIndex -> RelationalOperator -> LuaE e Bool -- | Returns True if the two values in acceptable indices -- index1 and index2 are equal, following the semantics -- of the Lua == operator (that is, may call metamethods). -- Otherwise returns False. Also returns False if any -- of the indices is non valid. Uses compare internally. equal :: LuaError e => StackIndex -> StackIndex -> LuaE e Bool -- | Tests whether the object under the first index is smaller than that -- under the second. Uses compare internally. lessthan :: LuaError e => StackIndex -> StackIndex -> LuaE e Bool -- | Returns True if the two values in indices idx1 and -- idx2 are primitively equal (that is, without calling the -- __eq metamethod). Otherwise returns False. Also -- returns False if any of the indices are not valid. -- -- Wraps lua_rawequal. rawequal :: StackIndex -> StackIndex -> LuaE e Bool -- | Pushes a boolean value with the given value onto the stack. -- -- This functions wraps lua_pushboolean. pushboolean :: Bool -> LuaE e () -- | 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 -- CFunction) -- -- Same as flip pushcclosure 0. lua_pushcfunction. pushcfunction :: CFunction -> LuaE e () -- | 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 §3.4); 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 pushcclosure is called to create and push the C function -- onto the stack, with the argument n telling how many values -- should be associated with the function. pushcclosure also pops these -- values from the stack. -- -- The maximum value for n is 255. -- -- Wraps lua_pushcclosure. pushcclosure :: CFunction -> NumArgs -> LuaE e () -- | Pushes an integer with with the given value onto the stack. -- -- Wraps lua_pushinteger. pushinteger :: Integer -> LuaE e () -- | Pushes a light userdata onto the stack. -- -- Userdata represent C values in Lua. A light userdata represents a -- pointer, a Ptr a (i.e., void* in C). 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. -- -- Wraps lua_pushlightuserdata. pushlightuserdata :: Ptr a -> LuaE e () -- | Pushes a nil value onto the stack. -- -- Wraps lua_pushnil. pushnil :: LuaE e () -- | Pushes a float with the given value onto the stack. -- -- Wraps lua_pushnumber. pushnumber :: Number -> LuaE e () -- | Pushes the 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. -- -- Wraps lua_pushlstring. pushstring :: ByteString -> LuaE e () -- | Pushes the current thread onto the stack. Returns True if -- this thread is the main thread of its state, False otherwise. -- -- Wraps lua_pushthread. pushthread :: LuaE e Bool -- | Pushes onto the stack the value of the global name. -- -- Errors on the Lua side are propagated. -- -- Wraps hslua_getglobal. getglobal :: LuaError e => Name -> LuaE e Type -- | Pushes onto the stack the value t[k], where t is the -- value at the given index and k 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 §2.4 of Lua's manual). -- -- Errors on the Lua side are caught and rethrown. -- -- Wraps hslua_gettable. See also: lua_gettable. gettable :: LuaError e => StackIndex -> LuaE e Type -- | Pushes onto the stack the value t[k], where t is the -- value at the given stack index. As in Lua, this function may trigger a -- metamethod for the "index" event (see §2.4 of Lua's manual). -- -- Errors on the Lua side are propagated. -- -- See also lua_getfield. getfield :: LuaError e => StackIndex -> Name -> LuaE e Type -- | Similar to gettable, but does a raw access (i.e., -- without metamethods). -- -- Wraps lua_rawget. rawget :: LuaError e => StackIndex -> LuaE e () -- | Pushes onto the stack the value t[n], where t is the -- table at the given index. The access is raw, that is, it does not -- invoke the __index metamethod. -- -- Wraps lua_rawgeti. rawgeti :: LuaError e => StackIndex -> Integer -> LuaE e () -- | 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. -- -- Wraps lua_createtable. createtable :: Int -> Int -> LuaE e () -- | Creates a new empty table and pushes it onto the stack. It is -- equivalent to createtable 0 0. -- -- See also: lua_newtable. newtable :: LuaE e () -- | 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. -- -- This function wraps lua_newuserdata. newuserdata :: Int -> LuaE e (Ptr ()) -- | If the value at the given index has a metatable, the function pushes -- that metatable onto the stack and returns True. Otherwise, -- the function returns False and pushes nothing on the stack. -- -- Wraps lua_getmetatable. getmetatable :: StackIndex -> LuaE e Bool -- | Pushes onto the stack the Lua value associated with the full userdata -- at the given index. -- -- Returns the type of the pushed value. -- -- Wraps lua_getuservalue. getuservalue :: StackIndex -> LuaE e Type -- | Pops a value from the stack and sets it as the new value of global -- name. -- -- Errors on the Lua side are caught and rethrown as Exception. -- -- Wraps hslua_setglobal. See also: lua_setglobal. setglobal :: LuaError e => Name -> LuaE e () -- | Does the equivalent to t[k] = v, where t is the -- value at the given index, v is the value at the top of the -- stack, and k 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 §2.4 of the Lua 5.3 Reference Manual). -- -- Errors on the Lua side are caught and rethrown. -- -- Wraps hslua_settable. settable :: LuaError e => StackIndex -> LuaE e () -- | Does the equivalent to t[k] = v, where t is the -- value at the given index and v 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 §2.4 of -- the Lua 5.3 Reference Manual). -- -- Errors on the Lua side are caught and rethrown as a -- Exception. -- -- See also: lua_setfield. setfield :: LuaError e => StackIndex -> Name -> LuaE e () -- | Similar to settable, but does a raw assignment (i.e., -- without metamethods). -- -- Wraps lua_rawset. rawset :: LuaError e => StackIndex -> LuaE e () -- | Does the equivalent of t[i] = v, where t is the -- table at the given index and v 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 __newindex metamethod. -- -- Wraps lua_rawseti. rawseti :: LuaError e => StackIndex -> Integer -> LuaE e () -- | Pops a table from the stack and sets it as the new metatable for the -- value at the given index. -- -- Wraps lua_setmetatable. setmetatable :: StackIndex -> LuaE e () -- | Pops a value from the stack and sets it as the new value associated to -- the full userdata at the given index. -- -- https://www.lua.org/manual/5.3/manual.html#lua_setuservalue setuservalue :: StackIndex -> LuaE e () -- | 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 call; nargs 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 -- nresults, unless nresults is multret. 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 is propagated as exception of -- type e. -- -- The following example shows how the host program can do the equivalent -- to this Lua code: -- --
-- a = f("how", t.x, 14)
--
--
-- Here it is in Haskell (assuming the OverloadedStrings language
-- extension):
--
-- -- 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' ---- -- 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 lua_call. call :: LuaError e => NumArgs -> NumResults -> LuaE e () -- | Calls a function in protected mode. -- -- Both nargs and nresults have the same meaning as in -- call. If there are no errors during the call, -- pcall behaves exactly like call. However, if -- there is any error, pcall catches it, pushes a single value -- on the stack (the error message), and returns the error code. Like -- call, pcall always removes the function and -- its arguments from the stack. -- -- If msgh is Nothing, then the error object returned -- on the stack is exactly the original error object. Otherwise, when -- msgh is Just idx, the stack index idx 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 pcall. -- -- 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 pcall, since by -- then the stack has unwound. -- -- This function wraps lua_pcall. pcall :: NumArgs -> NumResults -> Maybe StackIndex -> LuaE e Status -- | Loads a Lua chunk (without running it). If there are no errors, -- load pushes the compiled chunk as a Lua function on -- top of the stack. Otherwise, it pushes an error message. -- -- The return values of load are: -- --
-- chunkname:currentline: ---- -- 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. where' :: Int -> LuaE e () -- | Reference to a stored value. data Reference -- | Reference to a stored value Reference :: CInt -> Reference -- | Reference to a nil value RefNil :: Reference -- | Creates and returns a reference, in the table at index t, 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 t, ref ensures the -- uniqueness of the key it returns. You can retrieve an object referred -- by reference r by calling rawgeti t r. Function -- unref frees a reference and its associated object. -- -- If the object at the top of the stack is nil, ref -- returns the constant refnil. The constant -- noref is guaranteed to be different from any reference -- returned by ref. -- -- Wraps luaL_ref. ref :: StackIndex -> LuaE e Reference -- | Push referenced value from the table at the given index. getref :: LuaError e => StackIndex -> Reference -> LuaE e () -- | Releases reference ref from the table at index -- idx (see ref). The entry is removed from the -- table, so that the referred object can be collected. The reference -- ref is also freed to be used again. -- -- Wraps luaL_unref. See also: luaL_unref. unref :: StackIndex -> Reference -> LuaE e () -- | Convert a reference to its C representation. fromReference :: Reference -> CInt -- | Create a reference from its C representation. toReference :: CInt -> Reference -- | Value signaling that no reference was found. noref :: Int -- | Value signaling that no reference was created. refnil :: Int -- | Key to the registry field that holds the table of loaded modules. loaded :: Name -- | Key to the registry field that holds the table of loader functions. preload :: Name -- | Creates a new userdata wrapping the given Haskell object. The userdata -- is pushed to the top of the stack. newhsuserdata :: forall a e. a -> LuaE e () -- | 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. -- -- Using a metatable created by this functions ensures that the pointer -- to the Haskell value will be freed when the userdata object is garbage -- collected in Lua. -- -- The name may not contain a nul character. newudmetatable :: Name -> LuaE e Bool -- | Retrieves a Haskell object from userdata at the given index. The -- userdata must have the given name. fromuserdata :: forall a e. StackIndex -> Name -> LuaE e (Maybe a) -- | Replaces the Haskell value contained in the userdata value at -- index. Checks that the userdata is of type name and -- returns True on success, or False otherwise. putuserdata :: forall a e. StackIndex -> Name -> a -> LuaE e Bool -- | Haskell function that can be called from Lua. The HsLua equivallent of -- a PreCFunction. type HaskellFunction e = LuaE e NumResults -- | Pushes Haskell function as a callable userdata. All values created -- will be garbage collected. The function should behave similar to a -- CFunction. -- -- Error conditions should be indicated by raising a catchable exception -- or by returning the result of error. -- -- Example: -- --
-- mod23 :: Lua NumResults -- mod23 = do -- mn <- tointeger (nthBottom 1) -- case mn of -- Nothing -> pushstring "expected an integer" *> error -- Just n -> pushinteger (n `mod` 23) -- pushHaskellFunction mod23 -- setglobal "mod23" --pushHaskellFunction :: LuaError e => HaskellFunction e -> LuaE e () -- | Converts a pre C function to a Lua function and pushes it to the -- stack. -- -- Pre C functions collect parameters from the stack and return a -- CInt that represents number of return values left on the -- stack. See CFunction for more info. pushPreCFunction :: PreCFunction -> LuaE e () -- | Any type that you wish to use for error handling in HsLua must be an -- instance of the LuaError class. class Exception e => LuaError e -- | Converts the error at the top of the stack into an exception and pops -- the error off the stack. -- -- This function is expected to produce a valid result for any Lua value; -- neither a Haskell exception nor a Lua error may result when this is -- called. popException :: LuaError e => LuaE e e -- | Pushes an exception to the top of the Lua stack. The pushed Lua object -- is used as an error object, and it is recommended that calling -- tostring() on the object produces an informative message. pushException :: LuaError e => e -> LuaE e () -- | Creates a new exception with the given message. luaException :: LuaError e => String -> e -- | Default Lua error type. Exceptions raised by Lua-related operations. newtype Exception Exception :: String -> Exception [exceptionMessage] :: Exception -> String -- | Return either the result of a Lua computation or, if an exception was -- thrown, the error. try :: Exception e => LuaE e a -> LuaE e (Either e a) -- | Raises an exception in the Lua monad. failLua :: forall e a. LuaError e => String -> LuaE e a -- | Converts a Lua error at the top of the stack into a Haskell exception -- and throws it. throwErrorAsException :: LuaError e => LuaE e a -- | Raises an exception that's appropriate when the type of a Lua object -- at the given index did not match the expected type. The name or -- description of the expected type is taken as an argument. throwTypeMismatchError :: forall e a. LuaError e => ByteString -> StackIndex -> LuaE e a -- | Change the error type of a computation. changeErrorType :: forall old new a. LuaE old a -> LuaE new a -- | Retrieve and pop the top object as an error message. This is very -- similar to tostring', but ensures that we don't recurse if getting the -- message failed. -- -- This helpful as a "last resort" method when implementing -- peekException. popErrorMessage :: State -> IO ByteString -- | Creates an error to notify about a Lua type mismatch and pushes it to -- the stack. pushTypeMismatchError :: ByteString -> StackIndex -> LuaE e () -- | Load a module, defined by a Haskell action, under the given name. -- -- Similar to luaL_required: After checking "loaded" table, -- calls pushMod to push a module to the stack, and registers -- the result in package.loaded table. -- -- The pushMod function must push exactly one element to the top -- of the stack. This is not checked, but failure to do so will lead to -- problems. Lua's package module must have been loaded by the -- time this function is invoked. -- -- Leaves a copy of the module on the stack. requirehs :: LuaError e => Name -> LuaE e () -> LuaE e () -- | Registers a preloading function. Takes an module name and the Lua -- operation which produces the package. preloadhs :: LuaError e => Name -> LuaE e NumResults -> LuaE e ()