Browse Source

Update: FileHandle

AnnulusGames 1 year ago
parent
commit
b687c07b0c

+ 1 - 1
src/Lua/Standard/IO/CloseFunction.cs

@@ -11,7 +11,7 @@ public sealed class CloseFunction : LuaFunction
 
         try
         {
-            file.Stream.Close();
+            file.Close();
             buffer.Span[0] = true;
             return new(1);
         }

+ 1 - 1
src/Lua/Standard/IO/FileFlushFunction.cs

@@ -11,7 +11,7 @@ public sealed class FileFlushFunction : LuaFunction
 
         try
         {
-            file.Stream.Flush();
+            file.Flush();
             buffer.Span[0] = true;
             return new(1);
         }

+ 36 - 4
src/Lua/Standard/IO/FileHandle.cs

@@ -31,8 +31,8 @@ public class FileHandle : LuaUserData
         }
     }
 
-    public Stream Stream { get; }
-    public StreamReader? Reader { get; }
+    Stream stream;
+    StreamReader? reader;
 
     static readonly LuaTable fileHandleMetatable;
 
@@ -44,8 +44,40 @@ public class FileHandle : LuaUserData
 
     public FileHandle(Stream stream)
     {
-        Stream = stream;
-        if (stream.CanRead) Reader = new StreamReader(stream);
+        this.stream = stream;
+        if (stream.CanRead) reader = new StreamReader(stream);
         Metatable = fileHandleMetatable;
     }
+
+    public string? ReadLine()
+    {
+        return reader!.ReadLine();
+    }
+
+    public string ReadToEnd()
+    {
+        return reader!.ReadToEnd();
+    }
+
+    public void Write(ReadOnlySpan<byte> buffer)
+    {
+        stream.Write(buffer);
+    }
+
+    public void Flush()
+    {
+        stream.Flush();
+    }
+
+    public void Close()
+    {
+        if (reader != null)
+        {
+            reader.Dispose();
+        }
+        else
+        {
+            stream.Close();
+        }
+    }
 }

+ 6 - 8
src/Lua/Standard/IO/IOHelper.cs

@@ -19,7 +19,7 @@ internal static class IOHelper
                 {
                     using var fileBuffer = new PooledArray<byte>(str.Length * 3);
                     var bytesWritten = Encoding.UTF8.GetBytes(str, fileBuffer.AsSpan());
-                    file.Stream.Write(fileBuffer.AsSpan()[..bytesWritten]);
+                    file.Write(fileBuffer.AsSpan()[..bytesWritten]);
                 }
                 else if (arg.TryRead<double>(out var d))
                 {
@@ -28,7 +28,7 @@ internal static class IOHelper
                     {
                         throw new ArgumentException("Destination is too short.");
                     }
-                    file.Stream.Write(fileBuffer.AsSpan()[..bytesWritten]);
+                    file.Write(fileBuffer.AsSpan()[..bytesWritten]);
                 }
                 else
                 {
@@ -59,8 +59,6 @@ internal static class IOHelper
 
         try
         {
-            var reader = file.Reader!;
-
             for (int i = 0; i < formats.Length; i++)
             {
                 var format = formats[i];
@@ -74,15 +72,15 @@ internal static class IOHelper
                             throw new NotImplementedException();
                         case "*a":
                         case "*all":
-                            buffer.Span[i] = reader.ReadToEnd();
+                            buffer.Span[i] = file.ReadToEnd();
                             break;
                         case "*l":
                         case "*line":
-                            buffer.Span[i] = reader.ReadLine() ?? LuaValue.Nil;
+                            buffer.Span[i] = file.ReadLine() ?? LuaValue.Nil;
                             break;
                         case "L":
                         case "*L":
-                            var text = reader.ReadLine();
+                            var text = file.ReadLine();
                             buffer.Span[i] = text == null ? LuaValue.Nil : text + Environment.NewLine;
                             break;
                     }
@@ -93,7 +91,7 @@ internal static class IOHelper
                     {
                         throw new LuaRuntimeException(context.State.GetTraceback(), $"bad argument #{i + 1} to 'read' (number has no integer representation)");
                     }
-                    
+
                     // TODO:
 
                 }