Ver Fonte

refactor: update file opening methods to use LuaFileMode and simplify access handling

Akeit0 há 7 meses atrás
pai
commit
3ef61e3ae6

+ 17 - 2
src/Lua/IO/ILuaFileSystem.cs

@@ -6,7 +6,7 @@ public interface ILuaFileSystem
 {
 {
     public bool IsReadable(string path);
     public bool IsReadable(string path);
     public ValueTask<LuaFileContent> ReadFileContentAsync(string path, CancellationToken cancellationToken);
     public ValueTask<LuaFileContent> ReadFileContentAsync(string path, CancellationToken cancellationToken);
-    public IStream Open(string path, FileMode mode, FileAccess access);
+    public IStream Open(string path, LuaFileMode mode);
     public void Rename(string oldName, string newName);
     public void Rename(string oldName, string newName);
     public void Remove(string path);
     public void Remove(string path);
 }
 }
@@ -48,6 +48,20 @@ public sealed class FileSystem : ILuaFileSystem
 {
 {
     public static readonly FileSystem Instance = new();
     public static readonly FileSystem Instance = new();
 
 
+    public static (FileMode, FileAccess access) GetFileMode(LuaFileMode luaFileMode)
+    {
+        return luaFileMode switch
+        {
+            LuaFileMode.Read => (FileMode.Open, FileAccess.Read),
+            LuaFileMode.Write => (FileMode.Create, FileAccess.Write),
+            LuaFileMode.Append => (FileMode.Append, FileAccess.Write),
+            LuaFileMode.ReadWriteOpen => (FileMode.Open, FileAccess.ReadWrite),
+            LuaFileMode.ReadWriteCreate => (FileMode.Create, FileAccess.ReadWrite),
+            LuaFileMode.ReadAppend => (FileMode.Append, FileAccess.ReadWrite),
+            _ => throw new ArgumentOutOfRangeException(nameof(luaFileMode), luaFileMode, null)
+        };
+    }
+
     public bool IsReadable(string path)
     public bool IsReadable(string path)
     {
     {
         if (!File.Exists(path)) return false;
         if (!File.Exists(path)) return false;
@@ -68,8 +82,9 @@ public sealed class FileSystem : ILuaFileSystem
         return new(new LuaFileContent(bytes));
         return new(new LuaFileContent(bytes));
     }
     }
 
 
-    public IStream Open(string path, FileMode mode, FileAccess access)
+    public IStream Open(string path, LuaFileMode luaMode)
     {
     {
+        var (mode, access) = GetFileMode(luaMode);
         return new StreamWrapper(File.Open(path, mode, access));
         return new StreamWrapper(File.Open(path, mode, access));
     }
     }
 
 

+ 35 - 0
src/Lua/IO/LuaFileMode.cs

@@ -0,0 +1,35 @@
+namespace Lua.IO
+{
+    public enum LuaFileMode
+    {
+        /// <summary>
+        /// r
+        /// </summary>
+        Read,
+
+        /// <summary>
+        /// w
+        /// </summary>
+        Write,
+
+        /// <summary>
+        /// a
+        /// </summary>
+        Append,
+
+        /// <summary>
+        /// r+
+        /// </summary>
+        ReadWriteOpen,
+
+        /// <summary>
+        /// w+
+        /// </summary>
+        ReadWriteCreate,
+
+        /// <summary>
+        /// a+
+        /// </summary>
+        ReadAppend,
+    }
+}

+ 1 - 1
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!,

+ 3 - 2
src/Lua/Standard/IOLibrary.cs

@@ -1,3 +1,4 @@
+using Lua.IO;
 using Lua.Runtime;
 using Lua.Runtime;
 using Lua.Standard.Internal;
 using Lua.Standard.Internal;
 
 
@@ -74,7 +75,7 @@ public sealed class IOLibrary
         }
         }
         else
         else
         {
         {
-            var stream = context.State.FileSystem.Open(arg.ToString()!, FileMode.Open, FileAccess.ReadWrite);
+            var stream = context.State.FileSystem.Open(arg.ToString()!, LuaFileMode.ReadWriteOpen);
             var handle = new FileHandle(stream);
             var handle = new FileHandle(stream);
             registry["stdin"] = new(handle);
             registry["stdin"] = new(handle);
             return new(context.Return(new LuaValue(handle)));
             return new(context.Return(new LuaValue(handle)));
@@ -158,7 +159,7 @@ public sealed class IOLibrary
         }
         }
         else
         else
         {
         {
-            var stream = context.State.FileSystem.Open(arg.ToString()!, FileMode.Open, FileAccess.ReadWrite);
+            var stream = context.State.FileSystem.Open(arg.ToString()!, LuaFileMode.ReadWriteOpen);
             var handle = new FileHandle(stream);
             var handle = new FileHandle(stream);
             io["stdout"] = new(handle);
             io["stdout"] = new(handle);
             return new(context.Return(new LuaValue(handle)));
             return new(context.Return(new LuaValue(handle)));

+ 10 - 10
src/Lua/Standard/Internal/IOHelper.cs

@@ -1,5 +1,6 @@
 using System.Text;
 using System.Text;
 using Lua.Internal;
 using Lua.Internal;
+using Lua.IO;
 
 
 namespace Lua.Standard.Internal;
 namespace Lua.Standard.Internal;
 
 
@@ -9,22 +10,21 @@ internal static class IOHelper
     {
     {
         var fileMode = mode switch
         var fileMode = mode switch
         {
         {
-            "r" or "rb" or "r+" or "r+b" => FileMode.Open,
-            "w" or "wb" or "w+" or "w+b" => FileMode.Create,
-            "a" or "ab" or "a+" or "a+b" => FileMode.Append,
+            "r" or "rb" => LuaFileMode.Read,
+            "w" or "wb" => LuaFileMode.Write,
+            "a" or "ab" => LuaFileMode.Append,
+            "r+" or "rb+" => LuaFileMode.ReadWriteOpen,
+            "w+" or "wb+" => LuaFileMode.ReadWriteCreate,
+            "a+" or "ab+" => LuaFileMode.ReadAppend,
             _ => throw new LuaRuntimeException(thread, "bad argument #2 to 'open' (invalid mode)"),
             _ => throw new LuaRuntimeException(thread, "bad argument #2 to 'open' (invalid mode)"),
         };
         };
 
 
-        var fileAccess = mode switch
-        {
-            "r" or "rb" => FileAccess.Read,
-            "w" or "wb" or "a" or "ab" => FileAccess.Write,
-            _ => FileAccess.ReadWrite,
-        };
+        var binary = mode.Contains("b");
+        if (binary) throw new LuaRuntimeException(thread, "binary mode is not supported");
 
 
         try
         try
         {
         {
-            var stream = thread.State.FileSystem.Open(fileName, fileMode, fileAccess);
+            var stream = thread.State.FileSystem.Open(fileName, fileMode);
             thread.Stack.Push(new LuaValue(new FileHandle(stream)));
             thread.Stack.Push(new LuaValue(new FileHandle(stream)));
             return 1;
             return 1;
         }
         }