Lua

A tiny language designed to be embedded.

Lua is taught here in 13 lessons, running from Lua syntax and types through to Lua idioms, style and common pitfalls. Each lesson takes one topic, shows the working code, and links onward to the next, so the course can be read straight through in order.

TrackProgramming Languages
Lessons13
LevelBeginner to intermediate
Reading timeabout 3 hours
PrerequisitesHelpful, but not required: Scala

Lessons

  1. Lua syntax and typesLocals versus globals, the eight value types, 1-based tables, and the operators that surprise newcomers.
  2. Tables, metatables and object orientationHow __index and __newindex turn plain tables into classes, proxies and read-only views.
  3. Embedding Lua in a host programThe C API stack, passing values in both directions, registering host functions, and sandboxing untrusted scripts.
  4. Functions, closures and multiple returnsTreat functions as values, use varargs and multiple returns safely, and build closures and stateful callbacks with upvalues.
  5. Strings and Lua patternsBuild strings efficiently, format output with string.format, and use find, match, gmatch and gsub with Lua's own pattern syntax.
  6. Iterators: pairs, ipairs and generic forUnderstand what the generic for really calls, write stateless and stateful iterators, and sort tables with comparators.
  7. The standard library in daily usetable, math, string, os and io functions you actually call, plus the differences between Lua 5.1, 5.3, 5.4 and LuaJIT.
  8. Error handling: error, pcall and xpcallRaise errors with useful messages, catch them with pcall and xpcall, add tracebacks, and write cleanup that always runs.
  9. Modules, require and package.pathWrite modules that return a table, understand how require caches and searches, and avoid circular and global-leaking module patterns.
  10. Coroutines and cooperative multitaskingCreate, resume, yield and wrap coroutines, build producer-consumer pipelines, and implement the scheduler pattern game engines use.
  11. Environments, sandboxing and bytecodeControl what a chunk can see with _ENV and load, restrict globals for untrusted code, and treat precompiled bytecode as dangerous input.
  12. Performance, garbage collection and LuaJITSize tables correctly, avoid allocation in hot paths, tune the collector, and know when LuaJIT's traces or FFI are worth it.
  13. Lua idioms, style and common pitfallsWrite idiomatic Lua, avoid global leaks and silent coercions, and review someone else's Lua quickly and confidently.

More in Programming Languages

C C++ C# Scala Dart VBA

FAQ

How long does the Lua course take?
It has 13 lessons, about 3 hours of reading. Expect roughly twice that if you type out and run every example.
Do I need prior experience for Lua?
Not strictly. It helps to have read Scala first, because some lessons build on it, but every lesson explains its own assumptions.
What should I read after Lua?
Continue with Dart (13 lessons), the next course in Programming Languages.