lvm.c 47 KB

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