Browse Source

Fix: select (support negative value)

AnnulusGames 1 year ago
parent
commit
1234300898
1 changed files with 9 additions and 1 deletions
  1. 9 1
      src/Lua/Standard/Basic/SelectFunction.cs

+ 9 - 1
src/Lua/Standard/Basic/SelectFunction.cs

@@ -18,7 +18,15 @@ public sealed class SelectFunction : LuaFunction
 
 
             var index = (int)d;
             var index = (int)d;
 
 
-            var span = context.Arguments[index..];
+            if (Math.Abs(index) > context.ArgumentCount)
+            {
+                throw new LuaRuntimeException(context.State.GetTraceback(), "bad argument #1 to 'select' (index out of range)");
+            }
+
+            var span = index >= 0
+                ? context.Arguments[index..]
+                : context.Arguments[(context.ArgumentCount + index)..];
+
             span.CopyTo(buffer.Span);
             span.CopyTo(buffer.Span);
 
 
             return new(span.Length);
             return new(span.Length);