Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/Lua/Runtime/LuaVirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1652,7 +1652,6 @@ static async ValueTask<LuaValue> 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;
}
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -1955,9 +1958,8 @@ internal static async ValueTask<LuaValue> 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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -2092,7 +2091,7 @@ internal static async ValueTask<LuaValue> 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;
Expand Down Expand Up @@ -2165,16 +2164,16 @@ 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))
{
context.Pc++;
}

results.Clear();
context.Thread.PopCallStackFrameWithStackPop();
stack.PopUntil(newFrame.ReturnBase + 1);
context.Thread.PopCallStackFrame();

return true;
}
Expand Down Expand Up @@ -2254,14 +2253,14 @@ internal static async ValueTask<bool> 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();
}
}

Expand Down