lvm.c 42 KB

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