Commit 01f4200
refactor: collapse repeated argument guards into CheckArgs
Every method opened with the same 15-line preamble: an arity check, a type
check per argument, then an EnsureOpen check, each throwing and returning by
hand. That was 217 of 673 lines in luastate.cc, and SetGlobal was typical --
22 lines of which 2 did the work.
Adds CheckArgs(info, method, {Arg::String, ...}), which validates arity and
types and throws in the established wording, so a method now opens with one
line. The messages are generated rather than hardcoded, and were verified
byte-identical for all fourteen distinct forms, including CollectGarbage's
', try nodelua.GC.[TYPE]' hint and the singular/plural split between
'Requires 1 Argument' and 'Requires 2 Arguments'.
Also adds ThrowLuaError, which builds the message, pops the error and throws.
Popping was previously written out at each of the six call sites and is easy to
omit, which silently grows the stack. Replaces lua_error_message, whose callers
all had to remember to pop afterwards.
Extracts EnsureIndexable, the non-table check duplicated between SetField and
GetField.
Pop and SetTop keep their hand-written checks: their argument is optional, so
CheckArgs does not apply, and the comments now say so.
luastate.cc 672 -> 447 lines, guard boilerplate 217 -> 63, src/ down 152 lines
overall. No behaviour change: 62/62 tests pass and all three examples run.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>1 parent de2ee8f commit 01f4200
4 files changed
Lines changed: 126 additions & 278 deletions
0 commit comments