lvm.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /*
  2. ** $Id: lvm.c,v 2.232 2014/12/27 20:30:38 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) == vslen(obj) + 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) == vslen(obj) + 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 = tsslen(ls);
  217. const char *r = getstr(rs);
  218. size_t lr = tsslen(rs);
  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. #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0)
  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 (isemptystr(top - 1)) /* second operand is empty? */
  335. cast_void(tostring(L, top - 2)); /* result is first operand */
  336. else if (isemptystr(top - 2)) { /* first operand is an empty string? */
  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 = vslen(top - 1);
  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 = vslen(top - i - 1);
  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 = vslen(top - i);
  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 (ttype(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_TSHRSTR: {
  379. setivalue(ra, tsvalue(rb)->shrlen);
  380. return;
  381. }
  382. case LUA_TLNGSTR: {
  383. setivalue(ra, tsvalue(rb)->u.lnglen);
  384. return;
  385. }
  386. default: { /* try metamethod */
  387. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  388. if (ttisnil(tm)) /* no metamethod? */
  389. luaG_typeerror(L, rb, "get length of");
  390. break;
  391. }
  392. }
  393. luaT_callTM(L, tm, rb, rb, ra, 1);
  394. }
  395. /*
  396. ** Integer division; return 'm // n', that is, floor(m/n).
  397. ** C division truncates its result (rounds towards zero).
  398. ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
  399. ** otherwise 'floor(q) == trunc(q) - 1'.
  400. */
  401. lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) {
  402. if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */
  403. if (n == 0)
  404. luaG_runerror(L, "attempt to divide by zero");
  405. return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
  406. }
  407. else {
  408. lua_Integer q = m / n; /* perform C division */
  409. if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */
  410. q -= 1; /* correct result for different rounding */
  411. return q;
  412. }
  413. }
  414. /*
  415. ** Integer modulus; return 'm % n'. (Assume that C '%' with
  416. ** negative operands follows C99 behavior. See previous comment
  417. ** about luaV_div.)
  418. */
  419. lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
  420. if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */
  421. if (n == 0)
  422. luaG_runerror(L, "attempt to perform 'n%%0'");
  423. return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
  424. }
  425. else {
  426. lua_Integer r = m % n;
  427. if (r != 0 && (m ^ n) < 0) /* 'm/n' would be non-integer negative? */
  428. r += n; /* correct result for different rounding */
  429. return r;
  430. }
  431. }
  432. /* number of bits in an integer */
  433. #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
  434. /*
  435. ** Shift left operation. (Shift right just negates 'y'.)
  436. */
  437. lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
  438. if (y < 0) { /* shift right? */
  439. if (y <= -NBITS) return 0;
  440. else return intop(>>, x, -y);
  441. }
  442. else { /* shift left */
  443. if (y >= NBITS) return 0;
  444. else return intop(<<, x, y);
  445. }
  446. }
  447. /*
  448. ** check whether cached closure in prototype 'p' may be reused, that is,
  449. ** whether there is a cached closure with the same upvalues needed by
  450. ** new closure to be created.
  451. */
  452. static LClosure *getcached (Proto *p, UpVal **encup, StkId base) {
  453. LClosure *c = p->cache;
  454. if (c != NULL) { /* is there a cached closure? */
  455. int nup = p->sizeupvalues;
  456. Upvaldesc *uv = p->upvalues;
  457. int i;
  458. for (i = 0; i < nup; i++) { /* check whether it has right upvalues */
  459. TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;
  460. if (c->upvals[i]->v != v)
  461. return NULL; /* wrong upvalue; cannot reuse closure */
  462. }
  463. }
  464. return c; /* return cached closure (or NULL if no cached closure) */
  465. }
  466. /*
  467. ** create a new Lua closure, push it in the stack, and initialize
  468. ** its upvalues. Note that the closure is not cached if prototype is
  469. ** already black (which means that 'cache' was already cleared by the
  470. ** GC).
  471. */
  472. static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
  473. StkId ra) {
  474. int nup = p->sizeupvalues;
  475. Upvaldesc *uv = p->upvalues;
  476. int i;
  477. LClosure *ncl = luaF_newLclosure(L, nup);
  478. ncl->p = p;
  479. setclLvalue(L, ra, ncl); /* anchor new closure in stack */
  480. for (i = 0; i < nup; i++) { /* fill in its upvalues */
  481. if (uv[i].instack) /* upvalue refers to local variable? */
  482. ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
  483. else /* get upvalue from enclosing function */
  484. ncl->upvals[i] = encup[uv[i].idx];
  485. ncl->upvals[i]->refcount++;
  486. /* new closure is white, so we do not need a barrier here */
  487. }
  488. if (!isblack(p)) /* cache will not break GC invariant? */
  489. p->cache = ncl; /* save it on cache for reuse */
  490. }
  491. /*
  492. ** finish execution of an opcode interrupted by an yield
  493. */
  494. void luaV_finishOp (lua_State *L) {
  495. CallInfo *ci = L->ci;
  496. StkId base = ci->u.l.base;
  497. Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
  498. OpCode op = GET_OPCODE(inst);
  499. switch (op) { /* finish its execution */
  500. case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV:
  501. case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR:
  502. case OP_MOD: case OP_POW:
  503. case OP_UNM: case OP_BNOT: case OP_LEN:
  504. case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
  505. setobjs2s(L, base + GETARG_A(inst), --L->top);
  506. break;
  507. }
  508. case OP_LE: case OP_LT: case OP_EQ: {
  509. int res = !l_isfalse(L->top - 1);
  510. L->top--;
  511. /* metamethod should not be called when operand is K */
  512. lua_assert(!ISK(GETARG_B(inst)));
  513. if (op == OP_LE && /* "<=" using "<" instead? */
  514. ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
  515. res = !res; /* invert result */
  516. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  517. if (res != GETARG_A(inst)) /* condition failed? */
  518. ci->u.l.savedpc++; /* skip jump instruction */
  519. break;
  520. }
  521. case OP_CONCAT: {
  522. StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */
  523. int b = GETARG_B(inst); /* first element to concatenate */
  524. int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */
  525. setobj2s(L, top - 2, top); /* put TM result in proper position */
  526. if (total > 1) { /* are there elements to concat? */
  527. L->top = top - 1; /* top is one after last element (at top-2) */
  528. luaV_concat(L, total); /* concat them (may yield again) */
  529. }
  530. /* move final result to final position */
  531. setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1);
  532. L->top = ci->top; /* restore top */
  533. break;
  534. }
  535. case OP_TFORCALL: {
  536. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
  537. L->top = ci->top; /* correct top */
  538. break;
  539. }
  540. case OP_CALL: {
  541. if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */
  542. L->top = ci->top; /* adjust results */
  543. break;
  544. }
  545. case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
  546. break;
  547. default: lua_assert(0);
  548. }
  549. }
  550. /*
  551. ** {==================================================================
  552. ** Function 'luaV_execute': main interpreter loop
  553. ** ===================================================================
  554. */
  555. /*
  556. ** some macros for common tasks in 'luaV_execute'
  557. */
  558. #if !defined luai_runtimecheck
  559. #define luai_runtimecheck(L, c) /* void */
  560. #endif
  561. #define RA(i) (base+GETARG_A(i))
  562. /* to be used after possible stack reallocation */
  563. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  564. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  565. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  566. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  567. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  568. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  569. #define KBx(i) \
  570. (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
  571. /* execute a jump instruction */
  572. #define dojump(ci,i,e) \
  573. { int a = GETARG_A(i); \
  574. if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
  575. ci->u.l.savedpc += GETARG_sBx(i) + e; }
  576. /* for test instructions, execute the jump instruction that follows it */
  577. #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
  578. #define Protect(x) { {x;}; base = ci->u.l.base; }
  579. #define checkGC(L,c) \
  580. Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \
  581. luaC_step(L); \
  582. L->top = ci->top;}) /* restore top */ \
  583. luai_threadyield(L); )
  584. #define vmdispatch(o) switch(o)
  585. #define vmcase(l) case l:
  586. #define vmbreak break
  587. void luaV_execute (lua_State *L) {
  588. CallInfo *ci = L->ci;
  589. LClosure *cl;
  590. TValue *k;
  591. StkId base;
  592. newframe: /* reentry point when frame changes (call/return) */
  593. lua_assert(ci == L->ci);
  594. cl = clLvalue(ci->func);
  595. k = cl->p->k;
  596. base = ci->u.l.base;
  597. /* main loop of interpreter */
  598. for (;;) {
  599. Instruction i = *(ci->u.l.savedpc++);
  600. StkId ra;
  601. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  602. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  603. Protect(luaG_traceexec(L));
  604. }
  605. /* WARNING: several calls may realloc the stack and invalidate 'ra' */
  606. ra = RA(i);
  607. lua_assert(base == ci->u.l.base);
  608. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  609. vmdispatch (GET_OPCODE(i)) {
  610. vmcase(OP_MOVE) {
  611. setobjs2s(L, ra, RB(i));
  612. vmbreak;
  613. }
  614. vmcase(OP_LOADK) {
  615. TValue *rb = k + GETARG_Bx(i);
  616. setobj2s(L, ra, rb);
  617. vmbreak;
  618. }
  619. vmcase(OP_LOADKX) {
  620. TValue *rb;
  621. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  622. rb = k + GETARG_Ax(*ci->u.l.savedpc++);
  623. setobj2s(L, ra, rb);
  624. vmbreak;
  625. }
  626. vmcase(OP_LOADBOOL) {
  627. setbvalue(ra, GETARG_B(i));
  628. if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
  629. vmbreak;
  630. }
  631. vmcase(OP_LOADNIL) {
  632. int b = GETARG_B(i);
  633. do {
  634. setnilvalue(ra++);
  635. } while (b--);
  636. vmbreak;
  637. }
  638. vmcase(OP_GETUPVAL) {
  639. int b = GETARG_B(i);
  640. setobj2s(L, ra, cl->upvals[b]->v);
  641. vmbreak;
  642. }
  643. vmcase(OP_GETTABUP) {
  644. int b = GETARG_B(i);
  645. Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
  646. vmbreak;
  647. }
  648. vmcase(OP_GETTABLE) {
  649. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  650. vmbreak;
  651. }
  652. vmcase(OP_SETTABUP) {
  653. int a = GETARG_A(i);
  654. Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
  655. vmbreak;
  656. }
  657. vmcase(OP_SETUPVAL) {
  658. UpVal *uv = cl->upvals[GETARG_B(i)];
  659. setobj(L, uv->v, ra);
  660. luaC_upvalbarrier(L, uv);
  661. vmbreak;
  662. }
  663. vmcase(OP_SETTABLE) {
  664. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  665. vmbreak;
  666. }
  667. vmcase(OP_NEWTABLE) {
  668. int b = GETARG_B(i);
  669. int c = GETARG_C(i);
  670. Table *t = luaH_new(L);
  671. sethvalue(L, ra, t);
  672. if (b != 0 || c != 0)
  673. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  674. checkGC(L, ra + 1);
  675. vmbreak;
  676. }
  677. vmcase(OP_SELF) {
  678. StkId rb = RB(i);
  679. setobjs2s(L, ra+1, rb);
  680. Protect(luaV_gettable(L, rb, RKC(i), ra));
  681. vmbreak;
  682. }
  683. vmcase(OP_ADD) {
  684. TValue *rb = RKB(i);
  685. TValue *rc = RKC(i);
  686. lua_Number nb; lua_Number nc;
  687. if (ttisinteger(rb) && ttisinteger(rc)) {
  688. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  689. setivalue(ra, intop(+, ib, ic));
  690. }
  691. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  692. setfltvalue(ra, luai_numadd(L, nb, nc));
  693. }
  694. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
  695. vmbreak;
  696. }
  697. vmcase(OP_SUB) {
  698. TValue *rb = RKB(i);
  699. TValue *rc = RKC(i);
  700. lua_Number nb; lua_Number nc;
  701. if (ttisinteger(rb) && ttisinteger(rc)) {
  702. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  703. setivalue(ra, intop(-, ib, ic));
  704. }
  705. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  706. setfltvalue(ra, luai_numsub(L, nb, nc));
  707. }
  708. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
  709. vmbreak;
  710. }
  711. vmcase(OP_MUL) {
  712. TValue *rb = RKB(i);
  713. TValue *rc = RKC(i);
  714. lua_Number nb; lua_Number nc;
  715. if (ttisinteger(rb) && ttisinteger(rc)) {
  716. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  717. setivalue(ra, intop(*, ib, ic));
  718. }
  719. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  720. setfltvalue(ra, luai_nummul(L, nb, nc));
  721. }
  722. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
  723. vmbreak;
  724. }
  725. vmcase(OP_DIV) { /* float division (always with floats) */
  726. TValue *rb = RKB(i);
  727. TValue *rc = RKC(i);
  728. lua_Number nb; lua_Number nc;
  729. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  730. setfltvalue(ra, luai_numdiv(L, nb, nc));
  731. }
  732. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
  733. vmbreak;
  734. }
  735. vmcase(OP_BAND) {
  736. TValue *rb = RKB(i);
  737. TValue *rc = RKC(i);
  738. lua_Integer ib; lua_Integer ic;
  739. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  740. setivalue(ra, intop(&, ib, ic));
  741. }
  742. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); }
  743. vmbreak;
  744. }
  745. vmcase(OP_BOR) {
  746. TValue *rb = RKB(i);
  747. TValue *rc = RKC(i);
  748. lua_Integer ib; lua_Integer ic;
  749. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  750. setivalue(ra, intop(|, ib, ic));
  751. }
  752. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); }
  753. vmbreak;
  754. }
  755. vmcase(OP_BXOR) {
  756. TValue *rb = RKB(i);
  757. TValue *rc = RKC(i);
  758. lua_Integer ib; lua_Integer ic;
  759. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  760. setivalue(ra, intop(^, ib, ic));
  761. }
  762. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); }
  763. vmbreak;
  764. }
  765. vmcase(OP_SHL) {
  766. TValue *rb = RKB(i);
  767. TValue *rc = RKC(i);
  768. lua_Integer ib; lua_Integer ic;
  769. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  770. setivalue(ra, luaV_shiftl(ib, ic));
  771. }
  772. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); }
  773. vmbreak;
  774. }
  775. vmcase(OP_SHR) {
  776. TValue *rb = RKB(i);
  777. TValue *rc = RKC(i);
  778. lua_Integer ib; lua_Integer ic;
  779. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  780. setivalue(ra, luaV_shiftl(ib, -ic));
  781. }
  782. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); }
  783. vmbreak;
  784. }
  785. vmcase(OP_MOD) {
  786. TValue *rb = RKB(i);
  787. TValue *rc = RKC(i);
  788. lua_Number nb; lua_Number nc;
  789. if (ttisinteger(rb) && ttisinteger(rc)) {
  790. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  791. setivalue(ra, luaV_mod(L, ib, ic));
  792. }
  793. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  794. lua_Number m;
  795. luai_nummod(L, nb, nc, m);
  796. setfltvalue(ra, m);
  797. }
  798. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
  799. vmbreak;
  800. }
  801. vmcase(OP_IDIV) { /* floor division */
  802. TValue *rb = RKB(i);
  803. TValue *rc = RKC(i);
  804. lua_Number nb; lua_Number nc;
  805. if (ttisinteger(rb) && ttisinteger(rc)) {
  806. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  807. setivalue(ra, luaV_div(L, ib, ic));
  808. }
  809. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  810. setfltvalue(ra, luai_numidiv(L, nb, nc));
  811. }
  812. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); }
  813. vmbreak;
  814. }
  815. vmcase(OP_POW) {
  816. TValue *rb = RKB(i);
  817. TValue *rc = RKC(i);
  818. lua_Number nb; lua_Number nc;
  819. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  820. setfltvalue(ra, luai_numpow(L, nb, nc));
  821. }
  822. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
  823. vmbreak;
  824. }
  825. vmcase(OP_UNM) {
  826. TValue *rb = RB(i);
  827. lua_Number nb;
  828. if (ttisinteger(rb)) {
  829. lua_Integer ib = ivalue(rb);
  830. setivalue(ra, intop(-, 0, ib));
  831. }
  832. else if (tonumber(rb, &nb)) {
  833. setfltvalue(ra, luai_numunm(L, nb));
  834. }
  835. else {
  836. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  837. }
  838. vmbreak;
  839. }
  840. vmcase(OP_BNOT) {
  841. TValue *rb = RB(i);
  842. lua_Integer ib;
  843. if (tointeger(rb, &ib)) {
  844. setivalue(ra, intop(^, ~l_castS2U(0), ib));
  845. }
  846. else {
  847. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  848. }
  849. vmbreak;
  850. }
  851. vmcase(OP_NOT) {
  852. TValue *rb = RB(i);
  853. int res = l_isfalse(rb); /* next assignment may change this value */
  854. setbvalue(ra, res);
  855. vmbreak;
  856. }
  857. vmcase(OP_LEN) {
  858. Protect(luaV_objlen(L, ra, RB(i)));
  859. vmbreak;
  860. }
  861. vmcase(OP_CONCAT) {
  862. int b = GETARG_B(i);
  863. int c = GETARG_C(i);
  864. StkId rb;
  865. L->top = base + c + 1; /* mark the end of concat operands */
  866. Protect(luaV_concat(L, c - b + 1));
  867. ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
  868. rb = b + base;
  869. setobjs2s(L, ra, rb);
  870. checkGC(L, (ra >= rb ? ra + 1 : rb));
  871. L->top = ci->top; /* restore top */
  872. vmbreak;
  873. }
  874. vmcase(OP_JMP) {
  875. dojump(ci, i, 0);
  876. vmbreak;
  877. }
  878. vmcase(OP_EQ) {
  879. TValue *rb = RKB(i);
  880. TValue *rc = RKC(i);
  881. Protect(
  882. if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i))
  883. ci->u.l.savedpc++;
  884. else
  885. donextjump(ci);
  886. )
  887. vmbreak;
  888. }
  889. vmcase(OP_LT) {
  890. Protect(
  891. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
  892. ci->u.l.savedpc++;
  893. else
  894. donextjump(ci);
  895. )
  896. vmbreak;
  897. }
  898. vmcase(OP_LE) {
  899. Protect(
  900. if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
  901. ci->u.l.savedpc++;
  902. else
  903. donextjump(ci);
  904. )
  905. vmbreak;
  906. }
  907. vmcase(OP_TEST) {
  908. if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
  909. ci->u.l.savedpc++;
  910. else
  911. donextjump(ci);
  912. vmbreak;
  913. }
  914. vmcase(OP_TESTSET) {
  915. TValue *rb = RB(i);
  916. if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
  917. ci->u.l.savedpc++;
  918. else {
  919. setobjs2s(L, ra, rb);
  920. donextjump(ci);
  921. }
  922. vmbreak;
  923. }
  924. vmcase(OP_CALL) {
  925. int b = GETARG_B(i);
  926. int nresults = GETARG_C(i) - 1;
  927. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  928. if (luaD_precall(L, ra, nresults)) { /* C function? */
  929. if (nresults >= 0) L->top = ci->top; /* adjust results */
  930. base = ci->u.l.base;
  931. }
  932. else { /* Lua function */
  933. ci = L->ci;
  934. ci->callstatus |= CIST_REENTRY;
  935. goto newframe; /* restart luaV_execute over new Lua function */
  936. }
  937. vmbreak;
  938. }
  939. vmcase(OP_TAILCALL) {
  940. int b = GETARG_B(i);
  941. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  942. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  943. if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */
  944. base = ci->u.l.base;
  945. else {
  946. /* tail call: put called frame (n) in place of caller one (o) */
  947. CallInfo *nci = L->ci; /* called frame */
  948. CallInfo *oci = nci->previous; /* caller frame */
  949. StkId nfunc = nci->func; /* called function */
  950. StkId ofunc = oci->func; /* caller function */
  951. /* last stack slot filled by 'precall' */
  952. StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
  953. int aux;
  954. /* close all upvalues from previous call */
  955. if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
  956. /* move new frame into old one */
  957. for (aux = 0; nfunc + aux < lim; aux++)
  958. setobjs2s(L, ofunc + aux, nfunc + aux);
  959. oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */
  960. oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
  961. oci->u.l.savedpc = nci->u.l.savedpc;
  962. oci->callstatus |= CIST_TAIL; /* function was tail called */
  963. ci = L->ci = oci; /* remove new frame */
  964. lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
  965. goto newframe; /* restart luaV_execute over new Lua function */
  966. }
  967. vmbreak;
  968. }
  969. vmcase(OP_RETURN) {
  970. int b = GETARG_B(i);
  971. if (b != 0) L->top = ra+b-1;
  972. if (cl->p->sizep > 0) luaF_close(L, base);
  973. b = luaD_poscall(L, ra);
  974. if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */
  975. return; /* external invocation: return */
  976. else { /* invocation via reentry: continue execution */
  977. ci = L->ci;
  978. if (b) L->top = ci->top;
  979. lua_assert(isLua(ci));
  980. lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
  981. goto newframe; /* restart luaV_execute over new Lua function */
  982. }
  983. }
  984. vmcase(OP_FORLOOP) {
  985. if (ttisinteger(ra)) { /* integer loop? */
  986. lua_Integer step = ivalue(ra + 2);
  987. lua_Integer idx = ivalue(ra) + step; /* increment index */
  988. lua_Integer limit = ivalue(ra + 1);
  989. if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
  990. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  991. setivalue(ra, idx); /* update internal index... */
  992. setivalue(ra + 3, idx); /* ...and external index */
  993. }
  994. }
  995. else { /* floating loop */
  996. lua_Number step = fltvalue(ra + 2);
  997. lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */
  998. lua_Number limit = fltvalue(ra + 1);
  999. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  1000. : luai_numle(limit, idx)) {
  1001. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  1002. setfltvalue(ra, idx); /* update internal index... */
  1003. setfltvalue(ra + 3, idx); /* ...and external index */
  1004. }
  1005. }
  1006. vmbreak;
  1007. }
  1008. vmcase(OP_FORPREP) {
  1009. TValue *init = ra;
  1010. TValue *plimit = ra + 1;
  1011. TValue *pstep = ra + 2;
  1012. lua_Integer ilimit;
  1013. int stopnow;
  1014. if (ttisinteger(init) && ttisinteger(pstep) &&
  1015. forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
  1016. /* all values are integer */
  1017. lua_Integer initv = (stopnow ? 0 : ivalue(init));
  1018. setivalue(plimit, ilimit);
  1019. setivalue(init, initv - ivalue(pstep));
  1020. }
  1021. else { /* try making all values floats */
  1022. lua_Number ninit; lua_Number nlimit; lua_Number nstep;
  1023. if (!tonumber(plimit, &nlimit))
  1024. luaG_runerror(L, "'for' limit must be a number");
  1025. setfltvalue(plimit, nlimit);
  1026. if (!tonumber(pstep, &nstep))
  1027. luaG_runerror(L, "'for' step must be a number");
  1028. setfltvalue(pstep, nstep);
  1029. if (!tonumber(init, &ninit))
  1030. luaG_runerror(L, "'for' initial value must be a number");
  1031. setfltvalue(init, luai_numsub(L, ninit, nstep));
  1032. }
  1033. ci->u.l.savedpc += GETARG_sBx(i);
  1034. vmbreak;
  1035. }
  1036. vmcase(OP_TFORCALL) {
  1037. StkId cb = ra + 3; /* call base */
  1038. setobjs2s(L, cb+2, ra+2);
  1039. setobjs2s(L, cb+1, ra+1);
  1040. setobjs2s(L, cb, ra);
  1041. L->top = cb + 3; /* func. + 2 args (state and index) */
  1042. Protect(luaD_call(L, cb, GETARG_C(i), 1));
  1043. L->top = ci->top;
  1044. i = *(ci->u.l.savedpc++); /* go to next instruction */
  1045. ra = RA(i);
  1046. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  1047. goto l_tforloop;
  1048. }
  1049. vmcase(OP_TFORLOOP) {
  1050. l_tforloop:
  1051. if (!ttisnil(ra + 1)) { /* continue loop? */
  1052. setobjs2s(L, ra, ra + 1); /* save control variable */
  1053. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  1054. }
  1055. vmbreak;
  1056. }
  1057. vmcase(OP_SETLIST) {
  1058. int n = GETARG_B(i);
  1059. int c = GETARG_C(i);
  1060. unsigned int last;
  1061. Table *h;
  1062. if (n == 0) n = cast_int(L->top - ra) - 1;
  1063. if (c == 0) {
  1064. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  1065. c = GETARG_Ax(*ci->u.l.savedpc++);
  1066. }
  1067. luai_runtimecheck(L, ttistable(ra));
  1068. h = hvalue(ra);
  1069. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  1070. if (last > h->sizearray) /* needs more space? */
  1071. luaH_resizearray(L, h, last); /* pre-allocate it at once */
  1072. for (; n > 0; n--) {
  1073. TValue *val = ra+n;
  1074. luaH_setint(L, h, last--, val);
  1075. luaC_barrierback(L, h, val);
  1076. }
  1077. L->top = ci->top; /* correct top (in case of previous open call) */
  1078. vmbreak;
  1079. }
  1080. vmcase(OP_CLOSURE) {
  1081. Proto *p = cl->p->p[GETARG_Bx(i)];
  1082. LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  1083. if (ncl == NULL) /* no match? */
  1084. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  1085. else
  1086. setclLvalue(L, ra, ncl); /* push cashed closure */
  1087. checkGC(L, ra + 1);
  1088. vmbreak;
  1089. }
  1090. vmcase(OP_VARARG) {
  1091. int b = GETARG_B(i) - 1;
  1092. int j;
  1093. int n = cast_int(base - ci->func) - cl->p->numparams - 1;
  1094. if (b < 0) { /* B == 0? */
  1095. b = n; /* get all var. arguments */
  1096. Protect(luaD_checkstack(L, n));
  1097. ra = RA(i); /* previous call may change the stack */
  1098. L->top = ra + n;
  1099. }
  1100. for (j = 0; j < b; j++) {
  1101. if (j < n) {
  1102. setobjs2s(L, ra + j, base - n + j);
  1103. }
  1104. else {
  1105. setnilvalue(ra + j);
  1106. }
  1107. }
  1108. vmbreak;
  1109. }
  1110. vmcase(OP_EXTRAARG) {
  1111. lua_assert(0);
  1112. vmbreak;
  1113. }
  1114. }
  1115. }
  1116. }
  1117. /* }================================================================== */