Skip to content

Call NotifyTop after pushing the table length onto the stack#331

Open
Dargones wants to merge 2 commits into
nuskey8:mainfrom
Dargones:temp/afedchin/stack-top
Open

Call NotifyTop after pushing the table length onto the stack#331
Dargones wants to merge 2 commits into
nuskey8:mainfrom
Dargones:temp/afedchin/stack-top

Conversation

@Dargones

Copy link
Copy Markdown

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!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

[TestCase("tests-lua/coroutine.lua")]
[TestCase("tests-lua/db.lua")]
[TestCase("tests-lua/verybig.lua")]
[TestCase("tests-lua/table-length.lua")]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

3 participants