Browse Source

refactor: simplify FileHandle by removing reader/writer and using IStream directly

Akeit0 7 months ago
parent
commit
f25d1aa670
2 changed files with 48 additions and 67 deletions
  1. 37 42
      src/Lua/IO/ILuaFileSystem.cs
  2. 11 25
      src/Lua/Standard/FileHandle.cs

+ 37 - 42
src/Lua/IO/ILuaFileSystem.cs

@@ -13,8 +13,13 @@ public interface ILuaFileSystem
 
 
 public interface IStream : IDisposable
 public interface IStream : IDisposable
 {
 {
-    public IStreamReader? Reader { get; }
-    public IStreamWriter? Writer { get; }
+    public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken);
+    public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken);
+    public ValueTask<string?> ReadStringAsync(int count, CancellationToken cancellationToken);
+
+    public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken);
+    public ValueTask FlushAsync(CancellationToken cancellationToken);
+    public void SetVBuf(string mode, int size);
 
 
     public long Seek(long offset, SeekOrigin origin);
     public long Seek(long offset, SeekOrigin origin);
 
 
@@ -29,9 +34,6 @@ public interface IStream : IDisposable
 
 
 public interface IStreamReader : IDisposable
 public interface IStreamReader : IDisposable
 {
 {
-    public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken);
-    public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken);
-    public ValueTask<string?> ReadStringAsync(int count,CancellationToken cancellationToken);
 }
 }
 
 
 public interface IStreamWriter : IDisposable
 public interface IStreamWriter : IDisposable
@@ -84,24 +86,33 @@ public sealed class FileSystem : ILuaFileSystem
     }
     }
 }
 }
 
 
-public sealed class StreamReaderWrapper(StreamReader streamReader) : IStreamReader
+public sealed class StreamWrapper(Stream innerStream) : IStream
 {
 {
+    StreamReader? reader;
+    StreamWriter? writer;
+
     public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
     public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
     {
     {
-        return new(streamReader.ReadLine());
+        reader ??= new(innerStream);
+
+        return new(reader.ReadLine());
     }
     }
 
 
     public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken)
     public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken)
     {
     {
-        return new(streamReader.ReadToEnd());
+        reader ??= new(innerStream);
+
+        return new(reader.ReadToEnd());
     }
     }
 
 
-    public ValueTask<string?> ReadStringAsync(int count,CancellationToken cancellationToken)
+    public ValueTask<string?> ReadStringAsync(int count, CancellationToken cancellationToken)
     {
     {
+        reader ??= new(innerStream);
+
         using var byteBuffer = new PooledArray<char>(count);
         using var byteBuffer = new PooledArray<char>(count);
         var span = byteBuffer.AsSpan();
         var span = byteBuffer.AsSpan();
-        var ret=streamReader.Read(span);
-        if(ret != span.Length)
+        var ret = reader.Read(span);
+        if (ret != span.Length)
         {
         {
             return new(default(string));
             return new(default(string));
         }
         }
@@ -109,57 +120,41 @@ public sealed class StreamReaderWrapper(StreamReader streamReader) : IStreamRead
         return new(span.ToString());
         return new(span.ToString());
     }
     }
 
 
-    public void Dispose()
-    {
-        streamReader.Dispose();
-    }
-}
-
-public sealed class StreamWriterWrapper(StreamWriter streamWriter) : IStreamWriter
-{
     public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
     public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
     {
     {
-        streamWriter.Write(buffer.Span);
+        writer ??= new(innerStream);
+
+        writer.Write(buffer.Span);
         return new();
         return new();
     }
     }
 
 
     public ValueTask FlushAsync(CancellationToken cancellationToken)
     public ValueTask FlushAsync(CancellationToken cancellationToken)
     {
     {
-        streamWriter.Flush();
+        innerStream.Flush();
         return new();
         return new();
     }
     }
 
 
     public void SetVBuf(string mode, int size)
     public void SetVBuf(string mode, int size)
     {
     {
+        writer ??= new(innerStream);
         // Ignore size parameter
         // Ignore size parameter
-        streamWriter.AutoFlush = mode is "no" or "line";
+        writer.AutoFlush = mode is "no" or "line";
     }
     }
 
 
-    public void Dispose()
-    {
-        streamWriter.Dispose();
-    }
-}
-
-public sealed class StreamWrapper(Stream fileStream) : IStream
-{
-    public IStreamReader? Reader => fileStream.CanRead ? new StreamReaderWrapper(new(fileStream)) : null;
-    public IStreamWriter? Writer => fileStream.CanWrite ? new StreamWriterWrapper(new(fileStream)) : null;
-
-    public long Seek(long offset, SeekOrigin origin) => fileStream.Seek(offset, origin);
+    public long Seek(long offset, SeekOrigin origin) => innerStream.Seek(offset, origin);
 
 
-    public void SetLength(long value) => fileStream.SetLength(value);
+    public void SetLength(long value) => innerStream.SetLength(value);
 
 
-    public bool CanRead => fileStream.CanRead;
-    public bool CanSeek => fileStream.CanSeek;
-    public bool CanWrite => fileStream.CanWrite;
-    public long Length => fileStream.Length;
+    public bool CanRead => innerStream.CanRead;
+    public bool CanSeek => innerStream.CanSeek;
+    public bool CanWrite => innerStream.CanWrite;
+    public long Length => innerStream.Length;
 
 
     public long Position
     public long Position
     {
     {
-        get => fileStream.Position;
-        set => fileStream.Position = value;
+        get => innerStream.Position;
+        set => innerStream.Position = value;
     }
     }
 
 
-    public void Dispose() => fileStream.Dispose();
+    public void Dispose() => innerStream.Dispose();
 }
 }

+ 11 - 25
src/Lua/Standard/FileHandle.cs

@@ -17,7 +17,7 @@ public class FileHandle : ILuaUserData
         {
         {
             return new(context.Return(name switch
             return new(context.Return(name switch
             {
             {
-                "close" => CloseFunction!,
+                "close" => CloseFunction,
                 "flush" => FlushFunction!,
                 "flush" => FlushFunction!,
                 "lines" => LinesFunction!,
                 "lines" => LinesFunction!,
                 "read" => ReadFunction!,
                 "read" => ReadFunction!,
@@ -34,8 +34,6 @@ public class FileHandle : ILuaUserData
     });
     });
 
 
     IStream stream;
     IStream stream;
-    IStreamWriter? writer;
-    IStreamReader? reader;
     bool isClosed;
     bool isClosed;
 
 
     public bool IsClosed => Volatile.Read(ref isClosed);
     public bool IsClosed => Volatile.Read(ref isClosed);
@@ -46,7 +44,7 @@ public class FileHandle : ILuaUserData
 
 
     static FileHandle()
     static FileHandle()
     {
     {
-        fileHandleMetatable = new LuaTable();
+        fileHandleMetatable = new LuaTable(0,1);
         fileHandleMetatable[Metamethods.Index] = IndexMetamethod;
         fileHandleMetatable[Metamethods.Index] = IndexMetamethod;
     }
     }
 
 
@@ -55,28 +53,26 @@ public class FileHandle : ILuaUserData
     public FileHandle(IStream stream)
     public FileHandle(IStream stream)
     {
     {
         this.stream = stream;
         this.stream = stream;
-        if (stream.CanRead) reader = stream.Reader;
-        if (stream.CanWrite) writer = stream.Writer;
     }
     }
 
 
     public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
     public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken)
     {
     {
-        return reader!.ReadLineAsync(cancellationToken);
+        return stream.ReadLineAsync(cancellationToken);
     }
     }
 
 
     public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken)
     public ValueTask<string> ReadToEndAsync(CancellationToken cancellationToken)
     {
     {
-        return reader!.ReadToEndAsync(cancellationToken);
+        return stream.ReadToEndAsync(cancellationToken);
     }
     }
 
 
     public ValueTask<string?> ReadStringAsync(int count,CancellationToken cancellationToken)
     public ValueTask<string?> ReadStringAsync(int count,CancellationToken cancellationToken)
     {
     {
-        return reader!.ReadStringAsync(count,cancellationToken);
+        return stream.ReadStringAsync(count,cancellationToken);
     }
     }
 
 
     public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
     public ValueTask WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken)
     {
     {
-        return writer!.WriteAsync(buffer, cancellationToken);
+        return stream.WriteAsync(buffer, cancellationToken);
     }
     }
 
 
     public long Seek(string whence, long offset) =>
     public long Seek(string whence, long offset) =>
@@ -90,12 +86,12 @@ public class FileHandle : ILuaUserData
 
 
     public ValueTask FlushAsync(CancellationToken cancellationToken)
     public ValueTask FlushAsync(CancellationToken cancellationToken)
     {
     {
-        return writer!.FlushAsync(cancellationToken);
+        return stream.FlushAsync(cancellationToken);
     }
     }
 
 
     public void SetVBuf(string mode, int size)
     public void SetVBuf(string mode, int size)
     {
     {
-        writer!.SetVBuf(mode, size);
+        stream.SetVBuf(mode, size);
     }
     }
 
 
     public void Close()
     public void Close()
@@ -103,22 +99,12 @@ public class FileHandle : ILuaUserData
         if (isClosed) throw new ObjectDisposedException(nameof(FileHandle));
         if (isClosed) throw new ObjectDisposedException(nameof(FileHandle));
         Volatile.Write(ref isClosed, true);
         Volatile.Write(ref isClosed, true);
 
 
-        if (reader != null)
-        {
-            reader.Dispose();
-        }
-        else if (writer != null)
-        {
-            writer.Dispose();
-        }
-        else
-        {
+        
+        
             stream.Dispose();
             stream.Dispose();
-        }
+        
 
 
         stream = null!;
         stream = null!;
-        writer = null;
-        reader = null;
     }
     }
 
 
     static readonly LuaFunction CloseFunction = new("close", (context, cancellationToken) =>
     static readonly LuaFunction CloseFunction = new("close", (context, cancellationToken) =>