Browse Source

Fix: remove

AnnulusGames 1 year ago
parent
commit
8f2c2ed38d
3 changed files with 20 additions and 18 deletions
  1. 11 14
      src/Lua/LuaTable.cs
  2. 7 2
      src/Lua/Standard/Table/RemoveFunction.cs
  3. 2 2
      tests/Lua.Tests/TableTests.cs

+ 11 - 14
src/Lua/LuaTable.cs

@@ -133,26 +133,23 @@ public sealed class LuaTable
         return dictionary.ContainsKey(key);
         return dictionary.ContainsKey(key);
     }
     }
 
 
-    public LuaValue Remove(LuaValue key)
+    public LuaValue RemoveAt(int key)
     {
     {
-        if (TryGetInteger(key, out var i) && i > 0 && i <= array.Length)
+        if (key <= 0 || key > array.Length)
         {
         {
-            var index = i - 1;
-            var value = array[index];
+            return LuaValue.Nil;
+        }
 
 
-            if (index < array.Length - 1)
-            {
-                array.AsSpan(index + 1).CopyTo(array.AsSpan(index));
-            }
-            array[^1] = default;
+        var index = key - 1;
+        var value = array[index];
 
 
-            return value;
-        }
-        else
+        if (index < array.Length - 1)
         {
         {
-            dictionary.Remove(key, out var value);
-            return value;
+            array.AsSpan(index + 1).CopyTo(array.AsSpan(index));
         }
         }
+        array[^1] = default;
+
+        return value;
     }
     }
 
 
     public KeyValuePair<LuaValue, LuaValue> GetNext(LuaValue key)
     public KeyValuePair<LuaValue, LuaValue> GetNext(LuaValue key)

+ 7 - 2
src/Lua/Standard/Table/RemoveFunction.cs

@@ -9,10 +9,15 @@ public sealed class RemoveFunction : LuaFunction
     {
     {
         var arg0 = context.ReadArgument<LuaTable>(0);
         var arg0 = context.ReadArgument<LuaTable>(0);
         var arg1 = context.ArgumentCount >= 2
         var arg1 = context.ArgumentCount >= 2
-            ? (int)context.ReadArgument<double>(1)
+            ? context.ReadArgument<double>(1)
             : arg0.ArrayLength;
             : arg0.ArrayLength;
 
 
-        buffer.Span[0] = arg0.Remove(arg1);
+        if (!MathEx.IsInteger(arg1))
+        {
+            throw new LuaRuntimeException(context.State.GetTraceback(), "bad argument #2 to 'remove' (number has no integer representation)");
+        }
+
+        buffer.Span[0] = arg0.RemoveAt((int)arg1);
         return new(1);
         return new(1);
     }
     }
 }
 }

+ 2 - 2
tests/Lua.Tests/TableTests.cs

@@ -30,14 +30,14 @@ public class TableTests
     }
     }
 
 
     [Test]
     [Test]
-    public void Test_Remove()
+    public void Test_RemoveAt()
     {
     {
         var table = new LuaTable();
         var table = new LuaTable();
         table[1] = 1;
         table[1] = 1;
         table[2] = 2;
         table[2] = 2;
         table[3] = 3;
         table[3] = 3;
 
 
-        var value = table.Remove(2);
+        var value = table.RemoveAt(2);
         Assert.That(value, Is.EqualTo(new LuaValue(2)));
         Assert.That(value, Is.EqualTo(new LuaValue(2)));
         Assert.That(table[2], Is.EqualTo(new LuaValue(3)));
         Assert.That(table[2], Is.EqualTo(new LuaValue(3)));
     }
     }