Repository navigation
fix(vm): align table key semantics with Lua 5.3 §3.4.11 - #200
Merged
Merged
Conversation
Three closely-related table-semantics bugs in the VM and stdlib:
* Assigning `nil` to a key did not delete the key, so `#{nil}` returned
1, `t[k] = nil` left the entry visible to `pairs`, and `next(t)`
returned a key whose value was nil.
* Float keys with an exact integer value lived in a different slot from
their integer counterpart, so `t[2^0] = v` followed by `t[1]` read
nothing.
* `pairs()` and `ipairs()` with no argument or a non-table argument
crashed the BEAM with a `FunctionClauseError` instead of raising a
Lua-level "bad argument" error.
A new `Lua.VM.Table.put_data/3` helper centralizes the write-as-delete
and integer-coercion rules, with matching `get_data/2` and `has_data?/2`
read helpers. Every table-data mutation in `executor.ex`, `state.ex`,
and `stdlib.ex` (rawset, rawget, next, set_global) now flows through
those helpers, so the contract holds uniformly across the four write
paths (set_table, set_field, set_list, table_newindex) and rawset.
Eighteen new unit tests in `test/lua/vm/nextvar_semantics_test.exs`
pin the §3.4.11 rules on small focused inputs so any future regression
points at the specific semantic that broke.
This is the first half of plan A7. The second half — dead-key tracking
so `next(t, k)` survives `t[k] = nil` mid-iteration, plus the strict
"invalid key" raise that depends on it — is split out to plan A7a.
With dead keys in place, `nextvar.lua` will pass end-to-end and can be
promoted to the ready set in `lua53_suite_test.exs`.
Plan: A7
Refs #168
A7 ships PR #200 with three of four nextvar.lua root causes fixed. The remaining gap (dead-key tracking so `next(t, k)` survives `t[k] = nil` mid-iteration) is captured as plan A7a so `nextvar.lua` can be promoted to the ready set there.
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 nextvar.lua "concatenate nil" failure (partial)
Plan:
.agents/plans/A7-nextvar-suite.mdRefs #168
This is the first half of plan A7. The second half — dead-key tracking
so
next(t, k)survivest[k] = nilmid-iteration, plus the strict"invalid key to 'next'" raise that depends on it — is split out to
plan A7a so this PR can land cleanly without regressing iteration.
Goal
Make
nextvar.luapass. The original "concatenate nil" symptom wasalready gone after A1 landed; bisecting line by line surfaced four
distinct Lua 5.3 semantics gaps in the VM and stdlib, three of which
are fixed here.
Success criteria
mix testpasses (1402 → 1420, no regressions; +18 new tests).test/lua/vm/nextvar_semantics_test.exs.nextvar.luapasses end-to-end. Partial — three of four rootcauses fixed. The fourth (dead-key tracking for iterate-then-clear)
requires a table-representation change and is deferred to plan A7a.
Changes
A new
Lua.VM.Table.put_data/3helper centralizes the write-as-deleteand integer-coercion rules, with matching
get_data/2andhas_data?/2read helpers. Every table-data mutation now flows through them.
Discoveries (the four root causes)
Assigning
nilto a table key did not delete the key.set_table,set_field,set_list,table_newindex, andrawsetall calledMap.put(data, key, nil)instead of removing the entry. This violatedLua 5.3 §3.4.11 — fields with
nilvalues are absent from the table.Consequences:
#{nil} == 1(should be 0),for k in pairs(t) do t[k] = nil endleft every key visible, and
next(t)returned keys whose values were nil.Float keys with an exact integer value were stored as floats.
t[2.0] = vandt[2] = vlived in different slots, so the test atnextvar.lua:310(a[#a]afterfor i=0,50 do a[2^i] = true end)read nil because
2^0is1.0. Lua 5.3 §3.4.11 mandates integer-valuedfloats collapse to integers as table keys.
pairsandipairsdid not validate their argument. Calling themwith no argument or with a non-table value crashed the BEAM with a
FunctionClauseErrorinstead of raising the Lua-level "bad argument"error that
nextvar.lua:233-234checks for.next(t, k)does not handle keys that were valid mid-iteration andare now nil. Real Lua keeps removed keys reachable in the hash chain
(the "dead key" trick) so
for k,v in pairs(t) do t[k] = nil endworks. Without that, strict-
nextwould error mid-iteration. Thenexterror path here is left lenient (returnsnil, nil) so thisPR doesn't regress iteration; plan A7a will add dead-key tracking
and the strict raise together.
Verification
Out of scope (intentional)
nextvar.lua.nextduring iteration-with-deletion (A7a).nextvar.luafrom@skipped_teststo@ready_tests(deferred to A7a where the suite file actually passes end-to-end).
Table.invalid_key?/1is added in thisPR for the eventual NaN-key error path; A7a is the natural place to
start using it.