2
0

lvm.c 37 KB

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