lvm.c 47 KB

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