Skip to content

Reject unknown collectgarbage options#265

Open
ashtonmeuser wants to merge 2 commits intonuskey8:mainfrom
ashtonmeuser:fix-gc-option
Open

Reject unknown collectgarbage options#265
ashtonmeuser wants to merge 2 commits intonuskey8:mainfrom
ashtonmeuser:fix-gc-option

Conversation

@ashtonmeuser
Copy link
Contributor

Simple fix for failing Test_Lua("tests-lua/errors.lua"). This is not a complete implementation of Lua GC and, similar to existing behaviour, largely ignores the GC option. However, it now throws for unexpected option strings.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the Lua standard library surface to better match Lua’s error behavior by rejecting unknown collectgarbage option strings and adding a __gc metamethod entry for file handles.

Changes:

  • Add validation for collectgarbage(option) to throw on unexpected option strings.
  • Register a __gc metamethod on FileHandle metatables and implement a stub handler that errors when invoked without arguments.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/Lua/Standard/BasicLibrary.cs Validates collectgarbage option strings against a known set and throws on invalid options.
src/Lua/Standard/FileHandle.cs Adds __gc to the file handle metatable and introduces a GcFunction implementation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ashtonmeuser
Copy link
Contributor Author

This is a subset of #273. Focuses on just two failures encountered in errors.lua in the Lua test suite. @akeit0 feel free to close this if you'd prefer #273.

@akeit0 akeit0 self-requested a review March 20, 2026 03:03
var standardIO = globalState.Platform.StandardIO;
LuaValue stdin = new(new FileHandle(standardIO.Input));
var stdinHandle = new FileHandle(standardIO.Input);
((ILuaUserData)stdinHandle).Metatable!["__gc"] = new LuaFunction("stdin.__gc", (context, cancellationToken) => throw new LuaRuntimeException(context.State, "bad argument #1 to '__gc' (no value)"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Metatable is static.
Init in static constructor.

Copy link
Collaborator

Choose a reason for hiding this comment

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

print (getmetatable(io.stdin).__gc)
print (getmetatable(io.stdout).__gc)

function: 0x5564dbf8d6e0
function: 0x5564dbf8d6e0

In the first place, since __gc can cause people to mistakenly believe it will be called, it is better not to add it.

@akeit0
Copy link
Collaborator

akeit0 commented Mar 20, 2026

Revert or reopen with improving collectgarbage with KnownCollectGarbageOptions only may be better.

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