9
3

lvm.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. /*
  2. ** $Id: lvm.c $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lvm_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <float.h>
  10. #include <limits.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "lua.h"
  16. #include "ldebug.h"
  17. #include "ldo.h"
  18. #include "lfunc.h"
  19. #include "lgc.h"
  20. #include "lobject.h"
  21. #include "lopcodes.h"
  22. #include "lstate.h"
  23. #include "lstring.h"
  24. #include "ltable.h"
  25. #include "ltm.h"
  26. #include "lvm.h"
  27. /*
  28. ** By default, use jump tables in the main interpreter loop on gcc
  29. ** and compatible compilers.
  30. */
  31. #if !defined(LUA_USE_JUMPTABLE)
  32. #if defined(__GNUC__)
  33. #define LUA_USE_JUMPTABLE 1
  34. #else
  35. #define LUA_USE_JUMPTABLE 0
  36. #endif
  37. #endif
  38. /* limit for table tag-method chains (to avoid infinite loops) */
  39. #define MAXTAGLOOP 2000
  40. /*
  41. ** 'l_intfitsf' checks whether a given integer is in the range that
  42. ** can be converted to a float without rounding. Used in comparisons.
  43. */
  44. /* number of bits in the mantissa of a float */
  45. #define NBM (l_floatatt(MANT_DIG))
  46. /*
  47. ** Check whether some integers may not fit in a float, testing whether
  48. ** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.)
  49. ** (The shifts are done in parts, to avoid shifting by more than the size
  50. ** of an integer. In a worst case, NBM == 113 for long double and
  51. ** sizeof(long) == 32.)
  52. */
  53. #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \
  54. >> (NBM - (3 * (NBM / 4)))) > 0
  55. /* limit for integers that fit in a float */
  56. #define MAXINTFITSF ((lua_Unsigned)1 << NBM)
  57. /* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */
  58. #define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF))
  59. #else /* all integers fit in a float precisely */
  60. #define l_intfitsf(i) 1
  61. #endif
  62. /*
  63. ** Try to convert a value from string to a number value.
  64. ** If the value is not a string or is a string not representing
  65. ** a valid numeral (or if coercions from strings to numbers
  66. ** are disabled via macro 'cvt2num'), do not modify 'result'
  67. ** and return 0.
  68. */
  69. static int l_strton (const TValue *obj, TValue *result) {
  70. lua_assert(obj != result);
  71. if (!cvt2num(obj)) /* is object not a string? */
  72. return 0;
  73. else
  74. return (luaO_str2num(svalue(obj), result) == vslen(obj) + 1);
  75. }
  76. /*
  77. ** Try to convert a value to a float. The float case is already handled
  78. ** by the macro 'tonumber'.
  79. */
  80. int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
  81. TValue v;
  82. if (ttisinteger(obj)) {
  83. *n = cast_num(ivalue(obj));
  84. return 1;
  85. }
  86. else if (l_strton(obj, &v)) { /* string coercible to number? */
  87. *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */
  88. return 1;
  89. }
  90. else
  91. return 0; /* conversion failed */
  92. }
  93. /*
  94. ** try to convert a float to an integer, rounding according to 'mode'.
  95. */
  96. int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) {
  97. lua_Number f = l_floor(n);
  98. if (n != f) { /* not an integral value? */
  99. if (mode == F2Ieq) return 0; /* fails if mode demands integral value */
  100. else if (mode == F2Iceil) /* needs ceil? */
  101. f += 1; /* convert floor to ceil (remember: n != f) */
  102. }
  103. return lua_numbertointeger(f, p);
  104. }
  105. /*
  106. ** try to convert a value to an integer, rounding according to 'mode',
  107. ** without string coercion.
  108. ** ("Fast track" handled by macro 'tointegerns'.)
  109. */
  110. int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) {
  111. if (ttisfloat(obj))
  112. return luaV_flttointeger(fltvalue(obj), p, mode);
  113. else if (ttisinteger(obj)) {
  114. *p = ivalue(obj);
  115. return 1;
  116. }
  117. else
  118. return 0;
  119. }
  120. /*
  121. ** try to convert a value to an integer.
  122. */
  123. int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) {
  124. TValue v;
  125. if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */
  126. obj = &v; /* change it to point to its corresponding number */
  127. return luaV_tointegerns(obj, p, mode);
  128. }
  129. /*
  130. ** Try to convert a 'for' limit to an integer, preserving the semantics
  131. ** of the loop. Return true if the loop must not run; otherwise, '*p'
  132. ** gets the integer limit.
  133. ** (The following explanation assumes a positive step; it is valid for
  134. ** negative steps mutatis mutandis.)
  135. ** If the limit is an integer or can be converted to an integer,
  136. ** rounding down, that is the limit.
  137. ** Otherwise, check whether the limit can be converted to a float. If
  138. ** the float is too large, clip it to LUA_MAXINTEGER. If the float
  139. ** is too negative, the loop should not run, because any initial
  140. ** integer value is greater than such limit; so, the function returns
  141. ** true to signal that. (For this latter case, no integer limit would be
  142. ** correct; even a limit of LUA_MININTEGER would run the loop once for
  143. ** an initial value equal to LUA_MININTEGER.)
  144. */
  145. static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
  146. lua_Integer *p, lua_Integer step) {
  147. if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) {
  148. /* not coercible to in integer */
  149. lua_Number flim; /* try to convert to float */
  150. if (!tonumber(lim, &flim)) /* cannot convert to float? */
  151. luaG_forerror(L, lim, "limit");
  152. /* else 'flim' is a float out of integer bounds */
  153. if (luai_numlt(0, flim)) { /* if it is positive, it is too large */
  154. if (step < 0) return 1; /* initial value must be less than it */
  155. *p = LUA_MAXINTEGER; /* truncate */
  156. }
  157. else { /* it is less than min integer */
  158. if (step > 0) return 1; /* initial value must be greater than it */
  159. *p = LUA_MININTEGER; /* truncate */
  160. }
  161. }
  162. return (step > 0 ? init > *p : init < *p); /* not to run? */
  163. }
  164. /*
  165. ** Prepare a numerical for loop (opcode OP_FORPREP).
  166. ** Return true to skip the loop. Otherwise,
  167. ** after preparation, stack will be as follows:
  168. ** ra : internal index (safe copy of the control variable)
  169. ** ra + 1 : loop counter (integer loops) or limit (float loops)
  170. ** ra + 2 : step
  171. ** ra + 3 : control variable
  172. */
  173. static int forprep (lua_State *L, StkId ra) {
  174. TValue *pinit = s2v(ra);
  175. TValue *plimit = s2v(ra + 1);
  176. TValue *pstep = s2v(ra + 2);
  177. if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
  178. lua_Integer init = ivalue(pinit);
  179. lua_Integer step = ivalue(pstep);
  180. lua_Integer limit;
  181. if (step == 0)
  182. luaG_runerror(L, "'for' step is zero");
  183. setivalue(s2v(ra + 3), init); /* control variable */
  184. if (forlimit(L, init, plimit, &limit, step))
  185. return 1; /* skip the loop */
  186. else { /* prepare loop counter */
  187. lua_Unsigned count;
  188. if (step > 0) { /* ascending loop? */
  189. count = l_castS2U(limit) - l_castS2U(init);
  190. if (step != 1) /* avoid division in the too common case */
  191. count /= l_castS2U(step);
  192. }
  193. else { /* step < 0; descending loop */
  194. count = l_castS2U(init) - l_castS2U(limit);
  195. /* 'step+1' avoids negating 'mininteger' */
  196. count /= l_castS2U(-(step + 1)) + 1u;
  197. }
  198. /* store the counter in place of the limit (which won't be
  199. needed anymore) */
  200. setivalue(plimit, l_castU2S(count));
  201. }
  202. }
  203. else { /* try making all values floats */
  204. lua_Number init; lua_Number limit; lua_Number step;
  205. if (unlikely(!tonumber(plimit, &limit)))
  206. luaG_forerror(L, plimit, "limit");
  207. if (unlikely(!tonumber(pstep, &step)))
  208. luaG_forerror(L, pstep, "step");
  209. if (unlikely(!tonumber(pinit, &init)))
  210. luaG_forerror(L, pinit, "initial value");
  211. if (step == 0)
  212. luaG_runerror(L, "'for' step is zero");
  213. if (luai_numlt(0, step) ? luai_numlt(limit, init)
  214. : luai_numlt(init, limit))
  215. return 1; /* skip the loop */
  216. else {
  217. /* make sure internal values are all floats */
  218. setfltvalue(plimit, limit);
  219. setfltvalue(pstep, step);
  220. setfltvalue(s2v(ra), init); /* internal index */
  221. setfltvalue(s2v(ra + 3), init); /* control variable */
  222. }
  223. }
  224. return 0;
  225. }
  226. /*
  227. ** Execute a step of a float numerical for loop, returning
  228. ** true iff the loop must continue. (The integer case is
  229. ** written online with opcode OP_FORLOOP, for performance.)
  230. */
  231. static int floatforloop (StkId ra) {
  232. lua_Number step = fltvalue(s2v(ra + 2));
  233. lua_Number limit = fltvalue(s2v(ra + 1));
  234. lua_Number idx = fltvalue(s2v(ra)); /* internal index */
  235. idx = luai_numadd(L, idx, step); /* increment index */
  236. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  237. : luai_numle(limit, idx)) {
  238. chgfltvalue(s2v(ra), idx); /* update internal index */
  239. setfltvalue(s2v(ra + 3), idx); /* and control variable */
  240. return 1; /* jump back */
  241. }
  242. else
  243. return 0; /* finish the loop */
  244. }
  245. /*
  246. ** Finish the table access 'val = t[key]'.
  247. ** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to
  248. ** t[k] entry (which must be empty).
  249. */
  250. void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
  251. const TValue *slot) {
  252. int loop; /* counter to avoid infinite loops */
  253. const TValue *tm; /* metamethod */
  254. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  255. if (slot == NULL) { /* 't' is not a table? */
  256. lua_assert(!ttistable(t));
  257. tm = luaT_gettmbyobj(L, t, TM_INDEX);
  258. if (unlikely(notm(tm)))
  259. luaG_typeerror(L, t, "index"); /* no metamethod */
  260. /* else will try the metamethod */
  261. }
  262. else { /* 't' is a table */
  263. lua_assert(isempty(slot));
  264. tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */
  265. if (tm == NULL) { /* no metamethod? */
  266. setnilvalue(s2v(val)); /* result is nil */
  267. return;
  268. }
  269. /* else will try the metamethod */
  270. }
  271. if (ttisfunction(tm)) { /* is metamethod a function? */
  272. luaT_callTMres(L, tm, t, key, val); /* call it */
  273. return;
  274. }
  275. t = tm; /* else try to access 'tm[key]' */
  276. if (luaV_fastget(L, t, key, slot, luaH_get)) { /* fast track? */
  277. setobj2s(L, val, slot); /* done */
  278. return;
  279. }
  280. /* else repeat (tail call 'luaV_finishget') */
  281. }
  282. luaG_runerror(L, "'__index' chain too long; possible loop");
  283. }
  284. /*
  285. ** Finish a table assignment 't[key] = val'.
  286. ** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points
  287. ** to the entry 't[key]', or to a value with an absent key if there
  288. ** is no such entry. (The value at 'slot' must be empty, otherwise
  289. ** 'luaV_fastget' would have done the job.)
  290. */
  291. void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
  292. TValue *val, const TValue *slot) {
  293. int loop; /* counter to avoid infinite loops */
  294. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  295. const TValue *tm; /* '__newindex' metamethod */
  296. if (slot != NULL) { /* is 't' a table? */
  297. Table *h = hvalue(t); /* save 't' table */
  298. lua_assert(isempty(slot)); /* slot must be empty */
  299. tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */
  300. if (tm == NULL) { /* no metamethod? */
  301. if (isabstkey(slot)) /* no previous entry? */
  302. slot = luaH_newkey(L, h, key); /* create one */
  303. /* no metamethod and (now) there is an entry with given key */
  304. setobj2t(L, cast(TValue *, slot), val); /* set its new value */
  305. invalidateTMcache(h);
  306. luaC_barrierback(L, obj2gco(h), val);
  307. return;
  308. }
  309. /* else will try the metamethod */
  310. }
  311. else { /* not a table; check metamethod */
  312. tm = luaT_gettmbyobj(L, t, TM_NEWINDEX);
  313. if (unlikely(notm(tm)))
  314. luaG_typeerror(L, t, "index");
  315. }
  316. /* try the metamethod */
  317. if (ttisfunction(tm)) {
  318. luaT_callTM(L, tm, t, key, val);
  319. return;
  320. }
  321. t = tm; /* else repeat assignment over 'tm' */
  322. if (luaV_fastget(L, t, key, slot, luaH_get)) {
  323. luaV_finishfastset(L, t, slot, val);
  324. return; /* done */
  325. }
  326. /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */
  327. }
  328. luaG_runerror(L, "'__newindex' chain too long; possible loop");
  329. }
  330. /*
  331. ** Compare two strings 'ls' x 'rs', returning an integer less-equal-
  332. ** -greater than zero if 'ls' is less-equal-greater than 'rs'.
  333. ** The code is a little tricky because it allows '\0' in the strings
  334. ** and it uses 'strcoll' (to respect locales) for each segments
  335. ** of the strings.
  336. */
  337. static int l_strcmp (const TString *ls, const TString *rs) {
  338. const char *l = getstr(ls);
  339. size_t ll = tsslen(ls);
  340. const char *r = getstr(rs);
  341. size_t lr = tsslen(rs);
  342. for (;;) { /* for each segment */
  343. int temp = strcoll(l, r);
  344. if (temp != 0) /* not equal? */
  345. return temp; /* done */
  346. else { /* strings are equal up to a '\0' */
  347. size_t len = strlen(l); /* index of first '\0' in both strings */
  348. if (len == lr) /* 'rs' is finished? */
  349. return (len == ll) ? 0 : 1; /* check 'ls' */
  350. else if (len == ll) /* 'ls' is finished? */
  351. return -1; /* 'ls' is less than 'rs' ('rs' is not finished) */
  352. /* both strings longer than 'len'; go on comparing after the '\0' */
  353. len++;
  354. l += len; ll -= len; r += len; lr -= len;
  355. }
  356. }
  357. }
  358. /*
  359. ** Check whether integer 'i' is less than float 'f'. If 'i' has an
  360. ** exact representation as a float ('l_intfitsf'), compare numbers as
  361. ** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'.
  362. ** If 'ceil(f)' is out of integer range, either 'f' is greater than
  363. ** all integers or less than all integers.
  364. ** (The test with 'l_intfitsf' is only for performance; the else
  365. ** case is correct for all values, but it is slow due to the conversion
  366. ** from float to int.)
  367. ** When 'f' is NaN, comparisons must result in false.
  368. */
  369. static int LTintfloat (lua_Integer i, lua_Number f) {
  370. if (l_intfitsf(i))
  371. return luai_numlt(cast_num(i), f); /* compare them as floats */
  372. else { /* i < f <=> i < ceil(f) */
  373. lua_Integer fi;
  374. if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
  375. return i < fi; /* compare them as integers */
  376. else /* 'f' is either greater or less than all integers */
  377. return f > 0; /* greater? */
  378. }
  379. }
  380. /*
  381. ** Check whether integer 'i' is less than or equal to float 'f'.
  382. ** See comments on previous function.
  383. */
  384. static int LEintfloat (lua_Integer i, lua_Number f) {
  385. if (l_intfitsf(i))
  386. return luai_numle(cast_num(i), f); /* compare them as floats */
  387. else { /* i <= f <=> i <= floor(f) */
  388. lua_Integer fi;
  389. if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
  390. return i <= fi; /* compare them as integers */
  391. else /* 'f' is either greater or less than all integers */
  392. return f > 0; /* greater? */
  393. }
  394. }
  395. /*
  396. ** Check whether float 'f' is less than integer 'i'.
  397. ** See comments on previous function.
  398. */
  399. static int LTfloatint (lua_Number f, lua_Integer i) {
  400. if (l_intfitsf(i))
  401. return luai_numlt(f, cast_num(i)); /* compare them as floats */
  402. else { /* f < i <=> floor(f) < i */
  403. lua_Integer fi;
  404. if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
  405. return fi < i; /* compare them as integers */
  406. else /* 'f' is either greater or less than all integers */
  407. return f < 0; /* less? */
  408. }
  409. }
  410. /*
  411. ** Check whether float 'f' is less than or equal to integer 'i'.
  412. ** See comments on previous function.
  413. */
  414. static int LEfloatint (lua_Number f, lua_Integer i) {
  415. if (l_intfitsf(i))
  416. return luai_numle(f, cast_num(i)); /* compare them as floats */
  417. else { /* f <= i <=> ceil(f) <= i */
  418. lua_Integer fi;
  419. if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
  420. return fi <= i; /* compare them as integers */
  421. else /* 'f' is either greater or less than all integers */
  422. return f < 0; /* less? */
  423. }
  424. }
  425. /*
  426. ** Return 'l < r', for numbers.
  427. */
  428. static int LTnum (const TValue *l, const TValue *r) {
  429. lua_assert(ttisnumber(l) && ttisnumber(r));
  430. if (ttisinteger(l)) {
  431. lua_Integer li = ivalue(l);
  432. if (ttisinteger(r))
  433. return li < ivalue(r); /* both are integers */
  434. else /* 'l' is int and 'r' is float */
  435. return LTintfloat(li, fltvalue(r)); /* l < r ? */
  436. }
  437. else {
  438. lua_Number lf = fltvalue(l); /* 'l' must be float */
  439. if (ttisfloat(r))
  440. return luai_numlt(lf, fltvalue(r)); /* both are float */
  441. else /* 'l' is float and 'r' is int */
  442. return LTfloatint(lf, ivalue(r));
  443. }
  444. }
  445. /*
  446. ** Return 'l <= r', for numbers.
  447. */
  448. static int LEnum (const TValue *l, const TValue *r) {
  449. lua_assert(ttisnumber(l) && ttisnumber(r));
  450. if (ttisinteger(l)) {
  451. lua_Integer li = ivalue(l);
  452. if (ttisinteger(r))
  453. return li <= ivalue(r); /* both are integers */
  454. else /* 'l' is int and 'r' is float */
  455. return LEintfloat(li, fltvalue(r)); /* l <= r ? */
  456. }
  457. else {
  458. lua_Number lf = fltvalue(l); /* 'l' must be float */
  459. if (ttisfloat(r))
  460. return luai_numle(lf, fltvalue(r)); /* both are float */
  461. else /* 'l' is float and 'r' is int */
  462. return LEfloatint(lf, ivalue(r));
  463. }
  464. }
  465. /*
  466. ** return 'l < r' for non-numbers.
  467. */
  468. static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) {
  469. lua_assert(!ttisnumber(l) || !ttisnumber(r));
  470. if (ttisstring(l) && ttisstring(r)) /* both are strings? */
  471. return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
  472. else
  473. return luaT_callorderTM(L, l, r, TM_LT);
  474. }
  475. /*
  476. ** Main operation less than; return 'l < r'.
  477. */
  478. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  479. if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */
  480. return LTnum(l, r);
  481. else return lessthanothers(L, l, r);
  482. }
  483. /*
  484. ** return 'l <= r' for non-numbers.
  485. */
  486. static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) {
  487. lua_assert(!ttisnumber(l) || !ttisnumber(r));
  488. if (ttisstring(l) && ttisstring(r)) /* both are strings? */
  489. return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
  490. else
  491. return luaT_callorderTM(L, l, r, TM_LE);
  492. }
  493. /*
  494. ** Main operation less than or equal to; return 'l <= r'.
  495. */
  496. int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
  497. if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */
  498. return LEnum(l, r);
  499. else return lessequalothers(L, l, r);
  500. }
  501. /*
  502. ** Main operation for equality of Lua values; return 't1 == t2'.
  503. ** L == NULL means raw equality (no metamethods)
  504. */
  505. int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
  506. const TValue *tm;
  507. if (ttypetag(t1) != ttypetag(t2)) { /* not the same variant? */
  508. if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER)
  509. return 0; /* only numbers can be equal with different variants */
  510. else { /* two numbers with different variants */
  511. lua_Integer i1, i2; /* compare them as integers */
  512. return (tointegerns(t1, &i1) && tointegerns(t2, &i2) && i1 == i2);
  513. }
  514. }
  515. /* values have same type and same variant */
  516. switch (ttypetag(t1)) {
  517. case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: return 1;
  518. case LUA_VNUMINT: return (ivalue(t1) == ivalue(t2));
  519. case LUA_VNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
  520. case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  521. case LUA_VLCF: return fvalue(t1) == fvalue(t2);
  522. case LUA_VSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2));
  523. case LUA_VLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2));
  524. case LUA_VUSERDATA: {
  525. if (uvalue(t1) == uvalue(t2)) return 1;
  526. else if (L == NULL) return 0;
  527. tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
  528. if (tm == NULL)
  529. tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
  530. break; /* will try TM */
  531. }
  532. case LUA_VTABLE: {
  533. if (hvalue(t1) == hvalue(t2)) return 1;
  534. else if (L == NULL) return 0;
  535. tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
  536. if (tm == NULL)
  537. tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
  538. break; /* will try TM */
  539. }
  540. default:
  541. return gcvalue(t1) == gcvalue(t2);
  542. }
  543. if (tm == NULL) /* no TM? */
  544. return 0; /* objects are different */
  545. else {
  546. luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */
  547. return !l_isfalse(s2v(L->top));
  548. }
  549. }
  550. /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
  551. #define tostring(L,o) \
  552. (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
  553. #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0)
  554. /* copy strings in stack from top - n up to top - 1 to buffer */
  555. static void copy2buff (StkId top, int n, char *buff) {
  556. size_t tl = 0; /* size already copied */
  557. do {
  558. size_t l = vslen(s2v(top - n)); /* length of string being copied */
  559. memcpy(buff + tl, svalue(s2v(top - n)), l * sizeof(char));
  560. tl += l;
  561. } while (--n > 0);
  562. }
  563. /*
  564. ** Main operation for concatenation: concat 'total' values in the stack,
  565. ** from 'L->top - total' up to 'L->top - 1'.
  566. */
  567. void luaV_concat (lua_State *L, int total) {
  568. if (total == 1)
  569. return; /* "all" values already concatenated */
  570. do {
  571. StkId top = L->top;
  572. int n = 2; /* number of elements handled in this pass (at least 2) */
  573. if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) ||
  574. !tostring(L, s2v(top - 1)))
  575. luaT_tryconcatTM(L);
  576. else if (isemptystr(s2v(top - 1))) /* second operand is empty? */
  577. cast_void(tostring(L, s2v(top - 2))); /* result is first operand */
  578. else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */
  579. setobjs2s(L, top - 2, top - 1); /* result is second op. */
  580. }
  581. else {
  582. /* at least two non-empty string values; get as many as possible */
  583. size_t tl = vslen(s2v(top - 1));
  584. TString *ts;
  585. /* collect total length and number of strings */
  586. for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
  587. size_t l = vslen(s2v(top - n - 1));
  588. if (unlikely(l >= (MAX_SIZE/sizeof(char)) - tl))
  589. luaG_runerror(L, "string length overflow");
  590. tl += l;
  591. }
  592. if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */
  593. char buff[LUAI_MAXSHORTLEN];
  594. copy2buff(top, n, buff); /* copy strings to buffer */
  595. ts = luaS_newlstr(L, buff, tl);
  596. }
  597. else { /* long string; copy strings directly to final result */
  598. ts = luaS_createlngstrobj(L, tl);
  599. copy2buff(top, n, getstr(ts));
  600. }
  601. setsvalue2s(L, top - n, ts); /* create result */
  602. }
  603. total -= n-1; /* got 'n' strings to create 1 new */
  604. L->top -= n-1; /* popped 'n' strings and pushed one */
  605. } while (total > 1); /* repeat until only 1 result left */
  606. }
  607. /*
  608. ** Main operation 'ra = #rb'.
  609. */
  610. void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
  611. const TValue *tm;
  612. switch (ttypetag(rb)) {
  613. case LUA_VTABLE: {
  614. Table *h = hvalue(rb);
  615. tm = fasttm(L, h->metatable, TM_LEN);
  616. if (tm) break; /* metamethod? break switch to call it */
  617. setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */
  618. return;
  619. }
  620. case LUA_VSHRSTR: {
  621. setivalue(s2v(ra), tsvalue(rb)->shrlen);
  622. return;
  623. }
  624. case LUA_VLNGSTR: {
  625. setivalue(s2v(ra), tsvalue(rb)->u.lnglen);
  626. return;
  627. }
  628. default: { /* try metamethod */
  629. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  630. if (unlikely(notm(tm))) /* no metamethod? */
  631. luaG_typeerror(L, rb, "get length of");
  632. break;
  633. }
  634. }
  635. luaT_callTMres(L, tm, rb, rb, ra);
  636. }
  637. /*
  638. ** Integer division; return 'm // n', that is, floor(m/n).
  639. ** C division truncates its result (rounds towards zero).
  640. ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
  641. ** otherwise 'floor(q) == trunc(q) - 1'.
  642. */
  643. lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
  644. if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
  645. if (n == 0)
  646. luaG_runerror(L, "attempt to divide by zero");
  647. return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
  648. }
  649. else {
  650. lua_Integer q = m / n; /* perform C division */
  651. if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */
  652. q -= 1; /* correct result for different rounding */
  653. return q;
  654. }
  655. }
  656. /*
  657. ** Integer modulus; return 'm % n'. (Assume that C '%' with
  658. ** negative operands follows C99 behavior. See previous comment
  659. ** about luaV_idiv.)
  660. */
  661. lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
  662. if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
  663. if (n == 0)
  664. luaG_runerror(L, "attempt to perform 'n%%0'");
  665. return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
  666. }
  667. else {
  668. lua_Integer r = m % n;
  669. if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */
  670. r += n; /* correct result for different rounding */
  671. return r;
  672. }
  673. }
  674. /*
  675. ** Float modulus
  676. */
  677. lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) {
  678. lua_Number r;
  679. luai_nummod(L, m, n, r);
  680. return r;
  681. }
  682. /* number of bits in an integer */
  683. #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
  684. /*
  685. ** Shift left operation. (Shift right just negates 'y'.)
  686. */
  687. #define luaV_shiftr(x,y) luaV_shiftl(x,-(y))
  688. lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
  689. if (y < 0) { /* shift right? */
  690. if (y <= -NBITS) return 0;
  691. else return intop(>>, x, -y);
  692. }
  693. else { /* shift left */
  694. if (y >= NBITS) return 0;
  695. else return intop(<<, x, y);
  696. }
  697. }
  698. /*
  699. ** create a new Lua closure, push it in the stack, and initialize
  700. ** its upvalues.
  701. */
  702. static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
  703. StkId ra) {
  704. int nup = p->sizeupvalues;
  705. Upvaldesc *uv = p->upvalues;
  706. int i;
  707. LClosure *ncl = luaF_newLclosure(L, nup);
  708. ncl->p = p;
  709. setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */
  710. for (i = 0; i < nup; i++) { /* fill in its upvalues */
  711. if (uv[i].instack) /* upvalue refers to local variable? */
  712. ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
  713. else /* get upvalue from enclosing function */
  714. ncl->upvals[i] = encup[uv[i].idx];
  715. luaC_objbarrier(L, ncl, ncl->upvals[i]);
  716. }
  717. }
  718. /*
  719. ** finish execution of an opcode interrupted by a yield
  720. */
  721. void luaV_finishOp (lua_State *L) {
  722. CallInfo *ci = L->ci;
  723. StkId base = ci->func + 1;
  724. Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
  725. OpCode op = GET_OPCODE(inst);
  726. switch (op) { /* finish its execution */
  727. case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: {
  728. setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top);
  729. break;
  730. }
  731. case OP_UNM: case OP_BNOT: case OP_LEN:
  732. case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
  733. case OP_GETFIELD: case OP_SELF: {
  734. setobjs2s(L, base + GETARG_A(inst), --L->top);
  735. break;
  736. }
  737. case OP_LT: case OP_LE:
  738. case OP_LTI: case OP_LEI:
  739. case OP_GTI: case OP_GEI:
  740. case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */
  741. int res = !l_isfalse(s2v(L->top - 1));
  742. L->top--;
  743. #if defined(LUA_COMPAT_LT_LE)
  744. if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
  745. ci->callstatus ^= CIST_LEQ; /* clear mark */
  746. res = !res; /* negate result */
  747. }
  748. #endif
  749. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  750. if (res != GETARG_k(inst)) /* condition failed? */
  751. ci->u.l.savedpc++; /* skip jump instruction */
  752. break;
  753. }
  754. case OP_CONCAT: {
  755. StkId top = L->top - 1; /* top when 'luaT_tryconcatTM' was called */
  756. int a = GETARG_A(inst); /* first element to concatenate */
  757. int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */
  758. setobjs2s(L, top - 2, top); /* put TM result in proper position */
  759. L->top = top - 1; /* top is one after last element (at top-2) */
  760. luaV_concat(L, total); /* concat them (may yield again) */
  761. break;
  762. }
  763. default: {
  764. /* only these other opcodes can yield */
  765. lua_assert(op == OP_TFORCALL || op == OP_CALL ||
  766. op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE ||
  767. op == OP_SETI || op == OP_SETFIELD);
  768. break;
  769. }
  770. }
  771. }
  772. /*
  773. ** {==================================================================
  774. ** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute'
  775. ** ===================================================================
  776. */
  777. #define l_addi(L,a,b) intop(+, a, b)
  778. #define l_subi(L,a,b) intop(-, a, b)
  779. #define l_muli(L,a,b) intop(*, a, b)
  780. #define l_band(a,b) intop(&, a, b)
  781. #define l_bor(a,b) intop(|, a, b)
  782. #define l_bxor(a,b) intop(^, a, b)
  783. #define l_lti(a,b) (a < b)
  784. #define l_lei(a,b) (a <= b)
  785. #define l_gti(a,b) (a > b)
  786. #define l_gei(a,b) (a >= b)
  787. /*
  788. ** Arithmetic operations with immediate operands. 'iop' is the integer
  789. ** operation, 'fop' is the float operation.
  790. */
  791. #define op_arithI(L,iop,fop) { \
  792. TValue *v1 = vRB(i); \
  793. int imm = GETARG_sC(i); \
  794. if (ttisinteger(v1)) { \
  795. lua_Integer iv1 = ivalue(v1); \
  796. pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \
  797. } \
  798. else if (ttisfloat(v1)) { \
  799. lua_Number nb = fltvalue(v1); \
  800. lua_Number fimm = cast_num(imm); \
  801. pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \
  802. }}
  803. /*
  804. ** Auxiliary function for arithmetic operations over floats and others
  805. ** with two register operands.
  806. */
  807. #define op_arithf_aux(L,v1,v2,fop) { \
  808. lua_Number n1; lua_Number n2; \
  809. if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \
  810. pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \
  811. }}
  812. /*
  813. ** Arithmetic operations over floats and others with register operands.
  814. */
  815. #define op_arithf(L,fop) { \
  816. TValue *v1 = vRB(i); \
  817. TValue *v2 = vRC(i); \
  818. op_arithf_aux(L, v1, v2, fop); }
  819. /*
  820. ** Arithmetic operations with K operands for floats.
  821. */
  822. #define op_arithfK(L,fop) { \
  823. TValue *v1 = vRB(i); \
  824. TValue *v2 = KC(i); \
  825. op_arithf_aux(L, v1, v2, fop); }
  826. /*
  827. ** Arithmetic operations over integers and floats.
  828. */
  829. #define op_arith_aux(L,v1,v2,iop,fop) { \
  830. if (ttisinteger(v1) && ttisinteger(v2)) { \
  831. lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \
  832. pc++; setivalue(s2v(ra), iop(L, i1, i2)); \
  833. } \
  834. else op_arithf_aux(L, v1, v2, fop); }
  835. /*
  836. ** Arithmetic operations with register operands.
  837. */
  838. #define op_arith(L,iop,fop) { \
  839. TValue *v1 = vRB(i); \
  840. TValue *v2 = vRC(i); \
  841. op_arith_aux(L, v1, v2, iop, fop); }
  842. /*
  843. ** Arithmetic operations with K operands.
  844. */
  845. #define op_arithK(L,iop,fop) { \
  846. TValue *v1 = vRB(i); \
  847. TValue *v2 = KC(i); \
  848. op_arith_aux(L, v1, v2, iop, fop); }
  849. /*
  850. ** Bitwise operations with constant operand.
  851. */
  852. #define op_bitwiseK(L,op) { \
  853. TValue *v1 = vRB(i); \
  854. TValue *v2 = KC(i); \
  855. lua_Integer i1; \
  856. lua_Integer i2 = ivalue(v2); \
  857. if (tointegerns(v1, &i1)) { \
  858. pc++; setivalue(s2v(ra), op(i1, i2)); \
  859. }}
  860. /*
  861. ** Bitwise operations with register operands.
  862. */
  863. #define op_bitwise(L,op) { \
  864. TValue *v1 = vRB(i); \
  865. TValue *v2 = vRC(i); \
  866. lua_Integer i1; lua_Integer i2; \
  867. if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \
  868. pc++; setivalue(s2v(ra), op(i1, i2)); \
  869. }}
  870. /*
  871. ** Order operations with register operands. 'opn' actually works
  872. ** for all numbers, but the fast track improves performance for
  873. ** integers.
  874. */
  875. #define op_order(L,opi,opn,other) { \
  876. int cond; \
  877. TValue *rb = vRB(i); \
  878. if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \
  879. lua_Integer ia = ivalue(s2v(ra)); \
  880. lua_Integer ib = ivalue(rb); \
  881. cond = opi(ia, ib); \
  882. } \
  883. else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \
  884. cond = opn(s2v(ra), rb); \
  885. else \
  886. Protect(cond = other(L, s2v(ra), rb)); \
  887. docondjump(); }
  888. /*
  889. ** Order operations with immediate operand. (Immediate operand is
  890. ** always small enough to have an exact representation as a float.)
  891. */
  892. #define op_orderI(L,opi,opf,inv,tm) { \
  893. int cond; \
  894. int im = GETARG_sB(i); \
  895. if (ttisinteger(s2v(ra))) \
  896. cond = opi(ivalue(s2v(ra)), im); \
  897. else if (ttisfloat(s2v(ra))) { \
  898. lua_Number fa = fltvalue(s2v(ra)); \
  899. lua_Number fim = cast_num(im); \
  900. cond = opf(fa, fim); \
  901. } \
  902. else { \
  903. int isf = GETARG_C(i); \
  904. Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \
  905. } \
  906. docondjump(); }
  907. /* }================================================================== */
  908. /*
  909. ** {==================================================================
  910. ** Function 'luaV_execute': main interpreter loop
  911. ** ===================================================================
  912. */
  913. /*
  914. ** some macros for common tasks in 'luaV_execute'
  915. */
  916. #define RA(i) (base+GETARG_A(i))
  917. #define RB(i) (base+GETARG_B(i))
  918. #define vRB(i) s2v(RB(i))
  919. #define KB(i) (k+GETARG_B(i))
  920. #define RC(i) (base+GETARG_C(i))
  921. #define vRC(i) s2v(RC(i))
  922. #define KC(i) (k+GETARG_C(i))
  923. #define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
  924. #define updatetrap(ci) (trap = ci->u.l.trap)
  925. #define updatebase(ci) (base = ci->func + 1)
  926. #define updatestack(ci) { if (trap) { updatebase(ci); ra = RA(i); } }
  927. /*
  928. ** Execute a jump instruction. The 'updatetrap' allows signals to stop
  929. ** tight loops. (Without it, the local copy of 'trap' could never change.)
  930. */
  931. #define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); }
  932. /* for test instructions, execute the jump instruction that follows it */
  933. #define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); }
  934. /*
  935. ** do a conditional jump: skip next instruction if 'cond' is not what
  936. ** was expected (parameter 'k'), else do next instruction, which must
  937. ** be a jump.
  938. */
  939. #define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci);
  940. /*
  941. ** Correct global 'pc'.
  942. */
  943. #define savepc(L) (ci->u.l.savedpc = pc)
  944. /*
  945. ** Whenever code can raise errors, the global 'pc' and the global
  946. ** 'top' must be correct to report occasional errors.
  947. */
  948. #define savestate(L,ci) (savepc(L), L->top = ci->top)
  949. /*
  950. ** Protect code that, in general, can raise errors, reallocate the
  951. ** stack, and change the hooks.
  952. */
  953. #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci))
  954. /* special version that does not change the top */
  955. #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci))
  956. /*
  957. ** Protect code that can only raise errors. (That is, it cannnot change
  958. ** the stack or hooks.)
  959. */
  960. #define halfProtect(exp) (savestate(L,ci), (exp))
  961. /* 'c' is the limit of live values in the stack */
  962. #define checkGC(L,c) \
  963. { luaC_condGC(L, (savepc(L), L->top = (c)), \
  964. updatetrap(ci)); \
  965. luai_threadyield(L); }
  966. /* fetch an instruction and prepare its execution */
  967. #define vmfetch() { \
  968. if (trap) { /* stack reallocation or hooks? */ \
  969. trap = luaG_traceexec(L, pc); /* handle hooks */ \
  970. updatebase(ci); /* correct stack */ \
  971. } \
  972. i = *(pc++); \
  973. ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \
  974. }
  975. #define vmdispatch(o) switch(o)
  976. #define vmcase(l) case l:
  977. #define vmbreak break
  978. void luaV_execute (lua_State *L, CallInfo *ci) {
  979. LClosure *cl;
  980. TValue *k;
  981. StkId base;
  982. const Instruction *pc;
  983. int trap;
  984. #if LUA_USE_JUMPTABLE
  985. #include "ljumptab.h"
  986. #endif
  987. startfunc:
  988. trap = L->hookmask;
  989. returning: /* trap already set */
  990. cl = clLvalue(s2v(ci->func));
  991. k = cl->p->k;
  992. pc = ci->u.l.savedpc;
  993. if (trap) {
  994. if (pc == cl->p->code) { /* first instruction (not resuming)? */
  995. if (cl->p->is_vararg)
  996. trap = 0; /* hooks will start after VARARGPREP instruction */
  997. else /* check 'call' hook */
  998. luaD_hookcall(L, ci);
  999. }
  1000. ci->u.l.trap = 1; /* assume trap is on, for now */
  1001. }
  1002. base = ci->func + 1;
  1003. /* main loop of interpreter */
  1004. for (;;) {
  1005. Instruction i; /* instruction being executed */
  1006. StkId ra; /* instruction's A register */
  1007. vmfetch();
  1008. lua_assert(base == ci->func + 1);
  1009. lua_assert(base <= L->top && L->top < L->stack_last);
  1010. /* invalidate top for instructions not expecting it */
  1011. lua_assert(isIT(i) || (cast_void(L->top = base), 1));
  1012. vmdispatch (GET_OPCODE(i)) {
  1013. vmcase(OP_MOVE) {
  1014. setobjs2s(L, ra, RB(i));
  1015. vmbreak;
  1016. }
  1017. vmcase(OP_LOADI) {
  1018. lua_Integer b = GETARG_sBx(i);
  1019. setivalue(s2v(ra), b);
  1020. vmbreak;
  1021. }
  1022. vmcase(OP_LOADF) {
  1023. int b = GETARG_sBx(i);
  1024. setfltvalue(s2v(ra), cast_num(b));
  1025. vmbreak;
  1026. }
  1027. vmcase(OP_LOADK) {
  1028. TValue *rb = k + GETARG_Bx(i);
  1029. setobj2s(L, ra, rb);
  1030. vmbreak;
  1031. }
  1032. vmcase(OP_LOADKX) {
  1033. TValue *rb;
  1034. rb = k + GETARG_Ax(*pc); pc++;
  1035. setobj2s(L, ra, rb);
  1036. vmbreak;
  1037. }
  1038. vmcase(OP_LOADFALSE) {
  1039. setbfvalue(s2v(ra));
  1040. vmbreak;
  1041. }
  1042. vmcase(OP_LFALSESKIP) {
  1043. setbfvalue(s2v(ra));
  1044. pc++; /* skip next instruction */
  1045. vmbreak;
  1046. }
  1047. vmcase(OP_LOADTRUE) {
  1048. setbtvalue(s2v(ra));
  1049. vmbreak;
  1050. }
  1051. vmcase(OP_LOADNIL) {
  1052. int b = GETARG_B(i);
  1053. do {
  1054. setnilvalue(s2v(ra++));
  1055. } while (b--);
  1056. vmbreak;
  1057. }
  1058. vmcase(OP_GETUPVAL) {
  1059. int b = GETARG_B(i);
  1060. setobj2s(L, ra, cl->upvals[b]->v);
  1061. vmbreak;
  1062. }
  1063. vmcase(OP_SETUPVAL) {
  1064. UpVal *uv = cl->upvals[GETARG_B(i)];
  1065. setobj(L, uv->v, s2v(ra));
  1066. luaC_barrier(L, uv, s2v(ra));
  1067. vmbreak;
  1068. }
  1069. vmcase(OP_GETTABUP) {
  1070. const TValue *slot;
  1071. TValue *upval = cl->upvals[GETARG_B(i)]->v;
  1072. TValue *rc = KC(i);
  1073. TString *key = tsvalue(rc); /* key must be a string */
  1074. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  1075. setobj2s(L, ra, slot);
  1076. }
  1077. else
  1078. Protect(luaV_finishget(L, upval, rc, ra, slot));
  1079. vmbreak;
  1080. }
  1081. vmcase(OP_GETTABLE) {
  1082. const TValue *slot;
  1083. TValue *rb = vRB(i);
  1084. TValue *rc = vRC(i);
  1085. lua_Unsigned n;
  1086. if (ttisinteger(rc) /* fast track for integers? */
  1087. ? (cast_void(n = ivalue(rc)), luaV_fastgeti(L, rb, n, slot))
  1088. : luaV_fastget(L, rb, rc, slot, luaH_get)) {
  1089. setobj2s(L, ra, slot);
  1090. }
  1091. else
  1092. Protect(luaV_finishget(L, rb, rc, ra, slot));
  1093. vmbreak;
  1094. }
  1095. vmcase(OP_GETI) {
  1096. const TValue *slot;
  1097. TValue *rb = vRB(i);
  1098. int c = GETARG_C(i);
  1099. if (luaV_fastgeti(L, rb, c, slot)) {
  1100. setobj2s(L, ra, slot);
  1101. }
  1102. else {
  1103. TValue key;
  1104. setivalue(&key, c);
  1105. Protect(luaV_finishget(L, rb, &key, ra, slot));
  1106. }
  1107. vmbreak;
  1108. }
  1109. vmcase(OP_GETFIELD) {
  1110. const TValue *slot;
  1111. TValue *rb = vRB(i);
  1112. TValue *rc = KC(i);
  1113. TString *key = tsvalue(rc); /* key must be a string */
  1114. if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) {
  1115. setobj2s(L, ra, slot);
  1116. }
  1117. else
  1118. Protect(luaV_finishget(L, rb, rc, ra, slot));
  1119. vmbreak;
  1120. }
  1121. vmcase(OP_SETTABUP) {
  1122. const TValue *slot;
  1123. TValue *upval = cl->upvals[GETARG_A(i)]->v;
  1124. TValue *rb = KB(i);
  1125. TValue *rc = RKC(i);
  1126. TString *key = tsvalue(rb); /* key must be a string */
  1127. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  1128. luaV_finishfastset(L, upval, slot, rc);
  1129. }
  1130. else
  1131. Protect(luaV_finishset(L, upval, rb, rc, slot));
  1132. vmbreak;
  1133. }
  1134. vmcase(OP_SETTABLE) {
  1135. const TValue *slot;
  1136. TValue *rb = vRB(i); /* key (table is in 'ra') */
  1137. TValue *rc = RKC(i); /* value */
  1138. lua_Unsigned n;
  1139. if (ttisinteger(rb) /* fast track for integers? */
  1140. ? (cast_void(n = ivalue(rb)), luaV_fastgeti(L, s2v(ra), n, slot))
  1141. : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) {
  1142. luaV_finishfastset(L, s2v(ra), slot, rc);
  1143. }
  1144. else
  1145. Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
  1146. vmbreak;
  1147. }
  1148. vmcase(OP_SETI) {
  1149. const TValue *slot;
  1150. int c = GETARG_B(i);
  1151. TValue *rc = RKC(i);
  1152. if (luaV_fastgeti(L, s2v(ra), c, slot)) {
  1153. luaV_finishfastset(L, s2v(ra), slot, rc);
  1154. }
  1155. else {
  1156. TValue key;
  1157. setivalue(&key, c);
  1158. Protect(luaV_finishset(L, s2v(ra), &key, rc, slot));
  1159. }
  1160. vmbreak;
  1161. }
  1162. vmcase(OP_SETFIELD) {
  1163. const TValue *slot;
  1164. TValue *rb = KB(i);
  1165. TValue *rc = RKC(i);
  1166. TString *key = tsvalue(rb); /* key must be a string */
  1167. if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) {
  1168. luaV_finishfastset(L, s2v(ra), slot, rc);
  1169. }
  1170. else
  1171. Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
  1172. vmbreak;
  1173. }
  1174. vmcase(OP_NEWTABLE) {
  1175. int b = GETARG_B(i); /* log2(hash size) + 1 */
  1176. int c = GETARG_C(i); /* array size */
  1177. Table *t;
  1178. if (b > 0)
  1179. b = 1 << (b - 1); /* size is 2^(b - 1) */
  1180. lua_assert((!TESTARG_k(i)) == (GETARG_Ax(*pc) == 0));
  1181. if (TESTARG_k(i)) /* non-zero extra argument? */
  1182. c += GETARG_Ax(*pc) * (MAXARG_C + 1); /* add it to size */
  1183. pc++; /* skip extra argument */
  1184. L->top = ra + 1; /* correct top in case of emergency GC */
  1185. t = luaH_new(L); /* memory allocation */
  1186. sethvalue2s(L, ra, t);
  1187. if (b != 0 || c != 0)
  1188. luaH_resize(L, t, c, b); /* idem */
  1189. checkGC(L, ra + 1);
  1190. vmbreak;
  1191. }
  1192. vmcase(OP_SELF) {
  1193. const TValue *slot;
  1194. TValue *rb = vRB(i);
  1195. TValue *rc = RKC(i);
  1196. TString *key = tsvalue(rc); /* key must be a string */
  1197. setobj2s(L, ra + 1, rb);
  1198. if (luaV_fastget(L, rb, key, slot, luaH_getstr)) {
  1199. setobj2s(L, ra, slot);
  1200. }
  1201. else
  1202. Protect(luaV_finishget(L, rb, rc, ra, slot));
  1203. vmbreak;
  1204. }
  1205. vmcase(OP_ADDI) {
  1206. op_arithI(L, l_addi, luai_numadd);
  1207. vmbreak;
  1208. }
  1209. vmcase(OP_ADDK) {
  1210. op_arithK(L, l_addi, luai_numadd);
  1211. vmbreak;
  1212. }
  1213. vmcase(OP_SUBK) {
  1214. op_arithK(L, l_subi, luai_numsub);
  1215. vmbreak;
  1216. }
  1217. vmcase(OP_MULK) {
  1218. op_arithK(L, l_muli, luai_nummul);
  1219. vmbreak;
  1220. }
  1221. vmcase(OP_MODK) {
  1222. op_arithK(L, luaV_mod, luaV_modf);
  1223. vmbreak;
  1224. }
  1225. vmcase(OP_POWK) {
  1226. op_arithfK(L, luai_numpow);
  1227. vmbreak;
  1228. }
  1229. vmcase(OP_DIVK) {
  1230. op_arithfK(L, luai_numdiv);
  1231. vmbreak;
  1232. }
  1233. vmcase(OP_IDIVK) {
  1234. op_arithK(L, luaV_idiv, luai_numidiv);
  1235. vmbreak;
  1236. }
  1237. vmcase(OP_BANDK) {
  1238. op_bitwiseK(L, l_band);
  1239. vmbreak;
  1240. }
  1241. vmcase(OP_BORK) {
  1242. op_bitwiseK(L, l_bor);
  1243. vmbreak;
  1244. }
  1245. vmcase(OP_BXORK) {
  1246. op_bitwiseK(L, l_bxor);
  1247. vmbreak;
  1248. }
  1249. vmcase(OP_SHRI) {
  1250. TValue *rb = vRB(i);
  1251. int ic = GETARG_sC(i);
  1252. lua_Integer ib;
  1253. if (tointegerns(rb, &ib)) {
  1254. pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic));
  1255. }
  1256. vmbreak;
  1257. }
  1258. vmcase(OP_SHLI) {
  1259. TValue *rb = vRB(i);
  1260. int ic = GETARG_sC(i);
  1261. lua_Integer ib;
  1262. if (tointegerns(rb, &ib)) {
  1263. pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib));
  1264. }
  1265. vmbreak;
  1266. }
  1267. vmcase(OP_ADD) {
  1268. op_arith(L, l_addi, luai_numadd);
  1269. vmbreak;
  1270. }
  1271. vmcase(OP_SUB) {
  1272. op_arith(L, l_subi, luai_numsub);
  1273. vmbreak;
  1274. }
  1275. vmcase(OP_MUL) {
  1276. op_arith(L, l_muli, luai_nummul);
  1277. vmbreak;
  1278. }
  1279. vmcase(OP_MOD) {
  1280. op_arith(L, luaV_mod, luaV_modf);
  1281. vmbreak;
  1282. }
  1283. vmcase(OP_POW) {
  1284. op_arithf(L, luai_numpow);
  1285. vmbreak;
  1286. }
  1287. vmcase(OP_DIV) { /* float division (always with floats) */
  1288. op_arithf(L, luai_numdiv);
  1289. vmbreak;
  1290. }
  1291. vmcase(OP_IDIV) { /* floor division */
  1292. op_arith(L, luaV_idiv, luai_numidiv);
  1293. vmbreak;
  1294. }
  1295. vmcase(OP_BAND) {
  1296. op_bitwise(L, l_band);
  1297. vmbreak;
  1298. }
  1299. vmcase(OP_BOR) {
  1300. op_bitwise(L, l_bor);
  1301. vmbreak;
  1302. }
  1303. vmcase(OP_BXOR) {
  1304. op_bitwise(L, l_bxor);
  1305. vmbreak;
  1306. }
  1307. vmcase(OP_SHR) {
  1308. op_bitwise(L, luaV_shiftr);
  1309. vmbreak;
  1310. }
  1311. vmcase(OP_SHL) {
  1312. op_bitwise(L, luaV_shiftl);
  1313. vmbreak;
  1314. }
  1315. vmcase(OP_MMBIN) {
  1316. Instruction pi = *(pc - 2); /* original arith. expression */
  1317. TValue *rb = vRB(i);
  1318. TMS tm = (TMS)GETARG_C(i);
  1319. StkId result = RA(pi);
  1320. lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR);
  1321. Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm));
  1322. vmbreak;
  1323. }
  1324. vmcase(OP_MMBINI) {
  1325. Instruction pi = *(pc - 2); /* original arith. expression */
  1326. int imm = GETARG_sB(i);
  1327. TMS tm = (TMS)GETARG_C(i);
  1328. int flip = GETARG_k(i);
  1329. StkId result = RA(pi);
  1330. Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm));
  1331. vmbreak;
  1332. }
  1333. vmcase(OP_MMBINK) {
  1334. Instruction pi = *(pc - 2); /* original arith. expression */
  1335. TValue *imm = KB(i);
  1336. TMS tm = (TMS)GETARG_C(i);
  1337. int flip = GETARG_k(i);
  1338. StkId result = RA(pi);
  1339. Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm));
  1340. vmbreak;
  1341. }
  1342. vmcase(OP_UNM) {
  1343. TValue *rb = vRB(i);
  1344. lua_Number nb;
  1345. if (ttisinteger(rb)) {
  1346. lua_Integer ib = ivalue(rb);
  1347. setivalue(s2v(ra), intop(-, 0, ib));
  1348. }
  1349. else if (tonumberns(rb, nb)) {
  1350. setfltvalue(s2v(ra), luai_numunm(L, nb));
  1351. }
  1352. else
  1353. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  1354. vmbreak;
  1355. }
  1356. vmcase(OP_BNOT) {
  1357. TValue *rb = vRB(i);
  1358. lua_Integer ib;
  1359. if (tointegerns(rb, &ib)) {
  1360. setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
  1361. }
  1362. else
  1363. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  1364. vmbreak;
  1365. }
  1366. vmcase(OP_NOT) {
  1367. TValue *rb = vRB(i);
  1368. if (l_isfalse(rb))
  1369. setbtvalue(s2v(ra));
  1370. else
  1371. setbfvalue(s2v(ra));
  1372. vmbreak;
  1373. }
  1374. vmcase(OP_LEN) {
  1375. Protect(luaV_objlen(L, ra, vRB(i)));
  1376. vmbreak;
  1377. }
  1378. vmcase(OP_CONCAT) {
  1379. int n = GETARG_B(i); /* number of elements to concatenate */
  1380. L->top = ra + n; /* mark the end of concat operands */
  1381. ProtectNT(luaV_concat(L, n));
  1382. checkGC(L, L->top); /* 'luaV_concat' ensures correct top */
  1383. vmbreak;
  1384. }
  1385. vmcase(OP_CLOSE) {
  1386. Protect(luaF_close(L, ra, LUA_OK));
  1387. vmbreak;
  1388. }
  1389. vmcase(OP_TBC) {
  1390. /* create new to-be-closed upvalue */
  1391. halfProtect(luaF_newtbcupval(L, ra));
  1392. vmbreak;
  1393. }
  1394. vmcase(OP_JMP) {
  1395. dojump(ci, i, 0);
  1396. vmbreak;
  1397. }
  1398. vmcase(OP_EQ) {
  1399. int cond;
  1400. TValue *rb = vRB(i);
  1401. Protect(cond = luaV_equalobj(L, s2v(ra), rb));
  1402. docondjump();
  1403. vmbreak;
  1404. }
  1405. vmcase(OP_LT) {
  1406. op_order(L, l_lti, LTnum, lessthanothers);
  1407. vmbreak;
  1408. }
  1409. vmcase(OP_LE) {
  1410. op_order(L, l_lei, LEnum, lessequalothers);
  1411. vmbreak;
  1412. }
  1413. vmcase(OP_EQK) {
  1414. TValue *rb = KB(i);
  1415. /* basic types do not use '__eq'; we can use raw equality */
  1416. int cond = luaV_rawequalobj(s2v(ra), rb);
  1417. docondjump();
  1418. vmbreak;
  1419. }
  1420. vmcase(OP_EQI) {
  1421. int cond;
  1422. int im = GETARG_sB(i);
  1423. if (ttisinteger(s2v(ra)))
  1424. cond = (ivalue(s2v(ra)) == im);
  1425. else if (ttisfloat(s2v(ra)))
  1426. cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im));
  1427. else
  1428. cond = 0; /* other types cannot be equal to a number */
  1429. docondjump();
  1430. vmbreak;
  1431. }
  1432. vmcase(OP_LTI) {
  1433. op_orderI(L, l_lti, luai_numlt, 0, TM_LT);
  1434. vmbreak;
  1435. }
  1436. vmcase(OP_LEI) {
  1437. op_orderI(L, l_lei, luai_numle, 0, TM_LE);
  1438. vmbreak;
  1439. }
  1440. vmcase(OP_GTI) {
  1441. op_orderI(L, l_gti, luai_numgt, 1, TM_LT);
  1442. vmbreak;
  1443. }
  1444. vmcase(OP_GEI) {
  1445. op_orderI(L, l_gei, luai_numge, 1, TM_LE);
  1446. vmbreak;
  1447. }
  1448. vmcase(OP_TEST) {
  1449. int cond = !l_isfalse(s2v(ra));
  1450. docondjump();
  1451. vmbreak;
  1452. }
  1453. vmcase(OP_TESTSET) {
  1454. TValue *rb = vRB(i);
  1455. if (l_isfalse(rb) == GETARG_k(i))
  1456. pc++;
  1457. else {
  1458. setobj2s(L, ra, rb);
  1459. donextjump(ci);
  1460. }
  1461. vmbreak;
  1462. }
  1463. vmcase(OP_CALL) {
  1464. CallInfo *newci;
  1465. int b = GETARG_B(i);
  1466. int nresults = GETARG_C(i) - 1;
  1467. if (b != 0) /* fixed number of arguments? */
  1468. L->top = ra + b; /* top signals number of arguments */
  1469. /* else previous instruction set top */
  1470. savepc(L); /* in case of errors */
  1471. if ((newci = luaD_precall(L, ra, nresults)) == NULL)
  1472. updatetrap(ci); /* C call; nothing else to be done */
  1473. else { /* Lua call: run function in this same C frame */
  1474. ci = newci;
  1475. ci->callstatus = 0; /* call re-uses 'luaV_execute' */
  1476. goto startfunc;
  1477. }
  1478. vmbreak;
  1479. }
  1480. vmcase(OP_TAILCALL) {
  1481. int b = GETARG_B(i); /* number of arguments + 1 (function) */
  1482. int nparams1 = GETARG_C(i);
  1483. /* delta is virtual 'func' - real 'func' (vararg functions) */
  1484. int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
  1485. if (b != 0)
  1486. L->top = ra + b;
  1487. else /* previous instruction set top */
  1488. b = cast_int(L->top - ra);
  1489. savepc(ci); /* several calls here can raise errors */
  1490. if (TESTARG_k(i)) {
  1491. /* close upvalues from current call; the compiler ensures
  1492. that there are no to-be-closed variables here, so this
  1493. call cannot change the stack */
  1494. luaF_close(L, base, NOCLOSINGMETH);
  1495. lua_assert(base == ci->func + 1);
  1496. }
  1497. while (!ttisfunction(s2v(ra))) { /* not a function? */
  1498. luaD_tryfuncTM(L, ra); /* try '__call' metamethod */
  1499. b++; /* there is now one extra argument */
  1500. checkstackGCp(L, 1, ra);
  1501. }
  1502. if (!ttisLclosure(s2v(ra))) { /* C function? */
  1503. luaD_precall(L, ra, LUA_MULTRET); /* call it */
  1504. updatetrap(ci);
  1505. updatestack(ci); /* stack may have been relocated */
  1506. ci->func -= delta; /* restore 'func' (if vararg) */
  1507. luaD_poscall(L, ci, cast_int(L->top - ra)); /* finish caller */
  1508. updatetrap(ci); /* 'luaD_poscall' can change hooks */
  1509. goto ret; /* caller returns after the tail call */
  1510. }
  1511. ci->func -= delta; /* restore 'func' (if vararg) */
  1512. luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
  1513. goto startfunc; /* execute the callee */
  1514. }
  1515. vmcase(OP_RETURN) {
  1516. int n = GETARG_B(i) - 1; /* number of results */
  1517. int nparams1 = GETARG_C(i);
  1518. if (n < 0) /* not fixed? */
  1519. n = cast_int(L->top - ra); /* get what is available */
  1520. savepc(ci);
  1521. if (TESTARG_k(i)) { /* may there be open upvalues? */
  1522. if (L->top < ci->top)
  1523. L->top = ci->top;
  1524. luaF_close(L, base, LUA_OK);
  1525. updatetrap(ci);
  1526. updatestack(ci);
  1527. }
  1528. if (nparams1) /* vararg function? */
  1529. ci->func -= ci->u.l.nextraargs + nparams1;
  1530. L->top = ra + n; /* set call for 'luaD_poscall' */
  1531. luaD_poscall(L, ci, n);
  1532. updatetrap(ci); /* 'luaD_poscall' can change hooks */
  1533. goto ret;
  1534. }
  1535. vmcase(OP_RETURN0) {
  1536. if (L->hookmask) {
  1537. L->top = ra;
  1538. savepc(ci);
  1539. luaD_poscall(L, ci, 0); /* no hurry... */
  1540. trap = 1;
  1541. }
  1542. else { /* do the 'poscall' here */
  1543. int nres = ci->nresults;
  1544. L->ci = ci->previous; /* back to caller */
  1545. L->top = base - 1;
  1546. while (nres-- > 0)
  1547. setnilvalue(s2v(L->top++)); /* all results are nil */
  1548. }
  1549. goto ret;
  1550. }
  1551. vmcase(OP_RETURN1) {
  1552. if (L->hookmask) {
  1553. L->top = ra + 1;
  1554. savepc(ci);
  1555. luaD_poscall(L, ci, 1); /* no hurry... */
  1556. trap = 1;
  1557. }
  1558. else { /* do the 'poscall' here */
  1559. int nres = ci->nresults;
  1560. L->ci = ci->previous; /* back to caller */
  1561. if (nres == 0)
  1562. L->top = base - 1; /* asked for no results */
  1563. else {
  1564. setobjs2s(L, base - 1, ra); /* at least this result */
  1565. L->top = base;
  1566. while (--nres > 0) /* complete missing results */
  1567. setnilvalue(s2v(L->top++));
  1568. }
  1569. }
  1570. ret: /* return from a Lua function */
  1571. if (ci->callstatus & CIST_FRESH)
  1572. return; /* end this frame */
  1573. else {
  1574. ci = ci->previous;
  1575. goto returning; /* continue running caller in this frame */
  1576. }
  1577. }
  1578. vmcase(OP_FORLOOP) {
  1579. if (ttisinteger(s2v(ra + 2))) { /* integer loop? */
  1580. lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1)));
  1581. if (count > 0) { /* still more iterations? */
  1582. lua_Integer step = ivalue(s2v(ra + 2));
  1583. lua_Integer idx = ivalue(s2v(ra)); /* internal index */
  1584. chgivalue(s2v(ra + 1), count - 1); /* update counter */
  1585. idx = intop(+, idx, step); /* add step to index */
  1586. chgivalue(s2v(ra), idx); /* update internal index */
  1587. setivalue(s2v(ra + 3), idx); /* and control variable */
  1588. pc -= GETARG_Bx(i); /* jump back */
  1589. }
  1590. }
  1591. else if (floatforloop(ra)) /* float loop */
  1592. pc -= GETARG_Bx(i); /* jump back */
  1593. updatetrap(ci); /* allows a signal to break the loop */
  1594. vmbreak;
  1595. }
  1596. vmcase(OP_FORPREP) {
  1597. savestate(L, ci); /* in case of errors */
  1598. if (forprep(L, ra))
  1599. pc += GETARG_Bx(i) + 1; /* skip the loop */
  1600. vmbreak;
  1601. }
  1602. vmcase(OP_TFORPREP) {
  1603. /* create to-be-closed upvalue (if needed) */
  1604. halfProtect(luaF_newtbcupval(L, ra + 3));
  1605. pc += GETARG_Bx(i);
  1606. i = *(pc++); /* go to next instruction */
  1607. lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i));
  1608. goto l_tforcall;
  1609. }
  1610. vmcase(OP_TFORCALL) {
  1611. l_tforcall:
  1612. /* 'ra' has the iterator function, 'ra + 1' has the state,
  1613. 'ra + 2' has the control variable, and 'ra + 3' has the
  1614. to-be-closed variable. The call will use the stack after
  1615. these values (starting at 'ra + 4')
  1616. */
  1617. /* push function, state, and control variable */
  1618. memcpy(ra + 4, ra, 3 * sizeof(*ra));
  1619. L->top = ra + 4 + 3;
  1620. ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */
  1621. updatestack(ci); /* stack may have changed */
  1622. i = *(pc++); /* go to next instruction */
  1623. lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
  1624. goto l_tforloop;
  1625. }
  1626. vmcase(OP_TFORLOOP) {
  1627. l_tforloop:
  1628. if (!ttisnil(s2v(ra + 4))) { /* continue loop? */
  1629. setobjs2s(L, ra + 2, ra + 4); /* save control variable */
  1630. pc -= GETARG_Bx(i); /* jump back */
  1631. }
  1632. vmbreak;
  1633. }
  1634. vmcase(OP_SETLIST) {
  1635. int n = GETARG_B(i);
  1636. unsigned int last = GETARG_C(i);
  1637. Table *h = hvalue(s2v(ra));
  1638. if (n == 0)
  1639. n = cast_int(L->top - ra) - 1; /* get up to the top */
  1640. else
  1641. L->top = ci->top; /* correct top in case of emergency GC */
  1642. last += n;
  1643. if (TESTARG_k(i)) {
  1644. last += GETARG_Ax(*pc) * (MAXARG_C + 1);
  1645. pc++;
  1646. }
  1647. if (last > luaH_realasize(h)) /* needs more space? */
  1648. luaH_resizearray(L, h, last); /* preallocate it at once */
  1649. for (; n > 0; n--) {
  1650. TValue *val = s2v(ra + n);
  1651. setobj2t(L, &h->array[last - 1], val);
  1652. last--;
  1653. luaC_barrierback(L, obj2gco(h), val);
  1654. }
  1655. vmbreak;
  1656. }
  1657. vmcase(OP_CLOSURE) {
  1658. Proto *p = cl->p->p[GETARG_Bx(i)];
  1659. halfProtect(pushclosure(L, p, cl->upvals, base, ra));
  1660. checkGC(L, ra + 1);
  1661. vmbreak;
  1662. }
  1663. vmcase(OP_VARARG) {
  1664. int n = GETARG_C(i) - 1; /* required results */
  1665. Protect(luaT_getvarargs(L, ci, ra, n));
  1666. vmbreak;
  1667. }
  1668. vmcase(OP_VARARGPREP) {
  1669. ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p));
  1670. if (trap) {
  1671. luaD_hookcall(L, ci);
  1672. L->oldpc = 1; /* next opcode will be seen as a "new" line */
  1673. }
  1674. updatebase(ci); /* function has new base after adjustment */
  1675. vmbreak;
  1676. }
  1677. vmcase(OP_EXTRAARG) {
  1678. lua_assert(0);
  1679. vmbreak;
  1680. }
  1681. }
  1682. }
  1683. }
  1684. /* }================================================================== */