lvm.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*
  2. ** $Id: lvm.c,v 2.227 2014/11/02 19:19:04 roberto Exp roberto $
  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 <limits.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "lua.h"
  14. #include "ldebug.h"
  15. #include "ldo.h"
  16. #include "lfunc.h"
  17. #include "lgc.h"
  18. #include "lobject.h"
  19. #include "lopcodes.h"
  20. #include "lstate.h"
  21. #include "lstring.h"
  22. #include "ltable.h"
  23. #include "ltm.h"
  24. #include "lvm.h"
  25. /*
  26. ** You can define LUA_FLOORN2I if you want to convert floats to integers
  27. ** by flooring them (instead of raising an error if they are not
  28. ** integral values)
  29. */
  30. #if !defined(LUA_FLOORN2I)
  31. #define LUA_FLOORN2I 0
  32. #endif
  33. /* limit for table tag-method chains (to avoid loops) */
  34. #define MAXTAGLOOP 2000
  35. /*
  36. ** Similar to 'tonumber', but does not attempt to convert strings and
  37. ** ensure correct precision (no extra bits). Used in comparisons.
  38. */
  39. static int tofloat (const TValue *obj, lua_Number *n) {
  40. if (ttisfloat(obj)) *n = fltvalue(obj);
  41. else if (ttisinteger(obj)) {
  42. volatile lua_Number x = cast_num(ivalue(obj)); /* avoid extra precision */
  43. *n = x;
  44. }
  45. else {
  46. *n = 0; /* to avoid warnings */
  47. return 0;
  48. }
  49. return 1;
  50. }
  51. /*
  52. ** Try to convert a value to a float. Check 'isinteger' first, because
  53. ** in general the float case is already handled by the macro 'tonumber'.
  54. */
  55. int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
  56. TValue v;
  57. again:
  58. if (ttisinteger(obj)) {
  59. *n = cast_num(ivalue(obj));
  60. return 1;
  61. }
  62. else if (ttisfloat(obj)) {
  63. *n = fltvalue(obj);
  64. return 1;
  65. }
  66. else if (cvt2num(obj) && /* string convertible to number? */
  67. luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
  68. obj = &v;
  69. goto again; /* convert result from 'luaO_str2num' to a float */
  70. }
  71. return 0; /* conversion failed */
  72. }
  73. /*
  74. ** try to convert a value to an integer, rounding according to 'mode':
  75. ** mode == 0: accepts only integral values
  76. ** mode == 1: takes the floor of the number
  77. ** mode == 2: takes the ceil of the number
  78. */
  79. static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) {
  80. TValue v;
  81. again:
  82. if (ttisfloat(obj)) {
  83. lua_Number n = fltvalue(obj);
  84. lua_Number f = l_floor(n);
  85. if (n != f) { /* not an integral value? */
  86. if (mode == 0) return 0; /* fails if mode demands integral value */
  87. else if (mode > 1) /* needs ceil? */
  88. f += 1; /* convert floor to ceil (remember: n != f) */
  89. }
  90. return lua_numbertointeger(f, p);
  91. }
  92. else if (ttisinteger(obj)) {
  93. *p = ivalue(obj);
  94. return 1;
  95. }
  96. else if (cvt2num(obj) &&
  97. luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
  98. obj = &v;
  99. goto again; /* convert result from 'luaO_str2num' to an integer */
  100. }
  101. return 0; /* conversion failed */
  102. }
  103. /*
  104. ** try to convert a value to an integer
  105. */
  106. int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
  107. return tointeger_aux(obj, p, LUA_FLOORN2I);
  108. }
  109. /*
  110. ** Try to convert a 'for' limit to an integer, preserving the
  111. ** semantics of the loop.
  112. ** (The following explanation assumes a non-negative step; it is valid
  113. ** for negative steps mutatis mutandis.)
  114. ** If the limit can be converted to an integer, rounding down, that is
  115. ** it.
  116. ** Otherwise, check whether the limit can be converted to a number. If
  117. ** the number is too large, it is OK to set the limit as LUA_MAXINTEGER,
  118. ** which means no limit. If the number is too negative, the loop
  119. ** should not run, because any initial integer value is larger than the
  120. ** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects
  121. ** the extreme case when the initial value is LUA_MININTEGER, in which
  122. ** case the LUA_MININTEGER limit would still run the loop once.
  123. */
  124. static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
  125. int *stopnow) {
  126. *stopnow = 0; /* usually, let loops run */
  127. if (!tointeger_aux(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */
  128. lua_Number n; /* try to convert to float */
  129. if (!tonumber(obj, &n)) /* cannot convert to float? */
  130. return 0; /* not a number */
  131. if (n > 0) { /* if true, float is larger than max integer */
  132. *p = LUA_MAXINTEGER;
  133. if (step < 0) *stopnow = 1;
  134. }
  135. else { /* float is smaller than min integer */
  136. *p = LUA_MININTEGER;
  137. if (step >= 0) *stopnow = 1;
  138. }
  139. }
  140. return 1;
  141. }
  142. /*
  143. ** Main function for table access (invoking metamethods if needed).
  144. ** Compute 'val = t[key]'
  145. */
  146. void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  147. int loop; /* counter to avoid infinite loops */
  148. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  149. const TValue *tm;
  150. if (ttistable(t)) { /* 't' is a table? */
  151. Table *h = hvalue(t);
  152. const TValue *res = luaH_get(h, key); /* do a primitive get */
  153. if (!ttisnil(res) || /* result is not nil? */
  154. (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
  155. setobj2s(L, val, res); /* result is the raw get */
  156. return;
  157. }
  158. /* else will try metamethod */
  159. }
  160. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
  161. luaG_typeerror(L, t, "index"); /* no metamethod */
  162. if (ttisfunction(tm)) { /* metamethod is a function */
  163. luaT_callTM(L, tm, t, key, val, 1);
  164. return;
  165. }
  166. t = tm; /* else repeat access over 'tm' */
  167. }
  168. luaG_runerror(L, "gettable chain too long; possible loop");
  169. }
  170. /*
  171. ** Main function for table assignment (invoking metamethods if needed).
  172. ** Compute 't[key] = val'
  173. */
  174. void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  175. int loop; /* counter to avoid infinite loops */
  176. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  177. const TValue *tm;
  178. if (ttistable(t)) { /* 't' is a table? */
  179. Table *h = hvalue(t);
  180. TValue *oldval = cast(TValue *, luaH_get(h, key));
  181. /* if previous value is not nil, there must be a previous entry
  182. in the table; a metamethod has no relevance */
  183. if (!ttisnil(oldval) ||
  184. /* previous value is nil; must check the metamethod */
  185. ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
  186. /* no metamethod; is there a previous entry in the table? */
  187. (oldval != luaO_nilobject ||
  188. /* no previous entry; must create one. (The next test is
  189. always true; we only need the assignment.) */
  190. (oldval = luaH_newkey(L, h, key), 1)))) {
  191. /* no metamethod and (now) there is an entry with given key */
  192. setobj2t(L, oldval, val); /* assign new value to that entry */
  193. invalidateTMcache(h);
  194. luaC_barrierback(L, h, val);
  195. return;
  196. }
  197. /* else will try the metamethod */
  198. }
  199. else /* not a table; check metamethod */
  200. if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
  201. luaG_typeerror(L, t, "index");
  202. /* try the metamethod */
  203. if (ttisfunction(tm)) {
  204. luaT_callTM(L, tm, t, key, val, 0);
  205. return;
  206. }
  207. t = tm; /* else repeat assignment over 'tm' */
  208. }
  209. luaG_runerror(L, "settable chain too long; possible loop");
  210. }
  211. /*
  212. ** Compare two strings 'ls' x 'rs', returning an integer smaller-equal-
  213. ** -larger than zero if 'ls' is smaller-equal-larger than 'rs'.
  214. ** The code is a little tricky because it allows '\0' in the strings
  215. ** and it uses 'strcoll' (to respect locales) for each segments
  216. ** of the strings.
  217. */
  218. static int l_strcmp (const TString *ls, const TString *rs) {
  219. const char *l = getstr(ls);
  220. size_t ll = ls->len;
  221. const char *r = getstr(rs);
  222. size_t lr = rs->len;
  223. for (;;) { /* for each segment */
  224. int temp = strcoll(l, r);
  225. if (temp != 0) /* not equal? */
  226. return temp; /* done */
  227. else { /* strings are equal up to a '\0' */
  228. size_t len = strlen(l); /* index of first '\0' in both strings */
  229. if (len == lr) /* 'rs' is finished? */
  230. return (len == ll) ? 0 : 1; /* check 'ls' */
  231. else if (len == ll) /* 'ls' is finished? */
  232. return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */
  233. /* both strings longer than 'len'; go on comparing after the '\0' */
  234. len++;
  235. l += len; ll -= len; r += len; lr -= len;
  236. }
  237. }
  238. }
  239. /*
  240. ** Main operation less than; return 'l < r'.
  241. */
  242. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  243. int res;
  244. lua_Number nl, nr;
  245. if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */
  246. return (ivalue(l) < ivalue(r));
  247. else if (tofloat(l, &nl) && tofloat(r, &nr)) /* both are numbers? */
  248. return luai_numlt(nl, nr);
  249. else if (ttisstring(l) && ttisstring(r)) /* both are strings? */
  250. return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
  251. else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */
  252. luaG_ordererror(L, l, r); /* error */
  253. return res;
  254. }
  255. /*
  256. ** Main operation less than or equal to; return 'l <= r'.
  257. */
  258. int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
  259. int res;
  260. lua_Number nl, nr;
  261. if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */
  262. return (ivalue(l) <= ivalue(r));
  263. else if (tofloat(l, &nl) && tofloat(r, &nr)) /* both are numbers? */
  264. return luai_numle(nl, nr);
  265. else if (ttisstring(l) && ttisstring(r)) /* both are strings? */
  266. return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
  267. else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* first try 'le' */
  268. return res;
  269. else if ((res = luaT_callorderTM(L, r, l, TM_LT)) < 0) /* else try 'lt' */
  270. luaG_ordererror(L, l, r);
  271. return !res;
  272. }
  273. /*
  274. ** Main operation for equality of Lua values; return 't1 == t2'.
  275. ** L == NULL means raw equality (no metamethods)
  276. */
  277. int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
  278. const TValue *tm;
  279. if (ttype(t1) != ttype(t2)) { /* not the same variant? */
  280. if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER)
  281. return 0; /* only numbers can be equal with different variants */
  282. else { /* two numbers with different variants */
  283. lua_Number n1, n2; /* compare them as floats */
  284. lua_assert(ttisnumber(t1) && ttisnumber(t2));
  285. cast_void(tofloat(t1, &n1)); cast_void(tofloat(t2, &n2));
  286. return luai_numeq(n1, n2);
  287. }
  288. }
  289. /* values have same type and same variant */
  290. switch (ttype(t1)) {
  291. case LUA_TNIL: return 1;
  292. case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2));
  293. case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
  294. case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  295. case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  296. case LUA_TLCF: return fvalue(t1) == fvalue(t2);
  297. case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2));
  298. case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2));
  299. case LUA_TUSERDATA: {
  300. if (uvalue(t1) == uvalue(t2)) return 1;
  301. else if (L == NULL) return 0;
  302. tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
  303. if (tm == NULL)
  304. tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
  305. break; /* will try TM */
  306. }
  307. case LUA_TTABLE: {
  308. if (hvalue(t1) == hvalue(t2)) return 1;
  309. else if (L == NULL) return 0;
  310. tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
  311. if (tm == NULL)
  312. tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
  313. break; /* will try TM */
  314. }
  315. default:
  316. return gcvalue(t1) == gcvalue(t2);
  317. }
  318. if (tm == NULL) /* no TM? */
  319. return 0; /* objects are different */
  320. luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */
  321. return !l_isfalse(L->top);
  322. }
  323. /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
  324. #define tostring(L,o) \
  325. (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
  326. /*
  327. ** Main operation for concatenation: concat 'total' values in the stack,
  328. ** from 'L->top - total' up to 'L->top - 1'.
  329. */
  330. void luaV_concat (lua_State *L, int total) {
  331. lua_assert(total >= 2);
  332. do {
  333. StkId top = L->top;
  334. int n = 2; /* number of elements handled in this pass (at least 2) */
  335. if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1))
  336. luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT);
  337. else if (tsvalue(top-1)->len == 0) /* second operand is empty? */
  338. cast_void(tostring(L, top - 2)); /* result is first operand */
  339. else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) {
  340. setobjs2s(L, top - 2, top - 1); /* result is second op. */
  341. }
  342. else {
  343. /* at least two non-empty string values; get as many as possible */
  344. size_t tl = tsvalue(top-1)->len;
  345. char *buffer;
  346. int i;
  347. /* collect total length */
  348. for (i = 1; i < total && tostring(L, top-i-1); i++) {
  349. size_t l = tsvalue(top-i-1)->len;
  350. if (l >= (MAX_SIZE/sizeof(char)) - tl)
  351. luaG_runerror(L, "string length overflow");
  352. tl += l;
  353. }
  354. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  355. tl = 0;
  356. n = i;
  357. do { /* copy all strings to buffer */
  358. size_t l = tsvalue(top-i)->len;
  359. memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
  360. tl += l;
  361. } while (--i > 0);
  362. setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); /* create result */
  363. }
  364. total -= n-1; /* got 'n' strings to create 1 new */
  365. L->top -= n-1; /* popped 'n' strings and pushed one */
  366. } while (total > 1); /* repeat until only 1 result left */
  367. }
  368. /*
  369. ** Main operation 'ra' = #rb'.
  370. */
  371. void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
  372. const TValue *tm;
  373. switch (ttnov(rb)) {
  374. case LUA_TTABLE: {
  375. Table *h = hvalue(rb);
  376. tm = fasttm(L, h->metatable, TM_LEN);
  377. if (tm) break; /* metamethod? break switch to call it */
  378. setivalue(ra, luaH_getn(h)); /* else primitive len */
  379. return;
  380. }
  381. case LUA_TSTRING: {
  382. setivalue(ra, tsvalue(rb)->len);
  383. return;
  384. }
  385. default: { /* try metamethod */
  386. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  387. if (ttisnil(tm)) /* no metamethod? */
  388. luaG_typeerror(L, rb, "get length of");
  389. break;
  390. }
  391. }
  392. luaT_callTM(L, tm, rb, rb, ra, 1);
  393. }
  394. /*
  395. ** Integer division; return 'm // n'. (Assume that C division with
  396. ** negative operands follows C99 behavior.)
  397. */
  398. lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) {
  399. if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */
  400. if (n == 0)
  401. luaG_runerror(L, "attempt to divide by zero");
  402. return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
  403. }
  404. else {
  405. lua_Integer d = m / n; /* perform division */
  406. if ((m ^ n) >= 0 || m % n == 0) /* same signal or no rest? */
  407. return d;
  408. else
  409. return d - 1; /* correct 'div' for negative case */
  410. }
  411. }
  412. /*
  413. ** Integer modulus; return 'm % n'. (Assume that C '%' with
  414. ** negative operands follows C99 behavior.)
  415. */
  416. lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
  417. if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */
  418. if (n == 0)
  419. luaG_runerror(L, "attempt to perform 'n%%0'");
  420. return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
  421. }
  422. else {
  423. lua_Integer r = m % n;
  424. if (r == 0 || (m ^ n) >= 0) /* no rest or same signal? */
  425. return r;
  426. else
  427. return r + n; /* correct 'mod' for negative case */
  428. }
  429. }
  430. /* number of bits in an integer */
  431. #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
  432. /*
  433. ** Shift left operation. (Shift right just negates 'y'.)
  434. */
  435. lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
  436. if (y < 0) { /* shift right? */
  437. if (y <= -NBITS) return 0;
  438. else return intop(>>, x, -y);
  439. }
  440. else { /* shift left */
  441. if (y >= NBITS) return 0;
  442. else return intop(<<, x, y);
  443. }
  444. }
  445. /*
  446. ** check whether cached closure in prototype 'p' may be reused, that is,
  447. ** whether there is a cached closure with the same upvalues needed by
  448. ** new closure to be created.
  449. */
  450. static LClosure *getcached (Proto *p, UpVal **encup, StkId base) {
  451. LClosure *c = p->cache;
  452. if (c != NULL) { /* is there a cached closure? */
  453. int nup = p->sizeupvalues;
  454. Upvaldesc *uv = p->upvalues;
  455. int i;
  456. for (i = 0; i < nup; i++) { /* check whether it has right upvalues */
  457. TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;
  458. if (c->upvals[i]->v != v)
  459. return NULL; /* wrong upvalue; cannot reuse closure */
  460. }
  461. }
  462. return c; /* return cached closure (or NULL if no cached closure) */
  463. }
  464. /*
  465. ** create a new Lua closure, push it in the stack, and initialize
  466. ** its upvalues. Note that the closure is not cached if prototype is
  467. ** already black (which means that 'cache' was already cleared by the
  468. ** GC).
  469. */
  470. static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
  471. StkId ra) {
  472. int nup = p->sizeupvalues;
  473. Upvaldesc *uv = p->upvalues;
  474. int i;
  475. LClosure *ncl = luaF_newLclosure(L, nup);
  476. ncl->p = p;
  477. setclLvalue(L, ra, ncl); /* anchor new closure in stack */
  478. for (i = 0; i < nup; i++) { /* fill in its upvalues */
  479. if (uv[i].instack) /* upvalue refers to local variable? */
  480. ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
  481. else /* get upvalue from enclosing function */
  482. ncl->upvals[i] = encup[uv[i].idx];
  483. ncl->upvals[i]->refcount++;
  484. /* new closure is white, so we do not need a barrier here */
  485. }
  486. if (!isblack(p)) /* cache will not break GC invariant? */
  487. p->cache = ncl; /* save it on cache for reuse */
  488. }
  489. /*
  490. ** finish execution of an opcode interrupted by an yield
  491. */
  492. void luaV_finishOp (lua_State *L) {
  493. CallInfo *ci = L->ci;
  494. StkId base = ci->u.l.base;
  495. Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
  496. OpCode op = GET_OPCODE(inst);
  497. switch (op) { /* finish its execution */
  498. case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV:
  499. case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR:
  500. case OP_MOD: case OP_POW:
  501. case OP_UNM: case OP_BNOT: case OP_LEN:
  502. case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
  503. setobjs2s(L, base + GETARG_A(inst), --L->top);
  504. break;
  505. }
  506. case OP_LE: case OP_LT: case OP_EQ: {
  507. int res = !l_isfalse(L->top - 1);
  508. L->top--;
  509. /* metamethod should not be called when operand is K */
  510. lua_assert(!ISK(GETARG_B(inst)));
  511. if (op == OP_LE && /* "<=" using "<" instead? */
  512. ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
  513. res = !res; /* invert result */
  514. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  515. if (res != GETARG_A(inst)) /* condition failed? */
  516. ci->u.l.savedpc++; /* skip jump instruction */
  517. break;
  518. }
  519. case OP_CONCAT: {
  520. StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */
  521. int b = GETARG_B(inst); /* first element to concatenate */
  522. int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */
  523. setobj2s(L, top - 2, top); /* put TM result in proper position */
  524. if (total > 1) { /* are there elements to concat? */
  525. L->top = top - 1; /* top is one after last element (at top-2) */
  526. luaV_concat(L, total); /* concat them (may yield again) */
  527. }
  528. /* move final result to final position */
  529. setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1);
  530. L->top = ci->top; /* restore top */
  531. break;
  532. }
  533. case OP_TFORCALL: {
  534. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
  535. L->top = ci->top; /* correct top */
  536. break;
  537. }
  538. case OP_CALL: {
  539. if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */
  540. L->top = ci->top; /* adjust results */
  541. break;
  542. }
  543. case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
  544. break;
  545. default: lua_assert(0);
  546. }
  547. }
  548. /*
  549. ** {==================================================================
  550. ** Function 'luaV_execute': main interpreter loop
  551. ** ===================================================================
  552. */
  553. /*
  554. ** some macros for common tasks in 'luaV_execute'
  555. */
  556. #if !defined luai_runtimecheck
  557. #define luai_runtimecheck(L, c) /* void */
  558. #endif
  559. #define RA(i) (base+GETARG_A(i))
  560. /* to be used after possible stack reallocation */
  561. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  562. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  563. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  564. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  565. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  566. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  567. #define KBx(i) \
  568. (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
  569. /* execute a jump instruction */
  570. #define dojump(ci,i,e) \
  571. { int a = GETARG_A(i); \
  572. if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
  573. ci->u.l.savedpc += GETARG_sBx(i) + e; }
  574. /* for test instructions, execute the jump instruction that follows it */
  575. #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
  576. #define Protect(x) { {x;}; base = ci->u.l.base; }
  577. #define checkGC(L,c) \
  578. Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \
  579. luaC_step(L); \
  580. L->top = ci->top;}) /* restore top */ \
  581. luai_threadyield(L); )
  582. #define vmdispatch(o) switch(o)
  583. #define vmcase(l,b) case l: {b} break;
  584. #define vmcasenb(l,b) case l: {b} /* nb = no break */
  585. void luaV_execute (lua_State *L) {
  586. CallInfo *ci = L->ci;
  587. LClosure *cl;
  588. TValue *k;
  589. StkId base;
  590. newframe: /* reentry point when frame changes (call/return) */
  591. lua_assert(ci == L->ci);
  592. cl = clLvalue(ci->func);
  593. k = cl->p->k;
  594. base = ci->u.l.base;
  595. /* main loop of interpreter */
  596. for (;;) {
  597. Instruction i = *(ci->u.l.savedpc++);
  598. StkId ra;
  599. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  600. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  601. Protect(luaG_traceexec(L));
  602. }
  603. /* WARNING: several calls may realloc the stack and invalidate 'ra' */
  604. ra = RA(i);
  605. lua_assert(base == ci->u.l.base);
  606. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  607. vmdispatch (GET_OPCODE(i)) {
  608. vmcase(OP_MOVE,
  609. setobjs2s(L, ra, RB(i));
  610. )
  611. vmcase(OP_LOADK,
  612. TValue *rb = k + GETARG_Bx(i);
  613. setobj2s(L, ra, rb);
  614. )
  615. vmcase(OP_LOADKX,
  616. TValue *rb;
  617. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  618. rb = k + GETARG_Ax(*ci->u.l.savedpc++);
  619. setobj2s(L, ra, rb);
  620. )
  621. vmcase(OP_LOADBOOL,
  622. setbvalue(ra, GETARG_B(i));
  623. if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
  624. )
  625. vmcase(OP_LOADNIL,
  626. int b = GETARG_B(i);
  627. do {
  628. setnilvalue(ra++);
  629. } while (b--);
  630. )
  631. vmcase(OP_GETUPVAL,
  632. int b = GETARG_B(i);
  633. setobj2s(L, ra, cl->upvals[b]->v);
  634. )
  635. vmcase(OP_GETTABUP,
  636. int b = GETARG_B(i);
  637. Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
  638. )
  639. vmcase(OP_GETTABLE,
  640. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  641. )
  642. vmcase(OP_SETTABUP,
  643. int a = GETARG_A(i);
  644. Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
  645. )
  646. vmcase(OP_SETUPVAL,
  647. UpVal *uv = cl->upvals[GETARG_B(i)];
  648. setobj(L, uv->v, ra);
  649. luaC_upvalbarrier(L, uv);
  650. )
  651. vmcase(OP_SETTABLE,
  652. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  653. )
  654. vmcase(OP_NEWTABLE,
  655. int b = GETARG_B(i);
  656. int c = GETARG_C(i);
  657. Table *t = luaH_new(L);
  658. sethvalue(L, ra, t);
  659. if (b != 0 || c != 0)
  660. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  661. checkGC(L, ra + 1);
  662. )
  663. vmcase(OP_SELF,
  664. StkId rb = RB(i);
  665. setobjs2s(L, ra+1, rb);
  666. Protect(luaV_gettable(L, rb, RKC(i), ra));
  667. )
  668. vmcase(OP_ADD,
  669. TValue *rb = RKB(i);
  670. TValue *rc = RKC(i);
  671. lua_Number nb; lua_Number nc;
  672. if (ttisinteger(rb) && ttisinteger(rc)) {
  673. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  674. setivalue(ra, intop(+, ib, ic));
  675. }
  676. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  677. setfltvalue(ra, luai_numadd(L, nb, nc));
  678. }
  679. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
  680. )
  681. vmcase(OP_SUB,
  682. TValue *rb = RKB(i);
  683. TValue *rc = RKC(i);
  684. lua_Number nb; lua_Number nc;
  685. if (ttisinteger(rb) && ttisinteger(rc)) {
  686. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  687. setivalue(ra, intop(-, ib, ic));
  688. }
  689. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  690. setfltvalue(ra, luai_numsub(L, nb, nc));
  691. }
  692. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
  693. )
  694. vmcase(OP_MUL,
  695. TValue *rb = RKB(i);
  696. TValue *rc = RKC(i);
  697. lua_Number nb; lua_Number nc;
  698. if (ttisinteger(rb) && ttisinteger(rc)) {
  699. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  700. setivalue(ra, intop(*, ib, ic));
  701. }
  702. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  703. setfltvalue(ra, luai_nummul(L, nb, nc));
  704. }
  705. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
  706. )
  707. vmcase(OP_DIV, /* float division (always with floats) */
  708. TValue *rb = RKB(i);
  709. TValue *rc = RKC(i);
  710. lua_Number nb; lua_Number nc;
  711. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  712. setfltvalue(ra, luai_numdiv(L, nb, nc));
  713. }
  714. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
  715. )
  716. vmcase(OP_IDIV, /* integer division */
  717. TValue *rb = RKB(i);
  718. TValue *rc = RKC(i);
  719. lua_Integer ib; lua_Integer ic;
  720. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  721. setivalue(ra, luaV_div(L, ib, ic));
  722. }
  723. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); }
  724. )
  725. vmcase(OP_BAND,
  726. TValue *rb = RKB(i);
  727. TValue *rc = RKC(i);
  728. lua_Integer ib; lua_Integer ic;
  729. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  730. setivalue(ra, intop(&, ib, ic));
  731. }
  732. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); }
  733. )
  734. vmcase(OP_BOR,
  735. TValue *rb = RKB(i);
  736. TValue *rc = RKC(i);
  737. lua_Integer ib; lua_Integer ic;
  738. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  739. setivalue(ra, intop(|, ib, ic));
  740. }
  741. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); }
  742. )
  743. vmcase(OP_BXOR,
  744. TValue *rb = RKB(i);
  745. TValue *rc = RKC(i);
  746. lua_Integer ib; lua_Integer ic;
  747. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  748. setivalue(ra, intop(^, ib, ic));
  749. }
  750. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); }
  751. )
  752. vmcase(OP_SHL,
  753. TValue *rb = RKB(i);
  754. TValue *rc = RKC(i);
  755. lua_Integer ib; lua_Integer ic;
  756. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  757. setivalue(ra, luaV_shiftl(ib, ic));
  758. }
  759. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); }
  760. )
  761. vmcase(OP_SHR,
  762. TValue *rb = RKB(i);
  763. TValue *rc = RKC(i);
  764. lua_Integer ib; lua_Integer ic;
  765. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  766. setivalue(ra, luaV_shiftl(ib, -ic));
  767. }
  768. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); }
  769. )
  770. vmcase(OP_MOD,
  771. TValue *rb = RKB(i);
  772. TValue *rc = RKC(i);
  773. lua_Number nb; lua_Number nc;
  774. if (ttisinteger(rb) && ttisinteger(rc)) {
  775. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  776. setivalue(ra, luaV_mod(L, ib, ic));
  777. }
  778. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  779. lua_Number m;
  780. luai_nummod(L, nb, nc, m);
  781. setfltvalue(ra, m);
  782. }
  783. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
  784. )
  785. vmcase(OP_POW,
  786. TValue *rb = RKB(i);
  787. TValue *rc = RKC(i);
  788. lua_Number nb; lua_Number nc;
  789. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  790. setfltvalue(ra, luai_numpow(L, nb, nc));
  791. }
  792. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
  793. )
  794. vmcase(OP_UNM,
  795. TValue *rb = RB(i);
  796. lua_Number nb;
  797. if (ttisinteger(rb)) {
  798. lua_Integer ib = ivalue(rb);
  799. setivalue(ra, intop(-, 0, ib));
  800. }
  801. else if (tonumber(rb, &nb)) {
  802. setfltvalue(ra, luai_numunm(L, nb));
  803. }
  804. else {
  805. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  806. }
  807. )
  808. vmcase(OP_BNOT,
  809. TValue *rb = RB(i);
  810. lua_Integer ib;
  811. if (tointeger(rb, &ib)) {
  812. setivalue(ra, intop(^, ~l_castS2U(0), ib));
  813. }
  814. else {
  815. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  816. }
  817. )
  818. vmcase(OP_NOT,
  819. TValue *rb = RB(i);
  820. int res = l_isfalse(rb); /* next assignment may change this value */
  821. setbvalue(ra, res);
  822. )
  823. vmcase(OP_LEN,
  824. Protect(luaV_objlen(L, ra, RB(i)));
  825. )
  826. vmcase(OP_CONCAT,
  827. int b = GETARG_B(i);
  828. int c = GETARG_C(i);
  829. StkId rb;
  830. L->top = base + c + 1; /* mark the end of concat operands */
  831. Protect(luaV_concat(L, c - b + 1));
  832. ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
  833. rb = b + base;
  834. setobjs2s(L, ra, rb);
  835. checkGC(L, (ra >= rb ? ra + 1 : rb));
  836. L->top = ci->top; /* restore top */
  837. )
  838. vmcase(OP_JMP,
  839. dojump(ci, i, 0);
  840. )
  841. vmcase(OP_EQ,
  842. TValue *rb = RKB(i);
  843. TValue *rc = RKC(i);
  844. Protect(
  845. if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i))
  846. ci->u.l.savedpc++;
  847. else
  848. donextjump(ci);
  849. )
  850. )
  851. vmcase(OP_LT,
  852. Protect(
  853. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
  854. ci->u.l.savedpc++;
  855. else
  856. donextjump(ci);
  857. )
  858. )
  859. vmcase(OP_LE,
  860. Protect(
  861. if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
  862. ci->u.l.savedpc++;
  863. else
  864. donextjump(ci);
  865. )
  866. )
  867. vmcase(OP_TEST,
  868. if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
  869. ci->u.l.savedpc++;
  870. else
  871. donextjump(ci);
  872. )
  873. vmcase(OP_TESTSET,
  874. TValue *rb = RB(i);
  875. if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
  876. ci->u.l.savedpc++;
  877. else {
  878. setobjs2s(L, ra, rb);
  879. donextjump(ci);
  880. }
  881. )
  882. vmcase(OP_CALL,
  883. int b = GETARG_B(i);
  884. int nresults = GETARG_C(i) - 1;
  885. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  886. if (luaD_precall(L, ra, nresults)) { /* C function? */
  887. if (nresults >= 0) L->top = ci->top; /* adjust results */
  888. base = ci->u.l.base;
  889. }
  890. else { /* Lua function */
  891. ci = L->ci;
  892. ci->callstatus |= CIST_REENTRY;
  893. goto newframe; /* restart luaV_execute over new Lua function */
  894. }
  895. )
  896. vmcase(OP_TAILCALL,
  897. int b = GETARG_B(i);
  898. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  899. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  900. if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */
  901. base = ci->u.l.base;
  902. else {
  903. /* tail call: put called frame (n) in place of caller one (o) */
  904. CallInfo *nci = L->ci; /* called frame */
  905. CallInfo *oci = nci->previous; /* caller frame */
  906. StkId nfunc = nci->func; /* called function */
  907. StkId ofunc = oci->func; /* caller function */
  908. /* last stack slot filled by 'precall' */
  909. StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
  910. int aux;
  911. /* close all upvalues from previous call */
  912. if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
  913. /* move new frame into old one */
  914. for (aux = 0; nfunc + aux < lim; aux++)
  915. setobjs2s(L, ofunc + aux, nfunc + aux);
  916. oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */
  917. oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
  918. oci->u.l.savedpc = nci->u.l.savedpc;
  919. oci->callstatus |= CIST_TAIL; /* function was tail called */
  920. ci = L->ci = oci; /* remove new frame */
  921. lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
  922. goto newframe; /* restart luaV_execute over new Lua function */
  923. }
  924. )
  925. vmcasenb(OP_RETURN,
  926. int b = GETARG_B(i);
  927. if (b != 0) L->top = ra+b-1;
  928. if (cl->p->sizep > 0) luaF_close(L, base);
  929. b = luaD_poscall(L, ra);
  930. if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */
  931. return; /* external invocation: return */
  932. else { /* invocation via reentry: continue execution */
  933. ci = L->ci;
  934. if (b) L->top = ci->top;
  935. lua_assert(isLua(ci));
  936. lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
  937. goto newframe; /* restart luaV_execute over new Lua function */
  938. }
  939. )
  940. vmcase(OP_FORLOOP,
  941. if (ttisinteger(ra)) { /* integer loop? */
  942. lua_Integer step = ivalue(ra + 2);
  943. lua_Integer idx = ivalue(ra) + step; /* increment index */
  944. lua_Integer limit = ivalue(ra + 1);
  945. if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
  946. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  947. setivalue(ra, idx); /* update internal index... */
  948. setivalue(ra + 3, idx); /* ...and external index */
  949. }
  950. }
  951. else { /* floating loop */
  952. lua_Number step = fltvalue(ra + 2);
  953. lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */
  954. lua_Number limit = fltvalue(ra + 1);
  955. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  956. : luai_numle(limit, idx)) {
  957. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  958. setfltvalue(ra, idx); /* update internal index... */
  959. setfltvalue(ra + 3, idx); /* ...and external index */
  960. }
  961. }
  962. )
  963. vmcase(OP_FORPREP,
  964. TValue *init = ra;
  965. TValue *plimit = ra + 1;
  966. TValue *pstep = ra + 2;
  967. lua_Integer ilimit;
  968. int stopnow;
  969. if (ttisinteger(init) && ttisinteger(pstep) &&
  970. forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
  971. /* all values are integer */
  972. lua_Integer initv = (stopnow ? 0 : ivalue(init));
  973. setivalue(plimit, ilimit);
  974. setivalue(init, initv - ivalue(pstep));
  975. }
  976. else { /* try making all values floats */
  977. lua_Number ninit; lua_Number nlimit; lua_Number nstep;
  978. if (!tonumber(plimit, &nlimit))
  979. luaG_runerror(L, "'for' limit must be a number");
  980. setfltvalue(plimit, nlimit);
  981. if (!tonumber(pstep, &nstep))
  982. luaG_runerror(L, "'for' step must be a number");
  983. setfltvalue(pstep, nstep);
  984. if (!tonumber(init, &ninit))
  985. luaG_runerror(L, "'for' initial value must be a number");
  986. setfltvalue(init, luai_numsub(L, ninit, nstep));
  987. }
  988. ci->u.l.savedpc += GETARG_sBx(i);
  989. )
  990. vmcasenb(OP_TFORCALL,
  991. StkId cb = ra + 3; /* call base */
  992. setobjs2s(L, cb+2, ra+2);
  993. setobjs2s(L, cb+1, ra+1);
  994. setobjs2s(L, cb, ra);
  995. L->top = cb + 3; /* func. + 2 args (state and index) */
  996. Protect(luaD_call(L, cb, GETARG_C(i), 1));
  997. L->top = ci->top;
  998. i = *(ci->u.l.savedpc++); /* go to next instruction */
  999. ra = RA(i);
  1000. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  1001. goto l_tforloop;
  1002. )
  1003. vmcase(OP_TFORLOOP,
  1004. l_tforloop:
  1005. if (!ttisnil(ra + 1)) { /* continue loop? */
  1006. setobjs2s(L, ra, ra + 1); /* save control variable */
  1007. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  1008. }
  1009. )
  1010. vmcase(OP_SETLIST,
  1011. int n = GETARG_B(i);
  1012. int c = GETARG_C(i);
  1013. unsigned int last;
  1014. Table *h;
  1015. if (n == 0) n = cast_int(L->top - ra) - 1;
  1016. if (c == 0) {
  1017. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  1018. c = GETARG_Ax(*ci->u.l.savedpc++);
  1019. }
  1020. luai_runtimecheck(L, ttistable(ra));
  1021. h = hvalue(ra);
  1022. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  1023. if (last > h->sizearray) /* needs more space? */
  1024. luaH_resizearray(L, h, last); /* pre-allocate it at once */
  1025. for (; n > 0; n--) {
  1026. TValue *val = ra+n;
  1027. luaH_setint(L, h, last--, val);
  1028. luaC_barrierback(L, h, val);
  1029. }
  1030. L->top = ci->top; /* correct top (in case of previous open call) */
  1031. )
  1032. vmcase(OP_CLOSURE,
  1033. Proto *p = cl->p->p[GETARG_Bx(i)];
  1034. LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  1035. if (ncl == NULL) /* no match? */
  1036. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  1037. else
  1038. setclLvalue(L, ra, ncl); /* push cashed closure */
  1039. checkGC(L, ra + 1);
  1040. )
  1041. vmcase(OP_VARARG,
  1042. int b = GETARG_B(i) - 1;
  1043. int j;
  1044. int n = cast_int(base - ci->func) - cl->p->numparams - 1;
  1045. if (b < 0) { /* B == 0? */
  1046. b = n; /* get all var. arguments */
  1047. Protect(luaD_checkstack(L, n));
  1048. ra = RA(i); /* previous call may change the stack */
  1049. L->top = ra + n;
  1050. }
  1051. for (j = 0; j < b; j++) {
  1052. if (j < n) {
  1053. setobjs2s(L, ra + j, base - n + j);
  1054. }
  1055. else {
  1056. setnilvalue(ra + j);
  1057. }
  1058. }
  1059. )
  1060. vmcase(OP_EXTRAARG,
  1061. lua_assert(0);
  1062. )
  1063. }
  1064. }
  1065. }
  1066. /* }================================================================== */