lvm.c 38 KB

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