refactor: collapse repeated argument guards into CheckArgs - #9
Merged
Conversation
mschmicking
force-pushed
the
refactor-check-args
branch
2 times, most recently
from
August 8, 2026 08:26
57f2c34 to
19da4ee
Compare
mschmicking
force-pushed
the
refactor-check-args
branch
from
August 8, 2026 08:51
19da4ee to
c2b1495
Compare
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>
mschmicking
force-pushed
the
refactor-check-args
branch
from
August 8, 2026 08:57
c2b1495 to
c2cca33
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found while auditing
masterfor structure. 217 of 673 lines inluastate.ccwere argument guards.The problem
Every method opened with the same preamble — arity check, a type check per argument, then
EnsureOpen— each throwing and returning by hand.SetGlobalwas typical: 22 lines, of which 2 did the work.becomes
What was added
CheckArgs(info, method, {Arg::String, ...})— validates arity and types, throws in the established wording.ThrowLuaError(env, L, prefix)— builds the message, pops the error, throws. The pop was previously written out at each of the six call sites and is easy to omit, which silently grows the Lua stack. Replaceslua_error_message, whose callers all had to remember to pop.EnsureIndexable— the non-table check that was duplicated betweenSetFieldandGetField.PopandSetTopkeep their hand-written checks: their argument is optional, soCheckArgsdoes not apply. The comments now say so, rather than leaving it looking like an oversight.Behaviour is unchanged, and that was checked rather than assumed
Messages are now generated, so I verified all fourteen distinct forms are byte-identical to the hardcoded originals — including
CollectGarbage's, try nodelua.GC.[TYPE]suffix and the singular/plural split:62/62 tests pass, all three examples run.
Result
luastate.ccsrc/total🤖 Generated with Claude Code