lvm.c 38 KB

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