Lua#
Lua is the language inside other tools. The operator meets it embedded in Neovim, Nmap NSE scripts, Wireshark dissectors, Redis, OpenResty, HAProxy, and most modern game and middleware engines. Tiny interpreter, predictable semantics, easy to drop into a host program through the C API.
Write Lua to extend the tools they already run (custom Nmap probes, Neovim editor surface, Wireshark protocol dissection, OpenResty request filters) rather than as a standalone application language. Reading and writing Lua is a unit-level skill for anyone who lives in those tools.
Baseline. Lua 5.4 is the reference. LuaJIT (Lua 5.1 plus extensions, two orders of magnitude faster than the reference interpreter) is the dialect inside OpenResty, HAProxy, and many game engines; recipes call out where the two diverge.
Default toolchain. lua / luajit interpreters,
luarocks for packages, stylua for formatting, luacheck
or selene for linting, busted for tests.
Setup#
Picking the right interpreter and getting the toolchain on the host.
lua, luajit, luarocks, brew, apt.
Picking 5.4 vs LuaJIT and bootstrapping a project.
luarocks, stylua, luacheck, selene,
busted, ldoc. The toolchain around the
interpreter.
Table tricks, string patterns, file I/O, and / or
ternary.
Language#
The spec building blocks in the order every language page on this handbook follows.
--, --[[ ]], ;, true false nil. Comments,
identifiers, keywords, literals, chunks, statements
versus expressions.
local, =, a, b = 1, 2. Globals versus locals,
multiple assignment, scope, upvalues.
nil boolean number string function table userdata
thread, type, tostring, tonumber. The eight
base types and the one container.
+ - * / // % ^, == ~= < >, and or not,
.., #, & | ~ << >>. Arithmetic, relational,
logical, concat, length, bitwise.
if elseif else, while, repeat until,
for (numeric and generic), break, goto.
Branching, loops, jumps.
function, return, ..., closures, multiple
returns, tail calls, varargs, first-class functions.
setmetatable, __index, __newindex, self,
: method-call sugar. OOP by metatable, single and
multiple inheritance.
Patterns#
The layer above the spec. The dialect a Lua program written by an operator should be written in.
Modules as tables, configuration-as-code, metatable tricks, coroutines as iterators. Idiomatic Lua.
error, assert, pcall, xpcall,
debug.traceback. Exception-free error handling
through protected calls.
Data Structures#
The one container Lua has, and the algorithms that ride on it.
Tables as arrays, hash maps, sets, queues, stacks,
records, namespaces. ipairs / pairs, length
operator, sequence rules.
table.sort, table.insert, table.remove,
string.gmatch, search and traversal patterns on
tables.
I/O#
Talking to files, the network, the shell, and the host runtime.
io.open, io.read, io.write, io.lines,
string.format, string.gmatch. Files, streams,
formatting, parsing.
arg, argparse (rock), os.exit, stdin /
stdout streaming. Command-line scripts.
LuaSocket, cqueues, lua-http, OpenResty
cosockets. TCP, UDP, HTTP, TLS from Lua.
coroutine.create, coroutine.resume,
coroutine.yield, cqueues, OpenResty’s
light-thread model. Cooperative scheduling.
Runtime#
The interpreter, the C API, and the embedding boundary between the operator’s Lua and the host program.
require, package.path, package.cpath,
_G, _ENV, garbage collection, the C API, PUC-Rio
Lua versus LuaJIT.
Ecosystem#
LuaRocks and the host frameworks where Lua does most of its work.
Stdlib: string table math io os debug.
LuaRocks: LuaSocket, luaposix, lpeg,
luafilesystem, cjson, cqueues.
Web: OpenResty, Lapis. Game: LÖVE, Defold. Editor: Neovim. Net: Nmap NSE, Wireshark, Suricata, HAProxy.
Practice#
The disciplines that make scripts safe to ship and the end-to-end projects that exercise the whole stack.
busted, luaunit, luacov. Spec-style tests,
fixtures, coverage.
End-to-end Lua projects, an Nmap NSE script, a Wireshark dissector, an OpenResty filter, a Neovim plugin.