Browse Source

Fix: table.remove

AnnulusGames 1 year ago
parent
commit
5c8f11017b
1 changed files with 12 additions and 1 deletions
  1. 12 1
      src/Lua/Standard/Table/RemoveFunction.cs

+ 12 - 1
src/Lua/Standard/Table/RemoveFunction.cs

@@ -16,10 +16,21 @@ public sealed class RemoveFunction : LuaFunction
 
         var n = (int)n_arg;
 
-        if (n <= 0 || n > table.ArrayLength)
+        if (n <= 0 || n > table.GetArraySpan().Length)
         {
+            if (!context.HasArgument(1) && n == 0)
+            {
+                buffer.Span[0] = LuaValue.Nil;
+                return new(1);
+            }
+            
             throw new LuaRuntimeException(context.State.GetTraceback(), "bad argument #2 to 'remove' (position out of bounds)");
         }
+        else if (n > table.ArrayLength)
+        {
+            buffer.Span[0] = LuaValue.Nil;
+            return new(1);
+        }
 
         buffer.Span[0] = table.RemoveAt(n);
         return new(1);