-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Bindings to Lua, an embeddable scripting language -- -- HsLua provides bindings, wrappers, types, and helper functions to -- bridge Haskell and Lua. -- -- This package contains a full Lua interpreter version 5.3.5. If you -- want to link it with a system-wide Lua installation, use the -- system-lua flag. -- -- Example programs are available in a separate repository. @package hslua @version 1.0.2 -- | The core Lua types, including mappings of Lua types to Haskell. module Foreign.Lua.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 Lua a Lua :: ReaderT State IO a -> Lua a [unLua] :: Lua a -> ReaderT State IO a -- | An opaque structure that points to a thread and indirectly (through -- the thread) to the whole state of a Lua interpreter. The Lua library -- is fully reentrant: it has no global variables. All information about -- a state is accessible through this structure. -- -- Synonym for lua_State *. See lua_State. newtype State State :: Ptr () -> State -- | The reader function used by lua_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) -> Lua a -- | Turn a function of typ Lua.State -> a -> IO b into a -- monadic lua operation. liftLua1 :: (State -> a -> IO b) -> a -> Lua b -- | Get the lua state of this lua computation. state :: Lua State -- | Run lua computation with custom lua state. Errors are left unhandled, -- the caller of this function is responsible to catch lua errors. runWith :: State -> Lua a -> IO a -- | Enumeration used by gc function. data GCCONTROL GCSTOP :: GCCONTROL GCRESTART :: GCCONTROL GCCOLLECT :: GCCONTROL GCCOUNT :: GCCONTROL GCCOUNTB :: GCCONTROL GCSTEP :: GCCONTROL GCSETPAUSE :: GCCONTROL GCSETSTEPMUL :: GCCONTROL -- | 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 -- | 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 -- | 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, 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 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 (State -> IO NumResults) -- | Boolean value returned by a Lua C API function. This is a -- CInt and interpreted as False iff the -- value is 0, True otherwise. newtype LuaBool LuaBool :: CInt -> LuaBool -- | Lua representation of the value interpreted as false. false :: LuaBool -- | Generic Lua representation of a value interpreted as being true. true :: 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 -- | 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 -- | 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 -> 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 __gc metamethod. ErrGcmm :: Status -- | opening or reading a file failed. ErrFile :: Status -- | Integer code used to signal the status of a thread or computation. See -- Status. newtype StatusCode StatusCode :: CInt -> StatusCode -- | Convert C integer constant to LuaStatus. 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 instance GHC.Show.Show Foreign.Lua.Core.Types.Reference instance GHC.Classes.Eq Foreign.Lua.Core.Types.Reference instance GHC.Show.Show Foreign.Lua.Core.Types.NumResults instance GHC.Classes.Ord Foreign.Lua.Core.Types.NumResults instance GHC.Num.Num Foreign.Lua.Core.Types.NumResults instance GHC.Classes.Eq Foreign.Lua.Core.Types.NumResults instance GHC.Show.Show Foreign.Lua.Core.Types.NumArgs instance GHC.Classes.Ord Foreign.Lua.Core.Types.NumArgs instance GHC.Num.Num Foreign.Lua.Core.Types.NumArgs instance GHC.Classes.Eq Foreign.Lua.Core.Types.NumArgs instance GHC.Show.Show Foreign.Lua.Core.Types.StackIndex instance GHC.Classes.Ord Foreign.Lua.Core.Types.StackIndex instance GHC.Num.Num Foreign.Lua.Core.Types.StackIndex instance GHC.Classes.Eq Foreign.Lua.Core.Types.StackIndex instance GHC.Enum.Enum Foreign.Lua.Core.Types.StackIndex instance GHC.Show.Show Foreign.Lua.Core.Types.GCCONTROL instance GHC.Classes.Ord Foreign.Lua.Core.Types.GCCONTROL instance GHC.Classes.Eq Foreign.Lua.Core.Types.GCCONTROL instance GHC.Enum.Enum Foreign.Lua.Core.Types.GCCONTROL instance GHC.Classes.Eq Foreign.Lua.Core.Types.StatusCode instance GHC.Show.Show Foreign.Lua.Core.Types.Status instance GHC.Classes.Eq Foreign.Lua.Core.Types.Status instance GHC.Show.Show Foreign.Lua.Core.Types.RelationalOperator instance GHC.Classes.Ord Foreign.Lua.Core.Types.RelationalOperator instance GHC.Classes.Eq Foreign.Lua.Core.Types.RelationalOperator instance GHC.Show.Show Foreign.Lua.Core.Types.TypeCode instance GHC.Classes.Ord Foreign.Lua.Core.Types.TypeCode instance GHC.Classes.Eq Foreign.Lua.Core.Types.TypeCode instance GHC.Show.Show Foreign.Lua.Core.Types.Type instance GHC.Classes.Ord Foreign.Lua.Core.Types.Type instance GHC.Classes.Eq Foreign.Lua.Core.Types.Type instance GHC.Enum.Bounded Foreign.Lua.Core.Types.Type instance GHC.Show.Show Foreign.Lua.Core.Types.LuaBool instance Foreign.Storable.Storable Foreign.Lua.Core.Types.LuaBool instance GHC.Classes.Eq Foreign.Lua.Core.Types.LuaBool instance GHC.Show.Show Foreign.Lua.Core.Types.Number instance GHC.Real.RealFrac Foreign.Lua.Core.Types.Number instance GHC.Float.RealFloat Foreign.Lua.Core.Types.Number instance GHC.Real.Real Foreign.Lua.Core.Types.Number instance GHC.Classes.Ord Foreign.Lua.Core.Types.Number instance GHC.Num.Num Foreign.Lua.Core.Types.Number instance GHC.Real.Fractional Foreign.Lua.Core.Types.Number instance GHC.Float.Floating Foreign.Lua.Core.Types.Number instance GHC.Classes.Eq Foreign.Lua.Core.Types.Number instance GHC.Show.Show Foreign.Lua.Core.Types.Integer instance GHC.Real.Real Foreign.Lua.Core.Types.Integer instance GHC.Classes.Ord Foreign.Lua.Core.Types.Integer instance GHC.Num.Num Foreign.Lua.Core.Types.Integer instance GHC.Real.Integral Foreign.Lua.Core.Types.Integer instance GHC.Classes.Eq Foreign.Lua.Core.Types.Integer instance GHC.Enum.Enum Foreign.Lua.Core.Types.Integer instance GHC.Enum.Bounded Foreign.Lua.Core.Types.Integer instance Control.Monad.Catch.MonadThrow Foreign.Lua.Core.Types.Lua instance Control.Monad.Reader.Class.MonadReader Foreign.Lua.Core.Types.State Foreign.Lua.Core.Types.Lua instance Control.Monad.Catch.MonadMask Foreign.Lua.Core.Types.Lua instance Control.Monad.IO.Class.MonadIO Foreign.Lua.Core.Types.Lua instance Control.Monad.Catch.MonadCatch Foreign.Lua.Core.Types.Lua instance GHC.Base.Monad Foreign.Lua.Core.Types.Lua instance GHC.Base.Functor Foreign.Lua.Core.Types.Lua instance GHC.Base.Applicative Foreign.Lua.Core.Types.Lua instance GHC.Generics.Generic Foreign.Lua.Core.Types.State instance GHC.Classes.Eq Foreign.Lua.Core.Types.State instance GHC.Enum.Enum Foreign.Lua.Core.Types.Type -- | Lua constants module Foreign.Lua.Core.Constants -- | Alias for C constant LUA_MULTRET. See lua_call. multret :: NumResults -- | Alias for C constant LUA_REGISTRYINDEX. See Lua -- registry. registryindex :: StackIndex -- | Value signaling that no reference was created. refnil :: Int -- | Value signaling that no reference was found. noref :: Int -- | Lua exceptions and exception handling. module Foreign.Lua.Core.Error -- | Exceptions raised by Lua-related operations. newtype Exception Exception :: String -> Exception [exceptionMessage] :: Exception -> String -- | Catch a Lua Exception. catchException :: Lua a -> (Exception -> Lua a) -> Lua a -- | Raise a Lua Exception containing the given error -- message. throwException :: String -> Lua a -- | Catch Lua Exception, alter the message and rethrow. withExceptionMessage :: (String -> String) -> Lua a -> Lua a -- | Convert the object at the top of the stack into a string and throw it -- as an Exception. throwTopMessage :: Lua a -- | Return either the result of a Lua computation or, if an exception was -- thrown, the error. try :: Lua a -> Lua (Either Exception a) -- | CInt 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 used for additional type safety and gives the type -- into which the wrapped CInt should be converted. newtype Failable a Failable :: CInt -> Failable a -- | Convert from Failable to target type, throwing an error if the value -- indicates a failure. fromFailable :: (CInt -> a) -> Failable a -> Lua a -- | Throw a Haskell exception if the computation signaled a failure. throwOnError :: Failable () -> Lua () -- | Convert lua boolean to Haskell Bool, throwing an exception if the -- return value indicates that an error had happened. boolFromFailable :: Failable LuaBool -> Lua Bool -- | Registry field under which the special HsLua error indicator is -- stored. hsluaErrorRegistryField :: String instance GHC.Classes.Eq Foreign.Lua.Core.Error.Exception instance GHC.Show.Show Foreign.Lua.Core.Error.Exception instance GHC.Exception.Type.Exception Foreign.Lua.Core.Error.Exception instance GHC.Base.Alternative Foreign.Lua.Core.Types.Lua -- | Haskell bindings to lua C API functions. module Foreign.Lua.Core.RawBindings -- | See lua_close lua_close :: State -> IO () -- | See lua_absindex lua_absindex :: State -> StackIndex -> IO StackIndex -- | See lua_gettop lua_gettop :: State -> IO StackIndex -- | See lua_settop lua_settop :: State -> StackIndex -> IO () -- | See lua_pushvalue lua_pushvalue :: State -> StackIndex -> IO () -- | See lua_copy lua_copy :: State -> StackIndex -> StackIndex -> IO () -- | See lua_remove lua_remove :: State -> StackIndex -> IO () -- | See lua_insert lua_insert :: State -> StackIndex -> IO () -- | See lua_replace lua_replace :: State -> StackIndex -> IO () -- | See lua_checkstack lua_checkstack :: State -> CInt -> IO LuaBool -- | See lua_isnumber lua_isnumber :: State -> StackIndex -> IO LuaBool -- | See lua_isinteger lua_isinteger :: State -> StackIndex -> IO LuaBool -- | See lua_isstring lua_isstring :: State -> StackIndex -> IO LuaBool -- | See lua_iscfunction lua_iscfunction :: State -> StackIndex -> IO LuaBool -- | See lua_isuserdata lua_isuserdata :: State -> StackIndex -> IO LuaBool -- | See lua_type lua_type :: State -> StackIndex -> IO TypeCode -- | See lua_typename lua_typename :: State -> TypeCode -> IO CString -- | Wrapper around -- @lua_compare@ which catches any -- longjmps. hslua_compare :: State -> StackIndex -> StackIndex -> CInt -> IO (Failable LuaBool) -- | See lua_rawequal lua_rawequal :: State -> StackIndex -> StackIndex -> IO LuaBool -- | See lua_toboolean lua_toboolean :: State -> StackIndex -> IO LuaBool -- | See lua_tocfunction lua_tocfunction :: State -> StackIndex -> IO CFunction -- | See lua_tointegerx lua_tointegerx :: State -> StackIndex -> Ptr LuaBool -> IO Integer -- | See lua_tonumberx lua_tonumberx :: State -> StackIndex -> Ptr LuaBool -> IO Number -- | See lua_tolstring lua_tolstring :: State -> StackIndex -> Ptr CSize -> IO (Ptr CChar) -- | See lua_topointer lua_topointer :: State -> StackIndex -> IO (Ptr ()) -- | See lua_tothread lua_tothread :: State -> StackIndex -> IO State -- | See lua_touserdata lua_touserdata :: State -> StackIndex -> IO (Ptr a) -- | See lua_rawlen lua_rawlen :: State -> StackIndex -> IO CSize -- | See lua_pushnil lua_pushnil :: State -> IO () -- | See lua_pushnumber lua_pushnumber :: State -> Number -> IO () -- | See lua_pushinteger lua_pushinteger :: State -> Integer -> IO () -- | See lua_pushlstring lua_pushlstring :: State -> Ptr CChar -> CSize -> IO () -- | See lua_pushcclosure lua_pushcclosure :: State -> CFunction -> NumArgs -> IO () -- | See lua_pushboolean lua_pushboolean :: State -> LuaBool -> IO () -- | See lua_pushlightuserdata lua_pushlightuserdata :: State -> Ptr a -> IO () -- | See lua_pushthread lua_pushthread :: State -> IO CInt -- | Wrapper around -- @lua_gettable@ which catches any -- longjmps. hslua_gettable :: State -> StackIndex -> IO (Failable ()) -- | See lua_rawget lua_rawget :: State -> StackIndex -> IO () -- | See lua_rawgeti lua_rawgeti :: State -> StackIndex -> Integer -> IO () -- | See lua_createtable lua_createtable :: State -> CInt -> CInt -> IO () -- | See lua_newuserdata lua_newuserdata :: State -> CSize -> IO (Ptr ()) -- | See lua_getmetatable lua_getmetatable :: State -> StackIndex -> IO LuaBool -- | Wrapper around -- @lua_getglobal@ which catches any -- longjmps. hslua_getglobal :: State -> CString -> CSize -> IO (Failable ()) -- | Wrapper around -- @lua_settable@ which catches any -- longjmps. hslua_settable :: State -> StackIndex -> IO (Failable ()) -- | See lua_rawset lua_rawset :: State -> StackIndex -> IO () -- | See lua_rawseti lua_rawseti :: State -> StackIndex -> Integer -> IO () -- | See lua_setmetatable lua_setmetatable :: State -> StackIndex -> IO () -- | Wrapper around -- @lua_setglobal@ which catches any -- longjmps. hslua_setglobal :: State -> CString -> CSize -> IO (Failable ()) -- | See lua_pcall lua_pcall :: State -> NumArgs -> NumResults -> StackIndex -> IO StatusCode -- | See lua_load lua_load :: State -> Reader -> Ptr () -> CString -> CString -> IO StatusCode -- | See lua_status lua_status :: State -> IO StatusCode -- | See lua_gc lua_gc :: State -> CInt -> CInt -> IO CInt -- | Wrapper around -- @lua_next@ which catches any -- longjmps. hslua_next :: State -> StackIndex -> IO (Failable LuaBool) -- | Wrapper around -- @lua_concat@ which catches any -- longjmps. hslua_concat :: State -> NumArgs -> IO (Failable ()) lua_pushglobaltable :: State -> IO () -- | See luaL_openlibs luaL_openlibs :: State -> 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 -- | Core Lua API. This module provides thin wrappers around the respective -- functions of the Lua C API. C function 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 Foreign.Lua.Core -- | 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 Lua a Lua :: ReaderT State IO a -> Lua a [unLua] :: Lua a -> ReaderT State IO a -- | Run lua computation with custom lua state. Errors are left unhandled, -- the caller of this function is responsible to catch lua errors. runWith :: State -> Lua 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 :: Lua 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, 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 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 (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 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 LUA_MULTRET. See lua_call. multret :: NumResults -- | Alias for C constant LUA_REGISTRYINDEX. See Lua -- registry. registryindex :: StackIndex -- | Returns the pseudo-index that represents the i-th upvalue of -- the running function (see §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. -- -- 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. -- -- This is a wrapper function of 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). 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: lua_gettop. 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: lua_settop. settop :: StackIndex -> Lua () -- | Pushes a copy of the element at the given index onto the stack. -- -- See lua_pushvalue. pushvalue :: StackIndex -> Lua () -- | Copies the element at index fromidx into the valid index -- toidx, replacing the value at that position. Values at other -- positions are not affected. -- -- See also lua_copy 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: lua_insert. insert :: StackIndex -> Lua () -- | Pops n elements from the stack. -- -- See also: lua_pop. 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 lua_remove. 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 lua_replace. replace :: StackIndex -> Lua () -- | 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. -- -- This is a wrapper function of lua_checkstack. checkstack :: Int -> Lua 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 -- | 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 -- | Returns the type of the value in the given valid index, or -- TypeNone for a non-valid (but acceptable) index. -- -- See lua_type. ltype :: StackIndex -> Lua Type -- | Returns the name of the type encoded by the value tp, which -- must be one the values returned by ltype. -- -- See also: lua_typename. typename :: Type -> Lua String -- | Returns True if the value at the given index is a boolean, -- and False otherwise. -- -- See also: lua_isboolean. isboolean :: StackIndex -> Lua Bool -- | Returns True if the value at the given index is a C function, -- and False otherwise. -- -- See also: lua_iscfunction. iscfunction :: StackIndex -> Lua Bool -- | Returns True if the value at the given index is a function -- (either C or Lua), and False otherwise. -- -- See also: lua_isfunction. isfunction :: StackIndex -> Lua 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. isinteger :: StackIndex -> Lua Bool -- | Returns True if the value at the given index is a light -- userdata, and False otherwise. -- -- See also: -- <https://www.lua.org/manual/5.3/manual.html#lua_islightuserdata -- lua_islightuserdata>. islightuserdata :: StackIndex -> Lua Bool -- | Returns True if the value at the given index is nil, -- and False otherwise. -- -- See also: lua_isnil. isnil :: StackIndex -> Lua Bool -- | Returns True if the given index is not valid, and -- False otherwise. -- -- See also: lua_isnone. isnone :: StackIndex -> Lua Bool -- | Returns True if the given index is not valid or if the value -- at the given index is nil, and False otherwise. -- -- See also: lua_isnoneornil. isnoneornil :: StackIndex -> Lua Bool -- | Returns True if the value at the given index is a number or a -- string convertible to a number, and False otherwise. -- -- See also: lua_isnumber. isnumber :: StackIndex -> Lua 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. -- -- See also: lua_isstring. isstring :: StackIndex -> Lua Bool -- | Returns True if the value at the given index is a table, and -- False otherwise. -- -- See also: lua_istable. istable :: StackIndex -> Lua Bool -- | Returns True if the value at the given index is a thread, and -- False otherwise. -- -- See also: lua_isthread. isthread :: StackIndex -> Lua Bool -- | Returns True if the value at the given index is a userdata -- (either full or light), and False otherwise. -- -- See also: lua_isuserdata. isuserdata :: StackIndex -> Lua 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.) -- -- See also: lua_toboolean. toboolean :: StackIndex -> Lua Bool -- | Converts a value at the given index to a C function. That value must -- be a C function; otherwise, returns Nothing. -- -- See also: lua_tocfunction. tocfunction :: StackIndex -> Lua (Maybe CFunction) -- | Converts the Lua value at the given acceptable index to the signed -- integral type lua_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. -- -- See also: lua_tointeger. tointeger :: StackIndex -> Lua (Maybe Integer) -- | 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, tonumber returns Nothing. -- -- See lua_tonumber. tonumber :: StackIndex -> Lua (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. -- -- See also: lua_topointer. topointer :: StackIndex -> Lua (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.) -- -- See lua_tolstring. tostring :: StackIndex -> Lua (Maybe 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 Nothing. -- -- See also: lua_tothread. tothread :: StackIndex -> Lua (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.. -- -- See also: lua_touserdata. touserdata :: StackIndex -> Lua (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. -- -- See also: lua_rawlen. rawlen :: StackIndex -> Lua 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 -- | Convert relation operator to its C representation. fromRelationalOperator :: RelationalOperator -> CInt -- | 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 LuaComparerOp: -- -- OpEQ: compares for equality (==) OpLT: compares for less than (<) -- OpLE: compares for less or equal (<=) -- -- This is a wrapper function of lua_compare. compare :: StackIndex -> StackIndex -> RelationalOperator -> Lua 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 :: StackIndex -> StackIndex -> Lua Bool -- | Tests whether the object under the first index is smaller than that -- under the second. Uses compare internally. lessthan :: StackIndex -> StackIndex -> Lua 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. -- -- See also: lua_rawequal. rawequal :: StackIndex -> StackIndex -> Lua Bool -- | Pushes a boolean value with the given value onto the stack. -- -- See also: lua_pushboolean. 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 -- CFunction) -- -- See also: lua_pushcfunction. 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 §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 lua_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. lua_pushcclosure also -- pops these values from the stack. -- -- The maximum value for n is 255. -- -- See also: lua_pushcclosure. pushcclosure :: CFunction -> NumArgs -> Lua () -- | Pushes an integer with with the given value onto the stack. -- -- See also: lua_pushinteger. pushinteger :: Integer -> Lua () -- | Pushes a light userdata onto the stack. -- -- Userdata represent C values in Lua. A light userdata represents a -- pointer, a Ptr () (i.e., void* 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: lua_pushlightuserdata. pushlightuserdata :: Ptr a -> Lua () -- | Pushes a nil value onto the stack. -- -- See lua_pushnil. pushnil :: Lua () -- | Pushes a float with the given value onto the stack. -- -- See lua_pushnumber. pushnumber :: Number -> 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: -- <https://www.lua.org/manual/5.3/manual.html#lua_pushstring -- lua_pushstring>. pushstring :: ByteString -> Lua () -- | Pushes the current thread onto the stack. Returns True if -- this thread is the main thread of its state, False otherwise. -- -- See also: lua_pushthread. pushthread :: Lua Bool -- | Pushes onto the stack the value of the global name. -- -- Errors on the Lua side are caught and rethrown as -- Exception. -- -- Wrapper of lua_getglobal. getglobal :: String -> Lua () -- | 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 as -- Exception. -- -- See also: lua_gettable. gettable :: StackIndex -> Lua () -- | 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 caught and rethrown as -- Exception. -- -- See also: lua_getfield. getfield :: StackIndex -> String -> Lua () -- | Similar to gettable, but does a raw access (i.e., -- without metamethods). -- -- See also: lua_rawget. rawget :: StackIndex -> Lua () -- | 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. -- -- See also: lua_rawgeti. rawgeti :: StackIndex -> Integer -> 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 lua_createtable. createtable :: Int -> Int -> Lua () -- | Creates a new empty table and pushes it onto the stack. It is -- equivalent to createtable 0 0. -- -- See also: lua_newtable. 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: lua_newuserdata. newuserdata :: Int -> Lua (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. -- -- See also: lua_getmetatable. getmetatable :: StackIndex -> Lua Bool -- | 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 a -- Exception. -- -- See also: lua_setglobal. setglobal :: String -> Lua () -- | 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 as a -- Exception. -- -- See also: lua_settable. settable :: StackIndex -> Lua () -- | 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 :: StackIndex -> String -> Lua () -- | Similar to settable, but does a raw assignment (i.e., -- without metamethods). -- -- See also: lua_rawset. rawset :: StackIndex -> Lua () -- | 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. -- -- See also: lua_rawseti. rawseti :: StackIndex -> Integer -> Lua () -- | Pops a table from the stack and sets it as the new metatable for the -- value at the given index. -- -- See also: -- <https://www.lua.org/manual/5.3/manual.html#lua_setmetatable -- lua_setmetatable>. 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 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 cause a Exception -- to be thrown. -- -- 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 :: NumArgs -> NumResults -> Lua () -- | 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. -- -- See lua_pcall. pcall :: NumArgs -> NumResults -> Maybe StackIndex -> Lua 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: -- --
-- data Person = Person { name :: String, age :: Int }
-- deriving (Eq, Show, Typeable, Data)
--
--
-- we can simply do
--
-- -- instance Lua.Peekable Person where -- safePeek = safePeekAny -- -- instance Lua.Pushable Person where -- push = pushAny ---- -- The other functions can be used to exert more control over the -- userdata wrapping and unwrapping process. module Foreign.Lua.Userdata -- | Push data by wrapping it into a userdata object. pushAny :: Data a => a -> Lua () -- | Push data by wrapping it into a userdata object, using the object at -- the top of the stack after performing the given operation as -- metatable. pushAnyWithMetatable :: Lua () -> a -> Lua () -- | Retrieve data which has been pushed with pushAny. toAny :: Data a => StackIndex -> Lua (Maybe a) -- | Retrieve data which has been pushed with -- pushAnyWithMetatable, where *name* must is the value -- of the __name field of the metatable. toAnyWithName :: StackIndex -> String -> Lua (Maybe a) -- | Retrieve Haskell data which was pushed to Lua as userdata. peekAny :: Data a => StackIndex -> Lua a -- | Push the metatable used to define the behavior of the given value in -- Lua. The table will be created if it doesn't exist yet. ensureUserdataMetatable :: String -> Lua () -> Lua () -- | Return the default name for userdata to be used when wrapping an -- object as the given type as userdata. The argument is never evaluated. metatableName :: Data a => a -> String -- | Types for working with Lua. module Foreign.Lua.Types -- | HsLua utility functions. module Foreign.Lua.Util -- | Like getglobal, but knows about packages and nested tables. -- E.g. -- --
-- getglobal' "math.sin" ---- -- will return the function sin in package math. getglobal' :: String -> Lua () -- | Like setglobal, but knows about packages and nested tables. -- E.g. -- --
-- pushstring "0.9.4" -- setglobal' "mypackage.version" ---- -- 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. -- 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 :: 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. runEither :: Lua a -> IO (Either Exception a) -- | Raise a Lua error, using the given value as the error object. raiseError :: Pushable 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 -- | Try to convert the value at the given stack index to a Haskell value. -- Returns Left with an error message on failure. peekEither :: Peekable a => StackIndex -> Lua (Either String a) -- | Get a value by retrieving a String from Lua, then using -- readMaybe to convert the String into a Haskell value. peekRead :: Read a => StackIndex -> Lua a -- | Get, then pop the value at the top of the stack. The pop operation is -- executed even if the retrieval operation failed. popValue :: Peekable a => Lua a instance Foreign.Lua.Types.Peekable.Peekable a => Foreign.Lua.Types.Peekable.Peekable (Foreign.Lua.Util.Optional a) instance Foreign.Lua.Types.Pushable.Pushable a => Foreign.Lua.Types.Pushable.Pushable (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 Peekable a -- | Check if at index n there is a convertible Lua value and if -- so return it. Throws a Exception otherwise. peek :: Peekable 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 -- toHaskellFunction wrapper instead. class ToHaskellFunction a -- | Helper function, called by toHaskellFunction 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 Pushable a -- | Pushes a value onto Lua stack, casting it into meaningfully nearest -- Lua type. push :: Pushable a => a -> Lua () -- | Type of raw Haskell functions that can be made into CFunctions. type PreCFunction = State -> IO NumResults -- | Convert a Haskell function to Lua function. Any Haskell function can -- be converted provided that: -- --
-- toHaskellFunction (myFun `catchM` (\e -> raiseError (e :: FooException))) --toHaskellFunction :: ToHaskellFunction a => a -> HaskellFunction -- | Call a Lua function. Use as: -- --
-- v <- callfunc "proc" "abc" (1::Int) (5.0::Double) --callFunc :: LuaCallFunc a => String -> a -- | Free function pointer created with newcfunction. freeCFunction :: CFunction -> Lua () -- | Create new foreign Lua function. Function created can be called by Lua -- engine. Remeber to free the pointer with freecfunction. newCFunction :: ToHaskellFunction a => a -> Lua CFunction -- | Pushes Haskell function as a callable userdata. All values created -- will be garbage collected. Use as: -- --
-- pushHaskellFunction myfun -- setglobal "myfun" ---- -- Error conditions should be indicated by raising a Lua -- Exception or by returning the result of -- error. 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.Peekable.Peekable a => Foreign.Lua.FunctionCalling.LuaCallFunc (Foreign.Lua.Core.Types.Lua a) instance (Foreign.Lua.Types.Pushable.Pushable 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.Pushable.Pushable a => Foreign.Lua.FunctionCalling.ToHaskellFunction (Foreign.Lua.Core.Types.Lua a) instance (Foreign.Lua.Types.Peekable.Peekable 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. -- -- Basic access to the Lua API is provided by '@Foreign.Lua.Core@'. module Foreign.Lua -- | A value that can be read from the Lua stack. class Peekable a -- | Check if at index n there is a convertible Lua value and if -- so return it. Throws a Exception otherwise. peek :: Peekable a => StackIndex -> Lua a -- | Try to convert the value at the given stack index to a Haskell value. -- Returns Left with an error message on failure. peekEither :: Peekable a => StackIndex -> Lua (Either String a) -- | Read a table into a list peekList :: Peekable a => StackIndex -> Lua [a] -- | Read a table into a list of pairs. peekKeyValuePairs :: (Peekable a, Peekable b) => StackIndex -> Lua [(a, b)] -- | Get a value by retrieving a String from Lua, then using -- readMaybe to convert the String into a Haskell value. peekRead :: Read a => StackIndex -> Lua a -- | Retrieve Haskell data which was pushed to Lua as userdata. peekAny :: Data a => StackIndex -> Lua a -- | A value that can be pushed to the Lua stack. class Pushable a -- | Pushes a value onto Lua stack, casting it into meaningfully nearest -- Lua type. push :: Pushable a => a -> Lua () -- | Push list as numerically indexed table. pushList :: Pushable a => [a] -> Lua () -- | Push data by wrapping it into a userdata object. pushAny :: Data a => a -> Lua () -- | Type of raw Haskell functions that can be made into CFunctions. type PreCFunction = State -> 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 -- toHaskellFunction wrapper instead. class ToHaskellFunction a -- | Helper function, called by toHaskellFunction toHsFun :: ToHaskellFunction a => StackIndex -> a -> Lua NumResults -- | Convert a Haskell function to Lua function. Any Haskell function can -- be converted provided that: -- --
-- toHaskellFunction (myFun `catchM` (\e -> raiseError (e :: FooException))) --toHaskellFunction :: ToHaskellFunction a => a -> HaskellFunction -- | Call a Lua function. Use as: -- --
-- v <- callfunc "proc" "abc" (1::Int) (5.0::Double) --callFunc :: LuaCallFunc a => String -> a -- | Create new foreign Lua function. Function created can be called by Lua -- engine. Remeber to free the pointer with freecfunction. newCFunction :: ToHaskellFunction a => a -> Lua CFunction -- | Free function pointer created with newcfunction. freeCFunction :: CFunction -> Lua () -- | Pushes Haskell function as a callable userdata. All values created -- will be garbage collected. Use as: -- --
-- pushHaskellFunction myfun -- setglobal "myfun" ---- -- Error conditions should be indicated by raising a Lua -- Exception or by returning the result of -- error. 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. -- 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 :: 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. runEither :: Lua a -> IO (Either Exception a) -- | Like getglobal, but knows about packages and nested tables. -- E.g. -- --
-- getglobal' "math.sin" ---- -- will return the function sin in package math. getglobal' :: String -> Lua () -- | Like setglobal, but knows about packages and nested tables. -- E.g. -- --
-- pushstring "0.9.4" -- setglobal' "mypackage.version" ---- -- 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. raiseError :: Pushable 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 -- | Get, then pop the value at the top of the stack. The pop operation is -- executed even if the retrieval operation failed. popValue :: Peekable a => Lua a