hslua-core: Bindings to Lua, an embeddable scripting language

[ foreign, library, mit ] [ Propose Tags ]

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.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 1.0.0, 2.0.0, 2.0.0.1, 2.0.0.2, 2.1.0, 2.2.0, 2.2.1, 2.3.0, 2.3.1, 2.3.2
Change log CHANGELOG.md
Dependencies base (>=4.11 && <5), bytestring (>=0.10.2 && <0.13), exceptions (>=0.8 && <0.11), lua (>=2.3.1 && <2.4), mtl (>=2.2 && <2.4), text (>=1.2 && <2.2) [details]
License MIT
Copyright © 2007–2012 Gracjan Polak; © 2012–2016 Ömer Sinan Ağacan; © 2017-2024 Albert Krewinkel
Author Albert Krewinkel, Gracjan Polak, Ömer Sinan Ağacan
Maintainer tarleb@hslua.org
Category Foreign
Home page https://hslua.org/
Bug tracker https://github.com/hslua/hslua/issues
Source repo head: git clone https://github.com/hslua/hslua.git
Uploaded by tarleb at 2024-01-18T16:11:37Z
Distributions Arch:2.3.1, Fedora:2.3.1, LTSHaskell:2.3.2, NixOS:2.3.2, Stackage:2.3.2, openSUSE:2.3.2
Reverse Dependencies 18 direct, 178 indirect [details]
Downloads 8840 total (216 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-01-18 [all 1 reports]

Readme for hslua-core-2.3.2

[back to package description]

hslua-core

Build status AppVeyor Status Hackage

Basic building blocks to interface Haskell and Lua in a Haskell-idiomatic style.

Overview

Lua is a small, well-designed, embeddable scripting language. It has become the de-facto default to make programs extensible and is widely used everywhere from servers over games and desktop applications up to security software and embedded devices. This package provides the basic building blocks for coders to embed Lua into their programs.

This package is part of HsLua, a Haskell framework built around the embeddable scripting language Lua.

Interacting with Lua

HsLua core provides the Lua type to define Lua operations. The operations are executed by calling run. A simple “Hello, World” program, using the Lua print function, is given below:

import HsLua.Core.Lua as Lua

main :: IO ()
main = Lua.run prog
  where
    prog :: Lua ()
    prog = do
      Lua.openlibs  -- load Lua libraries so we can use 'print'
      Lua.getglobal "print"            -- push print function
      Lua.pushstring "Hello, World!"   -- push string argument
      Lua.call
        (NumArgs 1)    -- number of arguments passed to the function
        (NumResults 0) -- number of results expected
                       -- as return values

The Lua stack

Lua’s API is stack-centered: most operations involve pushing values to the stack or receiving items from the stack. E.g., calling a function is performed by pushing the function onto the stack, followed by the function arguments in the order they should be passed to the function. The API function call then invokes the function with given numbers of arguments, pops the function and parameters off the stack, and pushes the results.

,----------.
|  arg 3   |
+----------+
|  arg 2   |
+----------+
|  arg 1   |
+----------+                  ,----------.
| function |    call 3 1      | result 1 |
+----------+   ===========>   +----------+
|          |                  |          |
|  stack   |                  |  stack   |
|          |                  |          |

This package provides all basic building blocks to interact with the Lua stack. If you’d like more comfort, please consider using the hslua-packaging and hslua-classes packages.

Error handling

Errors and exceptions must always be caught and converted when passing language boundaries. The exception type which can be handled is encoded as the type e in the monad LuaE e. Only exceptions of this type may be thrown; throwing different exceptions across language boundaries will lead to a program crash.

Exceptions must support certain operations as defined by the LuaError typeclass. The class ensures that errors can be converted from and to Lua values, and that a new exception can be created from a String message.