Browse Source

Add: io.flush

AnnulusGames 1 year ago
parent
commit
b873e32145
1 changed files with 26 additions and 0 deletions
  1. 26 0
      src/Lua/Standard/IO/FlushFunction.cs

+ 26 - 0
src/Lua/Standard/IO/FlushFunction.cs

@@ -0,0 +1,26 @@
+namespace Lua.Standard.IO;
+
+public sealed class FlushFunction : LuaFunction
+{
+    public override string Name => "flush";
+    public static readonly FlushFunction Instance = new();
+
+    protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
+    {
+        var file = context.State.Environment["io"].Read<LuaTable>()["stdout"].Read<FileHandle>();
+
+        try
+        {
+            file.Flush();
+            buffer.Span[0] = true;
+            return new(1);
+        }
+        catch (IOException ex)
+        {
+            buffer.Span[0] = LuaValue.Nil;
+            buffer.Span[1] = ex.Message;
+            buffer.Span[2] = ex.HResult;
+            return new(3);
+        }
+    }
+}