Introduction
Lua is a small scripting language that is often
built into games and other programs. Lua source (.lua) is compiled to
bytecode for Lua’s virtual machine each time it is loaded. The luac tool
saves that bytecode to a file instead, a precompiled chunk, which loads faster and does
not show the source. Its default output name is luac.out; people usually
rename it .luac, and some programs load precompiled chunks under a plain
.lua name.
The bytecode changes between Lua versions, so a chunk only loads in the version that
wrote it, built with the same number sizes. LuaJIT, a separate fast implementation of
Lua 5.1, has its own bytecode format, which starts with 1B 4C 4A
(\x1BLJ).
Structure
A precompiled chunk starts with the byte 1B (escape) and
Lua, then one byte for the version: 0x51 for Lua 5.1,
0x52 for 5.2, 0x53 for 5.3 and 0x54 for 5.4.
Next is a format byte, 0 for the official format. The rest of the header records the
sizes the compiler used for integers, instructions and numbers, and, from Lua 5.2 on,
a few fixed bytes and sample numbers that let the loader detect a damaged file or a
different byte order. After the header comes the main function: its instructions,
constants, upvalues, nested functions and optional debug information.
The Lua developers do not document this layout and change it between versions, so tools that read it follow the source code of each version.