lvm.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /*
  2. ** $Id: lvm.c,v 2.264 2015/11/19 19:16:22 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. ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */
  682. newframe: /* reentry point when frame changes (call/return) */
  683. lua_assert(ci == L->ci);
  684. cl = clLvalue(ci->func); /* local reference to function's closure */
  685. k = cl->p->k; /* local reference to function's constant table */
  686. base = ci->u.l.base; /* local copy of function's base */
  687. /* main loop of interpreter */
  688. for (;;) {
  689. Instruction i = *(ci->u.l.savedpc++);
  690. StkId ra;
  691. if (L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT))
  692. Protect(luaG_traceexec(L));
  693. /* WARNING: several calls may realloc the stack and invalidate 'ra' */
  694. ra = RA(i);
  695. lua_assert(base == ci->u.l.base);
  696. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  697. vmdispatch (GET_OPCODE(i)) {
  698. vmcase(OP_MOVE) {
  699. setobjs2s(L, ra, RB(i));
  700. vmbreak;
  701. }
  702. vmcase(OP_LOADK) {
  703. TValue *rb = k + GETARG_Bx(i);
  704. setobj2s(L, ra, rb);
  705. vmbreak;
  706. }
  707. vmcase(OP_LOADKX) {
  708. TValue *rb;
  709. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  710. rb = k + GETARG_Ax(*ci->u.l.savedpc++);
  711. setobj2s(L, ra, rb);
  712. vmbreak;
  713. }
  714. vmcase(OP_LOADBOOL) {
  715. setbvalue(ra, GETARG_B(i));
  716. if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
  717. vmbreak;
  718. }
  719. vmcase(OP_LOADNIL) {
  720. int b = GETARG_B(i);
  721. do {
  722. setnilvalue(ra++);
  723. } while (b--);
  724. vmbreak;
  725. }
  726. vmcase(OP_GETUPVAL) {
  727. int b = GETARG_B(i);
  728. setobj2s(L, ra, cl->upvals[b]->v);
  729. vmbreak;
  730. }
  731. vmcase(OP_GETTABUP) {
  732. TValue *upval = cl->upvals[GETARG_B(i)]->v;
  733. TValue *rc = RKC(i);
  734. gettableProtected(L, upval, rc, ra);
  735. vmbreak;
  736. }
  737. vmcase(OP_GETTABLE) {
  738. StkId rb = RB(i);
  739. TValue *rc = RKC(i);
  740. gettableProtected(L, rb, rc, ra);
  741. vmbreak;
  742. }
  743. vmcase(OP_SETTABUP) {
  744. TValue *upval = cl->upvals[GETARG_A(i)]->v;
  745. TValue *rb = RKB(i);
  746. TValue *rc = RKC(i);
  747. settableProtected(L, upval, rb, rc);
  748. vmbreak;
  749. }
  750. vmcase(OP_SETUPVAL) {
  751. UpVal *uv = cl->upvals[GETARG_B(i)];
  752. setobj(L, uv->v, ra);
  753. luaC_upvalbarrier(L, uv);
  754. vmbreak;
  755. }
  756. vmcase(OP_SETTABLE) {
  757. TValue *rb = RKB(i);
  758. TValue *rc = RKC(i);
  759. settableProtected(L, ra, rb, rc);
  760. vmbreak;
  761. }
  762. vmcase(OP_NEWTABLE) {
  763. int b = GETARG_B(i);
  764. int c = GETARG_C(i);
  765. Table *t = luaH_new(L);
  766. sethvalue(L, ra, t);
  767. if (b != 0 || c != 0)
  768. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  769. checkGC(L, ra + 1);
  770. vmbreak;
  771. }
  772. vmcase(OP_SELF) {
  773. const TValue *aux;
  774. StkId rb = RB(i);
  775. TValue *rc = RKC(i);
  776. TString *key = tsvalue(rc); /* key must be a string */
  777. setobjs2s(L, ra + 1, rb);
  778. if (luaV_fastget(L, rb, key, aux, luaH_getstr)) {
  779. setobj2s(L, ra, aux);
  780. }
  781. else Protect(luaV_finishget(L, rb, rc, ra, aux));
  782. vmbreak;
  783. }
  784. vmcase(OP_ADD) {
  785. TValue *rb = RKB(i);
  786. TValue *rc = RKC(i);
  787. lua_Number nb; lua_Number nc;
  788. if (ttisinteger(rb) && ttisinteger(rc)) {
  789. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  790. setivalue(ra, intop(+, ib, ic));
  791. }
  792. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  793. setfltvalue(ra, luai_numadd(L, nb, nc));
  794. }
  795. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
  796. vmbreak;
  797. }
  798. vmcase(OP_SUB) {
  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, intop(-, ib, ic));
  805. }
  806. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  807. setfltvalue(ra, luai_numsub(L, nb, nc));
  808. }
  809. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
  810. vmbreak;
  811. }
  812. vmcase(OP_MUL) {
  813. TValue *rb = RKB(i);
  814. TValue *rc = RKC(i);
  815. lua_Number nb; lua_Number nc;
  816. if (ttisinteger(rb) && ttisinteger(rc)) {
  817. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  818. setivalue(ra, intop(*, ib, ic));
  819. }
  820. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  821. setfltvalue(ra, luai_nummul(L, nb, nc));
  822. }
  823. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
  824. vmbreak;
  825. }
  826. vmcase(OP_DIV) { /* float division (always with floats) */
  827. TValue *rb = RKB(i);
  828. TValue *rc = RKC(i);
  829. lua_Number nb; lua_Number nc;
  830. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  831. setfltvalue(ra, luai_numdiv(L, nb, nc));
  832. }
  833. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
  834. vmbreak;
  835. }
  836. vmcase(OP_BAND) {
  837. TValue *rb = RKB(i);
  838. TValue *rc = RKC(i);
  839. lua_Integer ib; lua_Integer ic;
  840. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  841. setivalue(ra, intop(&, ib, ic));
  842. }
  843. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); }
  844. vmbreak;
  845. }
  846. vmcase(OP_BOR) {
  847. TValue *rb = RKB(i);
  848. TValue *rc = RKC(i);
  849. lua_Integer ib; lua_Integer ic;
  850. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  851. setivalue(ra, intop(|, ib, ic));
  852. }
  853. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); }
  854. vmbreak;
  855. }
  856. vmcase(OP_BXOR) {
  857. TValue *rb = RKB(i);
  858. TValue *rc = RKC(i);
  859. lua_Integer ib; lua_Integer ic;
  860. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  861. setivalue(ra, intop(^, ib, ic));
  862. }
  863. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); }
  864. vmbreak;
  865. }
  866. vmcase(OP_SHL) {
  867. TValue *rb = RKB(i);
  868. TValue *rc = RKC(i);
  869. lua_Integer ib; lua_Integer ic;
  870. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  871. setivalue(ra, luaV_shiftl(ib, ic));
  872. }
  873. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); }
  874. vmbreak;
  875. }
  876. vmcase(OP_SHR) {
  877. TValue *rb = RKB(i);
  878. TValue *rc = RKC(i);
  879. lua_Integer ib; lua_Integer ic;
  880. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  881. setivalue(ra, luaV_shiftl(ib, -ic));
  882. }
  883. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); }
  884. vmbreak;
  885. }
  886. vmcase(OP_MOD) {
  887. TValue *rb = RKB(i);
  888. TValue *rc = RKC(i);
  889. lua_Number nb; lua_Number nc;
  890. if (ttisinteger(rb) && ttisinteger(rc)) {
  891. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  892. setivalue(ra, luaV_mod(L, ib, ic));
  893. }
  894. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  895. lua_Number m;
  896. luai_nummod(L, nb, nc, m);
  897. setfltvalue(ra, m);
  898. }
  899. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
  900. vmbreak;
  901. }
  902. vmcase(OP_IDIV) { /* floor division */
  903. TValue *rb = RKB(i);
  904. TValue *rc = RKC(i);
  905. lua_Number nb; lua_Number nc;
  906. if (ttisinteger(rb) && ttisinteger(rc)) {
  907. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  908. setivalue(ra, luaV_div(L, ib, ic));
  909. }
  910. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  911. setfltvalue(ra, luai_numidiv(L, nb, nc));
  912. }
  913. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); }
  914. vmbreak;
  915. }
  916. vmcase(OP_POW) {
  917. TValue *rb = RKB(i);
  918. TValue *rc = RKC(i);
  919. lua_Number nb; lua_Number nc;
  920. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  921. setfltvalue(ra, luai_numpow(L, nb, nc));
  922. }
  923. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
  924. vmbreak;
  925. }
  926. vmcase(OP_UNM) {
  927. TValue *rb = RB(i);
  928. lua_Number nb;
  929. if (ttisinteger(rb)) {
  930. lua_Integer ib = ivalue(rb);
  931. setivalue(ra, intop(-, 0, ib));
  932. }
  933. else if (tonumber(rb, &nb)) {
  934. setfltvalue(ra, luai_numunm(L, nb));
  935. }
  936. else {
  937. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  938. }
  939. vmbreak;
  940. }
  941. vmcase(OP_BNOT) {
  942. TValue *rb = RB(i);
  943. lua_Integer ib;
  944. if (tointeger(rb, &ib)) {
  945. setivalue(ra, intop(^, ~l_castS2U(0), ib));
  946. }
  947. else {
  948. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  949. }
  950. vmbreak;
  951. }
  952. vmcase(OP_NOT) {
  953. TValue *rb = RB(i);
  954. int res = l_isfalse(rb); /* next assignment may change this value */
  955. setbvalue(ra, res);
  956. vmbreak;
  957. }
  958. vmcase(OP_LEN) {
  959. Protect(luaV_objlen(L, ra, RB(i)));
  960. vmbreak;
  961. }
  962. vmcase(OP_CONCAT) {
  963. int b = GETARG_B(i);
  964. int c = GETARG_C(i);
  965. StkId rb;
  966. L->top = base + c + 1; /* mark the end of concat operands */
  967. Protect(luaV_concat(L, c - b + 1));
  968. ra = RA(i); /* 'luaV_concat' may invoke TMs and move the stack */
  969. rb = base + b;
  970. setobjs2s(L, ra, rb);
  971. checkGC(L, (ra >= rb ? ra + 1 : rb));
  972. L->top = ci->top; /* restore top */
  973. vmbreak;
  974. }
  975. vmcase(OP_JMP) {
  976. dojump(ci, i, 0);
  977. vmbreak;
  978. }
  979. vmcase(OP_EQ) {
  980. TValue *rb = RKB(i);
  981. TValue *rc = RKC(i);
  982. Protect(
  983. if (luaV_equalobj(L, rb, rc) != GETARG_A(i))
  984. ci->u.l.savedpc++;
  985. else
  986. donextjump(ci);
  987. )
  988. vmbreak;
  989. }
  990. vmcase(OP_LT) {
  991. Protect(
  992. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
  993. ci->u.l.savedpc++;
  994. else
  995. donextjump(ci);
  996. )
  997. vmbreak;
  998. }
  999. vmcase(OP_LE) {
  1000. Protect(
  1001. if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
  1002. ci->u.l.savedpc++;
  1003. else
  1004. donextjump(ci);
  1005. )
  1006. vmbreak;
  1007. }
  1008. vmcase(OP_TEST) {
  1009. if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
  1010. ci->u.l.savedpc++;
  1011. else
  1012. donextjump(ci);
  1013. vmbreak;
  1014. }
  1015. vmcase(OP_TESTSET) {
  1016. TValue *rb = RB(i);
  1017. if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
  1018. ci->u.l.savedpc++;
  1019. else {
  1020. setobjs2s(L, ra, rb);
  1021. donextjump(ci);
  1022. }
  1023. vmbreak;
  1024. }
  1025. vmcase(OP_CALL) {
  1026. int b = GETARG_B(i);
  1027. int nresults = GETARG_C(i) - 1;
  1028. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  1029. if (luaD_precall(L, ra, nresults)) { /* C function? */
  1030. if (nresults >= 0)
  1031. L->top = ci->top; /* adjust results */
  1032. Protect((void)0); /* update 'base' */
  1033. }
  1034. else { /* Lua function */
  1035. ci = L->ci;
  1036. goto newframe; /* restart luaV_execute over new Lua function */
  1037. }
  1038. vmbreak;
  1039. }
  1040. vmcase(OP_TAILCALL) {
  1041. int b = GETARG_B(i);
  1042. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  1043. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  1044. if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */
  1045. Protect((void)0); /* update 'base' */
  1046. }
  1047. else {
  1048. /* tail call: put called frame (n) in place of caller one (o) */
  1049. CallInfo *nci = L->ci; /* called frame */
  1050. CallInfo *oci = nci->previous; /* caller frame */
  1051. StkId nfunc = nci->func; /* called function */
  1052. StkId ofunc = oci->func; /* caller function */
  1053. /* last stack slot filled by 'precall' */
  1054. StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
  1055. int aux;
  1056. /* close all upvalues from previous call */
  1057. if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
  1058. /* move new frame into old one */
  1059. for (aux = 0; nfunc + aux < lim; aux++)
  1060. setobjs2s(L, ofunc + aux, nfunc + aux);
  1061. oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */
  1062. oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
  1063. oci->u.l.savedpc = nci->u.l.savedpc;
  1064. oci->callstatus |= CIST_TAIL; /* function was tail called */
  1065. ci = L->ci = oci; /* remove new frame */
  1066. lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
  1067. goto newframe; /* restart luaV_execute over new Lua function */
  1068. }
  1069. vmbreak;
  1070. }
  1071. vmcase(OP_RETURN) {
  1072. int b = GETARG_B(i);
  1073. if (cl->p->sizep > 0) luaF_close(L, base);
  1074. b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra)));
  1075. if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */
  1076. return; /* external invocation: return */
  1077. else { /* invocation via reentry: continue execution */
  1078. ci = L->ci;
  1079. if (b) L->top = ci->top;
  1080. lua_assert(isLua(ci));
  1081. lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
  1082. goto newframe; /* restart luaV_execute over new Lua function */
  1083. }
  1084. }
  1085. vmcase(OP_FORLOOP) {
  1086. if (ttisinteger(ra)) { /* integer loop? */
  1087. lua_Integer step = ivalue(ra + 2);
  1088. lua_Integer idx = intop(+, ivalue(ra), step); /* increment index */
  1089. lua_Integer limit = ivalue(ra + 1);
  1090. if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
  1091. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  1092. chgivalue(ra, idx); /* update internal index... */
  1093. setivalue(ra + 3, idx); /* ...and external index */
  1094. }
  1095. }
  1096. else { /* floating loop */
  1097. lua_Number step = fltvalue(ra + 2);
  1098. lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */
  1099. lua_Number limit = fltvalue(ra + 1);
  1100. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  1101. : luai_numle(limit, idx)) {
  1102. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  1103. chgfltvalue(ra, idx); /* update internal index... */
  1104. setfltvalue(ra + 3, idx); /* ...and external index */
  1105. }
  1106. }
  1107. vmbreak;
  1108. }
  1109. vmcase(OP_FORPREP) {
  1110. TValue *init = ra;
  1111. TValue *plimit = ra + 1;
  1112. TValue *pstep = ra + 2;
  1113. lua_Integer ilimit;
  1114. int stopnow;
  1115. if (ttisinteger(init) && ttisinteger(pstep) &&
  1116. forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
  1117. /* all values are integer */
  1118. lua_Integer initv = (stopnow ? 0 : ivalue(init));
  1119. setivalue(plimit, ilimit);
  1120. setivalue(init, intop(-, initv, ivalue(pstep)));
  1121. }
  1122. else { /* try making all values floats */
  1123. lua_Number ninit; lua_Number nlimit; lua_Number nstep;
  1124. if (!tonumber(plimit, &nlimit))
  1125. luaG_runerror(L, "'for' limit must be a number");
  1126. setfltvalue(plimit, nlimit);
  1127. if (!tonumber(pstep, &nstep))
  1128. luaG_runerror(L, "'for' step must be a number");
  1129. setfltvalue(pstep, nstep);
  1130. if (!tonumber(init, &ninit))
  1131. luaG_runerror(L, "'for' initial value must be a number");
  1132. setfltvalue(init, luai_numsub(L, ninit, nstep));
  1133. }
  1134. ci->u.l.savedpc += GETARG_sBx(i);
  1135. vmbreak;
  1136. }
  1137. vmcase(OP_TFORCALL) {
  1138. StkId cb = ra + 3; /* call base */
  1139. setobjs2s(L, cb+2, ra+2);
  1140. setobjs2s(L, cb+1, ra+1);
  1141. setobjs2s(L, cb, ra);
  1142. L->top = cb + 3; /* func. + 2 args (state and index) */
  1143. Protect(luaD_call(L, cb, GETARG_C(i)));
  1144. L->top = ci->top;
  1145. i = *(ci->u.l.savedpc++); /* go to next instruction */
  1146. ra = RA(i);
  1147. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  1148. goto l_tforloop;
  1149. }
  1150. vmcase(OP_TFORLOOP) {
  1151. l_tforloop:
  1152. if (!ttisnil(ra + 1)) { /* continue loop? */
  1153. setobjs2s(L, ra, ra + 1); /* save control variable */
  1154. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  1155. }
  1156. vmbreak;
  1157. }
  1158. vmcase(OP_SETLIST) {
  1159. int n = GETARG_B(i);
  1160. int c = GETARG_C(i);
  1161. unsigned int last;
  1162. Table *h;
  1163. if (n == 0) n = cast_int(L->top - ra) - 1;
  1164. if (c == 0) {
  1165. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  1166. c = GETARG_Ax(*ci->u.l.savedpc++);
  1167. }
  1168. h = hvalue(ra);
  1169. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  1170. if (last > h->sizearray) /* needs more space? */
  1171. luaH_resizearray(L, h, last); /* preallocate it at once */
  1172. for (; n > 0; n--) {
  1173. TValue *val = ra+n;
  1174. luaH_setint(L, h, last--, val);
  1175. luaC_barrierback(L, h, val);
  1176. }
  1177. L->top = ci->top; /* correct top (in case of previous open call) */
  1178. vmbreak;
  1179. }
  1180. vmcase(OP_CLOSURE) {
  1181. Proto *p = cl->p->p[GETARG_Bx(i)];
  1182. LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  1183. if (ncl == NULL) /* no match? */
  1184. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  1185. else
  1186. setclLvalue(L, ra, ncl); /* push cashed closure */
  1187. checkGC(L, ra + 1);
  1188. vmbreak;
  1189. }
  1190. vmcase(OP_VARARG) {
  1191. int b = GETARG_B(i) - 1; /* required results */
  1192. int j;
  1193. int n = cast_int(base - ci->func) - cl->p->numparams - 1;
  1194. if (n < 0) /* less arguments than parameters? */
  1195. n = 0; /* no vararg arguments */
  1196. if (b < 0) { /* B == 0? */
  1197. b = n; /* get all var. arguments */
  1198. Protect(luaD_checkstack(L, n));
  1199. ra = RA(i); /* previous call may change the stack */
  1200. L->top = ra + n;
  1201. }
  1202. for (j = 0; j < b && j < n; j++)
  1203. setobjs2s(L, ra + j, base - n + j);
  1204. for (; j < b; j++) /* complete required results with nil */
  1205. setnilvalue(ra + j);
  1206. vmbreak;
  1207. }
  1208. vmcase(OP_EXTRAARG) {
  1209. lua_assert(0);
  1210. vmbreak;
  1211. }
  1212. }
  1213. }
  1214. }
  1215. /* }================================================================== */