LuaVirtualMachine.cs 68 KB

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