lvm.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. /*
  2. ** $Id: lvm.c,v 2.301 2017/11/01 18:20:48 roberto Exp $
  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(s2v(val)); /* result is nil */
  152. return;
  153. }
  154. /* else will try the metamethod */
  155. }
  156. if (ttisfunction(tm)) { /* is metamethod a function? */
  157. luaT_callTMres(L, tm, t, key, val); /* 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. TValue *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);
  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_callTMres(L, tm, t1, t2, L->top); /* call TM */
  408. return !l_isfalse(s2v(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(s2v(top - n)); /* length of string being copied */
  419. memcpy(buff + tl, svalue(s2v(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(s2v(top - 2)) || cvt2str(s2v(top - 2))) ||
  433. !tostring(L, s2v(top - 1)))
  434. luaT_trybinTM(L, s2v(top - 2), s2v(top - 1), top - 2, TM_CONCAT);
  435. else if (isemptystr(s2v(top - 1))) /* second operand is empty? */
  436. cast_void(tostring(L, s2v(top - 2))); /* result is first operand */
  437. else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */
  438. setobjs2s(L, top - 2, top - 1); /* result is second op. */
  439. }
  440. else {
  441. /* at least two non-empty string values; get as many as possible */
  442. size_t tl = vslen(s2v(top - 1));
  443. TString *ts;
  444. /* collect total length and number of strings */
  445. for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
  446. size_t l = vslen(s2v(top - n - 1));
  447. if (l >= (MAX_SIZE/sizeof(char)) - tl)
  448. luaG_runerror(L, "string length overflow");
  449. tl += l;
  450. }
  451. if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */
  452. char buff[LUAI_MAXSHORTLEN];
  453. copy2buff(top, n, buff); /* copy strings to buffer */
  454. ts = luaS_newlstr(L, buff, tl);
  455. }
  456. else { /* long string; copy strings directly to final result */
  457. ts = luaS_createlngstrobj(L, tl);
  458. copy2buff(top, n, getstr(ts));
  459. }
  460. setsvalue2s(L, top - n, ts); /* create result */
  461. }
  462. total -= n-1; /* got 'n' strings to create 1 new */
  463. L->top -= n-1; /* popped 'n' strings and pushed one */
  464. } while (total > 1); /* repeat until only 1 result left */
  465. }
  466. /*
  467. ** Main operation 'ra' = #rb'.
  468. */
  469. void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
  470. const TValue *tm;
  471. switch (ttype(rb)) {
  472. case LUA_TTABLE: {
  473. Table *h = hvalue(rb);
  474. tm = fasttm(L, h->metatable, TM_LEN);
  475. if (tm) break; /* metamethod? break switch to call it */
  476. setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */
  477. return;
  478. }
  479. case LUA_TSHRSTR: {
  480. setivalue(s2v(ra), tsvalue(rb)->shrlen);
  481. return;
  482. }
  483. case LUA_TLNGSTR: {
  484. setivalue(s2v(ra), tsvalue(rb)->u.lnglen);
  485. return;
  486. }
  487. default: { /* try metamethod */
  488. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  489. if (ttisnil(tm)) /* no metamethod? */
  490. luaG_typeerror(L, rb, "get length of");
  491. break;
  492. }
  493. }
  494. luaT_callTMres(L, tm, rb, rb, ra);
  495. }
  496. /*
  497. ** Integer division; return 'm // n', that is, floor(m/n).
  498. ** C division truncates its result (rounds towards zero).
  499. ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
  500. ** otherwise 'floor(q) == trunc(q) - 1'.
  501. */
  502. lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) {
  503. if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */
  504. if (n == 0)
  505. luaG_runerror(L, "attempt to divide by zero");
  506. return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
  507. }
  508. else {
  509. lua_Integer q = m / n; /* perform C division */
  510. if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */
  511. q -= 1; /* correct result for different rounding */
  512. return q;
  513. }
  514. }
  515. /*
  516. ** Integer modulus; return 'm % n'. (Assume that C '%' with
  517. ** negative operands follows C99 behavior. See previous comment
  518. ** about luaV_div.)
  519. */
  520. lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
  521. if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */
  522. if (n == 0)
  523. luaG_runerror(L, "attempt to perform 'n%%0'");
  524. return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
  525. }
  526. else {
  527. lua_Integer r = m % n;
  528. if (r != 0 && (m ^ n) < 0) /* 'm/n' would be non-integer negative? */
  529. r += n; /* correct result for different rounding */
  530. return r;
  531. }
  532. }
  533. /* number of bits in an integer */
  534. #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
  535. /*
  536. ** Shift left operation. (Shift right just negates 'y'.)
  537. */
  538. lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
  539. if (y < 0) { /* shift right? */
  540. if (y <= -NBITS) return 0;
  541. else return intop(>>, x, -y);
  542. }
  543. else { /* shift left */
  544. if (y >= NBITS) return 0;
  545. else return intop(<<, x, y);
  546. }
  547. }
  548. /*
  549. ** check whether cached closure in prototype 'p' may be reused, that is,
  550. ** whether there is a cached closure with the same upvalues needed by
  551. ** new closure to be created.
  552. */
  553. static LClosure *getcached (Proto *p, UpVal **encup, StkId base) {
  554. LClosure *c = p->cache;
  555. if (c != NULL) { /* is there a cached closure? */
  556. int nup = p->sizeupvalues;
  557. Upvaldesc *uv = p->upvalues;
  558. int i;
  559. for (i = 0; i < nup; i++) { /* check whether it has right upvalues */
  560. TValue *v = uv[i].instack ? s2v(base + uv[i].idx) : encup[uv[i].idx]->v;
  561. if (c->upvals[i]->v != v)
  562. return NULL; /* wrong upvalue; cannot reuse closure */
  563. }
  564. p->cachemiss = 0; /* got a hit */
  565. }
  566. return c; /* return cached closure (or NULL if no cached closure) */
  567. }
  568. /*
  569. ** create a new Lua closure, push it in the stack, and initialize
  570. ** its upvalues. ???
  571. */
  572. static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
  573. StkId ra) {
  574. int nup = p->sizeupvalues;
  575. Upvaldesc *uv = p->upvalues;
  576. int i;
  577. LClosure *ncl = luaF_newLclosure(L, nup);
  578. ncl->p = p;
  579. setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */
  580. for (i = 0; i < nup; i++) { /* fill in its upvalues */
  581. if (uv[i].instack) /* upvalue refers to local variable? */
  582. ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
  583. else /* get upvalue from enclosing function */
  584. ncl->upvals[i] = encup[uv[i].idx];
  585. /* new closure is white, so we do not need a barrier here */
  586. }
  587. if (p->cachemiss >= MAXMISS) /* too many missings? */
  588. p->cache = NULL; /* give up cache */
  589. else {
  590. p->cache = ncl; /* save it on cache for reuse */
  591. luaC_protobarrier(L, p, ncl);
  592. p->cachemiss++;
  593. }
  594. }
  595. /*
  596. ** finish execution of an opcode interrupted by an yield
  597. */
  598. void luaV_finishOp (lua_State *L) {
  599. CallInfo *ci = L->ci;
  600. StkId base = ci->func + 1;
  601. Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
  602. OpCode op = GET_OPCODE(inst);
  603. switch (op) { /* finish its execution */
  604. case OP_ADDI: case OP_SUBI:
  605. case OP_MULI: case OP_DIVI: case OP_IDIVI:
  606. case OP_MODI: case OP_POWI:
  607. case OP_ADD: case OP_SUB:
  608. case OP_MUL: case OP_DIV: case OP_IDIV:
  609. case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR:
  610. case OP_MOD: case OP_POW:
  611. case OP_UNM: case OP_BNOT: case OP_LEN:
  612. case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
  613. case OP_GETFIELD: case OP_SELF: {
  614. setobjs2s(L, base + GETARG_A(inst), --L->top);
  615. break;
  616. }
  617. case OP_LE: case OP_LT: case OP_EQ: {
  618. int res = !l_isfalse(s2v(L->top - 1));
  619. L->top--;
  620. if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
  621. lua_assert(op == OP_LE);
  622. ci->callstatus ^= CIST_LEQ; /* clear mark */
  623. res = !res; /* negate result */
  624. }
  625. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  626. if (res != GETARG_A(inst)) /* condition failed? */
  627. ci->u.l.savedpc++; /* skip jump instruction */
  628. break;
  629. }
  630. case OP_CONCAT: {
  631. StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */
  632. int b = GETARG_B(inst); /* first element to concatenate */
  633. int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */
  634. setobjs2s(L, top - 2, top); /* put TM result in proper position */
  635. if (total > 1) { /* are there elements to concat? */
  636. L->top = top - 1; /* top is one after last element (at top-2) */
  637. luaV_concat(L, total); /* concat them (may yield again) */
  638. }
  639. /* move final result to final position */
  640. setobjs2s(L, ci->func + 1 + GETARG_A(inst), L->top - 1);
  641. L->top = ci->top; /* restore top */
  642. break;
  643. }
  644. case OP_TFORCALL: {
  645. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
  646. L->top = ci->top; /* correct top */
  647. break;
  648. }
  649. case OP_CALL: {
  650. if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */
  651. L->top = ci->top; /* adjust results */
  652. break;
  653. }
  654. case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
  655. case OP_SETI: case OP_SETFIELD:
  656. break;
  657. default: lua_assert(0);
  658. }
  659. }
  660. /*
  661. ** {==================================================================
  662. ** Function 'luaV_execute': main interpreter loop
  663. ** ===================================================================
  664. */
  665. /*
  666. ** some macros for common tasks in 'luaV_execute'
  667. */
  668. #define RA(i) (base+GETARG_A(i))
  669. #define RB(i) (base+GETARG_B(i))
  670. #define vRB(i) s2v(RB(i))
  671. #define KB(i) (k+GETARG_B(i))
  672. #define RC(i) (base+GETARG_C(i))
  673. #define vRC(i) s2v(RC(i))
  674. #define KC(i) (k+GETARG_C(i))
  675. #define RKC(i) ((GETARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
  676. #define updatemask(L) (mask = L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT))
  677. /*
  678. ** Execute a jump instruction. The 'updatemask' allows signals to stop
  679. ** tight loops. (Without it, the local copy of 'mask' could never change.)
  680. */
  681. #define dojump(ci,i,e) { 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(s2v(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(s2v(ra), b);
  743. vmbreak;
  744. }
  745. vmcase(OP_LOADF) {
  746. int b = GETARG_sBx(i);
  747. setfltvalue(s2v(ra), cast_num(b));
  748. vmbreak;
  749. }
  750. vmcase(OP_LOADKX) {
  751. TValue *rb;
  752. rb = k + GETARG_Ax(*pc); pc++;
  753. setobj2s(L, ra, rb);
  754. vmbreak;
  755. }
  756. vmcase(OP_LOADBOOL) {
  757. setbvalue(s2v(ra), GETARG_B(i));
  758. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  759. vmbreak;
  760. }
  761. vmcase(OP_LOADNIL) {
  762. int b = GETARG_B(i);
  763. do {
  764. setnilvalue(s2v(ra++));
  765. } while (b--);
  766. vmbreak;
  767. }
  768. vmcase(OP_GETUPVAL) {
  769. int b = GETARG_B(i);
  770. setobj2s(L, ra, cl->upvals[b]->v);
  771. vmbreak;
  772. }
  773. vmcase(OP_SETUPVAL) {
  774. UpVal *uv = cl->upvals[GETARG_B(i)];
  775. setobj(L, uv->v, s2v(ra));
  776. luaC_barrier(L, uv, s2v(ra));
  777. vmbreak;
  778. }
  779. vmcase(OP_GETTABUP) {
  780. const TValue *slot;
  781. TValue *upval = cl->upvals[GETARG_B(i)]->v;
  782. TValue *rc = KC(i);
  783. TString *key = tsvalue(rc); /* key must be a string */
  784. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  785. setobj2s(L, ra, slot);
  786. }
  787. else Protect(luaV_finishget(L, upval, rc, ra, slot));
  788. vmbreak;
  789. }
  790. vmcase(OP_GETTABLE) {
  791. const TValue *slot;
  792. TValue *rb = vRB(i);
  793. TValue *rc = vRC(i);
  794. lua_Unsigned n;
  795. if (ttisinteger(rc) /* fast track for integers? */
  796. ? (n = ivalue(rc), luaV_fastgeti(L, rb, n, slot))
  797. : luaV_fastget(L, rb, rc, slot, luaH_get)) {
  798. setobj2s(L, ra, slot);
  799. }
  800. else
  801. Protect(luaV_finishget(L, rb, rc, ra, slot));
  802. vmbreak;
  803. }
  804. vmcase(OP_GETI) {
  805. const TValue *slot;
  806. TValue *rb = vRB(i);
  807. int c = GETARG_C(i);
  808. if (luaV_fastgeti(L, rb, c, slot)) {
  809. setobj2s(L, ra, slot);
  810. }
  811. else {
  812. TValue key;
  813. setivalue(&key, c);
  814. Protect(luaV_finishget(L, rb, &key, ra, slot));
  815. }
  816. vmbreak;
  817. }
  818. vmcase(OP_GETFIELD) {
  819. const TValue *slot;
  820. TValue *rb = vRB(i);
  821. TValue *rc = KC(i);
  822. TString *key = tsvalue(rc); /* key must be a string */
  823. if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) {
  824. setobj2s(L, ra, slot);
  825. }
  826. else Protect(luaV_finishget(L, rb, rc, ra, slot));
  827. vmbreak;
  828. }
  829. vmcase(OP_SETTABUP) {
  830. const TValue *slot;
  831. TValue *upval = cl->upvals[GETARG_A(i)]->v;
  832. TValue *rb = KB(i);
  833. TValue *rc = RKC(i);
  834. TString *key = tsvalue(rb); /* key must be a string */
  835. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  836. luaV_finishfastset(L, upval, slot, rc);
  837. }
  838. else
  839. Protect(luaV_finishset(L, upval, rb, rc, slot));
  840. vmbreak;
  841. }
  842. vmcase(OP_SETTABLE) {
  843. const TValue *slot;
  844. TValue *rb = vRB(i); /* key (table is in 'ra') */
  845. TValue *rc = RKC(i); /* value */
  846. lua_Unsigned n;
  847. if (ttisinteger(rb) /* fast track for integers? */
  848. ? (n = ivalue(rb), luaV_fastgeti(L, s2v(ra), n, slot))
  849. : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) {
  850. luaV_finishfastset(L, s2v(ra), slot, rc);
  851. }
  852. else
  853. Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
  854. vmbreak;
  855. }
  856. vmcase(OP_SETI) {
  857. const TValue *slot;
  858. int c = GETARG_B(i);
  859. TValue *rc = RKC(i);
  860. if (luaV_fastgeti(L, s2v(ra), c, slot)) {
  861. luaV_finishfastset(L, s2v(ra), slot, rc);
  862. }
  863. else {
  864. TValue key;
  865. setivalue(&key, c);
  866. Protect(luaV_finishset(L, s2v(ra), &key, rc, slot));
  867. }
  868. vmbreak;
  869. }
  870. vmcase(OP_SETFIELD) {
  871. const TValue *slot;
  872. TValue *rb = KB(i);
  873. TValue *rc = RKC(i);
  874. TString *key = tsvalue(rb); /* key must be a string */
  875. if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) {
  876. luaV_finishfastset(L, s2v(ra), slot, rc);
  877. }
  878. else
  879. Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
  880. vmbreak;
  881. }
  882. vmcase(OP_NEWTABLE) {
  883. int b = GETARG_B(i);
  884. int c = GETARG_C(i);
  885. Table *t;
  886. savepc(L); /* in case of allocation errors */
  887. t = luaH_new(L);
  888. sethvalue2s(L, ra, t);
  889. if (b != 0 || c != 0)
  890. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  891. checkGC(L, ra + 1);
  892. vmbreak;
  893. }
  894. vmcase(OP_SELF) {
  895. const TValue *slot;
  896. TValue *rb = vRB(i);
  897. TValue *rc = RKC(i);
  898. TString *key = tsvalue(rc); /* key must be a string */
  899. setobj2s(L, ra + 1, rb);
  900. if (luaV_fastget(L, rb, key, slot, luaH_getstr)) {
  901. setobj2s(L, ra, slot);
  902. }
  903. else Protect(luaV_finishget(L, rb, rc, ra, slot));
  904. vmbreak;
  905. }
  906. vmcase(OP_ADDI) {
  907. TValue *rb = vRB(i);
  908. int ic = GETARG_sC(i);
  909. lua_Number nb;
  910. if (ttisinteger(rb)) {
  911. setivalue(s2v(ra), intop(+, ivalue(rb), ic));
  912. }
  913. else if (tonumberns(rb, nb)) {
  914. setfltvalue(s2v(ra), luai_numadd(L, nb, cast_num(ic)));
  915. }
  916. else
  917. Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_ADD));
  918. vmbreak;
  919. }
  920. vmcase(OP_SUBI) {
  921. TValue *rb = vRB(i);
  922. int ic = GETARG_sC(i);
  923. lua_Number nb;
  924. if (ttisinteger(rb)) {
  925. setivalue(s2v(ra), intop(-, ivalue(rb), ic));
  926. }
  927. else if (tonumberns(rb, nb)) {
  928. setfltvalue(s2v(ra), luai_numsub(L, nb, cast_num(ic)));
  929. }
  930. else
  931. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_SUB));
  932. vmbreak;
  933. }
  934. vmcase(OP_MULI) {
  935. TValue *rb = vRB(i);
  936. int ic = GETARG_sC(i);
  937. lua_Number nb;
  938. if (ttisinteger(rb)) {
  939. setivalue(s2v(ra), intop(*, ivalue(rb), ic));
  940. }
  941. else if (tonumberns(rb, nb)) {
  942. setfltvalue(s2v(ra), luai_nummul(L, nb, cast_num(ic)));
  943. }
  944. else
  945. Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_MUL));
  946. vmbreak;
  947. }
  948. vmcase(OP_MODI) {
  949. TValue *rb = vRB(i);
  950. int ic = GETARG_sC(i);
  951. lua_Number nb;
  952. if (ttisinteger(rb)) {
  953. setivalue(s2v(ra), luaV_mod(L, ivalue(rb), ic));
  954. }
  955. else if (tonumberns(rb, nb)) {
  956. lua_Number m;
  957. lua_Number nc = cast_num(ic);
  958. luai_nummod(L, nb, nc, m);
  959. setfltvalue(s2v(ra), m);
  960. }
  961. else
  962. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_MOD));
  963. vmbreak;
  964. }
  965. vmcase(OP_POWI) {
  966. TValue *rb = vRB(i);
  967. int ic = GETARG_sC(i);
  968. lua_Number nb;
  969. if (tonumberns(rb, nb)) {
  970. lua_Number nc = cast_num(ic);
  971. setfltvalue(s2v(ra), luai_numpow(L, nb, nc));
  972. }
  973. else
  974. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_POW));
  975. vmbreak;
  976. }
  977. vmcase(OP_DIVI) {
  978. TValue *rb = vRB(i);
  979. int ic = GETARG_sC(i);
  980. lua_Number nb;
  981. if (tonumberns(rb, nb)) {
  982. lua_Number nc = cast_num(ic);
  983. setfltvalue(s2v(ra), luai_numdiv(L, nb, nc));
  984. }
  985. else
  986. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_DIV));
  987. vmbreak;
  988. }
  989. vmcase(OP_IDIVI) {
  990. TValue *rb = vRB(i);
  991. int ic = GETARG_sC(i);
  992. lua_Number nb;
  993. if (ttisinteger(rb)) {
  994. setivalue(s2v(ra), luaV_div(L, ivalue(rb), ic));
  995. }
  996. else if (tonumberns(rb, nb)) {
  997. lua_Number nc = cast_num(ic);
  998. setfltvalue(s2v(ra), luai_numdiv(L, nb, nc));
  999. }
  1000. else
  1001. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_IDIV));
  1002. vmbreak;
  1003. }
  1004. vmcase(OP_ADD) {
  1005. TValue *rb = vRB(i);
  1006. TValue *rc = vRC(i);
  1007. lua_Number nb; lua_Number nc;
  1008. if (ttisinteger(rb) && ttisinteger(rc)) {
  1009. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1010. setivalue(s2v(ra), intop(+, ib, ic));
  1011. }
  1012. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1013. setfltvalue(s2v(ra), luai_numadd(L, nb, nc));
  1014. }
  1015. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
  1016. vmbreak;
  1017. }
  1018. vmcase(OP_SUB) {
  1019. TValue *rb = vRB(i);
  1020. TValue *rc = vRC(i);
  1021. lua_Number nb; lua_Number nc;
  1022. if (ttisinteger(rb) && ttisinteger(rc)) {
  1023. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1024. setivalue(s2v(ra), intop(-, ib, ic));
  1025. }
  1026. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1027. setfltvalue(s2v(ra), luai_numsub(L, nb, nc));
  1028. }
  1029. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
  1030. vmbreak;
  1031. }
  1032. vmcase(OP_MUL) {
  1033. TValue *rb = vRB(i);
  1034. TValue *rc = vRC(i);
  1035. lua_Number nb; lua_Number nc;
  1036. if (ttisinteger(rb) && ttisinteger(rc)) {
  1037. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1038. setivalue(s2v(ra), intop(*, ib, ic));
  1039. }
  1040. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1041. setfltvalue(s2v(ra), luai_nummul(L, nb, nc));
  1042. }
  1043. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
  1044. vmbreak;
  1045. }
  1046. vmcase(OP_DIV) { /* float division (always with floats) */
  1047. TValue *rb = vRB(i);
  1048. TValue *rc = vRC(i);
  1049. lua_Number nb; lua_Number nc;
  1050. if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1051. setfltvalue(s2v(ra), luai_numdiv(L, nb, nc));
  1052. }
  1053. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
  1054. vmbreak;
  1055. }
  1056. vmcase(OP_BAND) {
  1057. TValue *rb = vRB(i);
  1058. TValue *rc = vRC(i);
  1059. lua_Integer ib; lua_Integer ic;
  1060. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  1061. setivalue(s2v(ra), intop(&, ib, ic));
  1062. }
  1063. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); }
  1064. vmbreak;
  1065. }
  1066. vmcase(OP_BOR) {
  1067. TValue *rb = vRB(i);
  1068. TValue *rc = vRC(i);
  1069. lua_Integer ib; lua_Integer ic;
  1070. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  1071. setivalue(s2v(ra), intop(|, ib, ic));
  1072. }
  1073. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); }
  1074. vmbreak;
  1075. }
  1076. vmcase(OP_BXOR) {
  1077. TValue *rb = vRB(i);
  1078. TValue *rc = vRC(i);
  1079. lua_Integer ib; lua_Integer ic;
  1080. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  1081. setivalue(s2v(ra), intop(^, ib, ic));
  1082. }
  1083. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); }
  1084. vmbreak;
  1085. }
  1086. vmcase(OP_SHL) {
  1087. TValue *rb = vRB(i);
  1088. TValue *rc = vRC(i);
  1089. lua_Integer ib; lua_Integer ic;
  1090. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  1091. setivalue(s2v(ra), luaV_shiftl(ib, ic));
  1092. }
  1093. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); }
  1094. vmbreak;
  1095. }
  1096. vmcase(OP_SHR) {
  1097. TValue *rb = vRB(i);
  1098. TValue *rc = vRC(i);
  1099. lua_Integer ib; lua_Integer ic;
  1100. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  1101. setivalue(s2v(ra), luaV_shiftl(ib, -ic));
  1102. }
  1103. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); }
  1104. vmbreak;
  1105. }
  1106. vmcase(OP_MOD) {
  1107. TValue *rb = vRB(i);
  1108. TValue *rc = vRC(i);
  1109. lua_Number nb; lua_Number nc;
  1110. if (ttisinteger(rb) && ttisinteger(rc)) {
  1111. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1112. setivalue(s2v(ra), luaV_mod(L, ib, ic));
  1113. }
  1114. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1115. lua_Number m;
  1116. luai_nummod(L, nb, nc, m);
  1117. setfltvalue(s2v(ra), m);
  1118. }
  1119. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
  1120. vmbreak;
  1121. }
  1122. vmcase(OP_IDIV) { /* floor division */
  1123. TValue *rb = vRB(i);
  1124. TValue *rc = vRC(i);
  1125. lua_Number nb; lua_Number nc;
  1126. if (ttisinteger(rb) && ttisinteger(rc)) {
  1127. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1128. setivalue(s2v(ra), luaV_div(L, ib, ic));
  1129. }
  1130. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1131. setfltvalue(s2v(ra), luai_numidiv(L, nb, nc));
  1132. }
  1133. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); }
  1134. vmbreak;
  1135. }
  1136. vmcase(OP_POW) {
  1137. TValue *rb = vRB(i);
  1138. TValue *rc = vRC(i);
  1139. lua_Number nb; lua_Number nc;
  1140. if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1141. setfltvalue(s2v(ra), luai_numpow(L, nb, nc));
  1142. }
  1143. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
  1144. vmbreak;
  1145. }
  1146. vmcase(OP_UNM) {
  1147. TValue *rb = vRB(i);
  1148. lua_Number nb;
  1149. if (ttisinteger(rb)) {
  1150. lua_Integer ib = ivalue(rb);
  1151. setivalue(s2v(ra), intop(-, 0, ib));
  1152. }
  1153. else if (tonumberns(rb, nb)) {
  1154. setfltvalue(s2v(ra), luai_numunm(L, nb));
  1155. }
  1156. else {
  1157. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  1158. }
  1159. vmbreak;
  1160. }
  1161. vmcase(OP_BNOT) {
  1162. TValue *rb = vRB(i);
  1163. lua_Integer ib;
  1164. if (tointeger(rb, &ib)) {
  1165. setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
  1166. }
  1167. else {
  1168. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  1169. }
  1170. vmbreak;
  1171. }
  1172. vmcase(OP_NOT) {
  1173. TValue *rb = vRB(i);
  1174. int res = l_isfalse(rb); /* next assignment may change this value */
  1175. setbvalue(s2v(ra), res);
  1176. vmbreak;
  1177. }
  1178. vmcase(OP_LEN) {
  1179. Protect(luaV_objlen(L, ra, vRB(i)));
  1180. vmbreak;
  1181. }
  1182. vmcase(OP_CONCAT) {
  1183. int b = GETARG_B(i);
  1184. int c = GETARG_C(i);
  1185. StkId rb;
  1186. L->top = base + c + 1; /* mark the end of concat operands */
  1187. Protect(luaV_concat(L, c - b + 1));
  1188. ra = RA(i); /* 'luaV_concat' may invoke TMs and move the stack */
  1189. rb = base + b;
  1190. setobjs2s(L, ra, rb);
  1191. checkGC(L, (ra >= rb ? ra + 1 : rb));
  1192. L->top = ci->top; /* restore top */
  1193. vmbreak;
  1194. }
  1195. vmcase(OP_CLOSE) {
  1196. luaF_close(L, ra);
  1197. vmbreak;
  1198. }
  1199. vmcase(OP_JMP) {
  1200. dojump(ci, i, 0);
  1201. vmbreak;
  1202. }
  1203. vmcase(OP_EQ) {
  1204. TValue *rb = vRB(i);
  1205. TValue *rc = vRC(i);
  1206. Protect(
  1207. if (luaV_equalobj(L, rb, rc) != GETARG_A(i))
  1208. pc++;
  1209. else
  1210. donextjump(ci);
  1211. )
  1212. vmbreak;
  1213. }
  1214. vmcase(OP_LT) {
  1215. TValue *rb = vRB(i);
  1216. TValue *rc = vRC(i);
  1217. int res;
  1218. if (ttisinteger(rb) && ttisinteger(rc))
  1219. res = (ivalue(rb) < ivalue(rc));
  1220. else Protect(
  1221. res = luaV_lessthan(L, rb, rc);
  1222. )
  1223. if (res != GETARG_A(i))
  1224. pc++;
  1225. else
  1226. donextjump(ci);
  1227. vmbreak;
  1228. }
  1229. vmcase(OP_LE) {
  1230. TValue *rb = vRB(i);
  1231. TValue *rc = vRC(i);
  1232. int res;
  1233. if (ttisinteger(rb) && ttisinteger(rc))
  1234. res = (ivalue(rb) <= ivalue(rc));
  1235. else Protect(
  1236. res = luaV_lessequal(L, rb, rc);
  1237. )
  1238. if (res != GETARG_A(i))
  1239. pc++;
  1240. else
  1241. donextjump(ci);
  1242. vmbreak;
  1243. }
  1244. vmcase(OP_TEST) {
  1245. if (GETARG_C(i) ? l_isfalse(s2v(ra)) : !l_isfalse(s2v(ra)))
  1246. pc++;
  1247. else
  1248. donextjump(ci);
  1249. vmbreak;
  1250. }
  1251. vmcase(OP_TESTSET) {
  1252. TValue *rb = vRB(i);
  1253. if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
  1254. pc++;
  1255. else {
  1256. setobj2s(L, ra, rb);
  1257. donextjump(ci);
  1258. }
  1259. vmbreak;
  1260. }
  1261. vmcase(OP_CALL) {
  1262. int b = GETARG_B(i);
  1263. int nresults = GETARG_C(i) - 1;
  1264. int isC;
  1265. if (b != 0) /* fixed number of arguments? */
  1266. L->top = ra + b; /* top signals number of arguments */
  1267. /* else previous instruction set top */
  1268. Protect(isC = luaD_precall(L, ra, nresults));
  1269. if (isC) { /* C function? */
  1270. if (nresults >= 0) /* fixed number of results? */
  1271. L->top = ci->top; /* correct top */
  1272. /* else leave top for next instruction */
  1273. }
  1274. else { /* Lua function */
  1275. ci = L->ci;
  1276. goto newframe; /* restart luaV_execute over new Lua function */
  1277. }
  1278. vmbreak;
  1279. }
  1280. vmcase(OP_TAILCALL) {
  1281. int b = GETARG_B(i);
  1282. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  1283. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  1284. savepc(L);
  1285. if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */
  1286. Protect((void)0); /* update 'base' */
  1287. }
  1288. else {
  1289. /* tail call: put called frame (n) in place of caller one (o) */
  1290. CallInfo *nci = L->ci; /* called frame (new) */
  1291. CallInfo *oci = nci->previous; /* caller frame (old) */
  1292. StkId nfunc = nci->func; /* called function */
  1293. StkId ofunc = oci->func; /* caller function */
  1294. /* last stack slot filled by 'precall' */
  1295. StkId lim = nfunc + 1 + getproto(s2v(nfunc))->numparams;
  1296. int aux;
  1297. /* close all upvalues from previous call */
  1298. if (cl->p->sizep > 0) luaF_close(L, ofunc + 1);
  1299. /* move new frame into old one */
  1300. for (aux = 0; nfunc + aux < lim; aux++)
  1301. setobjs2s(L, ofunc + aux, nfunc + aux);
  1302. oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
  1303. oci->u.l.savedpc = nci->u.l.savedpc;
  1304. oci->callstatus |= CIST_TAIL; /* function was tail called */
  1305. ci = L->ci = oci; /* remove new frame */
  1306. lua_assert(L->top == ofunc + 1 + getproto(s2v(ofunc))->maxstacksize);
  1307. goto newframe; /* restart luaV_execute over new Lua function */
  1308. }
  1309. vmbreak;
  1310. }
  1311. vmcase(OP_RETURN) {
  1312. int b = GETARG_B(i);
  1313. if (cl->p->sizep > 0) luaF_close(L, base);
  1314. savepc(L);
  1315. b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra)));
  1316. if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */
  1317. return; /* external invocation: return */
  1318. else { /* invocation via reentry: continue execution */
  1319. ci = L->ci;
  1320. if (b) L->top = ci->top;
  1321. lua_assert(isLua(ci));
  1322. lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
  1323. goto newframe; /* restart luaV_execute over new Lua function */
  1324. }
  1325. }
  1326. vmcase(OP_FORLOOP) {
  1327. if (ttisinteger(s2v(ra))) { /* integer loop? */
  1328. lua_Integer step = ivalue(s2v(ra + 2));
  1329. lua_Integer idx = intop(+, ivalue(s2v(ra)), step); /* increment index */
  1330. lua_Integer limit = ivalue(s2v(ra + 1));
  1331. if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
  1332. pc -= GETARG_Bx(i); /* jump back */
  1333. chgivalue(s2v(ra), idx); /* update internal index... */
  1334. setivalue(s2v(ra + 3), idx); /* ...and external index */
  1335. }
  1336. }
  1337. else { /* floating loop */
  1338. lua_Number step = fltvalue(s2v(ra + 2));
  1339. lua_Number limit = fltvalue(s2v(ra + 1));
  1340. lua_Number idx = fltvalue(s2v(ra));
  1341. idx = luai_numadd(L, idx, step); /* inc. index */
  1342. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  1343. : luai_numle(limit, idx)) {
  1344. pc -= GETARG_Bx(i); /* jump back */
  1345. chgfltvalue(s2v(ra), idx); /* update internal index... */
  1346. setfltvalue(s2v(ra + 3), idx); /* ...and external index */
  1347. }
  1348. }
  1349. updatemask(L);
  1350. vmbreak;
  1351. }
  1352. vmcase(OP_FORPREP) {
  1353. TValue *init = s2v(ra);
  1354. TValue *plimit = s2v(ra + 1);
  1355. TValue *pstep = s2v(ra + 2);
  1356. lua_Integer ilimit;
  1357. int stopnow;
  1358. if (ttisinteger(init) && ttisinteger(pstep) &&
  1359. forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
  1360. /* all values are integer */
  1361. lua_Integer initv = (stopnow ? 0 : ivalue(init));
  1362. setivalue(plimit, ilimit);
  1363. setivalue(init, intop(-, initv, ivalue(pstep)));
  1364. }
  1365. else { /* try making all values floats */
  1366. lua_Number ninit; lua_Number nlimit; lua_Number nstep;
  1367. savepc(L); /* in case of errors */
  1368. if (!tonumber(plimit, &nlimit))
  1369. luaG_runerror(L, "'for' limit must be a number");
  1370. setfltvalue(plimit, nlimit);
  1371. if (!tonumber(pstep, &nstep))
  1372. luaG_runerror(L, "'for' step must be a number");
  1373. setfltvalue(pstep, nstep);
  1374. if (!tonumber(init, &ninit))
  1375. luaG_runerror(L, "'for' initial value must be a number");
  1376. setfltvalue(init, luai_numsub(L, ninit, nstep));
  1377. }
  1378. pc += GETARG_Bx(i);
  1379. vmbreak;
  1380. }
  1381. vmcase(OP_TFORCALL) {
  1382. StkId cb = ra + 3; /* call base */
  1383. setobjs2s(L, cb+2, ra+2);
  1384. setobjs2s(L, cb+1, ra+1);
  1385. setobjs2s(L, cb, ra);
  1386. L->top = cb + 3; /* func. + 2 args (state and index) */
  1387. Protect(luaD_call(L, cb, GETARG_C(i)));
  1388. L->top = ci->top;
  1389. i = *(pc++); /* go to next instruction */
  1390. ra = RA(i);
  1391. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  1392. goto l_tforloop;
  1393. }
  1394. vmcase(OP_TFORLOOP) {
  1395. l_tforloop:
  1396. if (!ttisnil(s2v(ra + 1))) { /* continue loop? */
  1397. setobjs2s(L, ra, ra + 1); /* save control variable */
  1398. pc -= GETARG_Bx(i); /* jump back */
  1399. }
  1400. vmbreak;
  1401. }
  1402. vmcase(OP_SETLIST) {
  1403. int n = GETARG_B(i);
  1404. int c = GETARG_C(i);
  1405. unsigned int last;
  1406. Table *h;
  1407. if (n == 0) n = cast_int(L->top - ra) - 1;
  1408. if (c == 0) {
  1409. c = GETARG_Ax(*pc); pc++;
  1410. }
  1411. h = hvalue(s2v(ra));
  1412. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  1413. savepc(L); /* in case of allocation errors */
  1414. if (last > h->sizearray) /* needs more space? */
  1415. luaH_resizearray(L, h, last); /* preallocate it at once */
  1416. for (; n > 0; n--) {
  1417. TValue *val = s2v(ra + n);
  1418. setobj2t(L, &h->array[last - 1], val);
  1419. last--;
  1420. luaC_barrierback(L, h, val);
  1421. }
  1422. L->top = ci->top; /* correct top (in case of previous open call) */
  1423. vmbreak;
  1424. }
  1425. vmcase(OP_CLOSURE) {
  1426. Proto *p = cl->p->p[GETARG_Bx(i)];
  1427. LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  1428. if (ncl == NULL) { /* no match? */
  1429. savepc(L); /* in case of allocation errors */
  1430. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  1431. }
  1432. else
  1433. setclLvalue2s(L, ra, ncl); /* push cashed closure */
  1434. checkGC(L, ra + 1);
  1435. vmbreak;
  1436. }
  1437. vmcase(OP_VARARG) {
  1438. int b = GETARG_B(i) - 1; /* required results */
  1439. TValue *vtab = vRC(i); /* vararg table */
  1440. Protect(luaT_getvarargs(L, vtab, ra, b));
  1441. vmbreak;
  1442. }
  1443. vmcase(OP_EXTRAARG) {
  1444. lua_assert(0);
  1445. vmbreak;
  1446. }
  1447. }
  1448. }
  1449. }
  1450. /* }================================================================== */