LuaVirtualMachine.cs 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Runtime.CompilerServices;
  3. using Lua.Internal;
  4. // ReSharper disable MethodHasAsyncOverload
  5. // ReSharper disable InconsistentNaming
  6. namespace Lua.Runtime;
  7. [SuppressMessage("Reliability", "CA2012:Use ValueTasks correctly")]
  8. public static partial class LuaVirtualMachine
  9. {
  10. class VirtualMachineExecutionContext
  11. : IPoolNode<VirtualMachineExecutionContext>
  12. {
  13. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  14. public static VirtualMachineExecutionContext Get(
  15. LuaThread thread,
  16. in CallStackFrame frame,
  17. CancellationToken cancellationToken)
  18. {
  19. if (!pool.TryPop(out var executionContext))
  20. {
  21. executionContext = new VirtualMachineExecutionContext();
  22. }
  23. executionContext.Init(thread, frame, cancellationToken);
  24. return executionContext;
  25. }
  26. void Init(
  27. LuaThread thread,
  28. in CallStackFrame frame,
  29. CancellationToken cancellationToken)
  30. {
  31. Stack = thread.Stack;
  32. Thread = thread;
  33. LuaClosure = (LuaClosure)frame.Function;
  34. FrameBase = frame.Base;
  35. VariableArgumentCount = frame.VariableArgumentCount;
  36. CurrentReturnFrameBase = frame.ReturnBase;
  37. CancellationToken = cancellationToken;
  38. Pc = -1;
  39. Instruction = default;
  40. PostOperation = PostOperationType.None;
  41. BaseCallStackCount = thread.CallStackFrameCount;
  42. LastHookPc = -1;
  43. Task = default;
  44. }
  45. public LuaState State => Thread.State;
  46. public LuaStack Stack = default!;
  47. public LuaClosure LuaClosure = default!;
  48. public LuaThread Thread = default!;
  49. public Prototype Prototype => LuaClosure.Proto;
  50. public int FrameBase;
  51. public int VariableArgumentCount;
  52. public CancellationToken CancellationToken;
  53. public int Pc;
  54. public Instruction Instruction;
  55. public int CurrentReturnFrameBase;
  56. public ValueTask<int> Task;
  57. public int LastHookPc;
  58. public bool IsTopLevel => BaseCallStackCount == Thread.CallStackFrameCount;
  59. public int BaseCallStackCount;
  60. public PostOperationType PostOperation;
  61. static LinkedPool<VirtualMachineExecutionContext> pool;
  62. VirtualMachineExecutionContext? nextNode;
  63. ref VirtualMachineExecutionContext? IPoolNode<VirtualMachineExecutionContext>.NextNode => ref nextNode;
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public bool Pop(Instruction instruction, int frameBase)
  66. {
  67. var count = instruction.B - 1;
  68. var src = instruction.A + frameBase;
  69. if (count == -1) count = Stack.Count - src;
  70. return PopFromBuffer(src, count);
  71. }
  72. [MethodImpl(MethodImplOptions.NoInlining)]
  73. public bool PopFromBuffer(int src, int srcCount)
  74. {
  75. var result = Stack.GetBuffer().Slice(src, srcCount);
  76. Re:
  77. var frames = Thread.GetCallStackFrames();
  78. if (frames.Length == BaseCallStackCount)
  79. {
  80. var returnBase = frames[^1].ReturnBase;
  81. if (src != returnBase)
  82. {
  83. result.CopyTo(Stack.GetBuffer()[returnBase..]);
  84. }
  85. Stack.PopUntil(returnBase + srcCount);
  86. return false;
  87. }
  88. ref readonly var frame = ref frames[^1];
  89. Pc = frame.CallerInstructionIndex;
  90. Thread.LastPc = Pc;
  91. ref readonly var lastFrame = ref frames[^2];
  92. LuaClosure = Unsafe.As<LuaClosure>(lastFrame.Function);
  93. CurrentReturnFrameBase = frame.ReturnBase;
  94. var callInstruction = Prototype.Code[Pc];
  95. if (callInstruction.OpCode == OpCode.TailCall)
  96. {
  97. Thread.PopCallStackFrame();
  98. goto Re;
  99. }
  100. FrameBase = lastFrame.Base;
  101. VariableArgumentCount = lastFrame.VariableArgumentCount;
  102. var opCode = callInstruction.OpCode;
  103. if (opCode is OpCode.Eq or OpCode.Lt or OpCode.Le)
  104. {
  105. var compareResult = srcCount > 0 && result[0].ToBoolean();
  106. if ((frame.Flags & CallStackFrameFlags.ReversedLe) != 0)
  107. {
  108. compareResult = !compareResult;
  109. }
  110. if (compareResult != (callInstruction.A == 1))
  111. {
  112. Pc++;
  113. }
  114. Thread.PopCallStackFrameWithStackPop();
  115. return true;
  116. }
  117. var target = callInstruction.A + FrameBase;
  118. var targetCount = result.Length;
  119. switch (opCode)
  120. {
  121. case OpCode.Call:
  122. {
  123. var c = callInstruction.C;
  124. if (c != 0)
  125. {
  126. targetCount = c - 1;
  127. }
  128. break;
  129. }
  130. case OpCode.TForCall:
  131. target += 3;
  132. targetCount = callInstruction.C;
  133. break;
  134. case OpCode.Self:
  135. Stack.Get(target) = result.Length == 0 ? LuaValue.Nil : result[0];
  136. Thread.PopCallStackFrameWithStackPop(target + 2);
  137. return true;
  138. case OpCode.SetTable or OpCode.SetTabUp:
  139. target = frame.Base;
  140. targetCount = 0;
  141. break;
  142. // Other opcodes has one result
  143. default:
  144. Stack.Get(target) = result.Length == 0 ? LuaValue.Nil : result[0];
  145. Thread.PopCallStackFrameWithStackPop(target + 1);
  146. return true;
  147. }
  148. Stack.EnsureCapacity(target + targetCount);
  149. if (0 < targetCount && src != target)
  150. {
  151. if (targetCount < result.Length)
  152. {
  153. result = result.Slice(0, targetCount);
  154. }
  155. result.CopyTo(Stack.GetBuffer().Slice(target, targetCount));
  156. }
  157. Stack.PopUntil(target + Math.Min(targetCount, srcCount));
  158. Stack.NotifyTop(target + targetCount);
  159. Thread.PopCallStackFrame();
  160. return true;
  161. }
  162. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  163. public void Push(in CallStackFrame frame)
  164. {
  165. Pc = -1;
  166. LuaClosure = (LuaClosure)(frame.Function);
  167. FrameBase = frame.Base;
  168. CurrentReturnFrameBase = frame.ReturnBase;
  169. VariableArgumentCount = frame.VariableArgumentCount;
  170. }
  171. public void PopOnTopCallStackFrames()
  172. {
  173. var count = Thread.CallStackFrameCount;
  174. if (count == BaseCallStackCount) return;
  175. Thread.PopCallStackFrameUntil(BaseCallStackCount);
  176. }
  177. bool ExecutePostOperation(PostOperationType postOperation)
  178. {
  179. var stackCount = Stack.Count;
  180. var resultsSpan = Stack.GetBuffer()[CurrentReturnFrameBase..];
  181. switch (postOperation)
  182. {
  183. case PostOperationType.Nop: break;
  184. case PostOperationType.SetResult:
  185. var RA = Instruction.A + FrameBase;
  186. Stack.Get(RA) = stackCount > CurrentReturnFrameBase ? Stack.Get(CurrentReturnFrameBase) : LuaValue.Nil;
  187. Stack.NotifyTop(RA + 1);
  188. Stack.PopUntil(RA + 1);
  189. break;
  190. case PostOperationType.TForCall:
  191. TForCallPostOperation(this);
  192. break;
  193. case PostOperationType.Call:
  194. CallPostOperation(this);
  195. break;
  196. case PostOperationType.TailCall:
  197. if (!PopFromBuffer(CurrentReturnFrameBase, Stack.Count - CurrentReturnFrameBase))
  198. {
  199. return false;
  200. }
  201. break;
  202. case PostOperationType.Self:
  203. SelfPostOperation(this, resultsSpan);
  204. break;
  205. case PostOperationType.Compare:
  206. ComparePostOperation(this, resultsSpan);
  207. break;
  208. }
  209. return true;
  210. }
  211. public async ValueTask<int> ExecuteClosureAsyncImpl()
  212. {
  213. var returnFrameBase = CurrentReturnFrameBase;
  214. try
  215. {
  216. while (MoveNext(this))
  217. {
  218. await Task;
  219. Task = default;
  220. if (PostOperation is not (PostOperationType.TailCall or PostOperationType.DontPop))
  221. {
  222. Thread.PopCallStackFrame();
  223. }
  224. if (!ExecutePostOperation(PostOperation))
  225. {
  226. break;
  227. }
  228. ThrowIfCancellationRequested();
  229. }
  230. return Thread.Stack.Count - returnFrameBase;
  231. }
  232. finally
  233. {
  234. pool.TryPush(this);
  235. }
  236. }
  237. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  238. public void ThrowIfCancellationRequested()
  239. {
  240. if (!CancellationToken.IsCancellationRequested) return;
  241. Throw();
  242. void Throw()
  243. {
  244. GetThreadWithCurrentPc(this).ThrowIfCancellationRequested(CancellationToken);
  245. }
  246. }
  247. }
  248. enum PostOperationType
  249. {
  250. None,
  251. Nop,
  252. SetResult,
  253. TForCall,
  254. Call,
  255. TailCall,
  256. Self,
  257. Compare,
  258. DontPop,
  259. }
  260. internal static ValueTask<int> ExecuteClosureAsync(LuaThread thread, CancellationToken cancellationToken)
  261. {
  262. ref readonly var frame = ref thread.GetCurrentFrame();
  263. var context = VirtualMachineExecutionContext.Get(thread, in frame,
  264. cancellationToken);
  265. return context.ExecuteClosureAsyncImpl();
  266. }
  267. static long DummyHookCount = 0;
  268. static bool DummyLineHookEnabled = false;
  269. static bool MoveNext(VirtualMachineExecutionContext context)
  270. {
  271. try
  272. {
  273. // This is a label to restart the execution when new function is called or restarted
  274. Restart:
  275. ref var instructionsHead = ref Unsafe.AsRef(in context.Prototype.Code[0]);
  276. var frameBase = context.FrameBase;
  277. var stack = context.Stack;
  278. stack.EnsureCapacity(frameBase + context.Prototype.MaxStackSize);
  279. ref var constHead = ref MemoryMarshalEx.UnsafeElementAt(context.Prototype.Constants, 0);
  280. ref var lineHookFlag = ref (context.Thread.IsInHook ? ref DummyLineHookEnabled : ref context.Thread.IsLineHookEnabled);
  281. ref var hookCount = ref (context.Thread.IsInHook ? ref DummyHookCount : ref context.Thread.HookCount);
  282. goto Loop;
  283. LineHook:
  284. {
  285. context.LastHookPc = context.Pc;
  286. if (ExecutePerInstructionHook(context))
  287. {
  288. {
  289. context.PostOperation = PostOperationType.Nop;
  290. return true;
  291. }
  292. }
  293. --context.Pc;
  294. }
  295. Loop:
  296. while (true)
  297. {
  298. var instruction = Unsafe.Add(ref instructionsHead, ++context.Pc);
  299. context.Instruction = instruction;
  300. if (--hookCount == 0 || (lineHookFlag && (context.Pc != context.LastHookPc)))
  301. {
  302. goto LineHook;
  303. }
  304. context.LastHookPc = -1;
  305. var iA = instruction.A;
  306. var opCode = instruction.OpCode;
  307. switch (opCode)
  308. {
  309. case OpCode.Move:
  310. Markers.Move();
  311. ref var stackHead = ref stack.FastGet(frameBase);
  312. Unsafe.Add(ref stackHead, iA) = Unsafe.Add(ref stackHead, instruction.B);
  313. stack.NotifyTop(iA + frameBase + 1);
  314. continue;
  315. case OpCode.LoadK:
  316. Markers.LoadK();
  317. stack.GetWithNotifyTop(iA + frameBase) = Unsafe.Add(ref constHead, instruction.Bx);
  318. continue;
  319. case OpCode.LoadKX:
  320. Markers.LoadKX();
  321. stack.GetWithNotifyTop(iA + frameBase) = Unsafe.Add(ref constHead, Unsafe.Add(ref instructionsHead, ++context.Pc).Ax);
  322. continue;
  323. case OpCode.LoadBool:
  324. Markers.LoadBool();
  325. stack.GetWithNotifyTop(iA + frameBase) = instruction.B != 0;
  326. if (instruction.C != 0) context.Pc++;
  327. continue;
  328. case OpCode.LoadNil:
  329. Markers.LoadNil();
  330. var ra1 = iA + frameBase + 1;
  331. var iB = instruction.B;
  332. stack.GetBuffer().Slice(ra1 - 1, iB + 1).Clear();
  333. stack.NotifyTop(ra1 + iB);
  334. continue;
  335. case OpCode.GetUpVal:
  336. Markers.GetUpVal();
  337. stack.GetWithNotifyTop(iA + frameBase) = context.LuaClosure.GetUpValue(instruction.B);
  338. continue;
  339. case OpCode.GetTabUp:
  340. case OpCode.GetTable:
  341. Markers.GetTabUp();
  342. Markers.GetTable();
  343. stackHead = ref stack.FastGet(frameBase);
  344. ref readonly var vc = ref RKC(ref stackHead, ref constHead, instruction);
  345. ref readonly var vb = ref (instruction.OpCode == OpCode.GetTable ? ref Unsafe.Add(ref stackHead, instruction.B) : ref context.LuaClosure.GetUpValueRef(instruction.B));
  346. var doRestart = false;
  347. if (vb.TryReadTable(out var luaTable) && luaTable.TryGetValue(vc, out var resultValue) || GetTableValueSlowPath(vb, vc, context, out resultValue, out doRestart))
  348. {
  349. if (doRestart) goto Restart;
  350. stack.GetWithNotifyTop(instruction.A + frameBase) = resultValue;
  351. continue;
  352. }
  353. return true;
  354. case OpCode.SetTabUp:
  355. case OpCode.SetTable:
  356. Markers.SetTabUp();
  357. Markers.SetTable();
  358. stackHead = ref stack.FastGet(frameBase);
  359. vb = ref RKB(ref stackHead, ref constHead, instruction);
  360. if (vb.TryReadNumber(out var numB))
  361. {
  362. if (double.IsNaN(numB))
  363. {
  364. ThrowLuaRuntimeException(context, "table index is NaN");
  365. return true;
  366. }
  367. }
  368. var table = opCode == OpCode.SetTabUp ? context.LuaClosure.GetUpValue(iA) : Unsafe.Add(ref stackHead, iA);
  369. if (table.TryReadTable(out luaTable))
  370. {
  371. ref var valueRef = ref luaTable.FindValue(vb);
  372. if (!Unsafe.IsNullRef(ref valueRef) && valueRef.Type != LuaValueType.Nil)
  373. {
  374. valueRef = RKC(ref stackHead, ref constHead, instruction);
  375. continue;
  376. }
  377. }
  378. vc = ref RKC(ref stackHead, ref constHead, instruction);
  379. if (SetTableValueSlowPath(table, vb, vc, context, out doRestart))
  380. {
  381. if (doRestart) goto Restart;
  382. continue;
  383. }
  384. return true;
  385. case OpCode.SetUpVal:
  386. Markers.SetUpVal();
  387. context.LuaClosure.SetUpValue(instruction.B, stack.FastGet(iA + frameBase));
  388. continue;
  389. case OpCode.NewTable:
  390. Markers.NewTable();
  391. stack.GetWithNotifyTop(iA + frameBase) = new LuaTable(instruction.B, instruction.C);
  392. continue;
  393. case OpCode.Self:
  394. Markers.Self();
  395. stackHead = ref stack.FastGet(frameBase);
  396. vc = ref RKC(ref stackHead, ref constHead, instruction);
  397. table = Unsafe.Add(ref stackHead, instruction.B);
  398. doRestart = false;
  399. if ((table.TryReadTable(out luaTable) && luaTable.TryGetValue(vc, out resultValue)) || GetTableValueSlowPath(table, vc, context, out resultValue, out doRestart))
  400. {
  401. if (doRestart) goto Restart;
  402. Unsafe.Add(ref stackHead, iA) = resultValue;
  403. Unsafe.Add(ref stackHead, iA + 1) = table;
  404. stack.NotifyTop(iA + frameBase + 2);
  405. continue;
  406. }
  407. return true;
  408. case OpCode.Add:
  409. case OpCode.Sub:
  410. case OpCode.Mul:
  411. case OpCode.Div:
  412. case OpCode.Mod:
  413. case OpCode.Pow:
  414. Markers.Add();
  415. Markers.Sub();
  416. Markers.Mul();
  417. Markers.Div();
  418. Markers.Mod();
  419. Markers.Pow();
  420. stackHead = ref stack.FastGet(frameBase);
  421. vb = ref RKB(ref stackHead, ref constHead, instruction);
  422. vc = ref RKC(ref stackHead, ref constHead, instruction);
  423. [MethodImpl(MethodImplOptions.NoInlining)]
  424. static double Mod(double a, double b)
  425. {
  426. var mod = a % b;
  427. if ((b > 0 && mod < 0) || (b < 0 && mod > 0))
  428. {
  429. mod += b;
  430. }
  431. return mod;
  432. }
  433. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  434. static double ArithmeticOperation(OpCode code, double a, double b)
  435. {
  436. return code switch
  437. {
  438. OpCode.Add => a + b,
  439. OpCode.Sub => a - b,
  440. OpCode.Mul => a * b,
  441. OpCode.Div => a / b,
  442. OpCode.Mod => Mod(a, b),
  443. OpCode.Pow => Math.Pow(a, b),
  444. _ => 0
  445. };
  446. }
  447. if (vb.Type == LuaValueType.Number && vc.Type == LuaValueType.Number)
  448. {
  449. Unsafe.Add(ref stackHead, iA) = ArithmeticOperation(opCode, vb.UnsafeReadDouble(), vc.UnsafeReadDouble());
  450. stack.NotifyTop(iA + frameBase + 1);
  451. continue;
  452. }
  453. if (vb.TryReadDouble(out numB) && vc.TryReadDouble(out var numC))
  454. {
  455. Unsafe.Add(ref stackHead, iA) = ArithmeticOperation(opCode, numB, numC);
  456. stack.NotifyTop(iA + frameBase + 1);
  457. continue;
  458. }
  459. if (ExecuteBinaryOperationMetaMethod(vb, vc, context, opCode, out doRestart))
  460. {
  461. if (doRestart) goto Restart;
  462. continue;
  463. }
  464. return true;
  465. case OpCode.Unm:
  466. Markers.Unm();
  467. stackHead = ref stack.FastGet(frameBase);
  468. vb = ref Unsafe.Add(ref stackHead, instruction.B);
  469. if (vb.TryReadDouble(out numB))
  470. {
  471. ra1 = iA + frameBase + 1;
  472. Unsafe.Add(ref stackHead, iA) = -numB;
  473. stack.NotifyTop(ra1);
  474. continue;
  475. }
  476. if (ExecuteUnaryOperationMetaMethod(vb, context, OpCode.Unm, out doRestart))
  477. {
  478. if (doRestart) goto Restart;
  479. continue;
  480. }
  481. return true;
  482. case OpCode.Not:
  483. Markers.Not();
  484. stackHead = ref stack.FastGet(frameBase);
  485. Unsafe.Add(ref stackHead, iA) = !Unsafe.Add(ref stackHead, instruction.B).ToBoolean();
  486. stack.NotifyTop(iA + frameBase + 1);
  487. continue;
  488. case OpCode.Len:
  489. Markers.Len();
  490. stackHead = ref stack.FastGet(frameBase);
  491. vb = ref Unsafe.Add(ref stackHead, instruction.B);
  492. if (vb.TryReadString(out var str))
  493. {
  494. ra1 = iA + frameBase + 1;
  495. Unsafe.Add(ref stackHead, iA) = str.Length;
  496. stack.NotifyTop(ra1);
  497. continue;
  498. }
  499. if (ExecuteUnaryOperationMetaMethod(vb, context, OpCode.Len, out doRestart))
  500. {
  501. if (doRestart) goto Restart;
  502. continue;
  503. }
  504. return true;
  505. case OpCode.Concat:
  506. Markers.Concat();
  507. if (Concat(context))
  508. {
  509. //if (doRestart) goto Restart;
  510. continue;
  511. }
  512. return true;
  513. case OpCode.Jmp:
  514. Markers.Jmp();
  515. context.Pc += instruction.SBx;
  516. if (iA != 0)
  517. {
  518. context.Thread.State.CloseUpValues(context.Thread, frameBase + iA - 1);
  519. }
  520. context.ThrowIfCancellationRequested();
  521. continue;
  522. case OpCode.Eq:
  523. Markers.Eq();
  524. stackHead = ref stack.Get(frameBase);
  525. vb = ref RKB(ref stackHead, ref constHead, instruction);
  526. vc = ref RKC(ref stackHead, ref constHead, instruction);
  527. if (vb == vc)
  528. {
  529. if (iA != 1)
  530. {
  531. context.Pc++;
  532. }
  533. continue;
  534. }
  535. if (ExecuteCompareOperationMetaMethod(vb, vc, context, OpCode.Eq, out doRestart))
  536. {
  537. if (doRestart) goto Restart;
  538. continue;
  539. }
  540. return true;
  541. case OpCode.Lt:
  542. case OpCode.Le:
  543. Markers.Lt();
  544. Markers.Le();
  545. stackHead = ref stack.Get(frameBase);
  546. vb = ref RKB(ref stackHead, ref constHead, instruction);
  547. vc = ref RKC(ref stackHead, ref constHead, instruction);
  548. if (vb.TryReadNumber(out numB) && vc.TryReadNumber(out numC))
  549. {
  550. var compareResult = opCode == OpCode.Lt ? numB < numC : numB <= numC;
  551. if (compareResult != (iA == 1))
  552. {
  553. context.Pc++;
  554. }
  555. continue;
  556. }
  557. if (vb.TryReadString(out var strB) && vc.TryReadString(out var strC))
  558. {
  559. var c = StringComparer.Ordinal.Compare(strB, strC);
  560. var compareResult = opCode == OpCode.Lt ? c < 0 : c <= 0;
  561. if (compareResult != (iA == 1))
  562. {
  563. context.Pc++;
  564. }
  565. continue;
  566. }
  567. if (ExecuteCompareOperationMetaMethod(vb, vc, context, opCode, out doRestart))
  568. {
  569. if (doRestart) goto Restart;
  570. continue;
  571. }
  572. return true;
  573. case OpCode.Test:
  574. Markers.Test();
  575. if (stack.Get(iA + frameBase).ToBoolean() != (instruction.C == 1))
  576. {
  577. context.Pc++;
  578. }
  579. continue;
  580. case OpCode.TestSet:
  581. Markers.TestSet();
  582. vb = ref stack.Get(instruction.B + frameBase);
  583. if (vb.ToBoolean() != (instruction.C == 1))
  584. {
  585. context.Pc++;
  586. }
  587. else
  588. {
  589. stack.GetWithNotifyTop(iA + frameBase) = vb;
  590. }
  591. continue;
  592. case OpCode.Call:
  593. Markers.Call();
  594. if (Call(context, out doRestart))
  595. {
  596. if (doRestart)
  597. {
  598. goto Restart;
  599. }
  600. continue;
  601. }
  602. return true;
  603. case OpCode.TailCall:
  604. Markers.TailCall();
  605. if (TailCall(context, out doRestart))
  606. {
  607. if (doRestart) goto Restart;
  608. if (context.IsTopLevel) goto End;
  609. continue;
  610. }
  611. return true;
  612. case OpCode.Return:
  613. Markers.Return();
  614. context.State.CloseUpValues(context.Thread, frameBase);
  615. if (context.Pop(instruction, frameBase))
  616. {
  617. goto Restart;
  618. }
  619. goto End;
  620. case OpCode.ForLoop:
  621. Markers.ForLoop();
  622. ref var indexRef = ref stack.Get(iA + frameBase);
  623. var limit = Unsafe.Add(ref indexRef, 1).UnsafeReadDouble();
  624. var step = Unsafe.Add(ref indexRef, 2).UnsafeReadDouble();
  625. var index = indexRef.UnsafeReadDouble() + step;
  626. if (step >= 0 ? index <= limit : limit <= index)
  627. {
  628. context.Pc += instruction.SBx;
  629. indexRef = index;
  630. Unsafe.Add(ref indexRef, 3) = index;
  631. stack.NotifyTop(iA + frameBase + 4);
  632. context.ThrowIfCancellationRequested();
  633. continue;
  634. }
  635. stack.NotifyTop(iA + frameBase + 1);
  636. continue;
  637. case OpCode.ForPrep:
  638. Markers.ForPrep();
  639. indexRef = ref stack.Get(iA + frameBase);
  640. if (!indexRef.TryReadDouble(out var init))
  641. {
  642. ThrowLuaRuntimeException(context, "'for' initial value must be a number");
  643. return true;
  644. }
  645. if (!LuaValue.TryReadOrSetDouble(ref Unsafe.Add(ref indexRef, 1), out _))
  646. {
  647. ThrowLuaRuntimeException(context, "'for' limit must be a number");
  648. return true;
  649. }
  650. if (!LuaValue.TryReadOrSetDouble(ref Unsafe.Add(ref indexRef, 2), out step))
  651. {
  652. ThrowLuaRuntimeException(context, "'for' step must be a number");
  653. return true;
  654. }
  655. indexRef = init - step;
  656. stack.NotifyTop(iA + frameBase + 1);
  657. context.Pc += instruction.SBx;
  658. continue;
  659. case OpCode.TForCall:
  660. Markers.TForCall();
  661. if (TForCall(context, out doRestart))
  662. {
  663. if (doRestart) goto Restart;
  664. continue;
  665. }
  666. return true;
  667. case OpCode.TForLoop:
  668. Markers.TForLoop();
  669. ref var forState = ref stack.Get(iA + frameBase + 1);
  670. if (forState.Type is not LuaValueType.Nil)
  671. {
  672. Unsafe.Add(ref forState, -1) = forState;
  673. context.Pc += instruction.SBx;
  674. }
  675. continue;
  676. case OpCode.SetList:
  677. Markers.SetList();
  678. SetList(context);
  679. continue;
  680. case OpCode.Closure:
  681. Markers.Closure();
  682. ra1 = iA + frameBase + 1;
  683. stack.EnsureCapacity(ra1);
  684. stack.Get(ra1 - 1) = new LuaClosure(context.Thread, context.Prototype.ChildPrototypes[instruction.Bx]);
  685. stack.NotifyTop(ra1);
  686. continue;
  687. case OpCode.VarArg:
  688. Markers.VarArg();
  689. VarArg(context);
  690. static void VarArg(VirtualMachineExecutionContext context)
  691. {
  692. var instruction = context.Instruction;
  693. var iA = instruction.A;
  694. var frameBase = context.FrameBase;
  695. var frameVariableArgumentCount = context.VariableArgumentCount;
  696. var count = instruction.B == 0
  697. ? frameVariableArgumentCount
  698. : instruction.B - 1;
  699. var ra = iA + frameBase;
  700. var stack = context.Stack;
  701. stack.EnsureCapacity(ra + count);
  702. ref var stackHead = ref stack.Get(0);
  703. for (int i = 0; i < count; i++)
  704. {
  705. Unsafe.Add(ref stackHead, ra + i) = frameVariableArgumentCount > i
  706. ? Unsafe.Add(ref stackHead, frameBase - (frameVariableArgumentCount - i))
  707. : default;
  708. }
  709. stack.NotifyTop(ra + count);
  710. }
  711. continue;
  712. case OpCode.ExtraArg:
  713. default:
  714. ThrowLuaNotImplementedException(context, context.Instruction.OpCode);
  715. return true;
  716. }
  717. }
  718. End:
  719. context.PostOperation = PostOperationType.None;
  720. return false;
  721. }
  722. catch (Exception e)
  723. {
  724. context.State.CloseUpValues(context.Thread, context.FrameBase);
  725. if (e is not (LuaRuntimeException or LuaCanceledException))
  726. {
  727. var newException = new LuaRuntimeException(context.Thread, e);
  728. context.PopOnTopCallStackFrames();
  729. throw newException;
  730. }
  731. context.PopOnTopCallStackFrames();
  732. throw;
  733. }
  734. }
  735. static void ThrowLuaRuntimeException(VirtualMachineExecutionContext context, string message)
  736. {
  737. throw new LuaRuntimeException(context.Thread, message);
  738. }
  739. static void ThrowLuaNotImplementedException(VirtualMachineExecutionContext context, OpCode opcode)
  740. {
  741. throw new LuaRuntimeException(context.Thread, $"OpCode {opcode} is not implemented");
  742. }
  743. static void SelfPostOperation(VirtualMachineExecutionContext context, Span<LuaValue> results)
  744. {
  745. var stack = context.Stack;
  746. var instruction = context.Instruction;
  747. var RA = instruction.A + context.FrameBase;
  748. var RB = instruction.B + context.FrameBase;
  749. ref var stackHead = ref stack.Get(0);
  750. var table = Unsafe.Add(ref stackHead, RB);
  751. Unsafe.Add(ref stackHead, RA + 1) = table;
  752. Unsafe.Add(ref stackHead, RA) = results.Length == 0 ? LuaValue.Nil : results[0];
  753. stack.NotifyTop(RA + 2);
  754. }
  755. [MethodImpl(MethodImplOptions.NoInlining)]
  756. static bool Concat(VirtualMachineExecutionContext context)
  757. {
  758. var instruction = context.Instruction;
  759. var stack = context.Stack;
  760. var b = instruction.B;
  761. var c = instruction.C;
  762. stack.NotifyTop(context.FrameBase + c + 1);
  763. var a = instruction.A;
  764. var task = Concat(context, context.FrameBase + a, c - b + 1);
  765. if (task.IsCompleted)
  766. {
  767. _ = task.Result;
  768. return true;
  769. }
  770. context.Task = task;
  771. context.PostOperation = PostOperationType.None;
  772. return false;
  773. }
  774. static async ValueTask<int> Concat(VirtualMachineExecutionContext context, int target, int total)
  775. {
  776. static bool ToString(ref LuaValue v)
  777. {
  778. if (v.Type == LuaValueType.String) return true;
  779. if (v.Type == LuaValueType.Number)
  780. {
  781. v = v.ToString();
  782. return true;
  783. }
  784. return false;
  785. }
  786. var stack = context.Stack;
  787. do
  788. {
  789. var top = context.Thread.Stack.Count;
  790. var n = 2;
  791. ref var lhs = ref stack.Get(top - 2);
  792. ref var rhs = ref stack.Get(top - 1);
  793. if (!(lhs.Type is LuaValueType.String or LuaValueType.Number) || !ToString(ref rhs))
  794. {
  795. await ExecuteBinaryOperationMetaMethod(top - 2, lhs, rhs, context, OpCode.Concat);
  796. }
  797. else if (rhs.UnsafeReadString().Length == 0)
  798. {
  799. ToString(ref lhs);
  800. }
  801. else if (lhs.TryReadString(out var str) && str.Length == 0)
  802. {
  803. lhs = rhs;
  804. }
  805. else
  806. {
  807. var tl = rhs.UnsafeReadString().Length;
  808. int i = 1;
  809. for (; i < total; i++)
  810. {
  811. ref var v = ref stack.Get(top - i - 1);
  812. if (!ToString(ref v))
  813. {
  814. break;
  815. }
  816. tl += v.UnsafeReadString().Length;
  817. }
  818. n = i;
  819. stack.Get(top - n) = string.Create(tl, (stack, top - n), static (span, pair) =>
  820. {
  821. var (stack, index) = pair;
  822. foreach (var v in stack.AsSpan().Slice(index))
  823. {
  824. var s = v.UnsafeReadString();
  825. if (s.Length == 0) continue;
  826. s.AsSpan().CopyTo(span);
  827. span = span[s.Length..];
  828. }
  829. });
  830. }
  831. total -= n - 1;
  832. stack.PopUntil(top - (n - 1));
  833. } while (total > 1);
  834. stack.Get(target) = stack.AsSpan()[^1];
  835. return 1;
  836. }
  837. internal static async ValueTask<LuaValue> Concat(LuaThread thread, int total, CancellationToken ct)
  838. {
  839. static bool ToString(ref LuaValue v)
  840. {
  841. if (v.Type == LuaValueType.String) return true;
  842. if (v.Type == LuaValueType.Number)
  843. {
  844. v = v.ToString();
  845. return true;
  846. }
  847. return false;
  848. }
  849. var stack = thread.Stack;
  850. do
  851. {
  852. var top = stack.Count;
  853. var n = 2;
  854. ref var lhs = ref stack.Get(top - 2);
  855. ref var rhs = ref stack.Get(top - 1);
  856. if (!(lhs.Type is LuaValueType.String or LuaValueType.Number) || !ToString(ref rhs))
  857. {
  858. var value = await ExecuteBinaryOperationMetaMethod(thread, lhs, rhs, OpCode.Concat, ct);
  859. stack.Get(top - 2) = value;
  860. }
  861. else if (rhs.UnsafeReadString().Length == 0)
  862. {
  863. ToString(ref lhs);
  864. }
  865. else if (lhs.TryReadString(out var str) && str.Length == 0)
  866. {
  867. lhs = rhs;
  868. }
  869. else
  870. {
  871. var tl = rhs.UnsafeReadString().Length;
  872. int i = 1;
  873. for (; i < total; i++)
  874. {
  875. ref var v = ref stack.Get(top - i - 1);
  876. if (!ToString(ref v))
  877. {
  878. break;
  879. }
  880. tl += v.UnsafeReadString().Length;
  881. }
  882. n = i;
  883. stack.Get(top - n) = string.Create(tl, (stack, top - n), static (span, pair) =>
  884. {
  885. var (stack, index) = pair;
  886. foreach (var v in stack.AsSpan().Slice(index))
  887. {
  888. var s = v.UnsafeReadString();
  889. if (s.Length == 0) continue;
  890. s.AsSpan().CopyTo(span);
  891. span = span[s.Length..];
  892. }
  893. });
  894. }
  895. total -= n - 1;
  896. stack.PopUntil(top - (n - 1));
  897. } while (total > 1);
  898. return stack.AsSpan()[^1];
  899. }
  900. static async ValueTask ExecuteBinaryOperationMetaMethod(int target, LuaValue vb, LuaValue vc,
  901. VirtualMachineExecutionContext context, OpCode opCode)
  902. {
  903. context.ThrowIfCancellationRequested();
  904. var (name, description) = opCode.GetNameAndDescription();
  905. if (vb.TryGetMetamethod(context.State, name, out var metamethod) ||
  906. vc.TryGetMetamethod(context.State, name, out metamethod))
  907. {
  908. var stack = context.Stack;
  909. var argCount = 2;
  910. var callable = metamethod;
  911. if (!metamethod.TryReadFunction(out var func))
  912. {
  913. if (metamethod.TryGetMetamethod(context.State, Metamethods.Call, out metamethod) &&
  914. metamethod.TryReadFunction(out func))
  915. {
  916. stack.Push(callable);
  917. argCount++;
  918. }
  919. else
  920. {
  921. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), "call", metamethod);
  922. }
  923. }
  924. stack.Push(vb);
  925. stack.Push(vc);
  926. var varArgCount = func.GetVariableArgumentCount(argCount);
  927. var newFrame = func.CreateNewFrame(context, stack.Count - argCount + varArgCount, target, varArgCount);
  928. var thread = context.Thread;
  929. var access = thread.PushCallStackFrame(newFrame);
  930. try
  931. {
  932. var functionContext = new LuaFunctionExecutionContext() { Access = access, ArgumentCount = argCount, ReturnFrameBase = target };
  933. if (thread.CallOrReturnHookMask.Value != 0 && !thread.IsInHook)
  934. {
  935. await ExecuteCallHook(functionContext, context.CancellationToken);
  936. stack.PopUntil(target + 1);
  937. context.PostOperation = PostOperationType.DontPop;
  938. context.ThrowIfCancellationRequested();
  939. return;
  940. }
  941. await func.Func(functionContext, context.CancellationToken);
  942. stack.PopUntil(target + 1);
  943. context.PostOperation = PostOperationType.DontPop;
  944. context.ThrowIfCancellationRequested();
  945. return;
  946. }
  947. finally
  948. {
  949. thread.PopCallStackFrame();
  950. }
  951. }
  952. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), description, vb, vc);
  953. return;
  954. }
  955. static bool Call(VirtualMachineExecutionContext context, out bool doRestart)
  956. {
  957. context.ThrowIfCancellationRequested();
  958. var instruction = context.Instruction;
  959. var RA = instruction.A + context.FrameBase;
  960. var newBase = RA + 1;
  961. var va = context.Stack.Get(RA);
  962. bool isMetamethod = false;
  963. if (!va.TryReadFunction(out var func))
  964. {
  965. if (va.TryGetMetamethod(context.State, Metamethods.Call, out var metamethod) &&
  966. metamethod.TryReadFunction(out func))
  967. {
  968. newBase -= 1;
  969. isMetamethod = true;
  970. }
  971. else
  972. {
  973. LuaRuntimeException.AttemptInvalidOperationOnLuaStack(GetThreadWithCurrentPc(context), "call", context.Pc, instruction.A);
  974. }
  975. }
  976. var thread = context.Thread;
  977. var (argumentCount, variableArgumentCount) = PrepareForFunctionCall(thread, func, instruction, newBase, isMetamethod);
  978. newBase += variableArgumentCount;
  979. thread.Stack.PopUntil(newBase + argumentCount);
  980. var newFrame = func.CreateNewFrame(context, newBase, RA, variableArgumentCount);
  981. var access = thread.PushCallStackFrame(newFrame);
  982. if (thread.CallOrReturnHookMask.Value != 0 && !context.Thread.IsInHook)
  983. {
  984. context.PostOperation = PostOperationType.Call;
  985. context.Task = ExecuteCallHook(context, newFrame, argumentCount);
  986. doRestart = false;
  987. return false;
  988. }
  989. if (func is LuaClosure)
  990. {
  991. context.Push(newFrame);
  992. doRestart = true;
  993. return true;
  994. }
  995. doRestart = false;
  996. return FuncCall(context, access, func, argumentCount, newFrame.ReturnBase);
  997. static bool FuncCall(VirtualMachineExecutionContext context, LuaThreadAccess access, LuaFunction func, int argumentCount, int returnBase)
  998. {
  999. var task = func.Func(new() { Access = access, ArgumentCount = argumentCount, ReturnFrameBase = returnBase }, context.CancellationToken);
  1000. if (!task.IsCompleted)
  1001. {
  1002. context.PostOperation = PostOperationType.Call;
  1003. context.Task = task;
  1004. return false;
  1005. }
  1006. var awaiter = task.GetAwaiter();
  1007. awaiter.GetResult();
  1008. context.Thread.ThrowIfCancellationRequested(context.CancellationToken);
  1009. var instruction = context.Instruction;
  1010. var ic = instruction.C;
  1011. if (ic != 0)
  1012. {
  1013. var resultCount = ic - 1;
  1014. var stack = context.Stack;
  1015. var top = instruction.A + context.FrameBase + resultCount;
  1016. stack.EnsureCapacity(top);
  1017. stack.PopUntil(top);
  1018. stack.NotifyTop(top);
  1019. }
  1020. context.Thread.PopCallStackFrame();
  1021. return true;
  1022. }
  1023. }
  1024. internal static async ValueTask<int> Call(LuaThread thread, int funcIndex, int returnBase, CancellationToken cancellationToken)
  1025. {
  1026. thread.ThrowIfCancellationRequested(cancellationToken);
  1027. var stack = thread.Stack;
  1028. var newBase = funcIndex + 1;
  1029. var va = stack.Get(funcIndex);
  1030. if (!va.TryReadFunction(out var func))
  1031. {
  1032. if (va.TryGetMetamethod(thread.State, Metamethods.Call, out va) &&
  1033. va.TryReadFunction(out func))
  1034. {
  1035. newBase--;
  1036. }
  1037. else
  1038. {
  1039. LuaRuntimeException.AttemptInvalidOperation(thread, "call", va);
  1040. }
  1041. }
  1042. var (argCount, variableArgumentCount) = PrepareForFunctionCall(thread, func, newBase);
  1043. newBase += variableArgumentCount;
  1044. var newFrame = new CallStackFrame() { Base = newBase, VariableArgumentCount = variableArgumentCount, Function = func, ReturnBase = returnBase };
  1045. var access = thread.PushCallStackFrame(newFrame);
  1046. try
  1047. {
  1048. var functionContext = new LuaFunctionExecutionContext() { Access = access, ArgumentCount = argCount, ReturnFrameBase = returnBase };
  1049. if (thread.CallOrReturnHookMask.Value != 0 && !thread.IsInHook)
  1050. {
  1051. await ExecuteCallHook(functionContext, cancellationToken);
  1052. }
  1053. else
  1054. {
  1055. await func.Func(functionContext, cancellationToken);
  1056. }
  1057. thread.ThrowIfCancellationRequested(cancellationToken);
  1058. return thread.Stack.Count - funcIndex;
  1059. }
  1060. catch (OperationCanceledException operationCanceledException)
  1061. {
  1062. if (operationCanceledException is not LuaCanceledException)
  1063. {
  1064. throw new LuaCanceledException(thread, cancellationToken, operationCanceledException);
  1065. }
  1066. throw;
  1067. }
  1068. finally
  1069. {
  1070. thread.PopCallStackFrame();
  1071. }
  1072. }
  1073. static void CallPostOperation(VirtualMachineExecutionContext context)
  1074. {
  1075. var instruction = context.Instruction;
  1076. var ic = instruction.C;
  1077. if (ic != 0)
  1078. {
  1079. var resultCount = ic - 1;
  1080. var stack = context.Stack;
  1081. var top = instruction.A + context.FrameBase + resultCount;
  1082. stack.EnsureCapacity(top);
  1083. stack.PopUntil(top);
  1084. stack.NotifyTop(top);
  1085. }
  1086. }
  1087. static bool TailCall(VirtualMachineExecutionContext context, out bool doRestart)
  1088. {
  1089. context.ThrowIfCancellationRequested();
  1090. var instruction = context.Instruction;
  1091. var stack = context.Stack;
  1092. var RA = instruction.A + context.FrameBase;
  1093. var newBase = RA + 1;
  1094. bool isMetamethod = false;
  1095. var state = context.State;
  1096. var thread = context.Thread;
  1097. state.CloseUpValues(thread, context.FrameBase);
  1098. var va = stack.Get(RA);
  1099. if (!va.TryReadFunction(out var func))
  1100. {
  1101. if (va.TryGetMetamethod(state, Metamethods.Call, out var metamethod) &&
  1102. metamethod.TryReadFunction(out func))
  1103. {
  1104. isMetamethod = true;
  1105. newBase -= 1;
  1106. }
  1107. else
  1108. {
  1109. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), "call", metamethod);
  1110. }
  1111. }
  1112. var (argumentCount, variableArgumentCount) = PrepareForFunctionTailCall(thread, func, instruction, newBase, isMetamethod);
  1113. newBase = context.FrameBase + variableArgumentCount;
  1114. stack.PopUntil(newBase + argumentCount);
  1115. var lastFrame = thread.GetCurrentFrame();
  1116. context.Thread.PopCallStackFrame();
  1117. var newFrame = func.CreateNewTailCallFrame(context, newBase, context.CurrentReturnFrameBase, variableArgumentCount);
  1118. newFrame.CallerInstructionIndex = lastFrame.CallerInstructionIndex;
  1119. newFrame.Version = lastFrame.Version;
  1120. var access = thread.PushCallStackFrame(newFrame);
  1121. if (thread.CallOrReturnHookMask.Value != 0 && !context.Thread.IsInHook)
  1122. {
  1123. context.PostOperation = PostOperationType.TailCall;
  1124. context.Task = ExecuteCallHook(context, newFrame, argumentCount, true);
  1125. doRestart = false;
  1126. return false;
  1127. }
  1128. if (func is LuaClosure)
  1129. {
  1130. context.Push(newFrame);
  1131. doRestart = true;
  1132. return true;
  1133. }
  1134. doRestart = false;
  1135. var task = func.Func(new() { Access = access, ArgumentCount = argumentCount, ReturnFrameBase = context.CurrentReturnFrameBase }, context.CancellationToken);
  1136. if (!task.IsCompleted)
  1137. {
  1138. context.PostOperation = PostOperationType.TailCall;
  1139. context.Task = task;
  1140. return false;
  1141. }
  1142. task.GetAwaiter().GetResult();
  1143. context.ThrowIfCancellationRequested();
  1144. if (!context.PopFromBuffer(context.CurrentReturnFrameBase, context.Stack.Count - context.CurrentReturnFrameBase))
  1145. {
  1146. return true;
  1147. }
  1148. doRestart = true;
  1149. return true;
  1150. }
  1151. static bool TForCall(VirtualMachineExecutionContext context, out bool doRestart)
  1152. {
  1153. context.ThrowIfCancellationRequested();
  1154. doRestart = false;
  1155. var instruction = context.Instruction;
  1156. var stack = context.Stack;
  1157. var RA = instruction.A + context.FrameBase;
  1158. bool isMetamethod = false;
  1159. var iteratorRaw = stack.Get(RA);
  1160. if (!iteratorRaw.TryReadFunction(out var iterator))
  1161. {
  1162. if (iteratorRaw.TryGetMetamethod(context.State, Metamethods.Call, out var metamethod) &&
  1163. metamethod.TryReadFunction(out iterator))
  1164. {
  1165. isMetamethod = true;
  1166. }
  1167. else
  1168. {
  1169. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), "call", metamethod);
  1170. }
  1171. }
  1172. var newBase = RA + 3 + instruction.C;
  1173. if (isMetamethod)
  1174. {
  1175. stack.Get(newBase) = iteratorRaw;
  1176. stack.Get(newBase + 1) = stack.Get(RA + 1);
  1177. stack.Get(newBase + 2) = stack.Get(RA + 2);
  1178. stack.SetTop(newBase + 3);
  1179. }
  1180. else
  1181. {
  1182. stack.Get(newBase) = stack.Get(RA + 1);
  1183. stack.Get(newBase + 1) = stack.Get(RA + 2);
  1184. stack.SetTop(newBase + 2);
  1185. }
  1186. var argumentCount = isMetamethod ? 3 : 2;
  1187. var variableArgumentCount = iterator.GetVariableArgumentCount(argumentCount);
  1188. if (variableArgumentCount != 0)
  1189. {
  1190. PrepareVariableArgument(stack, newBase, argumentCount, variableArgumentCount);
  1191. newBase += variableArgumentCount;
  1192. }
  1193. stack.PopUntil(newBase + argumentCount);
  1194. var newFrame = iterator.CreateNewFrame(context, newBase, RA + 3, variableArgumentCount);
  1195. var access = context.Thread.PushCallStackFrame(newFrame);
  1196. if (context.Thread.CallOrReturnHookMask.Value != 0 && !context.Thread.IsInHook)
  1197. {
  1198. context.PostOperation = PostOperationType.TForCall;
  1199. context.Task = ExecuteCallHook(context, newFrame, stack.Count - newBase);
  1200. doRestart = false;
  1201. return false;
  1202. }
  1203. if (iterator is LuaClosure)
  1204. {
  1205. context.Push(newFrame);
  1206. doRestart = true;
  1207. return true;
  1208. }
  1209. var task = iterator.Func(new() { Access = access, ArgumentCount = stack.Count - newBase, ReturnFrameBase = newFrame.ReturnBase }, context.CancellationToken);
  1210. if (!task.IsCompleted)
  1211. {
  1212. context.PostOperation = PostOperationType.TForCall;
  1213. context.Task = task;
  1214. return false;
  1215. }
  1216. task.GetAwaiter().GetResult();
  1217. context.ThrowIfCancellationRequested();
  1218. context.Thread.PopCallStackFrame();
  1219. TForCallPostOperation(context);
  1220. return true;
  1221. }
  1222. // ReSharper disable once InconsistentNaming
  1223. static void TForCallPostOperation(VirtualMachineExecutionContext context)
  1224. {
  1225. var stack = context.Stack;
  1226. var instruction = context.Instruction;
  1227. var RA = instruction.A + context.FrameBase;
  1228. stack.SetTop(RA + instruction.C + 3);
  1229. }
  1230. static void SetList(VirtualMachineExecutionContext context)
  1231. {
  1232. var instruction = context.Instruction;
  1233. var stack = context.Stack;
  1234. var RA = instruction.A + context.FrameBase;
  1235. if (!stack.Get(RA).TryReadTable(out var table))
  1236. {
  1237. throw new LuaRuntimeException(GetThreadWithCurrentPc(context), "internal error");
  1238. }
  1239. var count = instruction.B == 0
  1240. ? stack.Count - (RA + 1)
  1241. : instruction.B;
  1242. table.EnsureArrayCapacity((instruction.C - 1) * 50 + count);
  1243. stack.GetBuffer().Slice(RA + 1, count)
  1244. .CopyTo(table.GetArraySpan()[((instruction.C - 1) * 50)..]);
  1245. stack.PopUntil(RA + 1);
  1246. }
  1247. static void ComparePostOperation(VirtualMachineExecutionContext context, Span<LuaValue> results)
  1248. {
  1249. var compareResult = results.Length != 0 && results[0].ToBoolean();
  1250. if (compareResult != (context.Instruction.A == 1))
  1251. {
  1252. context.Pc++;
  1253. }
  1254. results.Clear();
  1255. }
  1256. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1257. static ref readonly LuaValue RKB(ref LuaValue stack, ref LuaValue constants, Instruction instruction)
  1258. {
  1259. var index = instruction.B;
  1260. return ref (index >= 256 ? ref Unsafe.Add(ref constants, index - 256) : ref Unsafe.Add(ref stack, index));
  1261. }
  1262. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1263. static ref readonly LuaValue RKC(ref LuaValue stack, ref LuaValue constants, Instruction instruction)
  1264. {
  1265. var index = instruction.C;
  1266. return ref (index >= 256 ? ref Unsafe.Add(ref constants, index - 256) : ref Unsafe.Add(ref stack, index));
  1267. }
  1268. [MethodImpl(MethodImplOptions.NoInlining)]
  1269. static bool GetTableValueSlowPath(LuaValue table, LuaValue key, VirtualMachineExecutionContext context, out LuaValue value, out bool doRestart)
  1270. {
  1271. // TODO: get table name if nil
  1272. // if (table.Type == LuaValueType.Nil)
  1273. // {
  1274. // }
  1275. var targetTable = table;
  1276. const int MAX_LOOP = 100;
  1277. doRestart = false;
  1278. var skip = targetTable.Type == LuaValueType.Table;
  1279. for (int i = 0; i < MAX_LOOP; i++)
  1280. {
  1281. if (table.TryReadTable(out var luaTable))
  1282. {
  1283. if (!skip && luaTable.TryGetValue(key, out value))
  1284. {
  1285. return true;
  1286. }
  1287. skip = false;
  1288. var metatable = luaTable.Metatable;
  1289. if (metatable != null && metatable.TryGetValue(Metamethods.Index, out table))
  1290. {
  1291. goto Function;
  1292. }
  1293. value = default;
  1294. return true;
  1295. }
  1296. if (!table.TryGetMetamethod(context.State, Metamethods.Index, out var metatableValue))
  1297. {
  1298. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), "index", table);
  1299. }
  1300. table = metatableValue;
  1301. Function:
  1302. if (table.TryReadFunction(out var function))
  1303. {
  1304. return CallGetTableFunc(targetTable, function, key, context, out value, out doRestart);
  1305. }
  1306. }
  1307. throw new LuaRuntimeException(GetThreadWithCurrentPc(context), "loop in gettable");
  1308. }
  1309. [MethodImpl(MethodImplOptions.NoInlining)]
  1310. static bool CallGetTableFunc(LuaValue table, LuaFunction indexTable, LuaValue key, VirtualMachineExecutionContext context, out LuaValue result, out bool doRestart)
  1311. {
  1312. doRestart = false;
  1313. var stack = context.Stack;
  1314. stack.Push(table);
  1315. stack.Push(key);
  1316. var newFrame = indexTable.CreateNewFrame(context, stack.Count - 2);
  1317. var access = context.Thread.PushCallStackFrame(newFrame);
  1318. if (context.Thread.CallOrReturnHookMask.Value != 0 && !context.Thread.IsInHook)
  1319. {
  1320. context.PostOperation = context.Instruction.OpCode == OpCode.GetTable ? PostOperationType.SetResult : PostOperationType.Self;
  1321. context.Task = ExecuteCallHook(context, newFrame, 2);
  1322. doRestart = false;
  1323. result = default;
  1324. return false;
  1325. }
  1326. if (indexTable is LuaClosure)
  1327. {
  1328. context.Push(newFrame);
  1329. doRestart = true;
  1330. result = default;
  1331. return true;
  1332. }
  1333. var task = indexTable.Func(new() { Access = access, ArgumentCount = 2, ReturnFrameBase = newFrame.ReturnBase }, context.CancellationToken);
  1334. if (!task.IsCompleted)
  1335. {
  1336. context.PostOperation = context.Instruction.OpCode == OpCode.GetTable ? PostOperationType.SetResult : PostOperationType.Self;
  1337. context.Task = task;
  1338. result = default;
  1339. return false;
  1340. }
  1341. var awaiter = task.GetAwaiter();
  1342. awaiter.GetResult();
  1343. var results = stack.GetBuffer()[newFrame.Base..];
  1344. result = results.Length == 0 ? default : results[0];
  1345. context.Thread.PopCallStackFrameWithStackPop();
  1346. return true;
  1347. }
  1348. [MethodImpl(MethodImplOptions.NoInlining)]
  1349. internal static ValueTask<LuaValue> ExecuteGetTableSlowPath(LuaThread thread, LuaValue table, LuaValue key, CancellationToken ct)
  1350. {
  1351. var targetTable = table;
  1352. const int MAX_LOOP = 100;
  1353. var skip = targetTable.Type == LuaValueType.Table;
  1354. for (int i = 0; i < MAX_LOOP; i++)
  1355. {
  1356. if (table.TryReadTable(out var luaTable))
  1357. {
  1358. if (!skip && luaTable.TryGetValue(key, out var value))
  1359. {
  1360. return new(value);
  1361. }
  1362. skip = false;
  1363. var metatable = luaTable.Metatable;
  1364. if (metatable != null && metatable.TryGetValue(Metamethods.Index, out table))
  1365. {
  1366. goto Function;
  1367. }
  1368. return default(ValueTask<LuaValue>);
  1369. }
  1370. if (!table.TryGetMetamethod(thread.State, Metamethods.Index, out var metatableValue))
  1371. {
  1372. LuaRuntimeException.AttemptInvalidOperation(thread, "index", table);
  1373. }
  1374. table = metatableValue;
  1375. Function:
  1376. if (table.TryReadFunction(out var function))
  1377. {
  1378. return CallGetTableFunc(thread, function, targetTable, key, ct);
  1379. }
  1380. }
  1381. throw new LuaRuntimeException(thread, "loop in gettable");
  1382. }
  1383. [MethodImpl(MethodImplOptions.NoInlining)]
  1384. static async ValueTask<LuaValue> CallGetTableFunc(LuaThread thread, LuaFunction indexTable, LuaValue table, LuaValue key, CancellationToken ct)
  1385. {
  1386. var stack = thread.Stack;
  1387. var top = stack.Count;
  1388. stack.Push(table);
  1389. stack.Push(key);
  1390. var varArgCount = indexTable.GetVariableArgumentCount(2);
  1391. var newFrame = new CallStackFrame() { Base = thread.Stack.Count - 2 + varArgCount, VariableArgumentCount = varArgCount, Function = indexTable, ReturnBase = top };
  1392. var access = thread.PushCallStackFrame(newFrame);
  1393. var functionContext = new LuaFunctionExecutionContext() { Access = access, ArgumentCount = 2, ReturnFrameBase = top };
  1394. if (thread.CallOrReturnHookMask.Value != 0 && !thread.IsInHook)
  1395. {
  1396. await ExecuteCallHook(functionContext, ct);
  1397. }
  1398. await indexTable.Func(functionContext, ct);
  1399. var results = stack.GetBuffer()[newFrame.ReturnBase..];
  1400. var result = results.Length == 0 ? default : results[0];
  1401. results.Clear();
  1402. thread.PopCallStackFrameWithStackPop();
  1403. return result;
  1404. }
  1405. [MethodImpl(MethodImplOptions.NoInlining)]
  1406. static bool SetTableValueSlowPath(LuaValue table, LuaValue key, LuaValue value,
  1407. VirtualMachineExecutionContext context, out bool doRestart)
  1408. {
  1409. var targetTable = table;
  1410. const int MAX_LOOP = 100;
  1411. doRestart = false;
  1412. var skip = targetTable.Type == LuaValueType.Table;
  1413. for (int i = 0; i < MAX_LOOP; i++)
  1414. {
  1415. if (table.TryReadTable(out var luaTable))
  1416. {
  1417. targetTable = luaTable;
  1418. ref var valueRef = ref (skip ? ref Unsafe.NullRef<LuaValue>() : ref luaTable.FindValue(key));
  1419. skip = false;
  1420. if (!Unsafe.IsNullRef(ref valueRef) && valueRef.Type != LuaValueType.Nil)
  1421. {
  1422. valueRef = value;
  1423. return true;
  1424. }
  1425. var metatable = luaTable.Metatable;
  1426. if (metatable == null || !metatable.TryGetValue(Metamethods.NewIndex, out table))
  1427. {
  1428. if (Unsafe.IsNullRef(ref valueRef))
  1429. {
  1430. luaTable[key] = value;
  1431. return true;
  1432. }
  1433. valueRef = value;
  1434. return true;
  1435. }
  1436. goto Function;
  1437. }
  1438. if (!table.TryGetMetamethod(context.State, Metamethods.NewIndex, out var metatableValue))
  1439. {
  1440. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), "index", table);
  1441. }
  1442. table = metatableValue;
  1443. Function:
  1444. if (table.TryReadFunction(out var function))
  1445. {
  1446. context.PostOperation = PostOperationType.Nop;
  1447. return CallSetTableFunc(targetTable, function, key, value, context, out doRestart);
  1448. }
  1449. }
  1450. throw new LuaRuntimeException(GetThreadWithCurrentPc(context), "loop in settable");
  1451. }
  1452. [MethodImpl(MethodImplOptions.NoInlining)]
  1453. static bool CallSetTableFunc(LuaValue table, LuaFunction newIndexFunction, LuaValue key, LuaValue value, VirtualMachineExecutionContext context, out bool doRestart)
  1454. {
  1455. doRestart = false;
  1456. var thread = context.Thread;
  1457. var stack = thread.Stack;
  1458. stack.Push(table);
  1459. stack.Push(key);
  1460. stack.Push(value);
  1461. var newFrame = newIndexFunction.CreateNewFrame(context, stack.Count - 3);
  1462. var access = context.Thread.PushCallStackFrame(newFrame);
  1463. if (context.Thread.CallOrReturnHookMask.Value != 0 && !context.Thread.IsInHook)
  1464. {
  1465. context.PostOperation = PostOperationType.Nop;
  1466. context.Task = ExecuteCallHook(context, newFrame, 3);
  1467. doRestart = false;
  1468. return false;
  1469. }
  1470. if (newIndexFunction is LuaClosure)
  1471. {
  1472. context.Push(newFrame);
  1473. doRestart = true;
  1474. return true;
  1475. }
  1476. var task = newIndexFunction.Func(new() { Access = access, ArgumentCount = 3, ReturnFrameBase = newFrame.ReturnBase }, context.CancellationToken);
  1477. if (!task.IsCompleted)
  1478. {
  1479. context.PostOperation = PostOperationType.Nop;
  1480. context.Task = task;
  1481. return false;
  1482. }
  1483. task.GetAwaiter().GetResult();
  1484. thread.PopCallStackFrameWithStackPop();
  1485. return true;
  1486. }
  1487. internal static ValueTask ExecuteSetTableSlowPath(LuaThread thread, LuaValue table, LuaValue key, LuaValue value, CancellationToken ct)
  1488. {
  1489. var targetTable = table;
  1490. const int MAX_LOOP = 100;
  1491. var skip = targetTable.Type == LuaValueType.Table;
  1492. for (int i = 0; i < MAX_LOOP; i++)
  1493. {
  1494. if (table.TryReadTable(out var luaTable))
  1495. {
  1496. targetTable = luaTable;
  1497. ref var valueRef = ref (skip ? ref Unsafe.NullRef<LuaValue>() : ref luaTable.FindValue(key));
  1498. skip = false;
  1499. if (!Unsafe.IsNullRef(ref valueRef) && valueRef.Type != LuaValueType.Nil)
  1500. {
  1501. valueRef = value;
  1502. return default(ValueTask);
  1503. }
  1504. var metatable = luaTable.Metatable;
  1505. if (metatable == null || !metatable.TryGetValue(Metamethods.NewIndex, out table))
  1506. {
  1507. if (Unsafe.IsNullRef(ref valueRef))
  1508. {
  1509. luaTable[key] = value;
  1510. return default(ValueTask);
  1511. }
  1512. valueRef = value;
  1513. return default;
  1514. }
  1515. goto Function;
  1516. }
  1517. if (!table.TryGetMetamethod(thread.State, Metamethods.NewIndex, out var metatableValue))
  1518. {
  1519. LuaRuntimeException.AttemptInvalidOperation(thread, "index", table);
  1520. }
  1521. table = metatableValue;
  1522. Function:
  1523. if (table.TryReadFunction(out var function))
  1524. {
  1525. return CallSetTableFunc(thread, function, targetTable, key, value, ct);
  1526. }
  1527. }
  1528. throw new LuaRuntimeException(thread, "loop in settable");
  1529. }
  1530. [MethodImpl(MethodImplOptions.NoInlining)]
  1531. static async ValueTask CallSetTableFunc(LuaThread thread, LuaFunction newIndexFunction, LuaValue table, LuaValue key, LuaValue value, CancellationToken ct)
  1532. {
  1533. var stack = thread.Stack;
  1534. var top = stack.Count;
  1535. stack.Push(table);
  1536. stack.Push(key);
  1537. stack.Push(value);
  1538. var varArgCount = newIndexFunction.GetVariableArgumentCount(3);
  1539. var newFrame = new CallStackFrame() { Base = thread.Stack.Count - 3 + varArgCount, VariableArgumentCount = varArgCount, Function = newIndexFunction, ReturnBase = top };
  1540. var access = thread.PushCallStackFrame(newFrame);
  1541. var functionContext = new LuaFunctionExecutionContext() { Access = access, ArgumentCount = 3, ReturnFrameBase = top };
  1542. if (thread.CallOrReturnHookMask.Value != 0 && !thread.IsInHook)
  1543. {
  1544. await ExecuteCallHook(functionContext, ct);
  1545. }
  1546. await newIndexFunction.Func(functionContext, ct);
  1547. var results = stack.GetBuffer()[newFrame.ReturnBase..];
  1548. results.Clear();
  1549. thread.PopCallStackFrameWithStackPop();
  1550. }
  1551. [MethodImpl(MethodImplOptions.NoInlining)]
  1552. static bool ExecuteBinaryOperationMetaMethod(LuaValue vb, LuaValue vc,
  1553. VirtualMachineExecutionContext context, OpCode opCode, out bool doRestart)
  1554. {
  1555. var (name, description) = opCode.GetNameAndDescription();
  1556. doRestart = false;
  1557. if (vb.TryGetMetamethod(context.State, name, out var metamethod) ||
  1558. vc.TryGetMetamethod(context.State, name, out metamethod))
  1559. {
  1560. var stack = context.Stack;
  1561. var newBase = stack.Count;
  1562. var callable = metamethod;
  1563. if (!metamethod.TryReadFunction(out var func))
  1564. {
  1565. if (metamethod.TryGetMetamethod(context.State, Metamethods.Call, out metamethod) &&
  1566. metamethod.TryReadFunction(out func))
  1567. {
  1568. stack.Push(callable);
  1569. }
  1570. else
  1571. {
  1572. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), "call", metamethod);
  1573. }
  1574. }
  1575. stack.Push(vb);
  1576. stack.Push(vc);
  1577. var (argCount, variableArgumentCount) = PrepareForFunctionCall(context.Thread, func, newBase);
  1578. newBase += variableArgumentCount;
  1579. var newFrame = func.CreateNewFrame(context, newBase, context.FrameBase + context.Instruction.A, variableArgumentCount);
  1580. var access = context.Thread.PushCallStackFrame(newFrame);
  1581. if (context.Thread.CallOrReturnHookMask.Value != 0 && !context.Thread.IsInHook)
  1582. {
  1583. context.PostOperation = PostOperationType.SetResult;
  1584. context.Task = ExecuteCallHook(context, newFrame, argCount);
  1585. doRestart = false;
  1586. return false;
  1587. }
  1588. if (func is LuaClosure)
  1589. {
  1590. context.Push(newFrame);
  1591. doRestart = true;
  1592. return true;
  1593. }
  1594. var task = func.Func(new() { Access = access, ArgumentCount = argCount, ReturnFrameBase = newFrame.ReturnBase }, context.CancellationToken);
  1595. if (!task.IsCompleted)
  1596. {
  1597. context.PostOperation = PostOperationType.SetResult;
  1598. context.Task = task;
  1599. return false;
  1600. }
  1601. task.GetAwaiter().GetResult();
  1602. var RA = context.Instruction.A + context.FrameBase;
  1603. var results = stack.GetBuffer()[newFrame.ReturnBase..];
  1604. stack.Get(RA) = results.Length == 0 ? default : results[0];
  1605. results.Clear();
  1606. context.Thread.PopCallStackFrameWithStackPop();
  1607. return true;
  1608. }
  1609. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), description, vb, vc);
  1610. return false;
  1611. }
  1612. [MethodImpl(MethodImplOptions.NoInlining)]
  1613. internal static async ValueTask<LuaValue> ExecuteBinaryOperationMetaMethod(LuaThread thread, LuaValue vb, LuaValue vc, OpCode opCode, CancellationToken ct)
  1614. {
  1615. var (name, description) = opCode.GetNameAndDescription();
  1616. if (vb.TryGetMetamethod(thread.State, name, out var metamethod) ||
  1617. vc.TryGetMetamethod(thread.State, name, out metamethod))
  1618. {
  1619. var stack = thread.Stack;
  1620. var newBase = stack.Count;
  1621. var callable = metamethod;
  1622. if (!metamethod.TryReadFunction(out var func))
  1623. {
  1624. if (metamethod.TryGetMetamethod(thread.State, Metamethods.Call, out metamethod) &&
  1625. metamethod.TryReadFunction(out func))
  1626. {
  1627. stack.Push(callable);
  1628. }
  1629. else
  1630. {
  1631. LuaRuntimeException.AttemptInvalidOperation(thread, "call", metamethod);
  1632. }
  1633. }
  1634. stack.Push(vb);
  1635. stack.Push(vc);
  1636. var (argCount, variableArgumentCount) = PrepareForFunctionCall(thread, func, newBase);
  1637. newBase += variableArgumentCount;
  1638. var newFrame = new CallStackFrame() { Base = newBase, VariableArgumentCount = variableArgumentCount, Function = func, ReturnBase = newBase };
  1639. var access = thread.PushCallStackFrame(newFrame);
  1640. try
  1641. {
  1642. var functionContext = new LuaFunctionExecutionContext() { Access = access, ArgumentCount = argCount, ReturnFrameBase = newBase };
  1643. if (thread.CallOrReturnHookMask.Value != 0 && !thread.IsInHook)
  1644. {
  1645. await ExecuteCallHook(functionContext, ct);
  1646. }
  1647. await func.Func(functionContext, ct);
  1648. var results = stack.GetBuffer()[newFrame.ReturnBase..];
  1649. var result = results.Length == 0 ? default : results[0];
  1650. results.Clear();
  1651. return result;
  1652. }
  1653. finally
  1654. {
  1655. thread.PopCallStackFrameWithStackPop();
  1656. }
  1657. }
  1658. LuaRuntimeException.AttemptInvalidOperation(thread, description, vb, vc);
  1659. return default;
  1660. }
  1661. [MethodImpl(MethodImplOptions.NoInlining)]
  1662. static bool ExecuteUnaryOperationMetaMethod(LuaValue vb, VirtualMachineExecutionContext context,
  1663. OpCode opCode, out bool doRestart)
  1664. {
  1665. var (name, description) = opCode.GetNameAndDescription();
  1666. doRestart = false;
  1667. var stack = context.Stack;
  1668. if (vb.TryGetMetamethod(context.State, name, out var metamethod))
  1669. {
  1670. var newBase = stack.Count;
  1671. var callable = metamethod;
  1672. if (!metamethod.TryReadFunction(out var func))
  1673. {
  1674. if (metamethod.TryGetMetamethod(context.State, Metamethods.Call, out metamethod) &&
  1675. metamethod.TryReadFunction(out func))
  1676. {
  1677. stack.Push(callable);
  1678. }
  1679. else
  1680. {
  1681. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), "call", metamethod);
  1682. }
  1683. }
  1684. stack.Push(vb);
  1685. stack.Push(vb);
  1686. var (argCount, variableArgumentCount) = PrepareForFunctionCall(context.Thread, func, newBase);
  1687. newBase += variableArgumentCount;
  1688. var newFrame = func.CreateNewFrame(context, newBase, context.FrameBase + context.Instruction.A, variableArgumentCount);
  1689. var access = context.Thread.PushCallStackFrame(newFrame);
  1690. if (context.Thread.CallOrReturnHookMask.Value != 0 && !context.Thread.IsInHook)
  1691. {
  1692. context.PostOperation = PostOperationType.SetResult;
  1693. context.Task = ExecuteCallHook(context, newFrame, argCount);
  1694. doRestart = false;
  1695. return false;
  1696. }
  1697. if (func is LuaClosure)
  1698. {
  1699. context.Push(newFrame);
  1700. doRestart = true;
  1701. return true;
  1702. }
  1703. var task = func.Func(new() { Access = access, ArgumentCount = argCount, ReturnFrameBase = newFrame.ReturnBase }, context.CancellationToken);
  1704. if (!task.IsCompleted)
  1705. {
  1706. context.PostOperation = PostOperationType.SetResult;
  1707. context.Task = task;
  1708. return false;
  1709. }
  1710. var RA = context.Instruction.A + context.FrameBase;
  1711. var results = stack.GetBuffer()[newFrame.Base..];
  1712. stack.Get(RA) = results.Length == 0 ? default : results[0];
  1713. results.Clear();
  1714. context.Thread.PopCallStackFrameWithStackPop();
  1715. return true;
  1716. }
  1717. if (opCode == OpCode.Len && vb.TryReadTable(out var table))
  1718. {
  1719. var RA = context.Instruction.A + context.FrameBase;
  1720. stack.Get(RA) = table.ArrayLength;
  1721. return true;
  1722. }
  1723. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), description, vb);
  1724. return true;
  1725. }
  1726. [MethodImpl(MethodImplOptions.NoInlining)]
  1727. internal static async ValueTask<LuaValue> ExecuteUnaryOperationMetaMethod(LuaThread thread, LuaValue vb, OpCode opCode, CancellationToken cancellationToken)
  1728. {
  1729. thread.ThrowIfCancellationRequested(cancellationToken);
  1730. var (name, description) = opCode.GetNameAndDescription();
  1731. if (vb.TryGetMetamethod(thread.State, name, out var metamethod))
  1732. {
  1733. var stack = thread.Stack;
  1734. var newBase = stack.Count;
  1735. var callable = metamethod;
  1736. if (!metamethod.TryReadFunction(out var func))
  1737. {
  1738. if (metamethod.TryGetMetamethod(thread.State, Metamethods.Call, out metamethod) &&
  1739. metamethod.TryReadFunction(out func))
  1740. {
  1741. stack.Push(callable);
  1742. }
  1743. else
  1744. {
  1745. LuaRuntimeException.AttemptInvalidOperation(thread, "call", metamethod);
  1746. }
  1747. }
  1748. stack.Push(vb);
  1749. stack.Push(vb);
  1750. var (argCount, variableArgumentCount) = PrepareForFunctionCall(thread, func, newBase);
  1751. newBase += variableArgumentCount;
  1752. var newFrame = new CallStackFrame() { Base = newBase, VariableArgumentCount = variableArgumentCount, Function = func, ReturnBase = newBase };
  1753. var access = thread.PushCallStackFrame(newFrame);
  1754. try
  1755. {
  1756. var functionContext = new LuaFunctionExecutionContext() { Access = access, ArgumentCount = argCount, ReturnFrameBase = newBase };
  1757. if (thread.CallOrReturnHookMask.Value != 0 && !thread.IsInHook)
  1758. {
  1759. await ExecuteCallHook(functionContext, cancellationToken);
  1760. }
  1761. else
  1762. {
  1763. await func.Func(functionContext, cancellationToken);
  1764. }
  1765. thread.ThrowIfCancellationRequested(cancellationToken);
  1766. var results = stack.GetBuffer()[newFrame.ReturnBase..];
  1767. var result = results.Length == 0 ? default : results[0];
  1768. results.Clear();
  1769. return result;
  1770. }
  1771. finally
  1772. {
  1773. thread.PopCallStackFrameWithStackPop();
  1774. }
  1775. }
  1776. LuaRuntimeException.AttemptInvalidOperation(thread, description, vb);
  1777. return default;
  1778. }
  1779. [MethodImpl(MethodImplOptions.NoInlining)]
  1780. static bool ExecuteCompareOperationMetaMethod(LuaValue vb, LuaValue vc,
  1781. VirtualMachineExecutionContext context, OpCode opCode, out bool doRestart)
  1782. {
  1783. var (name, description) = opCode.GetNameAndDescription();
  1784. doRestart = false;
  1785. bool reverseLe = false;
  1786. ReCheck:
  1787. if (vb.TryGetMetamethod(context.State, name, out var metamethod) ||
  1788. vc.TryGetMetamethod(context.State, name, out metamethod))
  1789. {
  1790. var stack = context.Stack;
  1791. var argCount = 2;
  1792. var callable = metamethod;
  1793. if (!metamethod.TryReadFunction(out var func))
  1794. {
  1795. if (metamethod.TryGetMetamethod(context.State, Metamethods.Call, out metamethod) &&
  1796. metamethod.TryReadFunction(out func))
  1797. {
  1798. stack.Push(callable);
  1799. argCount++;
  1800. }
  1801. else
  1802. {
  1803. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), "call", metamethod);
  1804. }
  1805. }
  1806. stack.Push(vb);
  1807. stack.Push(vc);
  1808. var varArgCount = func.GetVariableArgumentCount(argCount);
  1809. var newFrame = func.CreateNewFrame(context, stack.Count - argCount + varArgCount);
  1810. if (reverseLe) newFrame.Flags |= CallStackFrameFlags.ReversedLe;
  1811. var access = context.Thread.PushCallStackFrame(newFrame);
  1812. if (context.Thread.CallOrReturnHookMask.Value != 0 && !context.Thread.IsInHook)
  1813. {
  1814. context.PostOperation = PostOperationType.Compare;
  1815. context.Task = ExecuteCallHook(context, newFrame, argCount);
  1816. doRestart = false;
  1817. return false;
  1818. }
  1819. if (func is LuaClosure)
  1820. {
  1821. context.Push(newFrame);
  1822. doRestart = true;
  1823. return true;
  1824. }
  1825. var task = func.Func(new() { Access = access, ArgumentCount = argCount, ReturnFrameBase = newFrame.ReturnBase }, context.CancellationToken);
  1826. if (!task.IsCompleted)
  1827. {
  1828. context.PostOperation = PostOperationType.Compare;
  1829. context.Task = task;
  1830. return false;
  1831. }
  1832. var results = stack.GetBuffer()[newFrame.Base..];
  1833. var compareResult = results.Length == 0 && results[0].ToBoolean();
  1834. compareResult = reverseLe ? !compareResult : compareResult;
  1835. if (compareResult != (context.Instruction.A == 1))
  1836. {
  1837. context.Pc++;
  1838. }
  1839. results.Clear();
  1840. context.Thread.PopCallStackFrameWithStackPop();
  1841. return true;
  1842. }
  1843. if (opCode == OpCode.Le)
  1844. {
  1845. reverseLe = true;
  1846. name = Metamethods.Lt;
  1847. (vb, vc) = (vc, vb);
  1848. goto ReCheck;
  1849. }
  1850. if (opCode != OpCode.Eq)
  1851. {
  1852. if (reverseLe)
  1853. {
  1854. (vb, vc) = (vc, vb);
  1855. }
  1856. LuaRuntimeException.AttemptInvalidOperation(GetThreadWithCurrentPc(context), description, vb, vc);
  1857. }
  1858. else
  1859. {
  1860. if (context.Instruction.A == 1)
  1861. {
  1862. context.Pc++;
  1863. }
  1864. }
  1865. return true;
  1866. }
  1867. [MethodImpl(MethodImplOptions.NoInlining)]
  1868. internal static async ValueTask<bool> ExecuteCompareOperationMetaMethod(LuaThread thread, LuaValue vb, LuaValue vc, OpCode opCode, CancellationToken cancellationToken)
  1869. {
  1870. thread.ThrowIfCancellationRequested(cancellationToken);
  1871. var (name, description) = opCode.GetNameAndDescription();
  1872. bool reverseLe = false;
  1873. ReCheck:
  1874. if (vb.TryGetMetamethod(thread.State, name, out var metamethod) ||
  1875. vc.TryGetMetamethod(thread.State, name, out metamethod))
  1876. {
  1877. var stack = thread.Stack;
  1878. var newBase = stack.Count;
  1879. var callable = metamethod;
  1880. if (!metamethod.TryReadFunction(out var func))
  1881. {
  1882. if (metamethod.TryGetMetamethod(thread.State, Metamethods.Call, out metamethod) &&
  1883. metamethod.TryReadFunction(out func))
  1884. {
  1885. stack.Push(callable);
  1886. }
  1887. else
  1888. {
  1889. LuaRuntimeException.AttemptInvalidOperation(thread, "call", metamethod);
  1890. }
  1891. }
  1892. stack.Push(vb);
  1893. stack.Push(vc);
  1894. var (argCount, variableArgumentCount) = PrepareForFunctionCall(thread, func, newBase);
  1895. newBase += variableArgumentCount;
  1896. var newFrame = new CallStackFrame() { Base = newBase, VariableArgumentCount = variableArgumentCount, Function = func, ReturnBase = newBase };
  1897. var access = thread.PushCallStackFrame(newFrame);
  1898. try
  1899. {
  1900. var functionContext = new LuaFunctionExecutionContext() { Access = access, ArgumentCount = argCount, ReturnFrameBase = newBase };
  1901. if (thread.CallOrReturnHookMask.Value != 0 && !thread.IsInHook)
  1902. {
  1903. await ExecuteCallHook(functionContext, cancellationToken);
  1904. }
  1905. else
  1906. {
  1907. await func.Func(functionContext, cancellationToken);
  1908. }
  1909. thread.ThrowIfCancellationRequested(cancellationToken);
  1910. var results = stack.GetBuffer()[newFrame.ReturnBase..];
  1911. var result = results.Length == 0 ? default : results[0];
  1912. results.Clear();
  1913. return result.ToBoolean();
  1914. }
  1915. finally
  1916. {
  1917. thread.PopCallStackFrameWithStackPop();
  1918. }
  1919. }
  1920. if (opCode == OpCode.Le)
  1921. {
  1922. reverseLe = true;
  1923. name = Metamethods.Lt;
  1924. (vb, vc) = (vc, vb);
  1925. goto ReCheck;
  1926. }
  1927. if (opCode != OpCode.Eq)
  1928. {
  1929. if (reverseLe)
  1930. {
  1931. (vb, vc) = (vc, vb);
  1932. }
  1933. LuaRuntimeException.AttemptInvalidOperation(thread, description, vb, vc);
  1934. }
  1935. return default;
  1936. }
  1937. // 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.
  1938. // see: https://wubingzheng.github.io/build-lua-in-rust/en/ch08-02.arguments.html
  1939. [MethodImpl(MethodImplOptions.NoInlining)]
  1940. internal static void PrepareVariableArgument(LuaStack stack, int argumentCount,
  1941. int variableArgumentCount)
  1942. {
  1943. var top = stack.Count;
  1944. var newBase = stack.Count - argumentCount;
  1945. var temp = newBase;
  1946. newBase += variableArgumentCount;
  1947. stack.EnsureCapacity(newBase + argumentCount);
  1948. stack.NotifyTop(newBase + argumentCount);
  1949. var stackBuffer = stack.GetBuffer()[temp..];
  1950. stackBuffer[..argumentCount].CopyTo(stackBuffer[variableArgumentCount..]);
  1951. stackBuffer.Slice(argumentCount, variableArgumentCount).CopyTo(stackBuffer);
  1952. stack.PopUntil(top);
  1953. }
  1954. // 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.
  1955. // see: https://wubingzheng.github.io/build-lua-in-rust/en/ch08-02.arguments.html
  1956. [MethodImpl(MethodImplOptions.NoInlining)]
  1957. static (int ArgumentCount, int VariableArgumentCount) PrepareVariableArgument(LuaStack stack, int newBase, int argumentCount,
  1958. int variableArgumentCount)
  1959. {
  1960. var temp = newBase;
  1961. newBase += variableArgumentCount;
  1962. stack.EnsureCapacity(newBase + argumentCount);
  1963. stack.NotifyTop(newBase + argumentCount);
  1964. var stackBuffer = stack.GetBuffer()[temp..];
  1965. stackBuffer[..argumentCount].CopyTo(stackBuffer[variableArgumentCount..]);
  1966. stackBuffer.Slice(argumentCount, variableArgumentCount).CopyTo(stackBuffer);
  1967. return (argumentCount, variableArgumentCount);
  1968. }
  1969. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1970. static (int ArgumentCount, int VariableArgumentCount) PrepareForFunctionCall(LuaThread thread, LuaFunction function,
  1971. Instruction instruction, int newBase, bool isMetamethod)
  1972. {
  1973. var argumentCount = instruction.B - 1;
  1974. if (argumentCount == -1)
  1975. {
  1976. argumentCount = (ushort)(thread.Stack.Count - newBase);
  1977. }
  1978. else
  1979. {
  1980. if (isMetamethod)
  1981. {
  1982. argumentCount += 1;
  1983. }
  1984. thread.Stack.SetTop(newBase + argumentCount);
  1985. }
  1986. var variableArgumentCount = function.GetVariableArgumentCount(argumentCount);
  1987. if (variableArgumentCount < 0)
  1988. {
  1989. thread.Stack.SetTop(thread.Stack.Count - variableArgumentCount);
  1990. argumentCount -= variableArgumentCount;
  1991. variableArgumentCount = 0;
  1992. }
  1993. if (variableArgumentCount == 0)
  1994. {
  1995. return (argumentCount, 0);
  1996. }
  1997. return PrepareVariableArgument(thread.Stack, newBase, argumentCount, variableArgumentCount);
  1998. }
  1999. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2000. static (int ArgumentCount, int VariableArgumentCount) PrepareForFunctionCall(LuaThread thread, LuaFunction function,
  2001. int newBase)
  2002. {
  2003. var argumentCount = (int)(thread.Stack.Count - newBase);
  2004. var variableArgumentCount = function.GetVariableArgumentCount(argumentCount);
  2005. if (variableArgumentCount < 0)
  2006. {
  2007. thread.Stack.SetTop(thread.Stack.Count - variableArgumentCount);
  2008. argumentCount -= variableArgumentCount;
  2009. variableArgumentCount = 0;
  2010. }
  2011. if (variableArgumentCount == 0)
  2012. {
  2013. return (argumentCount, 0);
  2014. }
  2015. return PrepareVariableArgument(thread.Stack, newBase, argumentCount, variableArgumentCount);
  2016. }
  2017. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2018. static (int ArgumentCount, int VariableArgumentCount) PrepareForFunctionTailCall(LuaThread thread, LuaFunction function,
  2019. Instruction instruction, int newBase, bool isMetamethod)
  2020. {
  2021. var stack = thread.Stack;
  2022. var argumentCount = instruction.B - 1;
  2023. if (instruction.B == 0)
  2024. {
  2025. argumentCount = (ushort)(stack.Count - newBase);
  2026. }
  2027. else
  2028. {
  2029. if (isMetamethod)
  2030. {
  2031. argumentCount += 1;
  2032. }
  2033. thread.Stack.SetTop(newBase + argumentCount);
  2034. }
  2035. // In the case of tailcall, the local variables of the caller are immediately discarded, so there is no need to retain them.
  2036. // Therefore, a call can be made without allocating new registers.
  2037. var currentBase = thread.GetCurrentFrame().Base;
  2038. {
  2039. var stackBuffer = stack.GetBuffer();
  2040. if (argumentCount > 0)
  2041. stackBuffer.Slice(newBase, argumentCount).CopyTo(stackBuffer.Slice(currentBase, argumentCount));
  2042. newBase = currentBase;
  2043. }
  2044. var variableArgumentCount = function.GetVariableArgumentCount(argumentCount);
  2045. if (variableArgumentCount <= 0)
  2046. {
  2047. return (argumentCount, 0);
  2048. }
  2049. return PrepareVariableArgument(thread.Stack, newBase, argumentCount, variableArgumentCount);
  2050. }
  2051. static LuaThread GetThreadWithCurrentPc(VirtualMachineExecutionContext context)
  2052. {
  2053. GetThreadWithCurrentPc(context.Thread, context.Pc);
  2054. return context.Thread;
  2055. }
  2056. static void GetThreadWithCurrentPc(LuaThread thread, int pc)
  2057. {
  2058. var frame = thread.GetCurrentFrame();
  2059. thread.PushCallStackFrame(frame with { CallerInstructionIndex = pc });
  2060. }
  2061. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2062. static CallStackFrame CreateNewFrame(this LuaFunction function, VirtualMachineExecutionContext context, int newBase)
  2063. {
  2064. return new()
  2065. {
  2066. Base = newBase,
  2067. ReturnBase = newBase,
  2068. Function = function,
  2069. VariableArgumentCount = 0,
  2070. CallerInstructionIndex = context.Pc,
  2071. };
  2072. }
  2073. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2074. static CallStackFrame CreateNewFrame(this LuaFunction function, VirtualMachineExecutionContext context, int newBase, int returnBase, int variableArgumentCount)
  2075. {
  2076. return new()
  2077. {
  2078. Base = newBase,
  2079. ReturnBase = returnBase,
  2080. Function = function,
  2081. VariableArgumentCount = variableArgumentCount,
  2082. CallerInstructionIndex = context.Pc,
  2083. };
  2084. }
  2085. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  2086. static CallStackFrame CreateNewTailCallFrame(this LuaFunction function, VirtualMachineExecutionContext context, int newBase, int returnBase, int variableArgumentCount)
  2087. {
  2088. return new()
  2089. {
  2090. Base = newBase,
  2091. ReturnBase = returnBase,
  2092. Function = function,
  2093. VariableArgumentCount = variableArgumentCount,
  2094. CallerInstructionIndex = context.Pc,
  2095. Flags = CallStackFrameFlags.TailCall
  2096. };
  2097. }
  2098. }