Skip to content

fix(vm): pass operands through varargs to metamethod closures - #203

Merged
davydog187 merged 4 commits into
mainfrom
fix/events-suite
May 7, 2026
Merged

davydog187 merged 4 commits into
mainfrom
fix/events-suite

Conversation

@davydog187

Copy link
Copy Markdown
Contributor

Fix events.lua metamethod assertion

Plan: .agents/plans/A8-events-suite.md
Closes #169

Goal

Make events.lua (the metatable/metamethod test file) pass after the
A16 _ENV semantics work unblocked it from line 15. Re-triage post-A16
located two distinct bugs in metamethod dispatch that surface together
when a metamethod is declared function(...).

Success criteria

  • mix test passes (1446 → 1467, no regressions; +21 new tests
    in test/lua/vm/events_metamethod_vararg_test.exs)
  • Each fixed assertion has a unit test in
    test/lua/vm/events_metamethod_vararg_test.exs (covers all
    vararg-form arithmetic, bitwise, comparison, unary, concat, and
    equality metamethods, plus the events.lua factory pattern)
  • events.lua passes end-to-end — partially: advances from
    line 15 (pre-A16) to line 156 (post-this-fix). Line 156 is
    assert(a // (1/0) == a), blocked on 1/0 raising instead of
    yielding inf. Tracked separately as
    A8a.

What changed

  • lib/lua/vm/executor.ex — refactor try_binary_metamethod,
    try_unary_metamethod, try_equality_metamethod to delegate to
    call_function/3 instead of inlining a broken closure invocation
    that ignored proto.varargs. Net -91 lines.
  • test/lua/vm/events_metamethod_vararg_test.exs (new) — 21 unit
    tests pinning the calling convention for vararg-form metamethods.
 lib/lua/vm/executor.ex                         | 124 ++++-----
 test/lua/vm/events_metamethod_vararg_test.exs  | 378 +++++++++++++++++++++++++

Bugs fixed

1. Vararg metamethods received zero arguments.
try_binary_metamethod and friends invoked Lua closures by hand,
placing operands into registers but never populating proto.varargs.
A metamethod declared function(...) has param_count == 0 and
is_vararg == true, so it reads from varargs (which was empty)
instead of registers. Operands were silently dropped.

Reduced repro:

local t = {__add = function(...) print(select('#', ...)) end}
local b = setmetatable({}, t)
local _ = b + 5
-- expected: prints 2; actual: prints 0

2. Binary metamethod lookup didn't consult the second operand.
For '5' + b (string + metamethod-bearing table), the cond looked up
__add on the string metatable, got nil, and fell straight through
to the default arithmetic path — never checking b's metatable. Lua
5.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 expression 1/0 raises
"attempt to divide by zero" instead of producing inf per 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

mix format                          # clean
mix compile --warnings-as-errors    # clean
mix test                            # 1467 passing, 0 failures, 31 skipped
mix test --only lua53               # 29 tests, 0 failures, 24 skipped
mix test test/lua/vm/metatable_test.exs                    # 24 passing
mix test test/lua/vm/events_metamethod_vararg_test.exs     # 21 passing
mix dialyzer                        # 0 errors

Out of scope (intentional)

  • Adding new metamethods (the suite tests existing ones).
  • Reworking metamethod dispatch architecture (Phase 12 settled this).
  • The float div-by-zero issue blocking events.lua line 156 — see A8a.
  • Moving events.lua from @skipped_tests to @ready_tests — happens
    in A8a once the file passes end-to-end.

davydog187 added 4 commits May 7, 2026 06:23
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.
@davydog187
davydog187 merged commit 85e92a7 into main May 7, 2026
4 checks passed
@davydog187
davydog187 deleted the fix/events-suite branch May 7, 2026 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix events.lua metamethod assertion

1 participant