Browse Source

change: update Open method to include throwError parameter for error handling

Akeit0 7 months ago
parent
commit
12413f36dc

+ 15 - 3
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, LuaFileOpenMode mode);
+    public IStream? Open(string path, LuaFileOpenMode mode, bool throwError);
     public void Rename(string oldName, string newName);
     public void Rename(string oldName, string newName);
     public void Remove(string path);
     public void Remove(string path);
 }
 }
@@ -82,10 +82,22 @@ public sealed class FileSystem : ILuaFileSystem
         return new(new LuaFileContent(bytes));
         return new(new LuaFileContent(bytes));
     }
     }
 
 
-    public IStream Open(string path, LuaFileOpenMode luaMode)
+    public IStream? Open(string path, LuaFileOpenMode luaMode, bool throwError)
     {
     {
         var (mode, access) = GetFileMode(luaMode);
         var (mode, access) = GetFileMode(luaMode);
-        return new StreamWrapper(File.Open(path, mode, access));
+        try
+        {
+            return new StreamWrapper(File.Open(path, mode, access));
+        }
+        catch (Exception)
+        {
+            if (throwError)
+            {
+                throw;
+            }
+
+            return null;
+        }
     }
     }
 
 
     public void Rename(string oldName, string newName)
     public void Rename(string oldName, string newName)

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

@@ -75,7 +75,7 @@ public sealed class IOLibrary
         }
         }
         else
         else
         {
         {
-            var stream = context.State.FileSystem.Open(arg.ToString()!, LuaFileOpenMode.ReadWriteOpen);
+            var stream = context.State.FileSystem.Open(arg.ToString()!, LuaFileOpenMode.ReadWriteOpen,true)!;
             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)));
@@ -159,7 +159,7 @@ public sealed class IOLibrary
         }
         }
         else
         else
         {
         {
-            var stream = context.State.FileSystem.Open(arg.ToString()!, LuaFileOpenMode.ReadWriteOpen);
+            var stream = context.State.FileSystem.Open(arg.ToString()!, LuaFileOpenMode.ReadWriteOpen,true)!;
             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)));

+ 6 - 1
src/Lua/Standard/Internal/IOHelper.cs

@@ -24,7 +24,12 @@ internal static class IOHelper
 
 
         try
         try
         {
         {
-            var stream = thread.State.FileSystem.Open(fileName, fileMode);
+            var stream = thread.State.FileSystem.Open(fileName, fileMode, throwError);
+            if (stream == null)
+            {
+                return 0;
+            }
+
             thread.Stack.Push(new LuaValue(new FileHandle(stream)));
             thread.Stack.Push(new LuaValue(new FileHandle(stream)));
             return 1;
             return 1;
         }
         }