Browse Source

fix: update LuaFileContentType handling and improve ReadAllAsync method

Akeit0 6 months ago
parent
commit
2972fb9790
2 changed files with 7 additions and 9 deletions
  1. 4 5
      src/Lua/IO/LuaChunkStream.cs
  2. 3 4
      src/Lua/LuaFileContent.cs

+ 4 - 5
src/Lua/IO/LuaChunkStream.cs

@@ -57,16 +57,15 @@ public sealed class LuaChunkStream : ILuaIOStream
     }
 
     public LuaFileOpenMode Mode => LuaFileOpenMode.Read;
-    public LuaFileContentType ContentType => LuaFileContentType.Unknown;
+    public LuaFileContentType ContentType =>  
+        bytes.Span.StartsWith(LuaCompiler.LuaByteCodeSignature) ? LuaFileContentType.Binary : LuaFileContentType.Text;
 
     public ValueTask<LuaFileContent> ReadAllAsync(CancellationToken cancellationToken)
     {
         var span = bytes.Span;
-        if (span.StartsWith(LuaCompiler.LuaByteCodeSignature))
+        if (ContentType == LuaFileContentType.Binary)
         {
-            var array = ArrayPool<byte>.Shared.Rent(span.Length);
-            bytesToReturnToPool = array;
-            return new(new LuaFileContent(array.AsMemory(span.Length)));
+            return new(new LuaFileContent(bytes));
         }
         else
         {

+ 3 - 4
src/Lua/LuaFileContent.cs

@@ -4,7 +4,6 @@ namespace Lua;
 
 public enum LuaFileContentType
 {
-    Unknown = 0,
     Text,
     Binary
 }
@@ -24,20 +23,20 @@ public readonly struct LuaFileContent
 
     public LuaFileContent(ReadOnlyMemory<byte> memory)
     {
-        type = LuaFileContentType.Text;
+        type = LuaFileContentType.Binary;
         referenceValue = new BinaryData(memory);
     }
 
     public LuaFileContent(IBinaryData data)
     {
         type = LuaFileContentType.Binary;
-        referenceValue = data;
+        referenceValue = data ?? throw new ArgumentNullException(nameof(data), "Binary data cannot be null.");
     }
 
     public LuaFileContent(string text)
     {
         type = LuaFileContentType.Text;
-        referenceValue = text;
+        referenceValue = text ?? throw new ArgumentNullException(nameof(text), "Text cannot be null.");
     }
 
     public ReadOnlyMemory<char> ReadText()