Skip to content
LumenaryAcademy

Lumenary CS · Programming languages

Lua

The small, fast, embeddable language from Brazil that scripts games, editors, databases and devices, from a first program to metatables, coroutines, the C API and a complete project.

Lua is a small scripting language created in 1993 at the Pontifical Catholic University of Rio de Janeiro (PUC-Rio) in Brazil. It was designed to be embedded: a whole Lua interpreter is a few hundred kilobytes of portable C that a larger program, written in C, C++ or almost anything else, carries inside it so that its users can script it. That is why you meet Lua in game engines and in Roblox (through its descendant Luau), in the Neovim editor's configuration, inside Redis and nginx, in network tools and in embedded devices. Lua is also a pleasant language in its own right, with one data structure (the table), first-class functions, closures, coroutines and metatables, from which you can build almost anything.

This course is for anyone who already knows what a variable, a loop and a function are: the Start level of Lumenary CS or equivalent experience in any language. It begins with Lua's history and design and your first scripts, works through values, tables, functions and closures, metatables and object-oriented patterns, modules and error handling, and coroutines, and ends with how Lua is embedded through its C API, an overview of LuaJIT, the tools of the ecosystem, and a complete project.

To run the code, get Lua from lua.org/download.html: the source builds with a plain make on Linux and macOS, package managers such as Homebrew and those of Linux distributions provide it, and LuaBinaries offers ready-made builds for Windows. To try code without installing anything, the official Lua demo runs small programs in your browser. Every complete program in these lessons was run with Lua 5.5 by Lumenary's test suite, and the output shown is the output it printed; the lessons point out what changed from Lua 5.4. The lessons were drafted with AI assistance and are awaiting review by a human computer scientist.

AI-draftedAwaiting review by a computer scientist
  • Chloe · The Designer
    “Why is the language built this way?”
  • Margaret · The Profiler
    “What does it print, and what does it cost?”
  • Gideon · The Language Lawyer
    “What exactly does the language guarantee?”

Foundations · 3 lessons

Foundations

Install Lua and run your first scripts, then learn its values and types, strings, control flow and locals, and the table, Lua's one data structure.

  1. 1Getting started with LuaWhere Lua came from, why so many programs carry it inside them, and your first scripts. 45 min
  2. 2Values, strings and control flowLua's eight types, numbers and strings, truth, local and global variables, and every kind of loop and branch. 55 min
  3. 3Tables, the one data structureArrays, dictionaries, records, sets, queues and matrices, all built from one idea, with the table library and a first pattern. 55 min

Practice · 3 lessons

Practice

Use functions as values with closures and varargs, give tables new behaviour with metatables and build objects from them, and organise code into modules that handle errors well.

  1. 4Functions, closures and iteratorsFirst-class functions, multiple results, varargs and Lua 5.5's named vararg tables, closures and upvalues, iterators, and proper tail calls. 60 min
  2. 5Metatables and objectsGiving tables new behaviour with metamethods, and building classes, inheritance, read-only and strict tables, caches and private state. 65 min
  3. 6Modules and error handlingOrganising code with require and module tables, and raising, catching and cleaning up after errors with error, pcall, xpcall and to-be-closed variables. 60 min

Professional · 2 lessons

Professional

Write cooperative concurrency with coroutines, see how applications embed Lua through its C API and where LuaJIT fits, work with the ecosystem's tools, and build a complete project.

  1. 7CoroutinesFunctions that pause and resume, and what they make easy, from generators and pipelines to schedulers for game scripts and asynchronous code. 60 min
  2. 8Embedding, LuaJIT and a projectHow applications embed Lua through its C API, where LuaJIT fits, the tools Lua programmers use, and a complete sandboxed rule engine. 75 min

Further reading and resources

  • lua.orgThe official home of Lua: downloads, documentation, news, papers and community links.
  • Lua 5.5 Reference Manual · Roberto Ierusalimschy, Luiz Henrique de Figueiredo and Waldemar CelesThe official definition of the language, its standard libraries and its C API. Lua has no separate standard: this manual is the specification.
  • DocumentationThe manuals for every version of Lua, books, papers and other official documentation.
  • Programming in Lua · Roberto IerusalimschyThe official book on Lua by its chief architect. Its site has information on the current edition and the free online first edition.
  • Lua demoThe official page for running small Lua programs in a browser.
  • LuaRocksThe package manager and repository for Lua modules.