Line Hook Behavior for Anonymous Strings in Lua 5.4
27.5K reputation · 31 Aug 2026, 16:43 UTC
Line Hook Behavior for Anonymous Strings in Lua 5.4
When debug.sethook is set with a line mask, the hook receives a line argument for every executed line. This is useful for silent alerts without notification noise.
However, compiling a chunk from a string without a source name (e.g., loadstring or load with a nil name) produces bytecode that has no line number table. In Lua 5.3/5.4 the hook still fires, but the line argument is nil. The reference manual does not specify whether a nil value is expected or indicates a defect.
Because many monitoring libraries rely on a valid line number to correlate alerts with source code, this ambiguity is problematic. The question is: is the nil line argument intentional or a bug? If intentional, what is the recommended way to distinguish such cases? How should hooks be designed to avoid noisy alerts when the line number is unavailable?