Browse Source

fix: table.sort invalid access

Akeit0 6 months ago
parent
commit
f90e9a782a
2 changed files with 10 additions and 4 deletions
  1. 8 3
      src/Lua/Exceptions.cs
  2. 2 1
      src/Lua/Standard/TableLibrary.cs

+ 8 - 3
src/Lua/Exceptions.cs

@@ -207,16 +207,21 @@ public class LuaRuntimeException : Exception, ILuaTracebackBuildable
     {
         var current = thread.GetCurrentFrame();
         var pc = current.CallerInstructionIndex;
-        LuaClosure callerClosure;
+        LuaFunction callerFunction;
         if (current.IsTailCall)
         {
             pc = thread.LastPc;
-            callerClosure = (LuaClosure)thread.LastCallerFunction!;
+            callerFunction = thread.LastCallerFunction!;
         }
         else
         {
             var caller = thread.GetCallStackFrames()[^2];
-            callerClosure = (LuaClosure)caller.Function;
+            callerFunction = caller.Function;
+        }
+
+        if (callerFunction is not LuaClosure callerClosure)
+        {
+            return ("function", callerFunction.Name);
         }
 
         return (LuaDebug.GetFuncName(callerClosure.Proto, pc, out var name) ?? "", name ?? current.Function.Name);

+ 2 - 1
src/Lua/Standard/TableLibrary.cs

@@ -183,6 +183,7 @@ public sealed class TableLibrary
     {
         var pivot = memory.Span[high];
         int i = low - 1;
+        var access = context.Thread.CurrentAccess;
 
         for (int j = low; j < high; j++)
         {
@@ -190,7 +191,7 @@ public sealed class TableLibrary
             var top = stack.Count;
             stack.Push(memory.Span[j]);
             stack.Push(pivot);
-            await context.Access.RunAsync(comparer, 2, cancellationToken);
+            await access.RunAsync(comparer, 2, cancellationToken);
 
             if (context.Thread.Stack.Get(top).ToBoolean())
             {