Home / Lua
Lua
Expert Lua: environments and sandboxing, the debug library, metaprogramming, advanced coroutines, performance, embedding, DSLs, cleanup, testing, and three large projects.
Lua Advanced is the expert level of Lua. It assumes Lua Intermediate is complete and takes you from using Lua well to shaping how Lua itself behaves, inside games, tools and editors that embed it. Across 49 lessons you learn to sandbox untrusted code, inspect running programs, generate code, schedule coroutines, make programs faster, and build plug-in systems that survive failures.
Course contents
Work top to bottom — the sidebar is the course. Topics later in the list assume the ones before them.
Lua Advanced Introduction
Advanced · ⏱ 3 min · standalone lesson
Environments and Sandboxing
- Lua Environment Lookup
- Lua Custom Environments
- Lua Sandboxing
- Lua Environment Isolation
- Lua Config Files with Environments
The Debug Library
Start: Lua Debug Info ❯Metaprogramming
Start: Lua Loading Chunks ❯Advanced Coroutines
- Lua Coroutine Channels
- Lua Coroutine Async Patterns
- Lua Priority Coroutine Scheduler
- Lua Coroutine State Machines
- Lua Nested Coroutine Yields
- Lua Coroutine Timeouts
Performance
- Lua Profiling with Counters
- Lua Local Optimisation
- Lua Table Optimisation
- Lua String Optimisation
- Lua LuaJIT Notes
Embedding and Hosts
Start: Lua Host APIs ❯DSLs
Start: Lua Table DSLs ❯Errors and Cleanup
- Lua To-Be-Closed Variables
- Lua Close Order
- Lua Cleanup Patterns
- Lua Error Boundaries
- Lua Error Recovery
Testing and Packaging
Start: Lua Test Harnesses ❯Lua Project: Plug-in System
Advanced · ⏱ 7 min · standalone lesson
Lua Project: Config Interpreter
Advanced · ⏱ 7 min · standalone lesson
Lua Project: Task Scheduler
Advanced · ⏱ 7 min · standalone lesson
Learn Lua Advanced
Lua is small on purpose. A host program, a game, an editor or a tool, embeds Lua so users can extend it without rebuilding the host. At the advanced level you stop being only a Lua programmer and start being the person who designs that boundary: what guest code may touch, how it is loaded, and what happens when it fails.
Environments sit at the centre of this level. Every chunk reads its globals from an environment table, and once you can swap that table you can sandbox a plug-in, capture a configuration file's settings, and keep two guests from interfering with each other. The debug library adds the second half of control: inspecting functions, upvalues and locals, and watching a program run through hooks.
The rest grows from the same idea of control over running code. Coroutines become channels, async patterns and priority schedulers. To-be-closed variables give cleanup a guarantee instead of a hope. Metaprogramming turns code into data you can load, generate and protect. The three projects at the end pull it together: a plug-in system, a configuration interpreter, and a task scheduler.
What you will learn
By the final project, these are the things you can do in Lua:
- Swap a chunk's environment table and run untrusted code inside a sandbox with only safe functions.
- Inspect functions with the debug library: read info, upvalues and locals, and trace a program through hooks.
- Load and generate code at run time, give tables custom pairs behaviour, and lock a metatable against tampering.
- Move messages through coroutine channels, write async patterns, and schedule coroutines by priority with simulated ticks.
- Profile with counters and hook counts, and speed up programs with locals, table reuse and table.concat.
- Model a host program in Lua: expose an API, sandbox guests, register plug-ins, and reload modules without restarting.
- Describe behaviour as data with table, chaining, rule and declarative DSLs, and interpret a small configuration language.
- Guarantee cleanup with to-be-closed variables, close resources in reverse order, and isolate failures behind error boundaries.
- Write test harnesses and mocks, version modules, read a rockspec, and lay out a real project.
- Finish by building a plug-in system, a configuration interpreter, and a priority task scheduler.
Who this Lua Advanced course is for
This level is for readers who have completed Lua Intermediate and want the expert layer. If environments, the debug library and to-be-closed variables are still names you have only heard, this level turns them into tools you have used.
It fits people building games, editors or tools around Lua. Much of the level concerns the host side: the API guests see, the sandbox they run in, and the guarantees that hold when a plug-in fails.
It also suits programmers who want to understand a language from the inside. Environments, chunks and the debug library show how Lua itself works, and those ideas carry into any language with closures and first-class functions.
How this course works
Take the lessons in sequence. Later topics build on earlier ones: host sandboxes lean on environments, profiling leans on debug hooks, and the projects draw on the whole course.
The 49 lessons add up to 214 minutes of reading time, a little over three and a half hours, before practice time. The three projects deserve extra time beyond reading: bending them to fit your own ideas is where the real learning happens.
Each example in this course was executed two times under Lua 5.4.7 before publication, and the output printed on the page is exactly what the interpreter printed. LuaJIT appears only as notes: the code in this course runs on the standard interpreter. Every lesson closes with a four-option check, and its explanation walks through why the right answer is right.
Your place is kept for you as you go: the browser stores which lessons you have marked done. Your progress across the 49 lessons shows on the course home, so you can resume exactly where you left off.
Frequently asked questions
Do I need Lua Intermediate first?
Yes. This level uses metatables, closures, iterators, objects, coroutine pipelines, patterns and modules without teaching them again. If those ideas are new, start with Lua Intermediate and come back.
Is this Lua Advanced course free?
Yes. Every course on this platform is free to read, with no account and no paywall. You work through the lessons in your browser and your progress is saved on your own device.
Must I install Lua before starting?
No. Each lesson shows what the real interpreter printed, so you can learn by reading alone. If you want to run the code yourself, install the standard Lua 5.4 interpreter from lua.org and save each example as a file.
What is a sandbox in Lua?
A sandbox is an environment table that holds only the safe functions a guest chunk may use. The host loads the guest chunk under that table, so a name the sandbox does not hold reads as nil instead of reaching the real global.
Why does Lua need a debug library?
Because tools need to look inside running code. Debuggers, profilers and test tools use the debug library to read function info, upvalues and locals, and to watch calls and lines as they happen. Ordinary programs rarely call it directly.
Does this course teach LuaJIT?
No. LuaJIT appears only as notes about what changes under a just-in-time compiler. All the code in this course runs on standard Lua 5.4.7, and FFI, which belongs to LuaJIT, is not covered.
What projects will I be able to build?
Plug-in systems for games and tools, configuration languages that users can edit safely, coroutine schedulers for small servers and simulations, and test harnesses for your own Lua projects. The three closing projects are working starting points for all of them.
Next steps
- Lua ❯ — The prerequisite course: metatables in depth, closures, objects, coroutine pipelines, patterns, and files.
- Python Advanced ❯ — Another language's expert level, for readers who want a second advanced course after Lua.