LuaVirtualMachine.cs 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. using System.Diagnostics;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Globalization;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. using Lua.Internal;
  7. namespace Lua.Runtime;
  8. [SuppressMessage("Reliability", "CA2012:Use ValueTasks correctly")]
  9. public static partial class LuaVirtualMachine
  10. {
  11. [StructLayout(LayoutKind.Auto)]
  12. [method: MethodImpl(MethodImplOptions.AggressiveInlining)]
  13. struct VirtualMachineExecutionContext(
  14. LuaState state,
  15. LuaStack stack,
  16. LuaValue[] resultsBuffer,
  17. Memory<LuaValue> buffer,
  18. LuaThread thread,
  19. in CallStackFrame frame,
  20. CancellationToken cancellationToken)
  21. {
  22. public readonly LuaState State = state;
  23. public readonly LuaStack Stack = stack;
  24. public Closure Closure = (Closure)frame.Function;
  25. public readonly LuaValue[] ResultsBuffer = resultsBuffer;
  26. public readonly Memory<LuaValue> Buffer = buffer;
  27. public readonly LuaThread Thread = thread;
  28. public Chunk Chunk => Closure.Proto;
  29. public int FrameBase = frame.Base;
  30. public int VariableArgumentCount = frame.VariableArgumentCount;
  31. public readonly CancellationToken CancellationToken = cancellationToken;
  32. public int Pc = -1;
  33. public Instruction Instruction;
  34. public int ResultCount;
  35. public int TaskResult;
  36. public ValueTaskAwaiter<int> Awaiter;
  37. public bool IsTopLevel => BaseCallStackCount == Thread.CallStack.Count;
  38. readonly int BaseCallStackCount = thread.CallStack.Count;
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public bool Pop(Instruction instruction, int frameBase)
  41. {
  42. if (BaseCallStackCount == Thread.CallStack.Count) return false;
  43. var count = instruction.B - 1;
  44. var src = instruction.A + frameBase;
  45. if (count == -1) count = Stack.Count - src;
  46. return PopFromBuffer(Stack.GetBuffer().Slice(src, count));
  47. }
  48. [MethodImpl(MethodImplOptions.NoInlining)]
  49. public bool PopFromBuffer(Span<LuaValue> result)
  50. {
  51. ref var callStack = ref Thread.CallStack;
  52. Re:
  53. var frames = callStack.AsSpan();
  54. if (frames.Length == BaseCallStackCount) return false;
  55. ref readonly var frame = ref frames[^1];
  56. Pc = frame.CallerInstructionIndex!.Value;
  57. ref readonly var lastFrame = ref frames[^2];
  58. Closure = Unsafe.As<Closure>(lastFrame.Function);
  59. var callInstruction = Chunk.Instructions[Pc];
  60. FrameBase = lastFrame.Base;
  61. VariableArgumentCount = lastFrame.VariableArgumentCount;
  62. if (callInstruction.OpCode == OpCode.TailCall)
  63. {
  64. Thread.PopCallStackFrameUnsafe();
  65. goto Re;
  66. }
  67. var opCode = callInstruction.OpCode;
  68. if (opCode is OpCode.Eq or OpCode.Lt or OpCode.Le)
  69. {
  70. var compareResult = result.Length > 0 && result[0].ToBoolean();
  71. if (compareResult != (callInstruction.A == 1))
  72. {
  73. Pc++;
  74. }
  75. Thread.PopCallStackFrameUnsafe(frame.Base);
  76. return true;
  77. }
  78. var target = callInstruction.A + FrameBase;
  79. var targetCount = result.Length;
  80. if (opCode == OpCode.TForCall)
  81. {
  82. target += 3;
  83. targetCount = callInstruction.C;
  84. }
  85. else if (opCode == OpCode.Call)
  86. {
  87. var c = callInstruction.C;
  88. if (c != 0)
  89. {
  90. targetCount = c - 1;
  91. }
  92. }
  93. else if (opCode == OpCode.Self)
  94. {
  95. Stack.Get(target) = result.Length == 0 ? LuaValue.Nil : result[0];
  96. Thread.PopCallStackFrameUnsafe(target + 2);
  97. return true;
  98. }
  99. else if (opCode is OpCode.SetTable or OpCode.SetTabUp)
  100. {
  101. targetCount = 0;
  102. }
  103. else // Other opcodes has one result
  104. {
  105. targetCount = 1;
  106. }
  107. var count = Math.Min(result.Length, targetCount);
  108. Stack.EnsureCapacity(target + targetCount);
  109. var stackBuffer = Stack.GetBuffer();
  110. if (count > 0)
  111. {
  112. result[..count].CopyTo(stackBuffer.Slice(target, count));
  113. }
  114. if (targetCount > count)
  115. {
  116. stackBuffer.Slice(target + count, targetCount - count).Clear();
  117. }
  118. Stack.NotifyTop(target + targetCount);
  119. Thread.PopCallStackFrameUnsafe(target + targetCount);
  120. return true;
  121. }
  122. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  123. public void Push(in CallStackFrame frame)
  124. {
  125. Pc = -1;
  126. Closure = (frame.Function as Closure)!;
  127. FrameBase = frame.Base;
  128. VariableArgumentCount = frame.VariableArgumentCount;
  129. }
  130. public void PopOnTopCallStackFrames()
  131. {
  132. ref var callStack = ref Thread.CallStack;
  133. var count = callStack.Count;
  134. if (count == BaseCallStackCount) return;
  135. while (callStack.Count > BaseCallStackCount + 1)
  136. {
  137. callStack.TryPop();
  138. }
  139. Thread.PopCallStackFrame();
  140. }
  141. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  142. public void ClearResultsBuffer()
  143. {
  144. if (TaskResult == 0) return;
  145. if (TaskResult == 1)
  146. {
  147. ResultsBuffer[0] = default;
  148. return;
  149. }
  150. ResultsBuffer.AsSpan(0, TaskResult).Clear();
  151. }
  152. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  153. public void ClearResultsBuffer(int count)
  154. {
  155. if (count == 0) return;
  156. if (count == 1)
  157. {
  158. ResultsBuffer[0] = default;
  159. return;
  160. }
  161. ResultsBuffer.AsSpan(0, count).Clear();
  162. }
  163. }
  164. enum PostOperationType
  165. {
  166. None,
  167. Nop,
  168. SetResult,
  169. TForCall,
  170. Call,
  171. TailCall,
  172. Self,
  173. Compare,
  174. }
  175. [AsyncStateMachine(typeof(AsyncStateMachine))]
  176. internal static ValueTask<int> ExecuteClosureAsync(LuaState luaState, Memory<LuaValue> buffer,
  177. CancellationToken cancellationToken)
  178. {
  179. var thread = luaState.CurrentThread;
  180. ref readonly var frame = ref thread.GetCallStackFrames()[^1];
  181. var resultBuffer = LuaValueArrayPool.Rent1024();
  182. var stateMachine = new AsyncStateMachine
  183. {
  184. Context = new(luaState, thread.Stack, resultBuffer, buffer, thread, in frame,
  185. cancellationToken),
  186. Builder = new()
  187. };
  188. stateMachine.Builder.Start(ref stateMachine);
  189. return stateMachine.Builder.Task;
  190. }
  191. /// <summary>
  192. /// Manual implementation of the async state machine
  193. /// </summary>
  194. [StructLayout(LayoutKind.Auto)]
  195. struct AsyncStateMachine : IAsyncStateMachine
  196. {
  197. enum State
  198. {
  199. Running = 0,
  200. //Await is the state where the task is awaited
  201. Await,
  202. //End is the state where the function is done
  203. End
  204. }
  205. public VirtualMachineExecutionContext Context;
  206. public AsyncValueTaskMethodBuilder<int> Builder;
  207. State state;
  208. PostOperationType postOperation;
  209. #if NET6_0_OR_GREATER
  210. [MethodImpl(MethodImplOptions.AggressiveOptimization)]
  211. #endif
  212. public void MoveNext()
  213. {
  214. //If the state is end, the function is done, so set the result and return. I think this state is not reachable in this implementation
  215. if (state == State.End)
  216. {
  217. Builder.SetResult(Context.ResultCount);
  218. return;
  219. }
  220. ref var context = ref Context;
  221. try
  222. {
  223. //If the state is State.Await, it means the task is awaited, so get the result and continue
  224. if (state == State.Await)
  225. {
  226. context.TaskResult = context.Awaiter.GetResult();
  227. context.Awaiter = default;
  228. context.Thread.PopCallStackFrame();
  229. switch (postOperation)
  230. {
  231. case PostOperationType.Nop: break;
  232. case PostOperationType.SetResult:
  233. var RA = context.Instruction.A + context.FrameBase;
  234. context.Stack.Get(RA) = context.TaskResult == 0 ? LuaValue.Nil : context.ResultsBuffer[0];
  235. context.Stack.NotifyTop(RA + 1);
  236. context.ClearResultsBuffer();
  237. break;
  238. case PostOperationType.TForCall:
  239. TForCallPostOperation(ref context);
  240. break;
  241. case PostOperationType.Call:
  242. CallPostOperation(ref context);
  243. break;
  244. case PostOperationType.TailCall:
  245. var resultsSpan = context.ResultsBuffer.AsSpan(0, context.TaskResult);
  246. if (!context.PopFromBuffer(resultsSpan))
  247. {
  248. context.ResultCount = context.TaskResult;
  249. resultsSpan.CopyTo(context.Buffer.Span);
  250. state = State.End;
  251. resultsSpan.Clear();
  252. LuaValueArrayPool.Return1024(context.ResultsBuffer);
  253. Builder.SetResult(context.TaskResult);
  254. return;
  255. }
  256. resultsSpan.Clear();
  257. break;
  258. case PostOperationType.Self:
  259. SelfPostOperation(ref context);
  260. break;
  261. case PostOperationType.Compare:
  262. ComparePostOperation(ref context);
  263. break;
  264. }
  265. postOperation = 0;
  266. state = State.Running;
  267. }
  268. //This is a label to restart the execution when new function is called or restarted
  269. Restart:
  270. var instructions = context.Chunk.Instructions;
  271. var frameBase = context.FrameBase;
  272. var stack = context.Stack;
  273. ref var constHead = ref MemoryMarshalEx.UnsafeElementAt(context.Chunk.Constants, 0);
  274. do
  275. {
  276. var instruction = instructions[++context.Pc];
  277. context.Instruction = instruction;
  278. var iA = (int)instruction.A;
  279. var ra1 = iA + frameBase + 1;
  280. switch (instruction.OpCode)
  281. {
  282. case OpCode.Move:
  283. stack.EnsureCapacity(ra1);
  284. ref var stackHead = ref stack.Get(frameBase);
  285. Unsafe.Add(ref stackHead, iA) = Unsafe.Add(ref stackHead, instruction.UIntB);
  286. stack.NotifyTop(ra1);
  287. continue;
  288. case OpCode.LoadK:
  289. stack.EnsureCapacity(ra1);
  290. stack.Get(ra1 - 1) = Unsafe.Add(ref constHead, instruction.Bx);
  291. stack.NotifyTop(ra1);
  292. continue;
  293. case OpCode.LoadBool:
  294. stack.EnsureCapacity(ra1);
  295. stack.Get(ra1 - 1) = instruction.B != 0;
  296. stack.NotifyTop(ra1);
  297. if (instruction.C != 0) context.Pc++;
  298. continue;
  299. case OpCode.LoadNil:
  300. var iB = instruction.B;
  301. stack.EnsureCapacity(ra1 + iB);
  302. stack.GetBuffer().Slice(ra1 - 1, iB + 1).Clear();
  303. stack.NotifyTop(ra1 + iB);
  304. continue;
  305. case OpCode.GetUpVal:
  306. stack.EnsureCapacity(ra1);
  307. stack.Get(ra1 - 1) = context.Closure.GetUpValue(instruction.B);
  308. stack.NotifyTop(ra1);
  309. continue;
  310. case OpCode.GetTabUp:
  311. stack.EnsureCapacity(ra1);
  312. stackHead = ref stack.Get(frameBase);
  313. ref readonly var vc = ref RKC(ref stackHead, ref constHead, instruction);
  314. var table = context.Closure.GetUpValue(instruction.B);
  315. if (table.TryReadTable(out var luaTable) && luaTable.TryGetValue(vc, out var resultValue))
  316. {
  317. Unsafe.Add(ref stackHead, iA) = resultValue;
  318. stack.NotifyTop(ra1);
  319. continue;
  320. }
  321. if (TryGetMetaTableValue(table, vc, ref context, out var doRestart))
  322. {
  323. if (doRestart) goto Restart;
  324. continue;
  325. }
  326. postOperation = PostOperationType.SetResult;
  327. break;
  328. case OpCode.GetTable:
  329. stack.EnsureCapacity(ra1);
  330. stackHead = ref stack.Get(frameBase);
  331. ref readonly var vb = ref Unsafe.Add(ref stackHead, instruction.UIntB);
  332. vc = ref RKC(ref stackHead, ref constHead, instruction);
  333. if (vb.TryReadTable(out luaTable))
  334. {
  335. if (luaTable.TryGetValue(vc, out resultValue))
  336. {
  337. Unsafe.Add(ref stackHead, iA) = resultValue;
  338. stack.NotifyTop(ra1);
  339. continue;
  340. }
  341. }
  342. if (TryGetMetaTableValue(vb, vc, ref context, out doRestart))
  343. {
  344. if (doRestart) goto Restart;
  345. continue;
  346. }
  347. postOperation = PostOperationType.SetResult;
  348. break;
  349. case OpCode.SetTabUp:
  350. stackHead = ref stack.Get(frameBase);
  351. vb = ref RKB(ref stackHead, ref constHead, instruction);
  352. if (vb.TryReadNumber(out var numB))
  353. {
  354. if (double.IsNaN(numB))
  355. {
  356. ThrowLuaRuntimeException(ref context, "table index is NaN");
  357. return;
  358. }
  359. }
  360. table = context.Closure.GetUpValue(instruction.A);
  361. if (table.TryReadTable(out luaTable))
  362. {
  363. luaTable[vb] = RKC(ref stackHead, ref constHead, instruction);
  364. continue;
  365. }
  366. vc = ref RKC(ref stackHead, ref constHead, instruction);
  367. if (TrySetMetaTableValue(table, vb, vc, ref context, out doRestart))
  368. {
  369. if (doRestart) goto Restart;
  370. continue;
  371. }
  372. postOperation = PostOperationType.Nop;
  373. break;
  374. case OpCode.SetUpVal:
  375. context.Closure.SetUpValue(instruction.B, stack.Get(ra1 - 1));
  376. continue;
  377. case OpCode.SetTable:
  378. stackHead = ref stack.Get(frameBase);
  379. vb = ref RKB(ref stackHead, ref constHead, instruction);
  380. if (vb.TryReadNumber(out numB))
  381. {
  382. if (double.IsNaN(numB))
  383. {
  384. ThrowLuaRuntimeException(ref context, " table index is NaN");
  385. return;
  386. }
  387. }
  388. table = Unsafe.Add(ref stackHead, iA);
  389. if (table.TryReadTable(out luaTable))
  390. {
  391. if (luaTable.Metatable == null || !luaTable.Metatable!.ContainsKey(Metamethods.NewIndex))
  392. {
  393. luaTable[vb] = RKC(ref stackHead, ref constHead, instruction);
  394. continue;
  395. }
  396. if (luaTable.ContainsKey(vb))
  397. {
  398. luaTable[vb] = RKC(ref stackHead, ref constHead, instruction);
  399. continue;
  400. }
  401. }
  402. vc = ref RKC(ref stackHead, ref constHead, instruction);
  403. if (TrySetMetaTableValue(table, vb, vc, ref context, out doRestart))
  404. {
  405. if (doRestart) goto Restart;
  406. continue;
  407. }
  408. postOperation = PostOperationType.Nop;
  409. break;
  410. case OpCode.NewTable:
  411. stack.EnsureCapacity(ra1);
  412. stack.Get(ra1 - 1) = new LuaTable(instruction.B, instruction.C);
  413. stack.NotifyTop(ra1);
  414. continue;
  415. case OpCode.Self:
  416. stack.EnsureCapacity(ra1 + 1);
  417. stackHead = ref stack.Get(frameBase);
  418. vc = ref RKC(ref stackHead, ref constHead, instruction);
  419. table = Unsafe.Add(ref stackHead, instruction.UIntB);
  420. if (table.TryReadTable(out luaTable) && luaTable.TryGetValue(vc, out resultValue))
  421. {
  422. Unsafe.Add(ref stackHead, iA) = resultValue;
  423. Unsafe.Add(ref stackHead, iA + 1) = table;
  424. stack.NotifyTop(ra1 + 2);
  425. continue;
  426. }
  427. if (TryGetMetaTableValue(table, vc, ref context, out doRestart))
  428. {
  429. if (doRestart) goto Restart;
  430. continue;
  431. }
  432. postOperation = PostOperationType.Self;
  433. break;
  434. case OpCode.Add:
  435. stack.EnsureCapacity(ra1);
  436. stackHead = ref stack.Get(frameBase);
  437. vb = ref RKB(ref stackHead, ref constHead, instruction);
  438. vc = ref RKC(ref stackHead, ref constHead, instruction);
  439. if(vb.Type == LuaValueType.Number && vc.Type== LuaValueType.Number)
  440. {
  441. Unsafe.Add(ref stackHead, iA) = vb.UnsafeReadDouble() + vc.UnsafeReadDouble() ;
  442. stack.NotifyTop(ra1);
  443. continue;
  444. }
  445. if (vb.TryReadDouble(out numB) && vc.TryReadDouble(out var numC))
  446. {
  447. Unsafe.Add(ref stackHead, iA) = numB + numC;
  448. stack.NotifyTop(ra1);
  449. continue;
  450. }
  451. if (ExecuteBinaryOperationMetaMethod(vb, vc, ref context, Metamethods.Add, "add", out doRestart))
  452. {
  453. if (doRestart) goto Restart;
  454. continue;
  455. }
  456. postOperation = PostOperationType.SetResult;
  457. break;
  458. case OpCode.Sub:
  459. stack.EnsureCapacity(ra1);
  460. stackHead = ref stack.Get(frameBase);
  461. vb = ref RKB(ref stackHead, ref constHead, instruction);
  462. vc = ref RKC(ref stackHead, ref constHead, instruction);
  463. if(vb.Type == LuaValueType.Number && vc.Type== LuaValueType.Number)
  464. {
  465. Unsafe.Add(ref stackHead, iA) = vb.UnsafeReadDouble() - vc.UnsafeReadDouble() ;
  466. stack.NotifyTop(ra1);
  467. continue;
  468. }
  469. if (vb.TryReadDouble(out numB) && vc.TryReadDouble(out numC))
  470. {
  471. Unsafe.Add(ref stackHead, iA) = numB - numC;
  472. stack.NotifyTop(ra1);
  473. continue;
  474. }
  475. if (ExecuteBinaryOperationMetaMethod(vb, vc, ref context, Metamethods.Sub, "sub", out doRestart))
  476. {
  477. if (doRestart) goto Restart;
  478. continue;
  479. }
  480. postOperation = PostOperationType.SetResult;
  481. break;
  482. case OpCode.Mul:
  483. stack.EnsureCapacity(ra1);
  484. stackHead = ref stack.Get(frameBase);
  485. vb = ref RKB(ref stackHead, ref constHead, instruction);
  486. vc = ref RKC(ref stackHead, ref constHead, instruction);
  487. if(vb.Type == LuaValueType.Number && vc.Type== LuaValueType.Number)
  488. {
  489. Unsafe.Add(ref stackHead, iA) = vb.UnsafeReadDouble() * vc.UnsafeReadDouble() ;
  490. stack.NotifyTop(ra1);
  491. continue;
  492. }
  493. if (vb.TryReadDouble(out numB) && vc.TryReadDouble(out numC))
  494. {
  495. Unsafe.Add(ref stackHead, iA) = numB * numC;
  496. stack.NotifyTop(ra1);
  497. continue;
  498. }
  499. if (ExecuteBinaryOperationMetaMethod(vb, vc, ref context, Metamethods.Mul, "mul", out doRestart))
  500. {
  501. if (doRestart) goto Restart;
  502. continue;
  503. }
  504. postOperation = PostOperationType.SetResult;
  505. break;
  506. case OpCode.Div:
  507. stack.EnsureCapacity(ra1);
  508. stackHead = ref stack.Get(frameBase);
  509. vb = ref RKB(ref stackHead, ref constHead, instruction);
  510. vc = ref RKC(ref stackHead, ref constHead, instruction);
  511. if(vb.Type == LuaValueType.Number && vc.Type== LuaValueType.Number)
  512. {
  513. Unsafe.Add(ref stackHead, iA) = vb.UnsafeReadDouble() / vc.UnsafeReadDouble() ;
  514. stack.NotifyTop(ra1);
  515. continue;
  516. }
  517. if (vb.TryReadDouble(out numB) && vc.TryReadDouble(out numC))
  518. {
  519. Unsafe.Add(ref stackHead, iA) = numB / numC;
  520. stack.NotifyTop(ra1);
  521. continue;
  522. }
  523. if (ExecuteBinaryOperationMetaMethod(vb, vc, ref context, Metamethods.Div, "div", out doRestart))
  524. {
  525. if (doRestart) goto Restart;
  526. continue;
  527. }
  528. postOperation = PostOperationType.SetResult;
  529. break;
  530. case OpCode.Mod:
  531. stack.EnsureCapacity(ra1);
  532. stackHead = ref stack.Get(frameBase);
  533. vb = ref RKB(ref stackHead, ref constHead, instruction);
  534. vc = ref RKC(ref stackHead, ref constHead, instruction);
  535. if (vb.TryReadDouble(out numB) && vc.TryReadDouble(out numC))
  536. {
  537. var mod = numB % numC;
  538. if ((numC > 0 && mod < 0) || (numC < 0 && mod > 0))
  539. {
  540. mod += numC;
  541. }
  542. Unsafe.Add(ref stackHead, iA) = mod;
  543. stack.NotifyTop(ra1);
  544. continue;
  545. }
  546. if (ExecuteBinaryOperationMetaMethod(vb, vc, ref context, Metamethods.Mod, "mod", out doRestart))
  547. {
  548. if (doRestart) goto Restart;
  549. continue;
  550. }
  551. postOperation = PostOperationType.SetResult;
  552. break;
  553. case OpCode.Pow:
  554. stack.EnsureCapacity(ra1);
  555. stackHead = ref stack.Get(frameBase);
  556. vb = ref RKB(ref stackHead, ref constHead, instruction);
  557. vc = ref RKC(ref stackHead, ref constHead, instruction);
  558. if (vb.TryReadDouble(out numB) && vc.TryReadDouble(out numC))
  559. {
  560. Unsafe.Add(ref stackHead, iA) = Math.Pow(numB, numC);
  561. stack.NotifyTop(ra1);
  562. continue;
  563. }
  564. if (ExecuteBinaryOperationMetaMethod(vb, vc, ref context, Metamethods.Pow, "pow", out doRestart))
  565. {
  566. if (doRestart) goto Restart;
  567. continue;
  568. }
  569. postOperation = PostOperationType.SetResult;
  570. break;
  571. case OpCode.Unm:
  572. stack.EnsureCapacity(ra1);
  573. stackHead = ref stack.Get(frameBase);
  574. vb = ref Unsafe.Add(ref stackHead, instruction.UIntB);
  575. if (vb.TryReadDouble(out numB))
  576. {
  577. Unsafe.Add(ref stackHead, iA) = -numB;
  578. stack.NotifyTop(ra1);
  579. continue;
  580. }
  581. if (ExecuteUnaryOperationMetaMethod(vb, ref context, Metamethods.Unm, "unm", false, out doRestart))
  582. {
  583. if (doRestart) goto Restart;
  584. continue;
  585. }
  586. postOperation = PostOperationType.SetResult;
  587. break;
  588. case OpCode.Not:
  589. stack.EnsureCapacity(ra1);
  590. stackHead = ref stack.Get(frameBase);
  591. Unsafe.Add(ref stackHead, iA) = !Unsafe.Add(ref stackHead, instruction.UIntB).ToBoolean();
  592. stack.NotifyTop(ra1);
  593. continue;
  594. case OpCode.Len:
  595. stack.EnsureCapacity(ra1);
  596. stackHead = ref stack.Get(frameBase);
  597. vb = ref Unsafe.Add(ref stackHead, instruction.UIntB);
  598. if (vb.TryReadString(out var str))
  599. {
  600. Unsafe.Add(ref stackHead, iA) = str.Length;
  601. stack.NotifyTop(ra1);
  602. continue;
  603. }
  604. if (ExecuteUnaryOperationMetaMethod(vb, ref context, Metamethods.Len, "get length of", true, out doRestart))
  605. {
  606. if (doRestart) goto Restart;
  607. continue;
  608. }
  609. postOperation = PostOperationType.SetResult;
  610. break;
  611. case OpCode.Concat:
  612. if (Concat(ref context, out doRestart))
  613. {
  614. if (doRestart) goto Restart;
  615. continue;
  616. }
  617. postOperation = PostOperationType.SetResult;
  618. break;
  619. case OpCode.Jmp:
  620. context.Pc += instruction.SBx;
  621. if (iA != 0)
  622. {
  623. context.State.CloseUpValues(context.Thread, iA - 1);
  624. }
  625. continue;
  626. case OpCode.Eq:
  627. stack.EnsureCapacity(ra1);
  628. stackHead = ref stack.Get(frameBase);
  629. vb = ref RKB(ref stackHead, ref constHead, instruction);
  630. vc = ref RKC(ref stackHead, ref constHead, instruction);
  631. if (vb == vc)
  632. {
  633. if (iA != 1)
  634. {
  635. context.Pc++;
  636. }
  637. continue;
  638. }
  639. if (ExecuteCompareOperationMetaMethod(vb, vc, ref context, Metamethods.Eq, null, out doRestart))
  640. {
  641. if (doRestart) goto Restart;
  642. continue;
  643. }
  644. postOperation = PostOperationType.Compare;
  645. break;
  646. case OpCode.Lt:
  647. stack.EnsureCapacity(ra1);
  648. stackHead = ref stack.Get(frameBase);
  649. vb = ref RKB(ref stackHead, ref constHead, instruction);
  650. vc = ref RKC(ref stackHead, ref constHead, instruction);
  651. if (vb.TryReadNumber(out numB) && vc.TryReadNumber(out numC))
  652. {
  653. var compareResult = numB < numC;
  654. if (compareResult != (iA == 1))
  655. {
  656. context.Pc++;
  657. }
  658. continue;
  659. }
  660. if (vb.TryReadString(out var strB) && vc.TryReadString(out var strC))
  661. {
  662. var compareResult = StringComparer.Ordinal.Compare(strB, strC) < 0;
  663. if (compareResult != (iA == 1))
  664. {
  665. context.Pc++;
  666. }
  667. continue;
  668. }
  669. if (ExecuteCompareOperationMetaMethod(vb, vc, ref context, Metamethods.Lt, "less than", out doRestart))
  670. {
  671. if (doRestart) goto Restart;
  672. continue;
  673. }
  674. postOperation = PostOperationType.Compare;
  675. break;
  676. case OpCode.Le:
  677. stack.EnsureCapacity(ra1);
  678. stackHead = ref stack.Get(frameBase);
  679. vb = ref RKB(ref stackHead, ref constHead, instruction);
  680. vc = ref RKC(ref stackHead, ref constHead, instruction);
  681. if (vb.TryReadNumber(out numB) && vc.TryReadNumber(out numC))
  682. {
  683. var compareResult = numB <= numC;
  684. if (compareResult != (iA == 1))
  685. {
  686. context.Pc++;
  687. }
  688. continue;
  689. }
  690. if (vb.TryReadString(out strB) && vc.TryReadString(out strC))
  691. {
  692. var compareResult = StringComparer.Ordinal.Compare(strB, strC) <= 0;
  693. if (compareResult != (iA == 1))
  694. {
  695. context.Pc++;
  696. }
  697. continue;
  698. }
  699. if (ExecuteCompareOperationMetaMethod(vb, vc, ref context, Metamethods.Le, "less than or equals", out doRestart))
  700. {
  701. if (doRestart) goto Restart;
  702. continue;
  703. }
  704. postOperation = PostOperationType.Compare;
  705. break;
  706. case OpCode.Test:
  707. if (stack.Get(ra1 - 1).ToBoolean() != (instruction.C == 1))
  708. {
  709. context.Pc++;
  710. }
  711. continue;
  712. case OpCode.TestSet:
  713. vb = ref stack.Get(instruction.B + frameBase);
  714. if (vb.ToBoolean() != (instruction.C == 1))
  715. {
  716. context.Pc++;
  717. }
  718. else
  719. {
  720. stack.Get(ra1 - 1) = vb;
  721. stack.NotifyTop(ra1);
  722. }
  723. continue;
  724. case OpCode.Call:
  725. if (Call(ref context, out doRestart))
  726. {
  727. if (doRestart) goto Restart;
  728. continue;
  729. }
  730. postOperation = PostOperationType.Call;
  731. break;
  732. case OpCode.TailCall:
  733. if (TailCall(ref context, out doRestart))
  734. {
  735. if (doRestart) goto Restart;
  736. if (context.IsTopLevel) goto End;
  737. continue;
  738. }
  739. postOperation = PostOperationType.TailCall;
  740. break;
  741. case OpCode.Return:
  742. context.State.CloseUpValues(context.Thread, frameBase);
  743. if (context.Pop(instruction, frameBase)) goto Restart;
  744. var retCount = instruction.B - 1;
  745. if (retCount == -1)
  746. {
  747. retCount = stack.Count - (ra1 - 1);
  748. }
  749. if (0 < retCount)
  750. {
  751. stack.GetBuffer().Slice(ra1 - 1, retCount).CopyTo(context.Buffer.Span);
  752. }
  753. context.ResultCount = retCount;
  754. goto End;
  755. case OpCode.ForLoop:
  756. stack.EnsureCapacity(ra1 + 3);
  757. ref var indexRef = ref stack.Get(ra1 - 1);
  758. var lastIndex = indexRef.UnsafeReadDouble();
  759. var step = Unsafe.Add(ref indexRef, 2).UnsafeReadDouble();
  760. var index = lastIndex + step;
  761. var limit = Unsafe.Add(ref indexRef, 1).UnsafeReadDouble();
  762. if (step >= 0 ? index <= limit : limit <= index)
  763. {
  764. context.Pc += instruction.SBx;
  765. indexRef = index;
  766. Unsafe.Add(ref indexRef, 3) = index;
  767. stack.NotifyTop(ra1 + 3);
  768. continue;
  769. }
  770. stack.NotifyTop(ra1);
  771. continue;
  772. case OpCode.ForPrep:
  773. indexRef = ref stack.Get(ra1 - 1);
  774. if (!indexRef.TryReadDouble(out var init))
  775. {
  776. ThrowLuaRuntimeException(ref context, "'for' initial value must be a number");
  777. return;
  778. }
  779. if (!LuaValue.TryReadOrSetDouble(ref Unsafe.Add(ref indexRef, 1), out _))
  780. {
  781. ThrowLuaRuntimeException(ref context, "'for' limit must be a number");
  782. return;
  783. }
  784. if (!LuaValue.TryReadOrSetDouble(ref Unsafe.Add(ref indexRef, 2), out step))
  785. {
  786. ThrowLuaRuntimeException(ref context, "'for' step must be a number");
  787. return;
  788. }
  789. indexRef = init - step;
  790. stack.NotifyTop(ra1);
  791. context.Pc += instruction.SBx;
  792. continue;
  793. case OpCode.TForCall:
  794. if (TForCall(ref context, out doRestart))
  795. {
  796. if (doRestart) goto Restart;
  797. continue;
  798. }
  799. postOperation = PostOperationType.TForCall;
  800. break;
  801. case OpCode.TForLoop:
  802. ref var forState = ref stack.Get(ra1);
  803. if (forState.Type is not LuaValueType.Nil)
  804. {
  805. Unsafe.Add(ref forState, -1) = forState;
  806. context.Pc += instruction.SBx;
  807. }
  808. continue;
  809. case OpCode.SetList:
  810. SetList(ref context);
  811. continue;
  812. case OpCode.Closure:
  813. stack.EnsureCapacity(ra1);
  814. stack.Get(ra1 - 1) = new Closure(context.State, context.Chunk.Functions[instruction.SBx]);
  815. stack.NotifyTop(ra1);
  816. continue;
  817. case OpCode.VarArg:
  818. var frameVariableArgumentCount = context.VariableArgumentCount;
  819. var count = instruction.B == 0
  820. ? frameVariableArgumentCount
  821. : instruction.B - 1;
  822. var ra = ra1 - 1;
  823. stack.EnsureCapacity(ra + count);
  824. stackHead = ref stack.Get(0);
  825. for (int i = 0; i < count; i++)
  826. {
  827. Unsafe.Add(ref stackHead,ra + i) = frameVariableArgumentCount > i
  828. ? Unsafe.Add(ref stackHead,frameBase - (frameVariableArgumentCount - i))
  829. : default;
  830. }
  831. stack.NotifyTop(ra + count);
  832. continue;
  833. case OpCode.ExtraArg:
  834. default:
  835. ThrowLuaNotImplementedException(ref context, instruction.OpCode);
  836. return;
  837. }
  838. } while (postOperation == PostOperationType.None);
  839. //Set the state to await and return with setting this method as the task's continuation
  840. state = State.Await;
  841. Builder.AwaitOnCompleted(ref context.Awaiter, ref this);
  842. return;
  843. End:
  844. state = State.End;
  845. LuaValueArrayPool.Return1024(context.ResultsBuffer);
  846. Builder.SetResult(context.ResultCount);
  847. }
  848. catch (Exception e)
  849. {
  850. if (e is not LuaRuntimeException)
  851. {
  852. Console.WriteLine(GetTracebacks(ref context));
  853. }
  854. context.PopOnTopCallStackFrames();
  855. context.State.CloseUpValues(context.Thread, context.FrameBase);
  856. LuaValueArrayPool.Return1024(context.ResultsBuffer, true);
  857. state = State.End;
  858. context = default;
  859. Builder.SetException(e);
  860. }
  861. }
  862. [DebuggerHidden]
  863. public void SetStateMachine(IAsyncStateMachine stateMachine)
  864. {
  865. Builder.SetStateMachine(stateMachine);
  866. }
  867. static void ThrowLuaRuntimeException(ref VirtualMachineExecutionContext context, string message)
  868. {
  869. throw new LuaRuntimeException(context.State.GetTraceback(), message);
  870. }
  871. static void ThrowLuaNotImplementedException(ref VirtualMachineExecutionContext context, OpCode opcode)
  872. {
  873. throw new LuaRuntimeException(context.State.GetTraceback(), $"OpCode {opcode} is not implemented");
  874. }
  875. }
  876. static void SelfPostOperation(ref VirtualMachineExecutionContext context)
  877. {
  878. var stack = context.Stack;
  879. var instruction = context.Instruction;
  880. var RA = instruction.A + context.FrameBase;
  881. var RB = instruction.B + context.FrameBase;
  882. ref var stackHead = ref stack.Get(0);
  883. var table = Unsafe.Add(ref stackHead, RB);
  884. Unsafe.Add(ref stackHead, RA + 1) = table;
  885. Unsafe.Add(ref stackHead, RA) = context.TaskResult == 0 ? LuaValue.Nil : context.ResultsBuffer[0];
  886. stack.NotifyTop(RA + 2);
  887. context.ClearResultsBuffer();
  888. }
  889. static bool Concat(ref VirtualMachineExecutionContext context, out bool doRestart)
  890. {
  891. var instruction = context.Instruction;
  892. var stack = context.Stack;
  893. var RA = instruction.A + context.FrameBase;
  894. stack.EnsureCapacity(RA + 1);
  895. ref var stackHead = ref stack.Get(context.FrameBase);
  896. ref var constHead = ref context.Chunk.Constants[0];
  897. var vb = RKB(ref stackHead, ref constHead, instruction);
  898. var vc = RKC(ref stackHead, ref constHead, instruction);
  899. var bIsValid = vb.TryReadString(out var strB);
  900. var cIsValid = vc.TryReadString(out var strC);
  901. if (!bIsValid && vb.TryReadDouble(out var numB))
  902. {
  903. strB = numB.ToString(CultureInfo.InvariantCulture);
  904. bIsValid = true;
  905. }
  906. if (!cIsValid && vc.TryReadDouble(out var numC))
  907. {
  908. strC = numC.ToString(CultureInfo.InvariantCulture);
  909. cIsValid = true;
  910. }
  911. if (bIsValid && cIsValid)
  912. {
  913. stack.Get(RA) = strB + strC;
  914. stack.NotifyTop(RA + 1);
  915. doRestart = false;
  916. return true;
  917. }
  918. return ExecuteBinaryOperationMetaMethod(vb, vc, ref context, Metamethods.Concat, "concat", out doRestart);
  919. }
  920. static bool Call(ref VirtualMachineExecutionContext context, out bool doRestart)
  921. {
  922. var instruction = context.Instruction;
  923. var RA = instruction.A + context.FrameBase;
  924. var va = context.Stack.Get(RA);
  925. if (!va.TryReadFunction(out var func))
  926. {
  927. if (va.TryGetMetamethod(context.State, Metamethods.Call, out var metamethod) &&
  928. metamethod.TryReadFunction(out func))
  929. {
  930. }
  931. else
  932. {
  933. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), "call", metamethod);
  934. }
  935. }
  936. var thread = context.Thread;
  937. var (newBase, argumentCount, variableArgumentCount) = PrepareForFunctionCall(thread, func, instruction, RA);
  938. var newFrame = func.CreateNewFrame(ref context, newBase, variableArgumentCount);
  939. thread.PushCallStackFrame(newFrame);
  940. if (func.IsClosure)
  941. {
  942. context.Push(newFrame);
  943. doRestart = true;
  944. return true;
  945. }
  946. doRestart = false;
  947. return FuncCall(ref context, in newFrame, func, newBase, argumentCount);
  948. static bool FuncCall(ref VirtualMachineExecutionContext context, in CallStackFrame newFrame, LuaFunction func, int newBase, int argumentCount)
  949. {
  950. {
  951. var task = func.Invoke(ref context, newFrame, argumentCount);
  952. var awaiter = task.GetAwaiter();
  953. if (!awaiter.IsCompleted)
  954. {
  955. context.Awaiter = awaiter;
  956. return false;
  957. }
  958. context.Thread.PopCallStackFrameUnsafe(newBase);
  959. context.TaskResult = awaiter.GetResult();
  960. var instruction = context.Instruction;
  961. var rawResultCount = context.TaskResult;
  962. var resultCount = rawResultCount;
  963. var ic = instruction.C;
  964. if (ic != 0)
  965. {
  966. resultCount = ic - 1;
  967. }
  968. if (resultCount == 0)
  969. {
  970. context.Stack.Pop();
  971. }
  972. else
  973. {
  974. var stack = context.Stack;
  975. var RA = instruction.A + context.FrameBase;
  976. stack.EnsureCapacity(RA + resultCount);
  977. ref var stackHead = ref stack.Get(RA);
  978. var results = context.ResultsBuffer.AsSpan(0, rawResultCount);
  979. for (int i = 0; i < resultCount; i++)
  980. {
  981. Unsafe.Add(ref stackHead, i) = i >= rawResultCount
  982. ? default
  983. : results[i];
  984. }
  985. stack.NotifyTop(RA + resultCount);
  986. results.Clear();
  987. }
  988. return true;
  989. }
  990. }
  991. }
  992. static void CallPostOperation(ref VirtualMachineExecutionContext context)
  993. {
  994. var instruction = context.Instruction;
  995. var rawResultCount = context.TaskResult;
  996. var resultCount = rawResultCount;
  997. var ic = instruction.C;
  998. if (ic != 0)
  999. {
  1000. resultCount = ic - 1;
  1001. }
  1002. if (resultCount == 0)
  1003. {
  1004. context.Stack.Pop();
  1005. }
  1006. else
  1007. {
  1008. var stack = context.Stack;
  1009. var RA = instruction.A + context.FrameBase;
  1010. stack.EnsureCapacity(RA + resultCount);
  1011. ref var stackHead = ref stack.Get(RA);
  1012. var results = context.ResultsBuffer.AsSpan(0, rawResultCount);
  1013. for (int i = 0; i < resultCount; i++)
  1014. {
  1015. Unsafe.Add(ref stackHead, i) = i >= rawResultCount
  1016. ? default
  1017. : results[i];
  1018. }
  1019. stack.NotifyTop(RA + resultCount);
  1020. results.Clear();
  1021. }
  1022. }
  1023. static bool TailCall(ref VirtualMachineExecutionContext context, out bool doRestart)
  1024. {
  1025. var instruction = context.Instruction;
  1026. var stack = context.Stack;
  1027. var RA = instruction.A + context.FrameBase;
  1028. var state = context.State;
  1029. var thread = context.Thread;
  1030. state.CloseUpValues(thread, context.FrameBase);
  1031. var va = stack.Get(RA);
  1032. if (!va.TryReadFunction(out var func))
  1033. {
  1034. if (!va.TryGetMetamethod(state, Metamethods.Call, out var metamethod) &&
  1035. !metamethod.TryReadFunction(out func))
  1036. {
  1037. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), "call", metamethod);
  1038. }
  1039. }
  1040. var (newBase, argumentCount, variableArgumentCount) = PrepareForFunctionTailCall(thread, func, instruction, RA);
  1041. var newFrame = func.CreateNewFrame(ref context, newBase, variableArgumentCount);
  1042. thread.PushCallStackFrame(newFrame);
  1043. context.Push(newFrame);
  1044. if (func is Closure)
  1045. {
  1046. doRestart = true;
  1047. return true;
  1048. }
  1049. doRestart = false;
  1050. var task = func.Invoke(ref context, newFrame, argumentCount);
  1051. var awaiter = task.GetAwaiter();
  1052. if (!awaiter.IsCompleted)
  1053. {
  1054. context.Awaiter = awaiter;
  1055. return false;
  1056. }
  1057. context.Thread.PopCallStackFrame();
  1058. doRestart = true;
  1059. var resultCount = awaiter.GetResult();
  1060. var resultsSpan = context.ResultsBuffer.AsSpan(0, resultCount);
  1061. if (!context.PopFromBuffer(resultsSpan))
  1062. {
  1063. doRestart = false;
  1064. context.ResultCount = resultCount;
  1065. resultsSpan.CopyTo(context.Buffer.Span);
  1066. }
  1067. resultsSpan.Clear();
  1068. return true;
  1069. }
  1070. static bool TForCall(ref VirtualMachineExecutionContext context, out bool doRestart)
  1071. {
  1072. doRestart = false;
  1073. var instruction = context.Instruction;
  1074. var stack = context.Stack;
  1075. var RA = instruction.A + context.FrameBase;
  1076. var iteratorRaw = stack.Get(RA);
  1077. if (!iteratorRaw.TryReadFunction(out var iterator))
  1078. {
  1079. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), "call", iteratorRaw);
  1080. }
  1081. var newBase = RA + 3 + instruction.C;
  1082. stack.Get(newBase) = stack.Get(RA + 1);
  1083. stack.Get(newBase + 1) = stack.Get(RA + 2);
  1084. stack.NotifyTop(newBase + 2);
  1085. var newFrame = iterator.CreateNewFrame(ref context, newBase);
  1086. context.Thread.PushCallStackFrame(newFrame);
  1087. if (iterator.IsClosure)
  1088. {
  1089. context.Push(newFrame);
  1090. doRestart = true;
  1091. return true;
  1092. }
  1093. var task = iterator.Invoke(ref context, newFrame, 2);
  1094. var awaiter = task.GetAwaiter();
  1095. if (!awaiter.IsCompleted)
  1096. {
  1097. context.Awaiter = awaiter;
  1098. return false;
  1099. }
  1100. context.TaskResult = awaiter.GetResult();
  1101. context.Thread.PopCallStackFrame();
  1102. TForCallPostOperation(ref context);
  1103. return true;
  1104. }
  1105. static void TForCallPostOperation(ref VirtualMachineExecutionContext context)
  1106. {
  1107. var stack = context.Stack;
  1108. var instruction = context.Instruction;
  1109. var RA = instruction.A + context.FrameBase;
  1110. var resultBuffer = context.ResultsBuffer;
  1111. var resultCount = context.TaskResult;
  1112. stack.EnsureCapacity(RA + instruction.C + 3);
  1113. for (int i = 1; i <= instruction.C; i++)
  1114. {
  1115. var index = i - 1;
  1116. stack.Get(RA + 2 + i) = index >= resultCount
  1117. ? LuaValue.Nil
  1118. : resultBuffer[i - 1];
  1119. }
  1120. stack.NotifyTop(RA + instruction.C + 3);
  1121. context.ClearResultsBuffer(resultCount);
  1122. }
  1123. static void SetList(ref VirtualMachineExecutionContext context)
  1124. {
  1125. var instruction = context.Instruction;
  1126. var stack = context.Stack;
  1127. var RA = instruction.A + context.FrameBase;
  1128. if (!stack.Get(RA).TryReadTable(out var table))
  1129. {
  1130. throw new LuaException("internal error");
  1131. }
  1132. var count = instruction.B == 0
  1133. ? stack.Count - (RA + 1)
  1134. : instruction.B;
  1135. table.EnsureArrayCapacity((instruction.C - 1) * 50 + count);
  1136. stack.AsSpan().Slice(RA + 1, count)
  1137. .CopyTo(table.GetArraySpan()[((instruction.C - 1) * 50)..]);
  1138. }
  1139. static void ComparePostOperation(ref VirtualMachineExecutionContext context)
  1140. {
  1141. var compareResult = context.TaskResult != 0 && context.ResultsBuffer[0].ToBoolean();
  1142. if (compareResult != (context.Instruction.A == 1))
  1143. {
  1144. context.Pc++;
  1145. }
  1146. context.ClearResultsBuffer();
  1147. }
  1148. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1149. static ref readonly LuaValue RKB(ref LuaValue stack, ref LuaValue constants, Instruction instruction)
  1150. {
  1151. var index = instruction.UIntB;
  1152. return ref (index >= 256 ? ref Unsafe.Add(ref constants, index - 256) : ref Unsafe.Add(ref stack, index));
  1153. }
  1154. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1155. static ref readonly LuaValue RKC(ref LuaValue stack, ref LuaValue constants, Instruction instruction)
  1156. {
  1157. var index = instruction.UIntC;
  1158. return ref (index >= 256 ? ref Unsafe.Add(ref constants, index - 256) : ref Unsafe.Add(ref stack, index));
  1159. }
  1160. [MethodImpl(MethodImplOptions.NoInlining)]
  1161. static bool TryGetMetaTableValue(LuaValue table, LuaValue key, ref VirtualMachineExecutionContext context, out bool doRestart)
  1162. {
  1163. var isSelf = context.Instruction.OpCode == OpCode.Self;
  1164. doRestart = false;
  1165. var state = context.State;
  1166. if (table.TryGetMetamethod(state, Metamethods.Index, out var metamethod))
  1167. {
  1168. if (!metamethod.TryReadFunction(out var indexTable))
  1169. {
  1170. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), "call", metamethod);
  1171. }
  1172. var stack = context.Stack;
  1173. stack.Push(table);
  1174. stack.Push(key);
  1175. var newFrame = indexTable.CreateNewFrame(ref context, stack.Count - 2);
  1176. context.Thread.PushCallStackFrame(newFrame);
  1177. if (indexTable.IsClosure)
  1178. {
  1179. context.Push(newFrame);
  1180. doRestart = true;
  1181. return true;
  1182. }
  1183. var task = indexTable.Invoke(ref context, newFrame, 2);
  1184. var awaiter = task.GetAwaiter();
  1185. if (awaiter.IsCompleted)
  1186. {
  1187. context.Thread.PopCallStackFrame();
  1188. var ra = context.Instruction.A + context.FrameBase;
  1189. var resultCount = awaiter.GetResult();
  1190. context.Stack.Get(ra) = resultCount == 0 ? default : context.ResultsBuffer[0];
  1191. if (isSelf)
  1192. {
  1193. context.Stack.Get(ra + 1) = table;
  1194. context.Stack.NotifyTop(ra + 2);
  1195. }
  1196. else
  1197. {
  1198. context.Stack.NotifyTop(ra + 1);
  1199. }
  1200. context.ClearResultsBuffer(resultCount);
  1201. return true;
  1202. }
  1203. context.Awaiter = awaiter;
  1204. return false;
  1205. }
  1206. if (table.Type == LuaValueType.Table)
  1207. {
  1208. var ra = context.Instruction.A + context.FrameBase;
  1209. context.Stack.Get(ra) = default;
  1210. if (isSelf)
  1211. {
  1212. context.Stack.Get(ra + 1) = table;
  1213. context.Stack.NotifyTop(ra + 2);
  1214. }
  1215. else
  1216. {
  1217. context.Stack.NotifyTop(ra + 1);
  1218. }
  1219. return true;
  1220. }
  1221. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), "index", table);
  1222. return false;
  1223. }
  1224. static bool TrySetMetaTableValue(LuaValue table, LuaValue key, LuaValue value,
  1225. ref VirtualMachineExecutionContext context, out bool doRestart)
  1226. {
  1227. doRestart = false;
  1228. var state = context.State;
  1229. if (table.TryGetMetamethod(state, Metamethods.NewIndex, out var metamethod))
  1230. {
  1231. if (!metamethod.TryReadFunction(out var indexTable))
  1232. {
  1233. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), "call", metamethod);
  1234. }
  1235. var thread = context.Thread;
  1236. var stack = thread.Stack;
  1237. stack.Push(table);
  1238. stack.Push(key);
  1239. stack.Push(value);
  1240. var newFrame = indexTable.CreateNewFrame(ref context, stack.Count - 3);
  1241. context.Thread.PushCallStackFrame(newFrame);
  1242. if (indexTable.IsClosure)
  1243. {
  1244. context.Push(newFrame);
  1245. doRestart = true;
  1246. return true;
  1247. }
  1248. var task = indexTable.Invoke(ref context, newFrame, 3);
  1249. var awaiter = task.GetAwaiter();
  1250. if (awaiter.IsCompleted)
  1251. {
  1252. thread.PopCallStackFrame();
  1253. return true;
  1254. }
  1255. context.Awaiter = awaiter;
  1256. return false;
  1257. }
  1258. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), "index", table);
  1259. return false;
  1260. }
  1261. [MethodImpl(MethodImplOptions.NoInlining)]
  1262. static bool ExecuteBinaryOperationMetaMethod(LuaValue vb, LuaValue vc,
  1263. ref VirtualMachineExecutionContext context, string name, string description, out bool doRestart)
  1264. {
  1265. doRestart = false;
  1266. if (vb.TryGetMetamethod(context.State, name, out var metamethod) ||
  1267. vc.TryGetMetamethod(context.State, name, out metamethod))
  1268. {
  1269. if (!metamethod.TryReadFunction(out var func))
  1270. {
  1271. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), "call", metamethod);
  1272. }
  1273. var stack = context.Stack;
  1274. stack.Push(vb);
  1275. stack.Push(vc);
  1276. var newFrame = func.CreateNewFrame(ref context, stack.Count - 2);
  1277. context.Thread.PushCallStackFrame(newFrame);
  1278. if (func.IsClosure)
  1279. {
  1280. context.Push(newFrame);
  1281. doRestart = true;
  1282. return true;
  1283. }
  1284. var task = func.Invoke(ref context, newFrame, 2);
  1285. context.Awaiter = task.GetAwaiter();
  1286. if (context.Awaiter.IsCompleted)
  1287. {
  1288. var resultCount = context.Awaiter.GetResult();
  1289. context.Thread.PopCallStackFrame();
  1290. var RA = context.Instruction.A + context.FrameBase;
  1291. stack.Get(RA) = resultCount == 0 ? LuaValue.Nil : context.ResultsBuffer[0];
  1292. stack.NotifyTop(RA + 1);
  1293. context.ClearResultsBuffer(resultCount);
  1294. return true;
  1295. }
  1296. return false;
  1297. }
  1298. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), description, vb, vc);
  1299. return false;
  1300. }
  1301. [MethodImpl(MethodImplOptions.NoInlining)]
  1302. static bool ExecuteUnaryOperationMetaMethod(LuaValue vb, ref VirtualMachineExecutionContext context,
  1303. string name, string description, bool isLen, out bool doRestart)
  1304. {
  1305. doRestart = false;
  1306. var stack = context.Stack;
  1307. if (vb.TryGetMetamethod(context.State, name, out var metamethod))
  1308. {
  1309. if (!metamethod.TryReadFunction(out var func))
  1310. {
  1311. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), "call", metamethod);
  1312. }
  1313. stack.Push(vb);
  1314. var newFrame = func.CreateNewFrame(ref context, stack.Count - 1);
  1315. context.Thread.PushCallStackFrame(newFrame);
  1316. if (func.IsClosure)
  1317. {
  1318. context.Push(newFrame);
  1319. doRestart = true;
  1320. return true;
  1321. }
  1322. var task = func.Invoke(ref context, newFrame, 1);
  1323. context.Awaiter = task.GetAwaiter();
  1324. if (context.Awaiter.IsCompleted)
  1325. {
  1326. context.Thread.PopCallStackFrame();
  1327. var RA = context.Instruction.A + context.FrameBase;
  1328. var resultCount = context.Awaiter.GetResult();
  1329. stack.Get(RA) = resultCount == 0 ? LuaValue.Nil : context.ResultsBuffer[0];
  1330. stack.NotifyTop(RA + 1);
  1331. context.ClearResultsBuffer(resultCount);
  1332. return true;
  1333. }
  1334. return false;
  1335. }
  1336. if (isLen && vb.TryReadTable(out var table))
  1337. {
  1338. var RA = context.Instruction.A + context.FrameBase;
  1339. stack.Get(RA) = table.ArrayLength;
  1340. stack.NotifyTop(RA + 1);
  1341. return true;
  1342. }
  1343. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), description, vb);
  1344. return true;
  1345. }
  1346. [MethodImpl(MethodImplOptions.NoInlining)]
  1347. static bool ExecuteCompareOperationMetaMethod(LuaValue vb, LuaValue vc,
  1348. ref VirtualMachineExecutionContext context, string name, string? description, out bool doRestart)
  1349. {
  1350. doRestart = false;
  1351. if (vb.TryGetMetamethod(context.State, name, out var metamethod) ||
  1352. vc.TryGetMetamethod(context.State, name, out metamethod))
  1353. {
  1354. if (!metamethod.TryReadFunction(out var func))
  1355. {
  1356. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), "call", metamethod);
  1357. }
  1358. var stack = context.Stack;
  1359. stack.Push(vb);
  1360. stack.Push(vc);
  1361. var newFrame = func.CreateNewFrame(ref context, stack.Count - 2);
  1362. context.Thread.PushCallStackFrame(newFrame);
  1363. if (func.IsClosure)
  1364. {
  1365. context.Push(newFrame);
  1366. doRestart = true;
  1367. return true;
  1368. }
  1369. var task = func.Invoke(ref context, newFrame, 2);
  1370. context.Awaiter = task.GetAwaiter();
  1371. if (context.Awaiter.IsCompleted)
  1372. {
  1373. context.Thread.PopCallStackFrame();
  1374. var resultCount = context.Awaiter.GetResult();
  1375. var compareResult = resultCount != 0 && context.ResultsBuffer[0].ToBoolean();
  1376. if (compareResult != (context.Instruction.A == 1))
  1377. {
  1378. context.Pc++;
  1379. }
  1380. context.ClearResultsBuffer(resultCount);
  1381. return true;
  1382. }
  1383. return false;
  1384. }
  1385. if (description != null)
  1386. {
  1387. LuaRuntimeException.AttemptInvalidOperation(GetTracebacks(ref context), description, vb, vc);
  1388. }
  1389. else
  1390. {
  1391. if (context.Instruction.A == 1)
  1392. {
  1393. context.Pc++;
  1394. }
  1395. }
  1396. return true;
  1397. }
  1398. // 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.
  1399. // see: https://wubingzheng.github.io/build-lua-in-rust/en/ch08-02.arguments.html
  1400. [MethodImpl(MethodImplOptions.NoInlining)]
  1401. static (int FrameBase, int ArgumentCount, int VariableArgumentCount) PrepareVariableArgument(LuaStack stack, int newBase, int argumentCount,
  1402. int variableArgumentCount)
  1403. {
  1404. var temp = newBase;
  1405. newBase += variableArgumentCount;
  1406. stack.EnsureCapacity(newBase + argumentCount);
  1407. stack.NotifyTop(newBase + argumentCount);
  1408. var stackBuffer = stack.GetBuffer()[temp..];
  1409. stackBuffer[..argumentCount].CopyTo(stackBuffer[variableArgumentCount..]);
  1410. stackBuffer.Slice(argumentCount, variableArgumentCount).CopyTo(stackBuffer);
  1411. return (newBase, argumentCount, variableArgumentCount);
  1412. }
  1413. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1414. static (int FrameBase, int ArgumentCount, int VariableArgumentCount) PrepareForFunctionCall(LuaThread thread, LuaFunction function,
  1415. Instruction instruction, int RA)
  1416. {
  1417. var argumentCount = instruction.B - 1;
  1418. if (argumentCount == -1)
  1419. {
  1420. argumentCount = (ushort)(thread.Stack.Count - (RA + 1));
  1421. }
  1422. var newBase = RA + 1;
  1423. var variableArgumentCount = function.GetVariableArgumentCount(argumentCount);
  1424. if (variableArgumentCount <= 0)
  1425. {
  1426. return (newBase, argumentCount, 0);
  1427. }
  1428. return PrepareVariableArgument(thread.Stack, newBase, argumentCount, variableArgumentCount);
  1429. }
  1430. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1431. static (int FrameBase, int ArgumentCount, int VariableArgumentCount) PrepareForFunctionTailCall(LuaThread thread, LuaFunction function,
  1432. Instruction instruction, int RA)
  1433. {
  1434. var stack = thread.Stack;
  1435. var argumentCount = instruction.B - 1;
  1436. if (instruction.B == 0)
  1437. {
  1438. argumentCount = (ushort)(stack.Count - (RA + 1));
  1439. }
  1440. var newBase = RA + 1;
  1441. // In the case of tailcall, the local variables of the caller are immediately discarded, so there is no need to retain them.
  1442. // Therefore, a call can be made without allocating new registers.
  1443. var currentBase = thread.GetCurrentFrame().Base;
  1444. {
  1445. var stackBuffer = stack.GetBuffer();
  1446. if (argumentCount > 0)
  1447. stackBuffer.Slice(newBase, argumentCount).CopyTo(stackBuffer.Slice(currentBase, argumentCount));
  1448. newBase = currentBase;
  1449. }
  1450. var variableArgumentCount = function.GetVariableArgumentCount(argumentCount);
  1451. if (variableArgumentCount <= 0)
  1452. {
  1453. return (newBase, argumentCount, 0);
  1454. }
  1455. return PrepareVariableArgument(thread.Stack, newBase, argumentCount, variableArgumentCount);
  1456. }
  1457. static Traceback GetTracebacks(ref VirtualMachineExecutionContext context)
  1458. {
  1459. return GetTracebacks(context.State, context.Chunk, context.Pc);
  1460. }
  1461. static Traceback GetTracebacks(LuaState state, Chunk chunk, int pc)
  1462. {
  1463. var frame = state.CurrentThread.GetCurrentFrame();
  1464. state.CurrentThread.PushCallStackFrame(frame with
  1465. {
  1466. CallPosition = chunk.SourcePositions[pc],
  1467. ChunkName = chunk.Name,
  1468. RootChunkName = chunk.GetRoot().Name,
  1469. });
  1470. var tracebacks = state.GetTraceback();
  1471. state.CurrentThread.PopCallStackFrame();
  1472. return tracebacks;
  1473. }
  1474. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1475. static CallStackFrame CreateNewFrame(this LuaFunction function, ref VirtualMachineExecutionContext context, int newBase, int variableArgumentCount = 0)
  1476. {
  1477. return new()
  1478. {
  1479. Base = newBase,
  1480. CallPosition = context.Chunk.SourcePositions[context.Pc],
  1481. ChunkName = context.Chunk.Name,
  1482. RootChunkName = context.Chunk.GetRoot().Name,
  1483. Function = function,
  1484. VariableArgumentCount = variableArgumentCount,
  1485. CallerInstructionIndex = context.Pc,
  1486. };
  1487. }
  1488. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1489. static ValueTask<int> Invoke(this LuaFunction function, ref VirtualMachineExecutionContext context, in CallStackFrame frame, int arguments)
  1490. {
  1491. return function.Func(new()
  1492. {
  1493. State = context.State,
  1494. Thread = context.Thread,
  1495. ArgumentCount = arguments,
  1496. FrameBase = frame.Base,
  1497. SourcePosition = frame.CallPosition,
  1498. ChunkName = frame.ChunkName,
  1499. RootChunkName = frame.RootChunkName,
  1500. CallerInstructionIndex = frame.CallerInstructionIndex,
  1501. }, context.ResultsBuffer, context.CancellationToken);
  1502. }
  1503. }