Generated Lua
Generated Lua is part of the Lux developer experience. GMod errors happen in Lua stack traces, so emitted code must be legal, readable, and source correlated.
Single-file Output
Single-file compile output is a direct Lua chunk. It is useful for language
examples and backend debugging.
Project builds use module factories instead of a single top-level chunk.
Module Factories
GMod project modules are emitted as factories. The loader supplies
__lux_import, and the factory returns an export table.
Modules do not expose a global import function. Import stays inside the generated loader and module factories.
Export Tables
Exports map public names to internal bindings.
The internal name remains private. Other modules can import p_inv, not
player_inventory.
Source Comment Modes
none: production-friendly output with sidecar maps only.readable: comments at review anchors such as functions and branch blocks.boundary: comments whenever the mapped source line changes.dense: comments for every mapped generated line.
Use readable while inspecting generated Lua. Use none when generated files
should be smaller and source maps are available.
Source Maps
Source maps connect generated Lua positions back to Lux source. They are used by
luxc map-error:
Expected output shape:
Macro expansions and host transforms carry expansion spans so diagnostics can distinguish user call sites from macro implementation code.
match Lowering
Value-position match lowers to a temporary and branches. Lux avoids wrapping
ordinary matches in closures.
Return position can emit direct returns when that preserves evaluation order.
Optional Access Lowering
Optional access lowers to temporaries so receivers and indexes are evaluated only when needed.
The emitted shape may be compacted, but it must preserve nil-only coalescing and single evaluation of side-effectful expressions.
Local Limits
Lua 5.1 has a hard local-variable limit per function. Lux must never emit a chunk that GMod rejects with "function has more than 200 local variables".
The backend narrows temporary scopes and reports deterministic diagnostics when generated code cannot be made legal:
When this happens, split the function, move repeated expression work into a helper, or reduce generated temporary pressure in the source expression.