lvm.c 51 KB

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