lvm.c 42 KB

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