From 6314b329f5adf942ae9c452f56b017ef34a2322a Mon Sep 17 00:00:00 2001 From: Ashton Meuser Date: Sun, 15 Mar 2026 08:31:12 -0700 Subject: [PATCH 1/2] Reject unknown collectgarbage options --- src/Lua/Standard/BasicLibrary.cs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Lua/Standard/BasicLibrary.cs b/src/Lua/Standard/BasicLibrary.cs index f1bbab98..563b4e22 100644 --- a/src/Lua/Standard/BasicLibrary.cs +++ b/src/Lua/Standard/BasicLibrary.cs @@ -9,6 +9,20 @@ namespace Lua.Standard; public sealed class BasicLibrary { public static readonly BasicLibrary Instance = new(); + static readonly HashSet KnownCollectGarbageOptions = + [ + "collect", + "stop", + "restart", + "count", + "step", + "setpause", + "setstepmul", + "setmajorinc", + "isrunning", + "incremental", + "generational" + ]; public BasicLibrary() { @@ -83,7 +97,13 @@ public ValueTask CollectGarbage(LuaFunctionExecutionContext context, Cancel { if (context.HasArgument(0)) { - context.GetArgument(0); + var option = context.GetArgument(0); + if (!KnownCollectGarbageOptions.Contains(option)) + { + throw new LuaRuntimeException(context.State, "bad argument #1 to 'collectgarbage' (invalid option)"); + } + + // TODO: Implement Lua-compatible behavior for each collectgarbage option. } GC.Collect(); From f459bcb46f3edff6bf6d02b8b7e2ae087e43cde7 Mon Sep 17 00:00:00 2001 From: Ashton Meuser Date: Sun, 15 Mar 2026 09:33:48 -0700 Subject: [PATCH 2/2] Simple file handle __gc function --- src/Lua/Standard/FileHandle.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Lua/Standard/FileHandle.cs b/src/Lua/Standard/FileHandle.cs index c546a4aa..3870ce36 100644 --- a/src/Lua/Standard/FileHandle.cs +++ b/src/Lua/Standard/FileHandle.cs @@ -48,6 +48,7 @@ static FileHandle() { fileHandleMetatable = new(0, 1); fileHandleMetatable[Metamethods.Index] = IndexMetamethod; + fileHandleMetatable["__gc"] = GcFunction; fileHandleMetatable["__tostring"] = ToStringFunction; } @@ -233,4 +234,14 @@ public async ValueTask Close(CancellationToken cancellationToken) var file = context.GetArgument(0); return new(context.Return($"file ({(file.IsOpen ? file.stream.GetHashCode() : "closed")})")); }); -} \ No newline at end of file + + static readonly LuaFunction GcFunction = new("file.__gc", (context, cancellationToken) => + { + if (!context.HasArgument(0)) + { + throw new LuaRuntimeException(context.State, "bad argument #1 to '__gc' (no value)"); + } + + return new(context.Return()); + }); +}