lvm.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /*
  2. ** $Id: lvm.c,v 2.231 2014/12/19 13:36:32 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) case l:
  581. #define vmbreak 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. vmbreak;
  608. }
  609. vmcase(OP_LOADK) {
  610. TValue *rb = k + GETARG_Bx(i);
  611. setobj2s(L, ra, rb);
  612. vmbreak;
  613. }
  614. vmcase(OP_LOADKX) {
  615. TValue *rb;
  616. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  617. rb = k + GETARG_Ax(*ci->u.l.savedpc++);
  618. setobj2s(L, ra, rb);
  619. vmbreak;
  620. }
  621. vmcase(OP_LOADBOOL) {
  622. setbvalue(ra, GETARG_B(i));
  623. if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
  624. vmbreak;
  625. }
  626. vmcase(OP_LOADNIL) {
  627. int b = GETARG_B(i);
  628. do {
  629. setnilvalue(ra++);
  630. } while (b--);
  631. vmbreak;
  632. }
  633. vmcase(OP_GETUPVAL) {
  634. int b = GETARG_B(i);
  635. setobj2s(L, ra, cl->upvals[b]->v);
  636. vmbreak;
  637. }
  638. vmcase(OP_GETTABUP) {
  639. int b = GETARG_B(i);
  640. Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
  641. vmbreak;
  642. }
  643. vmcase(OP_GETTABLE) {
  644. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  645. vmbreak;
  646. }
  647. vmcase(OP_SETTABUP) {
  648. int a = GETARG_A(i);
  649. Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
  650. vmbreak;
  651. }
  652. vmcase(OP_SETUPVAL) {
  653. UpVal *uv = cl->upvals[GETARG_B(i)];
  654. setobj(L, uv->v, ra);
  655. luaC_upvalbarrier(L, uv);
  656. vmbreak;
  657. }
  658. vmcase(OP_SETTABLE) {
  659. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  660. vmbreak;
  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. vmbreak;
  671. }
  672. vmcase(OP_SELF) {
  673. StkId rb = RB(i);
  674. setobjs2s(L, ra+1, rb);
  675. Protect(luaV_gettable(L, rb, RKC(i), ra));
  676. vmbreak;
  677. }
  678. vmcase(OP_ADD) {
  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_numadd(L, nb, nc));
  688. }
  689. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
  690. vmbreak;
  691. }
  692. vmcase(OP_SUB) {
  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_numsub(L, nb, nc));
  702. }
  703. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
  704. vmbreak;
  705. }
  706. vmcase(OP_MUL) {
  707. TValue *rb = RKB(i);
  708. TValue *rc = RKC(i);
  709. lua_Number nb; lua_Number nc;
  710. if (ttisinteger(rb) && ttisinteger(rc)) {
  711. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  712. setivalue(ra, intop(*, ib, ic));
  713. }
  714. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  715. setfltvalue(ra, luai_nummul(L, nb, nc));
  716. }
  717. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
  718. vmbreak;
  719. }
  720. vmcase(OP_DIV) { /* float division (always with floats) */
  721. TValue *rb = RKB(i);
  722. TValue *rc = RKC(i);
  723. lua_Number nb; lua_Number nc;
  724. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  725. setfltvalue(ra, luai_numdiv(L, nb, nc));
  726. }
  727. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
  728. vmbreak;
  729. }
  730. vmcase(OP_BAND) {
  731. TValue *rb = RKB(i);
  732. TValue *rc = RKC(i);
  733. lua_Integer ib; lua_Integer ic;
  734. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  735. setivalue(ra, intop(&, ib, ic));
  736. }
  737. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); }
  738. vmbreak;
  739. }
  740. vmcase(OP_BOR) {
  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, intop(|, ib, ic));
  746. }
  747. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); }
  748. vmbreak;
  749. }
  750. vmcase(OP_BXOR) {
  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, intop(^, ib, ic));
  756. }
  757. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); }
  758. vmbreak;
  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. vmbreak;
  769. }
  770. vmcase(OP_SHR) {
  771. TValue *rb = RKB(i);
  772. TValue *rc = RKC(i);
  773. lua_Integer ib; lua_Integer ic;
  774. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  775. setivalue(ra, luaV_shiftl(ib, -ic));
  776. }
  777. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); }
  778. vmbreak;
  779. }
  780. vmcase(OP_MOD) {
  781. TValue *rb = RKB(i);
  782. TValue *rc = RKC(i);
  783. lua_Number nb; lua_Number nc;
  784. if (ttisinteger(rb) && ttisinteger(rc)) {
  785. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  786. setivalue(ra, luaV_mod(L, ib, ic));
  787. }
  788. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  789. lua_Number m;
  790. luai_nummod(L, nb, nc, m);
  791. setfltvalue(ra, m);
  792. }
  793. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
  794. vmbreak;
  795. }
  796. vmcase(OP_IDIV) { /* floor division */
  797. TValue *rb = RKB(i);
  798. TValue *rc = RKC(i);
  799. lua_Number nb; lua_Number nc;
  800. if (ttisinteger(rb) && ttisinteger(rc)) {
  801. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  802. setivalue(ra, luaV_div(L, ib, ic));
  803. }
  804. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  805. setfltvalue(ra, luai_numidiv(L, nb, nc));
  806. }
  807. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); }
  808. vmbreak;
  809. }
  810. vmcase(OP_POW) {
  811. TValue *rb = RKB(i);
  812. TValue *rc = RKC(i);
  813. lua_Number nb; lua_Number nc;
  814. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  815. setfltvalue(ra, luai_numpow(L, nb, nc));
  816. }
  817. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
  818. vmbreak;
  819. }
  820. vmcase(OP_UNM) {
  821. TValue *rb = RB(i);
  822. lua_Number nb;
  823. if (ttisinteger(rb)) {
  824. lua_Integer ib = ivalue(rb);
  825. setivalue(ra, intop(-, 0, ib));
  826. }
  827. else if (tonumber(rb, &nb)) {
  828. setfltvalue(ra, luai_numunm(L, nb));
  829. }
  830. else {
  831. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  832. }
  833. vmbreak;
  834. }
  835. vmcase(OP_BNOT) {
  836. TValue *rb = RB(i);
  837. lua_Integer ib;
  838. if (tointeger(rb, &ib)) {
  839. setivalue(ra, intop(^, ~l_castS2U(0), ib));
  840. }
  841. else {
  842. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  843. }
  844. vmbreak;
  845. }
  846. vmcase(OP_NOT) {
  847. TValue *rb = RB(i);
  848. int res = l_isfalse(rb); /* next assignment may change this value */
  849. setbvalue(ra, res);
  850. vmbreak;
  851. }
  852. vmcase(OP_LEN) {
  853. Protect(luaV_objlen(L, ra, RB(i)));
  854. vmbreak;
  855. }
  856. vmcase(OP_CONCAT) {
  857. int b = GETARG_B(i);
  858. int c = GETARG_C(i);
  859. StkId rb;
  860. L->top = base + c + 1; /* mark the end of concat operands */
  861. Protect(luaV_concat(L, c - b + 1));
  862. ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
  863. rb = b + base;
  864. setobjs2s(L, ra, rb);
  865. checkGC(L, (ra >= rb ? ra + 1 : rb));
  866. L->top = ci->top; /* restore top */
  867. vmbreak;
  868. }
  869. vmcase(OP_JMP) {
  870. dojump(ci, i, 0);
  871. vmbreak;
  872. }
  873. vmcase(OP_EQ) {
  874. TValue *rb = RKB(i);
  875. TValue *rc = RKC(i);
  876. Protect(
  877. if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i))
  878. ci->u.l.savedpc++;
  879. else
  880. donextjump(ci);
  881. )
  882. vmbreak;
  883. }
  884. vmcase(OP_LT) {
  885. Protect(
  886. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
  887. ci->u.l.savedpc++;
  888. else
  889. donextjump(ci);
  890. )
  891. vmbreak;
  892. }
  893. vmcase(OP_LE) {
  894. Protect(
  895. if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
  896. ci->u.l.savedpc++;
  897. else
  898. donextjump(ci);
  899. )
  900. vmbreak;
  901. }
  902. vmcase(OP_TEST) {
  903. if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
  904. ci->u.l.savedpc++;
  905. else
  906. donextjump(ci);
  907. vmbreak;
  908. }
  909. vmcase(OP_TESTSET) {
  910. TValue *rb = RB(i);
  911. if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
  912. ci->u.l.savedpc++;
  913. else {
  914. setobjs2s(L, ra, rb);
  915. donextjump(ci);
  916. }
  917. vmbreak;
  918. }
  919. vmcase(OP_CALL) {
  920. int b = GETARG_B(i);
  921. int nresults = GETARG_C(i) - 1;
  922. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  923. if (luaD_precall(L, ra, nresults)) { /* C function? */
  924. if (nresults >= 0) L->top = ci->top; /* adjust results */
  925. base = ci->u.l.base;
  926. }
  927. else { /* Lua function */
  928. ci = L->ci;
  929. ci->callstatus |= CIST_REENTRY;
  930. goto newframe; /* restart luaV_execute over new Lua function */
  931. }
  932. vmbreak;
  933. }
  934. vmcase(OP_TAILCALL) {
  935. int b = GETARG_B(i);
  936. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  937. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  938. if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */
  939. base = ci->u.l.base;
  940. else {
  941. /* tail call: put called frame (n) in place of caller one (o) */
  942. CallInfo *nci = L->ci; /* called frame */
  943. CallInfo *oci = nci->previous; /* caller frame */
  944. StkId nfunc = nci->func; /* called function */
  945. StkId ofunc = oci->func; /* caller function */
  946. /* last stack slot filled by 'precall' */
  947. StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
  948. int aux;
  949. /* close all upvalues from previous call */
  950. if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
  951. /* move new frame into old one */
  952. for (aux = 0; nfunc + aux < lim; aux++)
  953. setobjs2s(L, ofunc + aux, nfunc + aux);
  954. oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */
  955. oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
  956. oci->u.l.savedpc = nci->u.l.savedpc;
  957. oci->callstatus |= CIST_TAIL; /* function was tail called */
  958. ci = L->ci = oci; /* remove new frame */
  959. lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
  960. goto newframe; /* restart luaV_execute over new Lua function */
  961. }
  962. vmbreak;
  963. }
  964. vmcase(OP_RETURN) {
  965. int b = GETARG_B(i);
  966. if (b != 0) L->top = ra+b-1;
  967. if (cl->p->sizep > 0) luaF_close(L, base);
  968. b = luaD_poscall(L, ra);
  969. if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */
  970. return; /* external invocation: return */
  971. else { /* invocation via reentry: continue execution */
  972. ci = L->ci;
  973. if (b) L->top = ci->top;
  974. lua_assert(isLua(ci));
  975. lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
  976. goto newframe; /* restart luaV_execute over new Lua function */
  977. }
  978. }
  979. vmcase(OP_FORLOOP) {
  980. if (ttisinteger(ra)) { /* integer loop? */
  981. lua_Integer step = ivalue(ra + 2);
  982. lua_Integer idx = ivalue(ra) + step; /* increment index */
  983. lua_Integer limit = ivalue(ra + 1);
  984. if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
  985. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  986. setivalue(ra, idx); /* update internal index... */
  987. setivalue(ra + 3, idx); /* ...and external index */
  988. }
  989. }
  990. else { /* floating loop */
  991. lua_Number step = fltvalue(ra + 2);
  992. lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */
  993. lua_Number limit = fltvalue(ra + 1);
  994. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  995. : luai_numle(limit, idx)) {
  996. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  997. setfltvalue(ra, idx); /* update internal index... */
  998. setfltvalue(ra + 3, idx); /* ...and external index */
  999. }
  1000. }
  1001. vmbreak;
  1002. }
  1003. vmcase(OP_FORPREP) {
  1004. TValue *init = ra;
  1005. TValue *plimit = ra + 1;
  1006. TValue *pstep = ra + 2;
  1007. lua_Integer ilimit;
  1008. int stopnow;
  1009. if (ttisinteger(init) && ttisinteger(pstep) &&
  1010. forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
  1011. /* all values are integer */
  1012. lua_Integer initv = (stopnow ? 0 : ivalue(init));
  1013. setivalue(plimit, ilimit);
  1014. setivalue(init, initv - ivalue(pstep));
  1015. }
  1016. else { /* try making all values floats */
  1017. lua_Number ninit; lua_Number nlimit; lua_Number nstep;
  1018. if (!tonumber(plimit, &nlimit))
  1019. luaG_runerror(L, "'for' limit must be a number");
  1020. setfltvalue(plimit, nlimit);
  1021. if (!tonumber(pstep, &nstep))
  1022. luaG_runerror(L, "'for' step must be a number");
  1023. setfltvalue(pstep, nstep);
  1024. if (!tonumber(init, &ninit))
  1025. luaG_runerror(L, "'for' initial value must be a number");
  1026. setfltvalue(init, luai_numsub(L, ninit, nstep));
  1027. }
  1028. ci->u.l.savedpc += GETARG_sBx(i);
  1029. vmbreak;
  1030. }
  1031. vmcase(OP_TFORCALL) {
  1032. StkId cb = ra + 3; /* call base */
  1033. setobjs2s(L, cb+2, ra+2);
  1034. setobjs2s(L, cb+1, ra+1);
  1035. setobjs2s(L, cb, ra);
  1036. L->top = cb + 3; /* func. + 2 args (state and index) */
  1037. Protect(luaD_call(L, cb, GETARG_C(i), 1));
  1038. L->top = ci->top;
  1039. i = *(ci->u.l.savedpc++); /* go to next instruction */
  1040. ra = RA(i);
  1041. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  1042. goto l_tforloop;
  1043. }
  1044. vmcase(OP_TFORLOOP) {
  1045. l_tforloop:
  1046. if (!ttisnil(ra + 1)) { /* continue loop? */
  1047. setobjs2s(L, ra, ra + 1); /* save control variable */
  1048. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  1049. }
  1050. vmbreak;
  1051. }
  1052. vmcase(OP_SETLIST) {
  1053. int n = GETARG_B(i);
  1054. int c = GETARG_C(i);
  1055. unsigned int last;
  1056. Table *h;
  1057. if (n == 0) n = cast_int(L->top - ra) - 1;
  1058. if (c == 0) {
  1059. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  1060. c = GETARG_Ax(*ci->u.l.savedpc++);
  1061. }
  1062. luai_runtimecheck(L, ttistable(ra));
  1063. h = hvalue(ra);
  1064. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  1065. if (last > h->sizearray) /* needs more space? */
  1066. luaH_resizearray(L, h, last); /* pre-allocate it at once */
  1067. for (; n > 0; n--) {
  1068. TValue *val = ra+n;
  1069. luaH_setint(L, h, last--, val);
  1070. luaC_barrierback(L, h, val);
  1071. }
  1072. L->top = ci->top; /* correct top (in case of previous open call) */
  1073. vmbreak;
  1074. }
  1075. vmcase(OP_CLOSURE) {
  1076. Proto *p = cl->p->p[GETARG_Bx(i)];
  1077. LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  1078. if (ncl == NULL) /* no match? */
  1079. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  1080. else
  1081. setclLvalue(L, ra, ncl); /* push cashed closure */
  1082. checkGC(L, ra + 1);
  1083. vmbreak;
  1084. }
  1085. vmcase(OP_VARARG) {
  1086. int b = GETARG_B(i) - 1;
  1087. int j;
  1088. int n = cast_int(base - ci->func) - cl->p->numparams - 1;
  1089. if (b < 0) { /* B == 0? */
  1090. b = n; /* get all var. arguments */
  1091. Protect(luaD_checkstack(L, n));
  1092. ra = RA(i); /* previous call may change the stack */
  1093. L->top = ra + n;
  1094. }
  1095. for (j = 0; j < b; j++) {
  1096. if (j < n) {
  1097. setobjs2s(L, ra + j, base - n + j);
  1098. }
  1099. else {
  1100. setnilvalue(ra + j);
  1101. }
  1102. }
  1103. vmbreak;
  1104. }
  1105. vmcase(OP_EXTRAARG) {
  1106. lua_assert(0);
  1107. vmbreak;
  1108. }
  1109. }
  1110. }
  1111. }
  1112. /* }================================================================== */