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.

Setup

lua, luajit, luarocks, brew, apt. Picking 5.4 vs LuaJIT and bootstrapping a project.

Setup
Tooling

luarocks, stylua, luacheck, selene, busted, ldoc. The toolchain around the interpreter.

Tooling
Snippets

Table tricks, string patterns, file I/O, and / or ternary.

Snippets

Language#

The spec building blocks in the order every language page on this handbook follows.

Syntax

--, --[[ ]], ;, true false nil. Comments, identifiers, keywords, literals, chunks, statements versus expressions.

Syntax
Variables

local, =, a, b = 1, 2. Globals versus locals, multiple assignment, scope, upvalues.

Variables
Types

nil boolean number string function table userdata thread, type, tostring, tonumber. The eight base types and the one container.

Types
Operators

+ - * / // % ^, == ~= < >, and or not, .., #, & | ~ << >>. Arithmetic, relational, logical, concat, length, bitwise.

Operators
Controls

if elseif else, while, repeat until, for (numeric and generic), break, goto. Branching, loops, jumps.

Control flow
Functions

function, return, ..., closures, multiple returns, tail calls, varargs, first-class functions.

Functions
OOP

setmetatable, __index, __newindex, self, : method-call sugar. OOP by metatable, single and multiple inheritance.

OOP

Patterns#

The layer above the spec. The dialect a Lua program written by an operator should be written in.

Patterns

Modules as tables, configuration-as-code, metatable tricks, coroutines as iterators. Idiomatic Lua.

Patterns
Errors

error, assert, pcall, xpcall, debug.traceback. Exception-free error handling through protected calls.

Errors

Data Structures#

The one container Lua has, and the algorithms that ride on it.

Structures

Tables as arrays, hash maps, sets, queues, stacks, records, namespaces. ipairs / pairs, length operator, sequence rules.

Data Structures
Algorithms

table.sort, table.insert, table.remove, string.gmatch, search and traversal patterns on tables.

Algorithms

I/O#

Talking to files, the network, the shell, and the host runtime.

I/O

io.open, io.read, io.write, io.lines, string.format, string.gmatch. Files, streams, formatting, parsing.

I/O
CLI

arg, argparse (rock), os.exit, stdin / stdout streaming. Command-line scripts.

CLI
Networking

LuaSocket, cqueues, lua-http, OpenResty cosockets. TCP, UDP, HTTP, TLS from Lua.

Networking
Concurrency

coroutine.create, coroutine.resume, coroutine.yield, cqueues, OpenResty’s light-thread model. Cooperative scheduling.

Concurrency

Runtime#

The interpreter, the C API, and the embedding boundary between the operator’s Lua and the host program.

Runtime

require, package.path, package.cpath, _G, _ENV, garbage collection, the C API, PUC-Rio Lua versus LuaJIT.

Runtime

Ecosystem#

LuaRocks and the host frameworks where Lua does most of its work.

Libraries

Stdlib: string table math io os debug. LuaRocks: LuaSocket, luaposix, lpeg, luafilesystem, cjson, cqueues.

Libraries
Frameworks

Web: OpenResty, Lapis. Game: LÖVE, Defold. Editor: Neovim. Net: Nmap NSE, Wireshark, Suricata, HAProxy.

Frameworks

Practice#

The disciplines that make scripts safe to ship and the end-to-end projects that exercise the whole stack.

Testing

busted, luaunit, luacov. Spec-style tests, fixtures, coverage.

Testing
Projects

End-to-end Lua projects, an Nmap NSE script, a Wireshark dissector, an OpenResty filter, a Neovim plugin.

Projects