2
0

lvm.c 38 KB

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