lvm.c 38 KB

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