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
22 changes: 21 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 =
[
"collect",
"stop",
"restart",
"count",
"step",
"setpause",
"setstepmul",
"setmajorinc",
"isrunning",
"incremental",
"generational"
];

public BasicLibrary()
{
Expand Down Expand Up @@ -83,7 +97,13 @@ 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)");
}

// TODO: Implement Lua-compatible behavior for each collectgarbage option.
}

GC.Collect();
Expand Down
13 changes: 12 additions & 1 deletion src/Lua/Standard/FileHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static FileHandle()
{
fileHandleMetatable = new(0, 1);
fileHandleMetatable[Metamethods.Index] = IndexMetamethod;
fileHandleMetatable["__gc"] = GcFunction;
fileHandleMetatable["__tostring"] = ToStringFunction;
}

Expand Down Expand Up @@ -233,4 +234,14 @@ public async ValueTask Close(CancellationToken cancellationToken)
var file = context.GetArgument<FileHandle>(0);
return new(context.Return($"file ({(file.IsOpen ? file.stream.GetHashCode() : "closed")})"));
});
}

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());
});
}