Lua Programming Language

Created: by Pradeep Gowda Updated: Feb 20, 2024 Tagged: programming-language · lua

Learn

Lua associated languages

Pallene + Lua. Paper .Found via janet lang’s gitter channel. It is a “Friendly Fork” of Titan Language. Pallene is a moon of saturn?

Terra – low level lua like language.

teal; The compiler for Teal, a typed dialect of Lua

Luau is a fast, small, safe, gradually typed embeddable scripting language derived from Lua.

Cool Lua stuff

2020-06-04 oasislinux/oasis: a small statically-linked linux system; uses lua for setup, building, package spec etc.,

Mako Server This tiny, ready-to-run application server packs a punch with its super compact and efficient Lua web framework and its non-blocking asynchronous sockets. Plus, it comes with everything you need built-in, including an integrated database, SMTP, HTTP client/server, and even IoT protocols like MQTT and industrial protocols like OPC-UA and Modbus.

People

  • Calvin Rose – bakpakin.com (creator of Fennel lang, moonmint web framework, etc.,)
  • Stefano Trettelmoon* libs for graphics, etc.,

Compiling Lua source files

Let’s say you have a lua program – hello.lua.

print("Hello Lua!")

The source code can be compiled into a binary file using luac

$ luac -o hello.luac hello.lua

The file hello.luac can then be used in place of the source file. Using the binary file has some advantages like: faster loading, simple source hiding etc.,

To run a .luac file (this is my own convention, the Lua docs do not seem to prefer any particular suffix for the compiled file), you still need to call the lua interpreter,

$ lua hello.luac

Creating standalone executables for lua

A Lua source file can be converted into a executable using different methods. srlua appears to be the popular one.

Download srlua from here and build it using make. Copy glue and srlua files into a convenient location, say ~/bin.

You can now create standalone executable from the Lua source file –hello.lua thus:

$ ~/bin/glue ~/bin/srlua hello.lua hello
$ chmod +x hello
$ ./hello
Hello Lua!

Installing new software packages

Luarocks is the deployment and package management system for Lua. Think CPAN for Perl and PyPI for Python.

I have installed Lua in my $HOME directory. That is, lua interpreter resides in $HOME/bin, include files in $HOME/include etc.,

So, to install Luarocks:

$ export PREFIX=$HOME
$ ./configure --prefix=$PREFIX --sysconfdir=$PREFIX/luarocks --force-config

Installing new lua packages is as simple as:

$ cd
luarocks install luasocket

where luasocket is a new package that is fetched from the luarocks mirrors and installed.

To make this package available to the Lua interpreter export LUA_PATH thusly:

export LUA_PATH="/opt/local/share/lua/5.1//?.lua;/opt/local/lib/luarocks/?.lua;;"

Another LUA_PATH that makes more sense:

?;?.lua;/usr/local/lua/?/?.lua

Also, to explicitly add paths to a script:

# cat t_paths.lua
package.path = package.path.. ";/opt/local/share/lua/5.1/?.lua"
package.cpath = package.cpath.. ";/opt/local/lib/lua/5.1/?.so"
local s = require "socket"
print(s._VERSION)

Incidentally, on the day I was learning aobut luarocks, luaforge.net, the main lua modules repository was down. Friendly folks on the #lua IRC channel told me to add mirrors to the luarocks configuration.

$ vim ~/luarocks/config.lua

I replaced the luaforge.net line with

http://www.luarocks.org/repositories/rocks/",

Misc. Notes

Setting up LUA_PATH and LUA_CPATH is always more complicated than it needs to be. Remember not to have any spaces after the path seperator ;.

LuaDist appears promising to set up virtualenv like sandboxed environments (and more..) for lua.

Lua application packaging

luastatic README tells us that it can package lua applications into executables for windows, macos & linux. A few applications that use luastatic to do their packaging are given. It will be nice to find build setups which can compile to all the three OSes using github CI pipeline.

See also: Luarocks.