Repository navigation
fix(vm): pass operands through varargs to metamethod closures - #203
Merged
Merged
Conversation
Binary, unary, and equality metamethod helpers were inlining the Lua closure call themselves, placing operands into registers but never populating proto.varargs. A metamethod declared as function(...) has param_count == 0 and reads its arguments from varargs, so the operands were silently dropped. Delegating to call_function/3 reuses the regular calling convention (registers for declared params, varargs for the rest) and fixes the dispatch. While in there, fix a related lookup bug: for binary operations where the first operand's metatable defines no relevant metamethod (e.g. '5' + b, where '5' has the string metatable), the cond returned nil without consulting the second operand. Lua 5.3 §2.4 requires falling through to the second operand. Refactor metamethod lookup into a shared helper and chain with || across both operands. Plan: A8 Closes #169
Document the vararg metamethod bug found during re-triage of events.lua, the binary-metamethod operand-fallthrough bug surfaced in the same fix, and the remaining float div-by-zero gap that blocks events.lua line 156. Open A8a (.agents/plans/A8a-float-div-zero.md) to cover the divide-by- zero work separately, per the original plan's note that A8 may produce multiple smaller PRs.
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.
Fix events.lua metamethod assertion
Plan:
.agents/plans/A8-events-suite.mdCloses #169
Goal
Make
events.lua(the metatable/metamethod test file) pass after theA16
_ENVsemantics work unblocked it from line 15. Re-triage post-A16located two distinct bugs in metamethod dispatch that surface together
when a metamethod is declared
function(...).Success criteria
mix testpasses (1446 → 1467, no regressions; +21 new testsin
test/lua/vm/events_metamethod_vararg_test.exs)test/lua/vm/events_metamethod_vararg_test.exs(covers allvararg-form arithmetic, bitwise, comparison, unary, concat, and
equality metamethods, plus the
events.luafactory pattern)events.luapasses end-to-end — partially: advances fromline 15 (pre-A16) to line 156 (post-this-fix). Line 156 is
assert(a // (1/0) == a), blocked on1/0raising instead ofyielding
inf. Tracked separately asA8a.
What changed
lib/lua/vm/executor.ex— refactortry_binary_metamethod,try_unary_metamethod,try_equality_metamethodto delegate tocall_function/3instead of inlining a broken closure invocationthat ignored proto.varargs. Net
-91lines.test/lua/vm/events_metamethod_vararg_test.exs(new) — 21 unittests pinning the calling convention for vararg-form metamethods.
Bugs fixed
1. Vararg metamethods received zero arguments.
try_binary_metamethodand friends invoked Lua closures by hand,placing operands into registers but never populating
proto.varargs.A metamethod declared
function(...)hasparam_count == 0andis_vararg == true, so it reads fromvarargs(which was empty)instead of registers. Operands were silently dropped.
Reduced repro:
2. Binary metamethod lookup didn't consult the second operand.
For
'5' + b(string + metamethod-bearing table), the cond looked up__addon the string metatable, gotnil, and fell straight throughto the default arithmetic path — never checking
b's metatable. Lua5.3 §2.4 requires trying the first operand, then the second.
Discoveries
After the metamethod fix, events.lua advances from line 15 to line
156:
assert(a // (1/0) == a). The expression1/0raises"attempt to divide by zero"instead of producinginfper Lua 5.3§3.4.1. This is a stdlib-level semantics gap distinct from metamethod
dispatch. Tracked as
.agents/plans/A8a-float-div-zero.md.Verification
Out of scope (intentional)
@skipped_teststo@ready_tests— happensin A8a once the file passes end-to-end.