lvm.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /*
  2. ** $Id: lvm.c,v 2.243 2015/05/22 17:48:19 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. ** Main function for table access (invoking metamethods if needed).
  133. ** Compute 'val = t[key]'
  134. */
  135. void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  136. int loop; /* counter to avoid infinite loops */
  137. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  138. const TValue *tm;
  139. if (ttistable(t)) { /* 't' is a table? */
  140. Table *h = hvalue(t);
  141. const TValue *res = luaH_get(h, key); /* do a primitive get */
  142. if (!ttisnil(res) || /* result is not nil? */
  143. (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
  144. setobj2s(L, val, res); /* result is the raw get */
  145. return;
  146. }
  147. /* else will try metamethod */
  148. }
  149. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
  150. luaG_typeerror(L, t, "index"); /* no metamethod */
  151. if (ttisfunction(tm)) { /* metamethod is a function */
  152. luaT_callTM(L, tm, t, key, val, 1);
  153. return;
  154. }
  155. t = tm; /* else repeat access over 'tm' */
  156. }
  157. luaG_runerror(L, "gettable chain too long; possible loop");
  158. }
  159. /*
  160. ** Main function for table assignment (invoking metamethods if needed).
  161. ** Compute 't[key] = val'
  162. */
  163. void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  164. int loop; /* counter to avoid infinite loops */
  165. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  166. const TValue *tm;
  167. if (ttistable(t)) { /* 't' is a table? */
  168. Table *h = hvalue(t);
  169. TValue *oldval = cast(TValue *, luaH_get(h, key));
  170. /* if previous value is not nil, there must be a previous entry
  171. in the table; a metamethod has no relevance */
  172. if (!ttisnil(oldval) ||
  173. /* previous value is nil; must check the metamethod */
  174. ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
  175. /* no metamethod; is there a previous entry in the table? */
  176. (oldval != luaO_nilobject ||
  177. /* no previous entry; must create one. (The next test is
  178. always true; we only need the assignment.) */
  179. (oldval = luaH_newkey(L, h, key), 1)))) {
  180. /* no metamethod and (now) there is an entry with given key */
  181. setobj2t(L, oldval, val); /* assign new value to that entry */
  182. invalidateTMcache(h);
  183. luaC_barrierback(L, h, val);
  184. return;
  185. }
  186. /* else will try the metamethod */
  187. }
  188. else /* not a table; check metamethod */
  189. if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
  190. luaG_typeerror(L, t, "index");
  191. /* try the metamethod */
  192. if (ttisfunction(tm)) {
  193. luaT_callTM(L, tm, t, key, val, 0);
  194. return;
  195. }
  196. t = tm; /* else repeat assignment over 'tm' */
  197. }
  198. luaG_runerror(L, "settable chain too long; possible loop");
  199. }
  200. /*
  201. ** Compare two strings 'ls' x 'rs', returning an integer smaller-equal-
  202. ** -larger than zero if 'ls' is smaller-equal-larger than 'rs'.
  203. ** The code is a little tricky because it allows '\0' in the strings
  204. ** and it uses 'strcoll' (to respect locales) for each segments
  205. ** of the strings.
  206. */
  207. static int l_strcmp (const TString *ls, const TString *rs) {
  208. const char *l = getstr(ls);
  209. size_t ll = tsslen(ls);
  210. const char *r = getstr(rs);
  211. size_t lr = tsslen(rs);
  212. for (;;) { /* for each segment */
  213. int temp = strcoll(l, r);
  214. if (temp != 0) /* not equal? */
  215. return temp; /* done */
  216. else { /* strings are equal up to a '\0' */
  217. size_t len = strlen(l); /* index of first '\0' in both strings */
  218. if (len == lr) /* 'rs' is finished? */
  219. return (len == ll) ? 0 : 1; /* check 'ls' */
  220. else if (len == ll) /* 'ls' is finished? */
  221. return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */
  222. /* both strings longer than 'len'; go on comparing after the '\0' */
  223. len++;
  224. l += len; ll -= len; r += len; lr -= len;
  225. }
  226. }
  227. }
  228. /*
  229. ** Check whether integer 'i' is less than float 'f'. If 'i' has an
  230. ** exact representation as a float ('l_intfitsf'), compare numbers as
  231. ** floats. Otherwise, if 'f' is outside the range for integers, result
  232. ** is trivial. Otherwise, compare them as integers. (When 'i' has no
  233. ** float representation, either 'f' is "far away" from 'i' or 'f' has
  234. ** no precision left for a fractional part; either way, how 'f' is
  235. ** truncated is irrelevant.)
  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)) /* f <= minint */
  243. return 0; /* f <= minint <= i --> not(i < f) */
  244. else /* minint < f <= maxint */
  245. return (i < cast(lua_Integer, f)); /* compare them as integers */
  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)) /* f < minint */
  260. return 0; /* f < minint <= i --> not(i <= f) */
  261. else /* minint <= f <= maxint */
  262. return (i <= cast(lua_Integer, f)); /* compare them as integers */
  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. /*
  400. ** Main operation for concatenation: concat 'total' values in the stack,
  401. ** from 'L->top - total' up to 'L->top - 1'.
  402. */
  403. void luaV_concat (lua_State *L, int total) {
  404. lua_assert(total >= 2);
  405. do {
  406. StkId top = L->top;
  407. int n = 2; /* number of elements handled in this pass (at least 2) */
  408. if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1))
  409. luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT);
  410. else if (isemptystr(top - 1)) /* second operand is empty? */
  411. cast_void(tostring(L, top - 2)); /* result is first operand */
  412. else if (isemptystr(top - 2)) { /* first operand is an empty string? */
  413. setobjs2s(L, top - 2, top - 1); /* result is second op. */
  414. }
  415. else {
  416. /* at least two non-empty string values; get as many as possible */
  417. size_t tl = vslen(top - 1);
  418. char *buffer;
  419. int i;
  420. /* collect total length */
  421. for (i = 1; i < total && tostring(L, top-i-1); i++) {
  422. size_t l = vslen(top - i - 1);
  423. if (l >= (MAX_SIZE/sizeof(char)) - tl)
  424. luaG_runerror(L, "string length overflow");
  425. tl += l;
  426. }
  427. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  428. tl = 0;
  429. n = i;
  430. do { /* copy all strings to buffer */
  431. size_t l = vslen(top - i);
  432. memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
  433. tl += l;
  434. } while (--i > 0);
  435. setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); /* create result */
  436. }
  437. total -= n-1; /* got 'n' strings to create 1 new */
  438. L->top -= n-1; /* popped 'n' strings and pushed one */
  439. } while (total > 1); /* repeat until only 1 result left */
  440. }
  441. /*
  442. ** Main operation 'ra' = #rb'.
  443. */
  444. void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
  445. const TValue *tm;
  446. switch (ttype(rb)) {
  447. case LUA_TTABLE: {
  448. Table *h = hvalue(rb);
  449. tm = fasttm(L, h->metatable, TM_LEN);
  450. if (tm) break; /* metamethod? break switch to call it */
  451. setivalue(ra, luaH_getn(h)); /* else primitive len */
  452. return;
  453. }
  454. case LUA_TSHRSTR: {
  455. setivalue(ra, tsvalue(rb)->shrlen);
  456. return;
  457. }
  458. case LUA_TLNGSTR: {
  459. setivalue(ra, tsvalue(rb)->u.lnglen);
  460. return;
  461. }
  462. default: { /* try metamethod */
  463. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  464. if (ttisnil(tm)) /* no metamethod? */
  465. luaG_typeerror(L, rb, "get length of");
  466. break;
  467. }
  468. }
  469. luaT_callTM(L, tm, rb, rb, ra, 1);
  470. }
  471. /*
  472. ** Integer division; return 'm // n', that is, floor(m/n).
  473. ** C division truncates its result (rounds towards zero).
  474. ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
  475. ** otherwise 'floor(q) == trunc(q) - 1'.
  476. */
  477. lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) {
  478. if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */
  479. if (n == 0)
  480. luaG_runerror(L, "attempt to divide by zero");
  481. return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
  482. }
  483. else {
  484. lua_Integer q = m / n; /* perform C division */
  485. if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */
  486. q -= 1; /* correct result for different rounding */
  487. return q;
  488. }
  489. }
  490. /*
  491. ** Integer modulus; return 'm % n'. (Assume that C '%' with
  492. ** negative operands follows C99 behavior. See previous comment
  493. ** about luaV_div.)
  494. */
  495. lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
  496. if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */
  497. if (n == 0)
  498. luaG_runerror(L, "attempt to perform 'n%%0'");
  499. return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
  500. }
  501. else {
  502. lua_Integer r = m % n;
  503. if (r != 0 && (m ^ n) < 0) /* 'm/n' would be non-integer negative? */
  504. r += n; /* correct result for different rounding */
  505. return r;
  506. }
  507. }
  508. /* number of bits in an integer */
  509. #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
  510. /*
  511. ** Shift left operation. (Shift right just negates 'y'.)
  512. */
  513. lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
  514. if (y < 0) { /* shift right? */
  515. if (y <= -NBITS) return 0;
  516. else return intop(>>, x, -y);
  517. }
  518. else { /* shift left */
  519. if (y >= NBITS) return 0;
  520. else return intop(<<, x, y);
  521. }
  522. }
  523. /*
  524. ** check whether cached closure in prototype 'p' may be reused, that is,
  525. ** whether there is a cached closure with the same upvalues needed by
  526. ** new closure to be created.
  527. */
  528. static LClosure *getcached (Proto *p, UpVal **encup, StkId base) {
  529. LClosure *c = p->cache;
  530. if (c != NULL) { /* is there a cached closure? */
  531. int nup = p->sizeupvalues;
  532. Upvaldesc *uv = p->upvalues;
  533. int i;
  534. for (i = 0; i < nup; i++) { /* check whether it has right upvalues */
  535. TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;
  536. if (c->upvals[i]->v != v)
  537. return NULL; /* wrong upvalue; cannot reuse closure */
  538. }
  539. }
  540. return c; /* return cached closure (or NULL if no cached closure) */
  541. }
  542. /*
  543. ** create a new Lua closure, push it in the stack, and initialize
  544. ** its upvalues. Note that the closure is not cached if prototype is
  545. ** already black (which means that 'cache' was already cleared by the
  546. ** GC).
  547. */
  548. static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
  549. StkId ra) {
  550. int nup = p->sizeupvalues;
  551. Upvaldesc *uv = p->upvalues;
  552. int i;
  553. LClosure *ncl = luaF_newLclosure(L, nup);
  554. ncl->p = p;
  555. setclLvalue(L, ra, ncl); /* anchor new closure in stack */
  556. for (i = 0; i < nup; i++) { /* fill in its upvalues */
  557. if (uv[i].instack) /* upvalue refers to local variable? */
  558. ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
  559. else /* get upvalue from enclosing function */
  560. ncl->upvals[i] = encup[uv[i].idx];
  561. ncl->upvals[i]->refcount++;
  562. /* new closure is white, so we do not need a barrier here */
  563. }
  564. if (!isblack(p)) /* cache will not break GC invariant? */
  565. p->cache = ncl; /* save it on cache for reuse */
  566. }
  567. /*
  568. ** finish execution of an opcode interrupted by an yield
  569. */
  570. void luaV_finishOp (lua_State *L) {
  571. CallInfo *ci = L->ci;
  572. StkId base = ci->u.l.base;
  573. Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
  574. OpCode op = GET_OPCODE(inst);
  575. switch (op) { /* finish its execution */
  576. case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV:
  577. case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR:
  578. case OP_MOD: case OP_POW:
  579. case OP_UNM: case OP_BNOT: case OP_LEN:
  580. case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
  581. setobjs2s(L, base + GETARG_A(inst), --L->top);
  582. break;
  583. }
  584. case OP_LE: case OP_LT: case OP_EQ: {
  585. int res = !l_isfalse(L->top - 1);
  586. L->top--;
  587. if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
  588. lua_assert(op == OP_LE);
  589. ci->callstatus ^= CIST_LEQ; /* clear mark */
  590. res = !res; /* negate result */
  591. }
  592. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  593. if (res != GETARG_A(inst)) /* condition failed? */
  594. ci->u.l.savedpc++; /* skip jump instruction */
  595. break;
  596. }
  597. case OP_CONCAT: {
  598. StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */
  599. int b = GETARG_B(inst); /* first element to concatenate */
  600. int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */
  601. setobj2s(L, top - 2, top); /* put TM result in proper position */
  602. if (total > 1) { /* are there elements to concat? */
  603. L->top = top - 1; /* top is one after last element (at top-2) */
  604. luaV_concat(L, total); /* concat them (may yield again) */
  605. }
  606. /* move final result to final position */
  607. setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1);
  608. L->top = ci->top; /* restore top */
  609. break;
  610. }
  611. case OP_TFORCALL: {
  612. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
  613. L->top = ci->top; /* correct top */
  614. break;
  615. }
  616. case OP_CALL: {
  617. if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */
  618. L->top = ci->top; /* adjust results */
  619. break;
  620. }
  621. case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
  622. break;
  623. default: lua_assert(0);
  624. }
  625. }
  626. /*
  627. ** {==================================================================
  628. ** Function 'luaV_execute': main interpreter loop
  629. ** ===================================================================
  630. */
  631. /*
  632. ** some macros for common tasks in 'luaV_execute'
  633. */
  634. #if !defined(luai_runtimecheck)
  635. #define luai_runtimecheck(L, c) /* void */
  636. #endif
  637. #define RA(i) (base+GETARG_A(i))
  638. /* to be used after possible stack reallocation */
  639. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  640. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  641. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  642. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  643. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  644. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  645. #define KBx(i) \
  646. (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
  647. /* execute a jump instruction */
  648. #define dojump(ci,i,e) \
  649. { int a = GETARG_A(i); \
  650. if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
  651. ci->u.l.savedpc += GETARG_sBx(i) + e; }
  652. /* for test instructions, execute the jump instruction that follows it */
  653. #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
  654. #define Protect(x) { {x;}; base = ci->u.l.base; }
  655. #define checkGC(L,c) \
  656. Protect( luaC_condGC(L,{L->top = (c); /* limit of live values */ \
  657. luaC_step(L); \
  658. L->top = ci->top;}) /* restore top */ \
  659. luai_threadyield(L); )
  660. #define vmdispatch(o) switch(o)
  661. #define vmcase(l) case l:
  662. #define vmbreak break
  663. void luaV_execute (lua_State *L) {
  664. CallInfo *ci = L->ci;
  665. LClosure *cl;
  666. TValue *k;
  667. StkId base;
  668. newframe: /* reentry point when frame changes (call/return) */
  669. lua_assert(ci == L->ci);
  670. cl = clLvalue(ci->func);
  671. k = cl->p->k;
  672. base = ci->u.l.base;
  673. /* main loop of interpreter */
  674. for (;;) {
  675. Instruction i = *(ci->u.l.savedpc++);
  676. StkId ra;
  677. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  678. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  679. Protect(luaG_traceexec(L));
  680. }
  681. /* WARNING: several calls may realloc the stack and invalidate 'ra' */
  682. ra = RA(i);
  683. lua_assert(base == ci->u.l.base);
  684. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  685. vmdispatch (GET_OPCODE(i)) {
  686. vmcase(OP_MOVE) {
  687. setobjs2s(L, ra, RB(i));
  688. vmbreak;
  689. }
  690. vmcase(OP_LOADK) {
  691. TValue *rb = k + GETARG_Bx(i);
  692. setobj2s(L, ra, rb);
  693. vmbreak;
  694. }
  695. vmcase(OP_LOADKX) {
  696. TValue *rb;
  697. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  698. rb = k + GETARG_Ax(*ci->u.l.savedpc++);
  699. setobj2s(L, ra, rb);
  700. vmbreak;
  701. }
  702. vmcase(OP_LOADBOOL) {
  703. setbvalue(ra, GETARG_B(i));
  704. if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
  705. vmbreak;
  706. }
  707. vmcase(OP_LOADNIL) {
  708. int b = GETARG_B(i);
  709. do {
  710. setnilvalue(ra++);
  711. } while (b--);
  712. vmbreak;
  713. }
  714. vmcase(OP_GETUPVAL) {
  715. int b = GETARG_B(i);
  716. setobj2s(L, ra, cl->upvals[b]->v);
  717. vmbreak;
  718. }
  719. vmcase(OP_GETTABUP) {
  720. int b = GETARG_B(i);
  721. Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
  722. vmbreak;
  723. }
  724. vmcase(OP_GETTABLE) {
  725. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  726. vmbreak;
  727. }
  728. vmcase(OP_SETTABUP) {
  729. int a = GETARG_A(i);
  730. Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
  731. vmbreak;
  732. }
  733. vmcase(OP_SETUPVAL) {
  734. UpVal *uv = cl->upvals[GETARG_B(i)];
  735. setobj(L, uv->v, ra);
  736. luaC_upvalbarrier(L, uv);
  737. vmbreak;
  738. }
  739. vmcase(OP_SETTABLE) {
  740. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  741. vmbreak;
  742. }
  743. vmcase(OP_NEWTABLE) {
  744. int b = GETARG_B(i);
  745. int c = GETARG_C(i);
  746. Table *t = luaH_new(L);
  747. sethvalue(L, ra, t);
  748. if (b != 0 || c != 0)
  749. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  750. checkGC(L, ra + 1);
  751. vmbreak;
  752. }
  753. vmcase(OP_SELF) {
  754. StkId rb = RB(i);
  755. setobjs2s(L, ra+1, rb);
  756. Protect(luaV_gettable(L, rb, RKC(i), ra));
  757. vmbreak;
  758. }
  759. vmcase(OP_ADD) {
  760. TValue *rb = RKB(i);
  761. TValue *rc = RKC(i);
  762. lua_Number nb; lua_Number nc;
  763. if (ttisinteger(rb) && ttisinteger(rc)) {
  764. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  765. setivalue(ra, intop(+, ib, ic));
  766. }
  767. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  768. setfltvalue(ra, luai_numadd(L, nb, nc));
  769. }
  770. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
  771. vmbreak;
  772. }
  773. vmcase(OP_SUB) {
  774. TValue *rb = RKB(i);
  775. TValue *rc = RKC(i);
  776. lua_Number nb; lua_Number nc;
  777. if (ttisinteger(rb) && ttisinteger(rc)) {
  778. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  779. setivalue(ra, intop(-, ib, ic));
  780. }
  781. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  782. setfltvalue(ra, luai_numsub(L, nb, nc));
  783. }
  784. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
  785. vmbreak;
  786. }
  787. vmcase(OP_MUL) {
  788. TValue *rb = RKB(i);
  789. TValue *rc = RKC(i);
  790. lua_Number nb; lua_Number nc;
  791. if (ttisinteger(rb) && ttisinteger(rc)) {
  792. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  793. setivalue(ra, intop(*, ib, ic));
  794. }
  795. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  796. setfltvalue(ra, luai_nummul(L, nb, nc));
  797. }
  798. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
  799. vmbreak;
  800. }
  801. vmcase(OP_DIV) { /* float division (always with floats) */
  802. TValue *rb = RKB(i);
  803. TValue *rc = RKC(i);
  804. lua_Number nb; lua_Number nc;
  805. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  806. setfltvalue(ra, luai_numdiv(L, nb, nc));
  807. }
  808. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
  809. vmbreak;
  810. }
  811. vmcase(OP_BAND) {
  812. TValue *rb = RKB(i);
  813. TValue *rc = RKC(i);
  814. lua_Integer ib; lua_Integer ic;
  815. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  816. setivalue(ra, intop(&, ib, ic));
  817. }
  818. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); }
  819. vmbreak;
  820. }
  821. vmcase(OP_BOR) {
  822. TValue *rb = RKB(i);
  823. TValue *rc = RKC(i);
  824. lua_Integer ib; lua_Integer ic;
  825. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  826. setivalue(ra, intop(|, ib, ic));
  827. }
  828. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); }
  829. vmbreak;
  830. }
  831. vmcase(OP_BXOR) {
  832. TValue *rb = RKB(i);
  833. TValue *rc = RKC(i);
  834. lua_Integer ib; lua_Integer ic;
  835. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  836. setivalue(ra, intop(^, ib, ic));
  837. }
  838. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); }
  839. vmbreak;
  840. }
  841. vmcase(OP_SHL) {
  842. TValue *rb = RKB(i);
  843. TValue *rc = RKC(i);
  844. lua_Integer ib; lua_Integer ic;
  845. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  846. setivalue(ra, luaV_shiftl(ib, ic));
  847. }
  848. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); }
  849. vmbreak;
  850. }
  851. vmcase(OP_SHR) {
  852. TValue *rb = RKB(i);
  853. TValue *rc = RKC(i);
  854. lua_Integer ib; lua_Integer ic;
  855. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  856. setivalue(ra, luaV_shiftl(ib, -ic));
  857. }
  858. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); }
  859. vmbreak;
  860. }
  861. vmcase(OP_MOD) {
  862. TValue *rb = RKB(i);
  863. TValue *rc = RKC(i);
  864. lua_Number nb; lua_Number nc;
  865. if (ttisinteger(rb) && ttisinteger(rc)) {
  866. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  867. setivalue(ra, luaV_mod(L, ib, ic));
  868. }
  869. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  870. lua_Number m;
  871. luai_nummod(L, nb, nc, m);
  872. setfltvalue(ra, m);
  873. }
  874. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
  875. vmbreak;
  876. }
  877. vmcase(OP_IDIV) { /* floor division */
  878. TValue *rb = RKB(i);
  879. TValue *rc = RKC(i);
  880. lua_Number nb; lua_Number nc;
  881. if (ttisinteger(rb) && ttisinteger(rc)) {
  882. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  883. setivalue(ra, luaV_div(L, ib, ic));
  884. }
  885. else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  886. setfltvalue(ra, luai_numidiv(L, nb, nc));
  887. }
  888. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); }
  889. vmbreak;
  890. }
  891. vmcase(OP_POW) {
  892. TValue *rb = RKB(i);
  893. TValue *rc = RKC(i);
  894. lua_Number nb; lua_Number nc;
  895. if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
  896. setfltvalue(ra, luai_numpow(L, nb, nc));
  897. }
  898. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
  899. vmbreak;
  900. }
  901. vmcase(OP_UNM) {
  902. TValue *rb = RB(i);
  903. lua_Number nb;
  904. if (ttisinteger(rb)) {
  905. lua_Integer ib = ivalue(rb);
  906. setivalue(ra, intop(-, 0, ib));
  907. }
  908. else if (tonumber(rb, &nb)) {
  909. setfltvalue(ra, luai_numunm(L, nb));
  910. }
  911. else {
  912. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  913. }
  914. vmbreak;
  915. }
  916. vmcase(OP_BNOT) {
  917. TValue *rb = RB(i);
  918. lua_Integer ib;
  919. if (tointeger(rb, &ib)) {
  920. setivalue(ra, intop(^, ~l_castS2U(0), ib));
  921. }
  922. else {
  923. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  924. }
  925. vmbreak;
  926. }
  927. vmcase(OP_NOT) {
  928. TValue *rb = RB(i);
  929. int res = l_isfalse(rb); /* next assignment may change this value */
  930. setbvalue(ra, res);
  931. vmbreak;
  932. }
  933. vmcase(OP_LEN) {
  934. Protect(luaV_objlen(L, ra, RB(i)));
  935. vmbreak;
  936. }
  937. vmcase(OP_CONCAT) {
  938. int b = GETARG_B(i);
  939. int c = GETARG_C(i);
  940. StkId rb;
  941. L->top = base + c + 1; /* mark the end of concat operands */
  942. Protect(luaV_concat(L, c - b + 1));
  943. ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
  944. rb = base + b;
  945. setobjs2s(L, ra, rb);
  946. checkGC(L, (ra >= rb ? ra + 1 : rb));
  947. L->top = ci->top; /* restore top */
  948. vmbreak;
  949. }
  950. vmcase(OP_JMP) {
  951. dojump(ci, i, 0);
  952. vmbreak;
  953. }
  954. vmcase(OP_EQ) {
  955. TValue *rb = RKB(i);
  956. TValue *rc = RKC(i);
  957. Protect(
  958. if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i))
  959. ci->u.l.savedpc++;
  960. else
  961. donextjump(ci);
  962. )
  963. vmbreak;
  964. }
  965. vmcase(OP_LT) {
  966. Protect(
  967. if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
  968. ci->u.l.savedpc++;
  969. else
  970. donextjump(ci);
  971. )
  972. vmbreak;
  973. }
  974. vmcase(OP_LE) {
  975. Protect(
  976. if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
  977. ci->u.l.savedpc++;
  978. else
  979. donextjump(ci);
  980. )
  981. vmbreak;
  982. }
  983. vmcase(OP_TEST) {
  984. if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
  985. ci->u.l.savedpc++;
  986. else
  987. donextjump(ci);
  988. vmbreak;
  989. }
  990. vmcase(OP_TESTSET) {
  991. TValue *rb = RB(i);
  992. if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
  993. ci->u.l.savedpc++;
  994. else {
  995. setobjs2s(L, ra, rb);
  996. donextjump(ci);
  997. }
  998. vmbreak;
  999. }
  1000. vmcase(OP_CALL) {
  1001. int b = GETARG_B(i);
  1002. int nresults = GETARG_C(i) - 1;
  1003. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  1004. if (luaD_precall(L, ra, nresults)) { /* C function? */
  1005. if (nresults >= 0) L->top = ci->top; /* adjust results */
  1006. base = ci->u.l.base;
  1007. }
  1008. else { /* Lua function */
  1009. ci = L->ci;
  1010. ci->callstatus |= CIST_REENTRY;
  1011. goto newframe; /* restart luaV_execute over new Lua function */
  1012. }
  1013. vmbreak;
  1014. }
  1015. vmcase(OP_TAILCALL) {
  1016. int b = GETARG_B(i);
  1017. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  1018. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  1019. if (luaD_precall(L, ra, LUA_MULTRET)) /* C function? */
  1020. base = ci->u.l.base;
  1021. else {
  1022. /* tail call: put called frame (n) in place of caller one (o) */
  1023. CallInfo *nci = L->ci; /* called frame */
  1024. CallInfo *oci = nci->previous; /* caller frame */
  1025. StkId nfunc = nci->func; /* called function */
  1026. StkId ofunc = oci->func; /* caller function */
  1027. /* last stack slot filled by 'precall' */
  1028. StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
  1029. int aux;
  1030. /* close all upvalues from previous call */
  1031. if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
  1032. /* move new frame into old one */
  1033. for (aux = 0; nfunc + aux < lim; aux++)
  1034. setobjs2s(L, ofunc + aux, nfunc + aux);
  1035. oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */
  1036. oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
  1037. oci->u.l.savedpc = nci->u.l.savedpc;
  1038. oci->callstatus |= CIST_TAIL; /* function was tail called */
  1039. ci = L->ci = oci; /* remove new frame */
  1040. lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
  1041. goto newframe; /* restart luaV_execute over new Lua function */
  1042. }
  1043. vmbreak;
  1044. }
  1045. vmcase(OP_RETURN) {
  1046. int b = GETARG_B(i);
  1047. if (cl->p->sizep > 0) luaF_close(L, base);
  1048. b = luaD_poscall(L, ra, (b != 0 ? b - 1 : L->top - ra));
  1049. if (!(ci->callstatus & CIST_REENTRY)) /* 'ci' still the called one */
  1050. return; /* external invocation: return */
  1051. else { /* invocation via reentry: continue execution */
  1052. ci = L->ci;
  1053. if (b) L->top = ci->top;
  1054. lua_assert(isLua(ci));
  1055. lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
  1056. goto newframe; /* restart luaV_execute over new Lua function */
  1057. }
  1058. }
  1059. vmcase(OP_FORLOOP) {
  1060. if (ttisinteger(ra)) { /* integer loop? */
  1061. lua_Integer step = ivalue(ra + 2);
  1062. lua_Integer idx = ivalue(ra) + step; /* increment index */
  1063. lua_Integer limit = ivalue(ra + 1);
  1064. if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
  1065. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  1066. chgivalue(ra, idx); /* update internal index... */
  1067. setivalue(ra + 3, idx); /* ...and external index */
  1068. }
  1069. }
  1070. else { /* floating loop */
  1071. lua_Number step = fltvalue(ra + 2);
  1072. lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */
  1073. lua_Number limit = fltvalue(ra + 1);
  1074. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  1075. : luai_numle(limit, idx)) {
  1076. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  1077. chgfltvalue(ra, idx); /* update internal index... */
  1078. setfltvalue(ra + 3, idx); /* ...and external index */
  1079. }
  1080. }
  1081. vmbreak;
  1082. }
  1083. vmcase(OP_FORPREP) {
  1084. TValue *init = ra;
  1085. TValue *plimit = ra + 1;
  1086. TValue *pstep = ra + 2;
  1087. lua_Integer ilimit;
  1088. int stopnow;
  1089. if (ttisinteger(init) && ttisinteger(pstep) &&
  1090. forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
  1091. /* all values are integer */
  1092. lua_Integer initv = (stopnow ? 0 : ivalue(init));
  1093. setivalue(plimit, ilimit);
  1094. setivalue(init, initv - ivalue(pstep));
  1095. }
  1096. else { /* try making all values floats */
  1097. lua_Number ninit; lua_Number nlimit; lua_Number nstep;
  1098. if (!tonumber(plimit, &nlimit))
  1099. luaG_runerror(L, "'for' limit must be a number");
  1100. setfltvalue(plimit, nlimit);
  1101. if (!tonumber(pstep, &nstep))
  1102. luaG_runerror(L, "'for' step must be a number");
  1103. setfltvalue(pstep, nstep);
  1104. if (!tonumber(init, &ninit))
  1105. luaG_runerror(L, "'for' initial value must be a number");
  1106. setfltvalue(init, luai_numsub(L, ninit, nstep));
  1107. }
  1108. ci->u.l.savedpc += GETARG_sBx(i);
  1109. vmbreak;
  1110. }
  1111. vmcase(OP_TFORCALL) {
  1112. StkId cb = ra + 3; /* call base */
  1113. setobjs2s(L, cb+2, ra+2);
  1114. setobjs2s(L, cb+1, ra+1);
  1115. setobjs2s(L, cb, ra);
  1116. L->top = cb + 3; /* func. + 2 args (state and index) */
  1117. Protect(luaD_call(L, cb, GETARG_C(i), 1));
  1118. L->top = ci->top;
  1119. i = *(ci->u.l.savedpc++); /* go to next instruction */
  1120. ra = RA(i);
  1121. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  1122. goto l_tforloop;
  1123. }
  1124. vmcase(OP_TFORLOOP) {
  1125. l_tforloop:
  1126. if (!ttisnil(ra + 1)) { /* continue loop? */
  1127. setobjs2s(L, ra, ra + 1); /* save control variable */
  1128. ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
  1129. }
  1130. vmbreak;
  1131. }
  1132. vmcase(OP_SETLIST) {
  1133. int n = GETARG_B(i);
  1134. int c = GETARG_C(i);
  1135. unsigned int last;
  1136. Table *h;
  1137. if (n == 0) n = cast_int(L->top - ra) - 1;
  1138. if (c == 0) {
  1139. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
  1140. c = GETARG_Ax(*ci->u.l.savedpc++);
  1141. }
  1142. luai_runtimecheck(L, ttistable(ra));
  1143. h = hvalue(ra);
  1144. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  1145. if (last > h->sizearray) /* needs more space? */
  1146. luaH_resizearray(L, h, last); /* pre-allocate it at once */
  1147. for (; n > 0; n--) {
  1148. TValue *val = ra+n;
  1149. luaH_setint(L, h, last--, val);
  1150. luaC_barrierback(L, h, val);
  1151. }
  1152. L->top = ci->top; /* correct top (in case of previous open call) */
  1153. vmbreak;
  1154. }
  1155. vmcase(OP_CLOSURE) {
  1156. Proto *p = cl->p->p[GETARG_Bx(i)];
  1157. LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  1158. if (ncl == NULL) /* no match? */
  1159. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  1160. else
  1161. setclLvalue(L, ra, ncl); /* push cashed closure */
  1162. checkGC(L, ra + 1);
  1163. vmbreak;
  1164. }
  1165. vmcase(OP_VARARG) {
  1166. int b = GETARG_B(i) - 1;
  1167. int j;
  1168. int n = cast_int(base - ci->func) - cl->p->numparams - 1;
  1169. if (b < 0) { /* B == 0? */
  1170. b = n; /* get all var. arguments */
  1171. Protect(luaD_checkstack(L, n));
  1172. ra = RA(i); /* previous call may change the stack */
  1173. L->top = ra + n;
  1174. }
  1175. for (j = 0; j < b; j++) {
  1176. if (j < n) {
  1177. setobjs2s(L, ra + j, base - n + j);
  1178. }
  1179. else {
  1180. setnilvalue(ra + j);
  1181. }
  1182. }
  1183. vmbreak;
  1184. }
  1185. vmcase(OP_EXTRAARG) {
  1186. lua_assert(0);
  1187. vmbreak;
  1188. }
  1189. }
  1190. }
  1191. }
  1192. /* }================================================================== */