Browse Source

fix: adjust LuaTable initialization and fix remove method argument validation

Akeit0 5 months ago
parent
commit
a20cc28927
2 changed files with 6 additions and 10 deletions
  1. 1 1
      src/Lua/LuaTable.cs
  2. 5 9
      src/Lua/Standard/TableLibrary.cs

+ 1 - 1
src/Lua/LuaTable.cs

@@ -12,7 +12,7 @@ public sealed class LuaTable : IEnumerable<KeyValuePair<LuaValue, LuaValue>>
 
     public LuaTable(int arrayCapacity, int dictionaryCapacity)
     {
-        array = new LuaValue[Math.Max(arrayCapacity, 8)];
+        array = arrayCapacity>1?new LuaValue[arrayCapacity] :[];
         dictionary = new(dictionaryCapacity);
     }
 

+ 5 - 9
src/Lua/Standard/TableLibrary.cs

@@ -130,18 +130,14 @@ public sealed class TableLibrary
 
         var n = (int)n_arg;
 
-        if (n <= 0 || n > table.GetArraySpan().Length)
+        if ((!context.HasArgument(1) && n == 0) || n == table.GetArraySpan().Length + 1)
         {
-            if (!context.HasArgument(1) && n == 0)
-            {
-                return new(context.Return(LuaValue.Nil));
-            }
-
-            throw new LuaRuntimeException(context.Thread, "bad argument #2 to 'remove' (position out of bounds)");
+            return new(context.Return(LuaValue.Nil));
         }
-        else if (n > table.ArrayLength)
+
+        if (n <= 0 || n > table.GetArraySpan().Length)
         {
-            return new(context.Return(LuaValue.Nil));
+            throw new LuaRuntimeException(context.Thread, "bad argument #2 to 'remove' (position out of bounds)");
         }
 
         return new(context.Return(table.RemoveAt(n)));