lvm.c 39 KB

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