lvm.c 38 KB

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