lvm.c 43 KB

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