Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Lua/Standard/BasicLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ namespace Lua.Standard;
public sealed class BasicLibrary
{
public static readonly BasicLibrary Instance = new();
static readonly HashSet<string> KnownCollectGarbageOptions = new(StringComparer.Ordinal)
{
"collect",
"stop",
"restart",
"count",
"step",
"setpause",
"setstepmul",
"setmajorinc",
"isrunning",
"incremental",
"generational"
};

public BasicLibrary()
{
Expand Down Expand Up @@ -83,7 +97,11 @@ public ValueTask<int> CollectGarbage(LuaFunctionExecutionContext context, Cancel
{
if (context.HasArgument(0))
{
context.GetArgument<string>(0);
var option = context.GetArgument<string>(0);
if (!KnownCollectGarbageOptions.Contains(option))
{
throw new LuaRuntimeException(context.State, $"bad argument #1 to 'collectgarbage' (invalid option '{option}')");
}
}

GC.Collect();
Expand Down
4 changes: 3 additions & 1 deletion src/Lua/Standard/OpenLibsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public static void OpenIOLibrary(this LuaState state)

var registry = globalState.Registry;
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.

LuaValue stdin = new(stdinHandle);
LuaValue stdout = new(new FileHandle(standardIO.Output));
LuaValue stderr = new(new FileHandle(standardIO.Error));
registry["_IO_input"] = stdin;
Expand Down