lvm.c 37 KB

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