lvm.c 37 KB

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