Call NotifyTop after pushing the table length onto the stack#331
Open
Dargones wants to merge 2 commits into
Open
Call NotifyTop after pushing the table length onto the stack#331Dargones wants to merge 2 commits into
Dargones wants to merge 2 commits into
Conversation
…fter computing the length of a table
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a Lua VM stack-top bookkeeping bug when computing the length (#) of a table via the unary-operation/metamethod execution path, which could leave a stale value on the stack and affect subsequent calls. It adds a focused regression test to reproduce the original assertion failure.
Changes:
- Update table-length handling to call
stack.NotifyTop(...)after writing the result into the destination register. - Add a Lua regression program that fails if a stale stack value is incorrectly treated as an argument.
- Include the new Lua test file in the existing Lua test suite runner.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Lua/Runtime/LuaVirtualMachine.cs | Ensures stack top is updated after pushing table length result, preventing stale stack values from leaking across calls. |
| tests/Lua.Tests/tests-lua/table-length.lua | Adds a minimal repro verifying f2() receives nil for missing args after a #table return. |
| tests/Lua.Tests/LuaTests.cs | Registers the new Lua repro file as part of the NUnit test matrix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
akeit0
reviewed
Jul 22, 2026
| [TestCase("tests-lua/coroutine.lua")] | ||
| [TestCase("tests-lua/db.lua")] | ||
| [TestCase("tests-lua/verybig.lua")] | ||
| [TestCase("tests-lua/table-length.lua")] |
Collaborator
There was a problem hiding this comment.
Although not explicitly stated, LuaTests (tests-lua) is a port of the official Lua tests; therefore, if possible, please write dedicated test in the appropriate classes.
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.
This PR fixes an issue that causes an assertion failure when executing the program added as a test case. The program executes successfully in other Lua implementations, e.g., https://onecompiler.com/lua and https://www.tutorialspoint.com/compilers/online-lua-compiler.htm.
Based on my understanding, the issue was that the stack top was not updated after computing the length of a table (unlike when computing the length of a string). Because of this, the computed value was not cleared on function return. This left a stale value on the stack, causing the subsequent call in the added test case to receive an unexpected argument and fail its assertion.
Thank you!