diff --git a/src/Lua/Runtime/LuaVirtualMachine.cs b/src/Lua/Runtime/LuaVirtualMachine.cs index abd4ea09..25b4c706 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.cs @@ -93,7 +93,7 @@ public bool PopFromBuffer(int src, int srcCount) var returnBase = frames[^1].ReturnBase; if (src != returnBase) { - result.CopyTo(Stack.GetBuffer()[returnBase..]); + result.CopyTo(Stack.AsSpan()[returnBase..]); } Stack.PopUntil(returnBase + srcCount); @@ -1583,7 +1583,7 @@ static bool CallGetTableFunc(LuaValue table, LuaFunction indexTable, LuaValue ke var awaiter = task.GetAwaiter(); awaiter.GetResult(); - var results = stack.GetBuffer()[newFrame.Base..]; + var results = stack.GetBuffer()[newFrame.ReturnBase..]; result = results.Length == 0 ? default : results[0]; context.Thread.PopCallStackFrameWithStackPop(); return true; @@ -1652,7 +1652,6 @@ static async ValueTask CallGetTableFunc(LuaThread thread, LuaFunction await indexTable.Func(functionContext, ct); var results = stack.GetBuffer()[newFrame.ReturnBase..]; var result = results.Length == 0 ? default : results[0]; - results.Clear(); thread.PopCallStackFrameWithStackPop(); return result; } @@ -1836,8 +1835,6 @@ static async ValueTask CallSetTableFunc(LuaThread thread, LuaFunction newIndexFu } await newIndexFunction.Func(functionContext, ct); - var results = stack.GetBuffer()[newFrame.ReturnBase..]; - results.Clear(); thread.PopCallStackFrameWithStackPop(); } @@ -1903,8 +1900,14 @@ static bool ExecuteBinaryOperationMetaMethod(LuaValue vb, LuaValue vc, var RA = context.Instruction.A + context.FrameBase; var results = stack.GetBuffer()[newFrame.ReturnBase..]; - stack.Get(RA) = results.Length == 0 ? default : results[0]; - results.Clear(); + LuaValue result = default; + if (results.Length != 0) + { + result = results[0]; + results.Clear(); + } + + stack.Get(RA) = result; context.Thread.PopCallStackFrameWithStackPop(); return true; } @@ -1955,9 +1958,8 @@ internal static async ValueTask ExecuteBinaryOperationMetaMethod(LuaTh await func.Func(functionContext, ct); - var results = stack.GetBuffer()[newFrame.ReturnBase..]; + var results = stack.AsSpan()[newFrame.ReturnBase..]; var result = results.Length == 0 ? default : results[0]; - results.Clear(); return result; } finally @@ -2028,11 +2030,8 @@ static bool ExecuteUnaryOperationMetaMethod(LuaValue vb, VirtualMachineExecution return false; } - var RA = context.Instruction.A + context.FrameBase; - var results = stack.GetBuffer()[newFrame.Base..]; - stack.Get(RA) = results.Length == 0 ? default : results[0]; - results.Clear(); - context.Thread.PopCallStackFrameWithStackPop(); + stack.PopUntil(newFrame.ReturnBase + 1); + context.Thread.PopCallStackFrame(); return true; } @@ -2092,7 +2091,7 @@ internal static async ValueTask ExecuteUnaryOperationMetaMethod(LuaThr } thread.ThrowIfCancellationRequested(cancellationToken); - var results = stack.GetBuffer()[newFrame.ReturnBase..]; + var results = stack.AsSpan()[newFrame.ReturnBase..]; var result = results.Length == 0 ? default : results[0]; results.Clear(); return result; @@ -2165,7 +2164,7 @@ static bool ExecuteCompareOperationMetaMethod(LuaValue vb, LuaValue vc, return false; } - var results = stack.GetBuffer()[newFrame.Base..]; + var results = stack.AsSpan()[newFrame.ReturnBase..]; var compareResult = results.Length == 0 && results[0].ToBoolean(); compareResult = reverseLe ? !compareResult : compareResult; if (compareResult != (context.Instruction.A == 1)) @@ -2173,8 +2172,8 @@ static bool ExecuteCompareOperationMetaMethod(LuaValue vb, LuaValue vc, context.Pc++; } - results.Clear(); - context.Thread.PopCallStackFrameWithStackPop(); + stack.PopUntil(newFrame.ReturnBase + 1); + context.Thread.PopCallStackFrame(); return true; } @@ -2254,14 +2253,14 @@ internal static async ValueTask ExecuteCompareOperationMetaMethod(LuaThrea } thread.ThrowIfCancellationRequested(cancellationToken); - var results = stack.GetBuffer()[newFrame.ReturnBase..]; + var results = stack.AsSpan()[newFrame.ReturnBase..]; var result = results.Length == 0 ? default : results[0]; results.Clear(); return result.ToBoolean(); } finally { - thread.PopCallStackFrameWithStackPop(); + thread.PopCallStackFrame(); } }