hslua
A Lua language interpreter embedding in Haskell
| Version on this page: | 0.8.0 |
| LTS Haskell 24.17: | 2.3.1 |
| Stackage Nightly 2025-10-30: | 2.4.0 |
| Latest on Hackage: | 2.4.0 |
hslua-0.8.0@sha256:8bfaf6220ae7cd4a28bd1196c899f9c292c54ce3757eb6190c7d539358c4219f,7340Module documentation for 0.8.0
hslua – Lua interpreter interface for Haskell
Hslua provides bindings, wrappers, types, and helper functions to bridge haskell and lua.
Using a different lua version
To use system-wide installed Lua/LuaJIT when linking hslua as a dependency,
build/install your package using --constraint="hslua +system-lua" or for
LuaJIT: --constraint="hslua +system-lua +luajit". For example, you can install
Pandoc with hslua that uses system-wide LuaJIT like this:
cabal install pandoc --constraint="hslua +system-lua +luajit"
FAQ
Where are the coroutine related functions? Yielding from a coroutine works
via longjmp, which plays very badly with Haskell’s RTS. Tests to get
coroutines working with HsLua were unsuccessful. No coroutine related functions
are exported from the default module for that reason. However, raw bindings to
the C API functions are still provided in Foreign.Lua.RawBindings. If you get
coroutines to work, or just believe that there should be wrapper functions for
other reasons, we’d love to hear from you.
Why are there no predefined stack instances for default numerical types?
HsLua defines instances for the FromLuaStack and ToLuaStack type-classes
only if the following law holds: return x == push x *> peek x. Lua can be
compiled with customized number types, making it impossible to verify the
correctness of the above equation. Furthermore, instances for numerical types
can be based on those of LuaInteger and LuaNumber and are easy to write.
Therefor hslua doesn’t provide any such instances.
Changes
Changelog
0.8.0
- Use newtype definitions instead of type aliases for LuaNumber and LuaInteger. This makes it easier to ensure the correct numeric instances in situations where Lua might have been compiled with 32-bit numbers.
- Instances of
FromLuaStackandToLuaStackforIntare removed. The correctness of these instances cannot be guaranteed if Lua was compiled with a non-standard integer type.
0.7.1
- The flag
lua_32bitswas added to allow users to compile Lua for 32-bit systems. - When reading a list, throw an error if the lua value isn’t a table instead of silently returning an empty list.
0.7.0
- Tuples from pairs to octuples have been made instances of
FromLuaStackandToLuaStack. - New functions
dostringanddofileare provided to load and run strings and files in a single step. LuaStatuswas renamed toStatus, the Lua prefix was removed from its type constructors.- The constructor
ErrFilewas added toStatus. It is returned byloadfileif the file cannot be read. - Remove unused FFI bindings and unused types, including all functions unsafe to use from within Haskell and the library functions added with 0.5.0. Users with special requirements should define their own wrappers and raw bindings.
- The module Foreign.Lua.Api.SafeBindings was merge into Foreign.Lua.Api.RawBindings.
- FFI bindings are changed to use newtypes where sensible, most notably
StackIndex,NumArgs, andNumResults, but also the newly introduced newtypesStatusCode,TypeCode, andLuaBool. - Add functions
tointegerxandtonumberxwhich can be used to get and check values from the stack in a single step. - The signature of
concatwas changed fromInt -> Lua ()toNumArgs -> Lua (). - The signature of
loadfilewas changed fromString -> Lua InttoString -> Lua Status. - The type
LTYPEwas renamed toType, its constructors were renamed to follow the patternType<Typename>.LuaRelationwas renamed toRelationalOperator, the Lua prefix was removed from its constructors. - Add function
tolistto allow getting a generic list from the stack without having to worry about the overlapping instance with[Char].
0.6.0
- Supported Lua Versions now include Lua 5.2 and Lua 5.3. LuaJIT and Lua 5.1 remain supported as well.
- Flag
use-pkgconfigwas added to allow discovery of library and include paths via pkg-config. Setting a specific Lua version flag now impliessystem-lua. (Sean Proctor) - The module was renamed from
Scripting.LuatoForeign.Lua. The code is now split over multiple sub-modules. Files processed with hsc2hs are restricted to Foreign.Lua.Api. - A
Luamonad (reader monad over LuaState) is introduced. Functions which took a LuaState as their first argument are changed into monadic functions within that monad. - Error handling has been redesigned completely. A new LuaException was
introduced and is thrown in unexpected situations. Errors in lua which are
leading to a
longjmpare now caught with the help of additional C wrapper functions. Those no longer lead to uncontrolled program termination but are converted into a LuaException. peekno longer returnsMaybe abut justa. A LuaException is thrown if an error occurs (i.e. in situtations where Nothing would have been returned previously).- The
StackValuetypeclass has been split intoFromLuaStackandToLuaStack. Instances not satisfying the lawx == push x *> peek (-1)have been dropped. - Documentation of API functions was improved. Most docstrings have been copied from the official Lua manual, enriched with proper markup and links, and changed to properly describe hslua specifics when necessary.
- Example programs have been moved to a separate repository.
- Unused files were removed. (Sean Proctor)
0.5.0
- New raw functions for
luaopen_base,luaopen_package,luaopen_string,luaopen_table,luaopen_math,luaopen_io,luaopen_os,luaopen_debugand their high-level wrappers (with namesopenbase,opentableetc.) implemented. - Remove custom versions of
loadfileandloadstring. - Drop support for GHC versions < 7.8, avoid compiler warnings.
- Ensure no symbols are stripped when linking the bundled lua interpreter.
- Simplify
tostringfunction definition. (Sean Proctor) - Explicitly deprecate
strlen. (Sean Proctor) - Add links to lua documentation for functions wrapping the official lua C API. (Sean Proctor).
0.4.1
- Bugfix(#30):
tolistwasn’t popping elements of the list from stack.
0.4.0
pushstringandtostringnow usesByteStringinstead of[Char].StackValue [Char]instance is removed,StackValue ByteStringis added.StackValue a => StackValue [a]instance is added. It pushes a Lua array to the stack.pushlist,islistandtolistfunctions are added.- Type errors in Haskell functions now propagated differently. See the
Scripting.Luadocumentation for detailed explanation. This should fix segfaults reported several times. lua_errorfunction is removed, it’s never safe to call in Haskell.
Related issues and pull requests: #12, #26, #24, #23, #18.
0.3.14
- Pkgconf-based setup removed. Cabal is now using
extra-librariesto link with Lua. luajitflag is added to link hslua with LuaJIT.
0.3.13
- Small bugfix related with GHCi running under Windows.
0.3.12
pushrawhsfunctionandregisterrawhsfunctionfunctions are added.apicheckflag is added to Cabal package to enable Lua API checking. (useful for debugging)
0.3.11
luaL_refandluaL_unreffunctions are added.