Browse Source

Add: remove

AnnulusGames 1 year ago
parent
commit
0403b824b7
3 changed files with 55 additions and 2 deletions
  1. 24 2
      src/Lua/LuaTable.cs
  2. 18 0
      src/Lua/Standard/Table/RemoveFunction.cs
  3. 13 0
      tests/Lua.Tests/TableTests.cs

+ 24 - 2
src/Lua/LuaTable.cs

@@ -48,7 +48,7 @@ public sealed class LuaTable
                     return array[index - 1];
                     return array[index - 1];
                 }
                 }
             }
             }
-            
+
             if (dictionary.TryGetValue(key, out var value)) return value;
             if (dictionary.TryGetValue(key, out var value)) return value;
             return LuaValue.Nil;
             return LuaValue.Nil;
         }
         }
@@ -133,6 +133,28 @@ public sealed class LuaTable
         return dictionary.ContainsKey(key);
         return dictionary.ContainsKey(key);
     }
     }
 
 
+    public LuaValue Remove(LuaValue key)
+    {
+        if (TryGetInteger(key, out var i) && i > 0 && i <= array.Length)
+        {
+            var index = i - 1;
+            var value = array[index];
+
+            if (index < array.Length - 1)
+            {
+                array.AsSpan(index + 1).CopyTo(array.AsSpan(index));
+            }
+            array[^1] = default;
+
+            return value;
+        }
+        else
+        {
+            dictionary.Remove(key, out var value);
+            return value;
+        }
+    }
+
     public KeyValuePair<LuaValue, LuaValue> GetNext(LuaValue key)
     public KeyValuePair<LuaValue, LuaValue> GetNext(LuaValue key)
     {
     {
         var index = -1;
         var index = -1;
@@ -187,7 +209,7 @@ public sealed class LuaTable
     {
     {
         return array.AsSpan();
         return array.AsSpan();
     }
     }
-    
+
     internal void EnsureArrayCapacity(int newCapacity)
     internal void EnsureArrayCapacity(int newCapacity)
     {
     {
         if (array.Length >= newCapacity) return;
         if (array.Length >= newCapacity) return;

+ 18 - 0
src/Lua/Standard/Table/RemoveFunction.cs

@@ -0,0 +1,18 @@
+namespace Lua.Standard.Table;
+
+public sealed class RemoveFunction : LuaFunction
+{
+    public override string Name => "remove";
+    public static readonly RemoveFunction Instance = new();
+
+    protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
+    {
+        var arg0 = context.ReadArgument<LuaTable>(0);
+        var arg1 = context.ArgumentCount >= 2
+            ? (int)context.ReadArgument<double>(1)
+            : arg0.ArrayLength;
+
+        buffer.Span[0] = arg0.Remove(arg1);
+        return new(1);
+    }
+}

+ 13 - 0
tests/Lua.Tests/TableTests.cs

@@ -28,4 +28,17 @@ public class TableTests
 
 
         Assert.That(table[32], Is.EqualTo(new LuaValue(10)));
         Assert.That(table[32], Is.EqualTo(new LuaValue(10)));
     }
     }
+
+    [Test]
+    public void Test_Remove()
+    {
+        var table = new LuaTable();
+        table[1] = 1;
+        table[2] = 2;
+        table[3] = 3;
+
+        var value = table.Remove(2);
+        Assert.That(value, Is.EqualTo(new LuaValue(2)));
+        Assert.That(table[2], Is.EqualTo(new LuaValue(3)));
+    }
 }
 }