| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123 |
- using System.Buffers;
- using System.Runtime.CompilerServices;
- using Lua.Internal;
- namespace Lua.Runtime;
- public static partial class LuaVirtualMachine
- {
- internal async static ValueTask<int> ExecuteClosureAsync(LuaState state, CallStackFrame frame, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var thread = state.CurrentThread;
- var stack = thread.Stack;
- var closure = (Closure)frame.Function;
- var chunk = closure.Proto;
- var rootChunk = chunk.GetRoot();
- var resultBuffer = ArrayPool<LuaValue>.Shared.Rent(1024);
- try
- {
- for (var pc = 0; pc < chunk.Instructions.Length; pc++)
- {
- var instruction = chunk.Instructions[pc];
- var RA = instruction.A + frame.Base;
- var RB = instruction.B + frame.Base;
- switch (instruction.OpCode)
- {
- case OpCode.Move:
- stack.EnsureCapacity(RA + 1);
- stack.UnsafeGet(RA) = stack.UnsafeGet(RB);
- stack.NotifyTop(RA + 1);
- break;
- case OpCode.LoadK:
- stack.EnsureCapacity(RA + 1);
- stack.UnsafeGet(RA) = chunk.Constants[instruction.Bx];
- stack.NotifyTop(RA + 1);
- break;
- case OpCode.LoadKX:
- throw new NotImplementedException();
- case OpCode.LoadBool:
- stack.EnsureCapacity(RA + 1);
- stack.UnsafeGet(RA) = instruction.B != 0;
- stack.NotifyTop(RA + 1);
- if (instruction.C != 0) pc++;
- break;
- case OpCode.LoadNil:
- stack.EnsureCapacity(RA + instruction.B + 1);
- stack.GetBuffer().Slice(RA, instruction.B + 1).Clear();
- stack.NotifyTop(RA + instruction.B + 1);
- break;
- case OpCode.GetUpVal:
- {
- stack.EnsureCapacity(RA + 1);
- var upValue = MemoryMarshalEx.UnsafeElementAt(closure.UpValues, instruction.B);
- stack.UnsafeGet(RA) = upValue.GetValue();
- stack.NotifyTop(RA + 1);
- break;
- }
- case OpCode.GetTabUp:
- {
- stack.EnsureCapacity(RA + 1);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- var upValue = MemoryMarshalEx.UnsafeElementAt(closure.UpValues, instruction.B);
- var table = upValue.GetValue();
- await GetTableValue(state, thread, chunk, rootChunk, pc, table, vc, resultBuffer.AsMemory(), cancellationToken);
- var value = resultBuffer[0];
- stack.UnsafeGet(RA) = value;
- stack.NotifyTop(RA + 1);
- break;
- }
- case OpCode.GetTable:
- {
- stack.EnsureCapacity(RA + 1);
- var table = stack.UnsafeGet(RB);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- await GetTableValue(state, thread, chunk, rootChunk, pc, table, vc, resultBuffer.AsMemory(), cancellationToken);
- var value = resultBuffer[0];
- stack.UnsafeGet(RA) = value;
- stack.NotifyTop(RA + 1);
- }
- break;
- case OpCode.SetTabUp:
- {
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- var upValue = MemoryMarshalEx.UnsafeElementAt(closure.UpValues, instruction.A);
- var table = upValue.GetValue();
- await SetTableValue(state, thread, chunk, rootChunk, pc, table, vb, vc, resultBuffer.AsMemory(), cancellationToken);
- break;
- }
- case OpCode.SetUpVal:
- {
- var upValue = MemoryMarshalEx.UnsafeElementAt(closure.UpValues, instruction.B);
- upValue.SetValue(stack.UnsafeGet(RA));
- break;
- }
- case OpCode.SetTable:
- {
- var table = stack.UnsafeGet(RA);
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- await SetTableValue(state, thread, chunk, rootChunk, pc, table, vb, vc, resultBuffer.AsMemory(), cancellationToken);
- }
- break;
- case OpCode.NewTable:
- stack.EnsureCapacity(RA + 1);
- stack.UnsafeGet(RA) = new LuaTable(instruction.B, instruction.C);
- stack.NotifyTop(RA + 1);
- break;
- case OpCode.Self:
- {
- stack.EnsureCapacity(RA + 2);
- var table = stack.UnsafeGet(RB);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- await GetTableValue(state, thread, chunk, rootChunk, pc, table, vc, resultBuffer.AsMemory(), cancellationToken);
- var value = resultBuffer[0];
- stack.UnsafeGet(RA + 1) = table;
- stack.UnsafeGet(RA) = value;
- stack.NotifyTop(RA + 2);
- }
- break;
- case OpCode.Add:
- {
- stack.EnsureCapacity(RA + 1);
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- if (vb.TryRead<double>(out var valueB) && vc.TryRead<double>(out var valueC))
- {
- stack.UnsafeGet(RA) = valueB + valueC;
- }
- else if (vb.TryGetMetamethod(state, Metamethods.Add, out var metamethod) || vc.TryGetMetamethod(state, Metamethods.Add, out metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- stack.Push(vc);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- FrameBase = stack.Count - 2,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- stack.UnsafeGet(RA) = resultBuffer[0];
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "add", vb, vc);
- }
- stack.NotifyTop(RA + 1);
- }
- break;
- case OpCode.Sub:
- {
- stack.EnsureCapacity(RA + 1);
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- if (vb.TryRead<double>(out var valueB) && vc.TryRead<double>(out var valueC))
- {
- stack.UnsafeGet(RA) = valueB - valueC;
- }
- else if (vb.TryGetMetamethod(state, Metamethods.Sub, out var metamethod) || vc.TryGetMetamethod(state, Metamethods.Sub, out metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- stack.Push(vc);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- FrameBase = stack.Count - 2,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- stack.UnsafeGet(RA) = resultBuffer[0];
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "sub", vb, vc);
- }
- stack.NotifyTop(RA + 1);
- }
- break;
- case OpCode.Mul:
- {
- stack.EnsureCapacity(RA + 1);
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- if (vb.TryRead<double>(out var valueB) && vc.TryRead<double>(out var valueC))
- {
- stack.UnsafeGet(RA) = valueB * valueC;
- }
- else if (vb.TryGetMetamethod(state, Metamethods.Mul, out var metamethod) || vc.TryGetMetamethod(state, Metamethods.Mul, out metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- stack.Push(vc);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- FrameBase = stack.Count - 2,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- stack.UnsafeGet(RA) = resultBuffer[0];
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "mul", vb, vc);
- }
- stack.NotifyTop(RA + 1);
- }
- break;
- case OpCode.Div:
- {
- stack.EnsureCapacity(RA + 1);
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- if (vb.TryRead<double>(out var valueB) && vc.TryRead<double>(out var valueC))
- {
- stack.UnsafeGet(RA) = valueB / valueC;
- }
- else if (vb.TryGetMetamethod(state, Metamethods.Div, out var metamethod) || vc.TryGetMetamethod(state, Metamethods.Div, out metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- stack.Push(vc);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- FrameBase = stack.Count - 2,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- stack.UnsafeGet(RA) = resultBuffer[0];
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "div", vb, vc);
- }
- stack.NotifyTop(RA + 1);
- }
- break;
- case OpCode.Mod:
- {
- stack.EnsureCapacity(RA + 1);
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- if (vb.TryRead<double>(out var valueB) && vc.TryRead<double>(out var valueC))
- {
- var mod = valueB % valueC;
- if ((valueC > 0 && mod < 0) || (valueC < 0 && mod > 0))
- {
- mod += valueC;
- }
- stack.UnsafeGet(RA) = mod;
- }
- else if (vb.TryGetMetamethod(state, Metamethods.Mod, out var metamethod) || vc.TryGetMetamethod(state, Metamethods.Mod, out metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- stack.Push(vc);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- FrameBase = stack.Count - 2,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- stack.UnsafeGet(RA) = resultBuffer[0];
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "mod", vb, vc);
- }
- stack.NotifyTop(RA + 1);
- }
- break;
- case OpCode.Pow:
- {
- stack.EnsureCapacity(RA + 1);
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- if (vb.TryRead<double>(out var valueB) && vc.TryRead<double>(out var valueC))
- {
- stack.UnsafeGet(RA) = Math.Pow(valueB, valueC);
- }
- else if (vb.TryGetMetamethod(state, Metamethods.Pow, out var metamethod) || vc.TryGetMetamethod(state, Metamethods.Pow, out metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- stack.Push(vc);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- FrameBase = stack.Count - 2,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- stack.UnsafeGet(RA) = resultBuffer[0];
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "pow", vb, vc);
- }
- stack.NotifyTop(RA + 1);
- }
- break;
- case OpCode.Unm:
- {
- stack.EnsureCapacity(RA + 1);
- var vb = stack.UnsafeGet(RB);
- if (vb.TryRead<double>(out var valueB))
- {
- stack.UnsafeGet(RA) = -valueB;
- }
- else if (vb.TryGetMetamethod(state, Metamethods.Unm, out var metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 1,
- FrameBase = stack.Count - 1,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- stack.UnsafeGet(RA) = resultBuffer[0];
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "unm", vb);
- }
- stack.NotifyTop(RA + 1);
- }
- break;
- case OpCode.Not:
- {
- stack.EnsureCapacity(RA + 1);
- stack.UnsafeGet(RA) = !stack.UnsafeGet(RB).ToBoolean();
- stack.NotifyTop(RA + 1);
- }
- break;
- case OpCode.Len:
- {
- stack.EnsureCapacity(RA + 1);
- var vb = stack.UnsafeGet(RB);
- if (vb.TryRead<string>(out var str))
- {
- stack.UnsafeGet(RA) = str.Length;
- }
- else if (vb.TryGetMetamethod(state, Metamethods.Len, out var metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 1,
- FrameBase = stack.Count - 1,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- stack.UnsafeGet(RA) = resultBuffer[0];
- }
- else if (vb.TryRead<LuaTable>(out var table))
- {
- stack.UnsafeGet(RA) = table.ArrayLength;
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "get length of", vb);
- }
- stack.NotifyTop(RA + 1);
- }
- break;
- case OpCode.Concat:
- {
- stack.EnsureCapacity(RA + 1);
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- var bIsValid = vb.TryRead<string>(out var strB);
- var cIsValid = vc.TryRead<string>(out var strC);
- if (!bIsValid && vb.TryRead<double>(out var numB))
- {
- strB = numB.ToString();
- bIsValid = true;
- }
- if (!cIsValid && vc.TryRead<double>(out var numC))
- {
- strC = numC.ToString();
- cIsValid = true;
- }
- if (bIsValid && cIsValid)
- {
- stack.UnsafeGet(RA) = strB + strC;
- }
- else if (vb.TryGetMetamethod(state, Metamethods.Concat, out var metamethod) || vc.TryGetMetamethod(state, Metamethods.Concat, out metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- stack.Push(vc);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- FrameBase = stack.Count - 2,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- stack.UnsafeGet(RA) = resultBuffer[0];
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "concat", vb, vc);
- }
- stack.NotifyTop(RA + 1);
- }
- break;
- case OpCode.Jmp:
- pc += instruction.SBx;
- if (instruction.A != 0)
- {
- state.CloseUpValues(thread, instruction.A - 1);
- }
- break;
- case OpCode.Eq:
- {
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- var compareResult = vb == vc;
- if (!compareResult && (vb.TryGetMetamethod(state, Metamethods.Eq, out var metamethod) || vc.TryGetMetamethod(state, Metamethods.Eq, out metamethod)))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- stack.Push(vc);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- FrameBase = stack.Count - 2,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- compareResult = resultBuffer[0].ToBoolean();
- }
- if (compareResult != (instruction.A == 1))
- {
- pc++;
- }
- }
- break;
- case OpCode.Lt:
- {
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- var compareResult = false;
- if (vb.TryRead<string>(out var strB) && vc.TryRead<string>(out var strC))
- {
- compareResult = StringComparer.Ordinal.Compare(strB, strC) < 0;
- }
- else if (vb.TryRead<double>(out var valueB) && vc.TryRead<double>(out var valueC))
- {
- compareResult = valueB < valueC;
- }
- else if (vb.TryGetMetamethod(state, Metamethods.Lt, out var metamethod) || vc.TryGetMetamethod(state, Metamethods.Lt, out metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- stack.Push(vc);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- FrameBase = stack.Count - 2,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- compareResult = resultBuffer[0].ToBoolean();
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "less than", vb, vc);
- }
- if (compareResult != (instruction.A == 1))
- {
- pc++;
- }
- }
- break;
- case OpCode.Le:
- {
- var vb = RK(stack, chunk, instruction.B, frame.Base);
- var vc = RK(stack, chunk, instruction.C, frame.Base);
- var compareResult = false;
- if (vb.TryRead<string>(out var strB) && vc.TryRead<string>(out var strC))
- {
- compareResult = StringComparer.Ordinal.Compare(strB, strC) <= 0;
- }
- else if (vb.TryRead<double>(out var valueB) && vc.TryRead<double>(out var valueC))
- {
- compareResult = valueB <= valueC;
- }
- else if (vb.TryGetMetamethod(state, Metamethods.Le, out var metamethod) || vc.TryGetMetamethod(state, Metamethods.Le, out metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(vb);
- stack.Push(vc);
- await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- FrameBase = stack.Count - 2,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- compareResult = resultBuffer[0].ToBoolean();
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "less than or equals", vb, vc);
- }
- if (compareResult != (instruction.A == 1))
- {
- pc++;
- }
- }
- break;
- case OpCode.Test:
- {
- if (stack.UnsafeGet(RA).ToBoolean() != (instruction.C == 1))
- {
- pc++;
- }
- }
- break;
- case OpCode.TestSet:
- {
- if (stack.UnsafeGet(RB).ToBoolean() != (instruction.C == 1))
- {
- pc++;
- }
- else
- {
- stack.UnsafeGet(RA) = stack.UnsafeGet(RB);
- stack.NotifyTop(RA + 1);
- }
- }
- break;
- case OpCode.Call:
- {
- var va = stack.UnsafeGet(RA);
- if (!va.TryRead<LuaFunction>(out var func))
- {
- if (va.TryGetMetamethod(state, Metamethods.Call, out var metamethod) && metamethod.TryRead<LuaFunction>(out func))
- {
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- }
- (var newBase, var argumentCount) = PrepareForFunctionCall(thread, func, instruction, RA, resultBuffer.AsSpan(), false);
-
- var callPosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc);
- var chunkName = chunk.Name ?? LuaState.DefaultChunkName;
- var rootChunkName = rootChunk.Name ?? LuaState.DefaultChunkName;
- thread.PushCallStackFrame(new CallStackFrame
- {
- Base = newBase,
- CallPosition = callPosition,
- ChunkName = chunkName,
- RootChunkName = rootChunkName,
- VariableArgumentCount = func is Closure cl ? Math.Max(argumentCount - cl.Proto.ParameterCount, 0) : 0,
- Function = func,
- });
- int rawResultCount;
- try
- {
- rawResultCount = await func.Func(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = argumentCount,
- FrameBase = newBase,
- SourcePosition = callPosition,
- ChunkName = chunkName,
- RootChunkName = rootChunkName,
- }, resultBuffer.AsMemory(), cancellationToken);
- }
- finally
- {
- thread.PopCallStackFrame();
- }
- var resultCount = rawResultCount;
- if (instruction.C != 0)
- {
- resultCount = instruction.C - 1;
- }
- if (resultCount == 0)
- {
- stack.Pop();
- }
- else
- {
- stack.EnsureCapacity(RA + resultCount);
- for (int i = 0; i < resultCount; i++)
- {
- stack.UnsafeGet(RA + i) = i >= rawResultCount
- ? LuaValue.Nil
- : resultBuffer[i];
- }
- stack.NotifyTop(RA + resultCount);
- }
- }
- break;
- case OpCode.TailCall:
- {
- state.CloseUpValues(thread, frame.Base);
- var va = stack.UnsafeGet(RA);
- if (!va.TryRead<LuaFunction>(out var func))
- {
- if (!va.TryGetMetamethod(state, Metamethods.Call, out var metamethod) && !metamethod.TryRead<LuaFunction>(out func))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- }
- (var newBase, var argumentCount) = PrepareForFunctionCall(thread, func, instruction, RA, resultBuffer.AsSpan(), true);
- return await func.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = argumentCount,
- FrameBase = newBase,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, buffer, cancellationToken);
- }
- case OpCode.Return:
- {
- state.CloseUpValues(thread, frame.Base);
- var retCount = instruction.B - 1;
- if (retCount == -1)
- {
- retCount = stack.Count - RA;
- }
- for (int i = 0; i < retCount; i++)
- {
- buffer.Span[i] = stack.UnsafeGet(RA + i);
- }
- return retCount;
- }
- case OpCode.ForLoop:
- {
- stack.EnsureCapacity(RA + 4);
- if (!stack.UnsafeGet(RA).TryRead<double>(out var init))
- {
- throw new LuaRuntimeException(state.GetTraceback(), "'for' initial value must be a number");
- }
- if (!stack.UnsafeGet(RA + 1).TryRead<double>(out var limit))
- {
- throw new LuaRuntimeException(state.GetTraceback(), "'for' limit must be a number");
- }
- if (!stack.UnsafeGet(RA + 2).TryRead<double>(out var step))
- {
- throw new LuaRuntimeException(state.GetTraceback(), "'for' step must be a number");
- }
- var va = init + step;
- stack.UnsafeGet(RA) = va;
- if (step >= 0 ? va <= limit : va >= limit)
- {
- pc += instruction.SBx;
- stack.UnsafeGet(RA + 3) = va;
- stack.NotifyTop(RA + 4);
- }
- else
- {
- stack.NotifyTop(RA + 1);
- }
- }
- break;
- case OpCode.ForPrep:
- {
- if (!stack.UnsafeGet(RA).TryRead<double>(out var init))
- {
- throw new LuaRuntimeException(state.GetTraceback(), "'for' initial value must be a number");
- }
- if (!stack.UnsafeGet(RA + 2).TryRead<double>(out var step))
- {
- throw new LuaRuntimeException(state.GetTraceback(), "'for' step must be a number");
- }
- stack.UnsafeGet(RA) = init - step;
- stack.NotifyTop(RA + 1);
- pc += instruction.SBx;
- }
- break;
- case OpCode.TForCall:
- {
- var iteratorRaw = stack.UnsafeGet(RA);
- if (!iteratorRaw.TryRead<LuaFunction>(out var iterator))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", iteratorRaw);
- }
- var nextBase = RA + 3 + instruction.C;
- stack.UnsafeGet(nextBase) = stack.UnsafeGet(RA + 1);
- stack.UnsafeGet(nextBase + 1) = stack.UnsafeGet(RA + 2);
- stack.NotifyTop(nextBase + 2);
- var resultCount = await iterator.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- FrameBase = nextBase,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, resultBuffer.AsMemory(), cancellationToken);
- stack.EnsureCapacity(RA + instruction.C + 3);
- for (int i = 1; i <= instruction.C; i++)
- {
- var index = i - 1;
- stack.UnsafeGet(RA + 2 + i) = index >= resultCount
- ? LuaValue.Nil
- : resultBuffer[i - 1];
- }
- stack.NotifyTop(RA + instruction.C + 3);
- }
- break;
- case OpCode.TForLoop:
- {
- var forState = stack.UnsafeGet(RA + 1);
- if (forState.Type is not LuaValueType.Nil)
- {
- stack.UnsafeGet(RA) = forState;
- pc += instruction.SBx;
- }
- }
- break;
- case OpCode.SetList:
- {
- if (!stack.UnsafeGet(RA).TryRead<LuaTable>(out var table))
- {
- throw new LuaException("internal error");
- }
- var count = instruction.B == 0
- ? stack.Count - (RA + 1)
- : instruction.B;
- table.EnsureArrayCapacity((instruction.C - 1) * 50 + count);
- stack.AsSpan().Slice(RA + 1, count)
- .CopyTo(table.GetArraySpan()[((instruction.C - 1) * 50)..]);
- }
- break;
- case OpCode.Closure:
- stack.EnsureCapacity(RA + 1);
- stack.UnsafeGet(RA) = new Closure(state, chunk.Functions[instruction.SBx]);
- stack.NotifyTop(RA + 1);
- break;
- case OpCode.VarArg:
- {
- var count = instruction.B == 0
- ? frame.VariableArgumentCount
- : instruction.B - 1;
- stack.EnsureCapacity(RA + count);
- for (int i = 0; i < count; i++)
- {
- stack.UnsafeGet(RA + i) = frame.VariableArgumentCount > i
- ? stack.UnsafeGet(frame.Base - (frame.VariableArgumentCount - i))
- : LuaValue.Nil;
- }
- stack.NotifyTop(RA + count);
- }
- break;
- case OpCode.ExtraArg:
- throw new NotImplementedException();
- default:
- break;
- }
- }
- }
- catch (Exception)
- {
- state.CloseUpValues(thread, frame.Base);
- throw;
- }
- finally
- {
- ArrayPool<LuaValue>.Shared.Return(resultBuffer);
- }
- return 0;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- static ref LuaValue RK(LuaStack stack, Chunk chunk, ushort index, int frameBase)
- {
- if (index >= 256)
- {
- return ref MemoryMarshalEx.UnsafeElementAt(chunk.Constants, index - 256);
- }
- else
- {
- return ref stack.UnsafeGet(index + frameBase);
- }
- }
- static ValueTask<int> GetTableValue(LuaState state, LuaThread thread, Chunk chunk, Chunk rootChunk, int pc, LuaValue table, LuaValue key, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var stack = thread.Stack;
- var isTable = table.TryRead<LuaTable>(out var t);
- if (isTable && t.TryGetValue(key, out var result))
- {
- buffer.Span[0] = result;
- return new(1);
- }
- else if (table.TryGetMetamethod(state, Metamethods.Index, out var metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var indexTable))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(table);
- stack.Push(key);
- return indexTable.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 2,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- FrameBase = stack.Count - 2,
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, buffer, cancellationToken);
- }
- else if (isTable)
- {
- buffer.Span[0] = LuaValue.Nil;
- return new(1);
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "index", table);
- return default; // dummy
- }
- }
- static ValueTask<int> SetTableValue(LuaState state, LuaThread thread, Chunk chunk, Chunk rootChunk, int pc, LuaValue table, LuaValue key, LuaValue value, Memory<LuaValue> buffer, CancellationToken cancellationToken)
- {
- var stack = thread.Stack;
- var isTable = table.TryRead<LuaTable>(out var t);
- if (key.Type is LuaValueType.Number)
- {
- var d = key.UnsafeRead<double>();
- if (double.IsNaN(d))
- {
- throw new LuaRuntimeException(GetTracebacks(state, chunk, pc), "table index is NaN");
- }
- }
- if (isTable)
- {
- t[key] = value;
- return new(1);
- }
- else if (table.TryGetMetamethod(state, Metamethods.NewIndex, out var metamethod))
- {
- if (!metamethod.TryRead<LuaFunction>(out var indexTable))
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "call", metamethod);
- }
- stack.Push(table);
- stack.Push(key);
- stack.Push(value);
- return indexTable.InvokeAsync(new()
- {
- State = state,
- Thread = thread,
- ArgumentCount = 3,
- FrameBase = stack.Count - 3,
- SourcePosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = rootChunk.Name,
- }, buffer, cancellationToken);
- }
- else
- {
- LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(state, chunk, pc), "index", table);
- return default; // dummy
- }
- }
- static (int FrameBase, int ArgumentCount) PrepareForFunctionCall(LuaThread thread, LuaFunction function, Instruction instruction, int RA, Span<LuaValue> buffer, bool isTailCall)
- {
- var stack = thread.Stack;
- var argumentCount = instruction.B - 1;
- if (instruction.B == 0)
- {
- argumentCount = (ushort)(stack.Count - (RA + 1));
- }
- var newBase = RA + 1;
- // In the case of tailcall, the local variables of the caller are immediately discarded, so there is no need to retain them.
- // Therefore, a call can be made without allocating new registers.
- if (isTailCall)
- {
- var currentBase = thread.GetCurrentFrame().Base;
- var stackBuffer = stack.GetBuffer();
- stackBuffer.Slice(newBase, argumentCount).CopyTo(stackBuffer.Slice(currentBase, argumentCount));
- newBase = currentBase;
- }
- var variableArgumentCount = function.GetVariableArgumentCount(argumentCount);
- // If there are variable arguments, the base of the stack is moved by that number and the values of the variable arguments are placed in front of it.
- // see: https://wubingzheng.github.io/build-lua-in-rust/en/ch08-02.arguments.html
- if (variableArgumentCount > 0)
- {
- var temp = newBase;
- newBase += variableArgumentCount;
- stack.EnsureCapacity(newBase + argumentCount);
- stack.NotifyTop(newBase + argumentCount);
- var stackBuffer = stack.GetBuffer();
- stackBuffer.Slice(temp, argumentCount).CopyTo(buffer);
- buffer.Slice(0, argumentCount).CopyTo(stackBuffer[newBase..]);
- buffer.Slice(argumentCount - variableArgumentCount, variableArgumentCount).CopyTo(stackBuffer[temp..]);
- }
- return (newBase, argumentCount);
- }
- static Traceback GetTracebacks(LuaState state, Chunk chunk, int pc)
- {
- var frame = state.CurrentThread.GetCurrentFrame();
- state.CurrentThread.PushCallStackFrame(frame with
- {
- CallPosition = MemoryMarshalEx.UnsafeElementAt(chunk.SourcePositions, pc),
- ChunkName = chunk.Name,
- RootChunkName = chunk.GetRoot().Name,
- });
- var tracebacks = state.GetTraceback();
- state.CurrentThread.PopCallStackFrame();
- return tracebacks;
- }
- }
|