lvm.c 43 KB

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