Reject unknown collectgarbage options#265
Conversation
There was a problem hiding this comment.
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
__gcmetamethod onFileHandlemetatables 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.
7624814 to
85ec681
Compare
f77a230 to
c433b37
Compare
c433b37 to
09d125d
Compare
| 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)")); |
There was a problem hiding this comment.
Metatable is static.
Init in static constructor.
There was a problem hiding this comment.
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.
|
Revert or reopen with improving |
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.