lvm.c 50 KB

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