lvm.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. /*
  2. ** $Id: lvm.c,v 2.298 2017/10/04 15:49:24 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_SUBI:
  605. case OP_MULI: case OP_DIVI: case OP_IDIVI:
  606. case OP_MODI: case OP_POWI:
  607. case OP_ADD: case OP_SUB:
  608. case OP_MUL: case OP_DIV: case OP_IDIV:
  609. case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR:
  610. case OP_MOD: case OP_POW:
  611. case OP_UNM: case OP_BNOT: case OP_LEN:
  612. case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
  613. case OP_GETFIELD: case OP_SELF: {
  614. setobjs2s(L, base + GETARG_A(inst), --L->top);
  615. break;
  616. }
  617. case OP_LE: case OP_LT: case OP_EQ: {
  618. int res = !l_isfalse(s2v(L->top - 1));
  619. L->top--;
  620. if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
  621. lua_assert(op == OP_LE);
  622. ci->callstatus ^= CIST_LEQ; /* clear mark */
  623. res = !res; /* negate result */
  624. }
  625. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  626. if (res != GETARG_A(inst)) /* condition failed? */
  627. ci->u.l.savedpc++; /* skip jump instruction */
  628. break;
  629. }
  630. case OP_CONCAT: {
  631. StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */
  632. int b = GETARG_B(inst); /* first element to concatenate */
  633. int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */
  634. setobjs2s(L, top - 2, top); /* put TM result in proper position */
  635. if (total > 1) { /* are there elements to concat? */
  636. L->top = top - 1; /* top is one after last element (at top-2) */
  637. luaV_concat(L, total); /* concat them (may yield again) */
  638. }
  639. /* move final result to final position */
  640. setobjs2s(L, ci->func + 1 + GETARG_A(inst), L->top - 1);
  641. L->top = ci->top; /* restore top */
  642. break;
  643. }
  644. case OP_TFORCALL: {
  645. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
  646. L->top = ci->top; /* correct top */
  647. break;
  648. }
  649. case OP_CALL: {
  650. if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */
  651. L->top = ci->top; /* adjust results */
  652. break;
  653. }
  654. case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
  655. case OP_SETI: case OP_SETFIELD:
  656. break;
  657. default: lua_assert(0);
  658. }
  659. }
  660. /*
  661. ** {==================================================================
  662. ** Function 'luaV_execute': main interpreter loop
  663. ** ===================================================================
  664. */
  665. /*
  666. ** some macros for common tasks in 'luaV_execute'
  667. */
  668. #define RA(i) (base+GETARG_A(i))
  669. #define RB(i) (base+GETARG_B(i))
  670. #define vRB(i) s2v(RB(i))
  671. #define KB(i) (k+GETARG_B(i))
  672. #define RC(i) (base+GETARG_C(i))
  673. #define vRC(i) s2v(RC(i))
  674. #define KC(i) (k+GETARG_C(i))
  675. #define RKC(i) ((GETARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
  676. #define updatemask(L) (mask = L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT))
  677. /*
  678. ** Execute a jump instruction. The 'updatemask' allows signals to stop
  679. ** tight loops. (Without it, the local copy of 'mask' could never change.)
  680. */
  681. #define dojump(ci,i,e) { pc += GETARG_sBx(i) + e; updatemask(L); }
  682. /* for test instructions, execute the jump instruction that follows it */
  683. #define donextjump(ci) { i = *pc; dojump(ci, i, 1); }
  684. /*
  685. ** Whenever code can raise errors (including memory errors), the global
  686. ** 'pc' must be correct to report occasional errors.
  687. */
  688. #define savepc(L) (ci->u.l.savedpc = pc)
  689. /*
  690. ** Protect code that, in general, can raise errors, reallocate the
  691. ** stack, and change the hooks.
  692. */
  693. #define Protect(code) \
  694. { savepc(L); {code;}; base = ci->func + 1; updatemask(L); }
  695. #define checkGC(L,c) \
  696. { luaC_condGC(L, L->top = (c), /* limit of live values */ \
  697. Protect(L->top = ci->top)); /* restore top */ \
  698. luai_threadyield(L); }
  699. /* fetch an instruction and prepare its execution */
  700. #define vmfetch() { \
  701. i = *(pc++); \
  702. if (mask) Protect(luaG_traceexec(L)); \
  703. ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \
  704. }
  705. #define vmdispatch(o) switch(o)
  706. #define vmcase(l) case l:
  707. #define vmbreak break
  708. void luaV_execute (lua_State *L) {
  709. CallInfo *ci = L->ci;
  710. LClosure *cl;
  711. TValue *k;
  712. StkId base; /* local copy of 'ci->func + 1' */
  713. int mask; /* local copy of 'L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)' */
  714. const Instruction *pc; /* local copy of 'ci->u.l.savedpc' */
  715. ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */
  716. newframe: /* reentry point when frame changes (call/return) */
  717. lua_assert(ci == L->ci);
  718. cl = clLvalue(s2v(ci->func)); /* local reference to function's closure */
  719. k = cl->p->k; /* local reference to function's constant table */
  720. updatemask(L);
  721. base = ci->func + 1;
  722. pc = ci->u.l.savedpc;
  723. /* main loop of interpreter */
  724. for (;;) {
  725. Instruction i;
  726. StkId ra;
  727. vmfetch();
  728. lua_assert(base == ci->func + 1);
  729. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  730. vmdispatch (GET_OPCODE(i)) {
  731. vmcase(OP_MOVE) {
  732. setobjs2s(L, ra, RB(i));
  733. vmbreak;
  734. }
  735. vmcase(OP_LOADK) {
  736. TValue *rb = k + GETARG_Bx(i);
  737. setobj2s(L, ra, rb);
  738. vmbreak;
  739. }
  740. vmcase(OP_LOADI) {
  741. lua_Integer b = GETARG_sBx(i);
  742. setivalue(s2v(ra), b);
  743. vmbreak;
  744. }
  745. vmcase(OP_LOADF) {
  746. int b = GETARG_sBx(i);
  747. setfltvalue(s2v(ra), cast_num(b));
  748. vmbreak;
  749. }
  750. vmcase(OP_LOADKX) {
  751. TValue *rb;
  752. rb = k + GETARG_Ax(*pc); pc++;
  753. setobj2s(L, ra, rb);
  754. vmbreak;
  755. }
  756. vmcase(OP_LOADBOOL) {
  757. setbvalue(s2v(ra), GETARG_B(i));
  758. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  759. vmbreak;
  760. }
  761. vmcase(OP_LOADNIL) {
  762. int b = GETARG_B(i);
  763. do {
  764. setnilvalue(s2v(ra++));
  765. } while (b--);
  766. vmbreak;
  767. }
  768. vmcase(OP_GETUPVAL) {
  769. int b = GETARG_B(i);
  770. setobj2s(L, ra, cl->upvals[b]->v);
  771. vmbreak;
  772. }
  773. vmcase(OP_SETUPVAL) {
  774. UpVal *uv = cl->upvals[GETARG_B(i)];
  775. setobj(L, uv->v, s2v(ra));
  776. luaC_barrier(L, uv, s2v(ra));
  777. vmbreak;
  778. }
  779. vmcase(OP_GETTABUP) {
  780. const TValue *slot;
  781. TValue *upval = cl->upvals[GETARG_B(i)]->v;
  782. TValue *rc = KC(i);
  783. TString *key = tsvalue(rc); /* key must be a string */
  784. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  785. setobj2s(L, ra, slot);
  786. }
  787. else Protect(luaV_finishget(L, upval, rc, ra, slot));
  788. vmbreak;
  789. }
  790. vmcase(OP_GETTABLE) {
  791. const TValue *slot;
  792. TValue *rb = vRB(i);
  793. TValue *rc = vRC(i);
  794. lua_Unsigned n;
  795. if (ttisinteger(rc) /* fast track for integers? */
  796. ? (n = ivalue(rc), luaV_fastgeti(L, rb, n, slot))
  797. : luaV_fastget(L, rb, rc, slot, luaH_get)) {
  798. setobj2s(L, ra, slot);
  799. }
  800. else
  801. Protect(luaV_finishget(L, rb, rc, ra, slot));
  802. vmbreak;
  803. }
  804. vmcase(OP_GETI) {
  805. const TValue *slot;
  806. TValue *rb = vRB(i);
  807. int c = GETARG_C(i);
  808. if (luaV_fastgeti(L, rb, c, slot)) {
  809. setobj2s(L, ra, slot);
  810. }
  811. else {
  812. TValue key;
  813. setivalue(&key, c);
  814. Protect(luaV_finishget(L, rb, &key, ra, slot));
  815. }
  816. vmbreak;
  817. }
  818. vmcase(OP_GETFIELD) {
  819. const TValue *slot;
  820. TValue *rb = vRB(i);
  821. TValue *rc = KC(i);
  822. TString *key = tsvalue(rc); /* key must be a string */
  823. if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) {
  824. setobj2s(L, ra, slot);
  825. }
  826. else Protect(luaV_finishget(L, rb, rc, ra, slot));
  827. vmbreak;
  828. }
  829. vmcase(OP_SETTABUP) {
  830. const TValue *slot;
  831. TValue *upval = cl->upvals[GETARG_A(i)]->v;
  832. TValue *rb = KB(i);
  833. TValue *rc = RKC(i);
  834. TString *key = tsvalue(rb); /* key must be a string */
  835. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  836. luaV_finishfastset(L, upval, slot, rc);
  837. }
  838. else
  839. Protect(luaV_finishset(L, upval, rb, rc, slot));
  840. vmbreak;
  841. }
  842. vmcase(OP_SETTABLE) {
  843. const TValue *slot;
  844. TValue *rb = vRB(i); /* key (table is in 'ra') */
  845. TValue *rc = RKC(i); /* value */
  846. lua_Unsigned n;
  847. if (ttisinteger(rb) /* fast track for integers? */
  848. ? (n = ivalue(rb), luaV_fastgeti(L, s2v(ra), n, slot))
  849. : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) {
  850. luaV_finishfastset(L, s2v(ra), slot, rc);
  851. }
  852. else
  853. Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
  854. vmbreak;
  855. }
  856. vmcase(OP_SETI) {
  857. const TValue *slot;
  858. int c = GETARG_B(i);
  859. TValue *rc = RKC(i);
  860. if (luaV_fastgeti(L, s2v(ra), c, slot)) {
  861. luaV_finishfastset(L, s2v(ra), slot, rc);
  862. }
  863. else {
  864. TValue key;
  865. setivalue(&key, c);
  866. Protect(luaV_finishset(L, s2v(ra), &key, rc, slot));
  867. }
  868. vmbreak;
  869. }
  870. vmcase(OP_SETFIELD) {
  871. const TValue *slot;
  872. TValue *rb = KB(i);
  873. TValue *rc = RKC(i);
  874. TString *key = tsvalue(rb); /* key must be a string */
  875. if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) {
  876. luaV_finishfastset(L, s2v(ra), slot, rc);
  877. }
  878. else
  879. Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
  880. vmbreak;
  881. }
  882. vmcase(OP_NEWTABLE) {
  883. int b = GETARG_B(i);
  884. int c = GETARG_C(i);
  885. Table *t;
  886. savepc(L); /* in case of allocation errors */
  887. t = luaH_new(L);
  888. sethvalue2s(L, ra, t);
  889. if (b != 0 || c != 0)
  890. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
  891. checkGC(L, ra + 1);
  892. vmbreak;
  893. }
  894. vmcase(OP_SELF) {
  895. const TValue *slot;
  896. TValue *rb = vRB(i);
  897. TValue *rc = RKC(i);
  898. TString *key = tsvalue(rc); /* key must be a string */
  899. setobj2s(L, ra + 1, rb);
  900. if (luaV_fastget(L, rb, key, slot, luaH_getstr)) {
  901. setobj2s(L, ra, slot);
  902. }
  903. else Protect(luaV_finishget(L, rb, rc, ra, slot));
  904. vmbreak;
  905. }
  906. vmcase(OP_ADDI) {
  907. TValue *rb = vRB(i);
  908. int ic = GETARG_sC(i);
  909. lua_Number nb;
  910. if (ttisinteger(rb)) {
  911. setivalue(s2v(ra), intop(+, ivalue(rb), ic));
  912. }
  913. else if (tonumberns(rb, nb)) {
  914. setfltvalue(s2v(ra), luai_numadd(L, nb, cast_num(ic)));
  915. }
  916. else
  917. Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_ADD));
  918. vmbreak;
  919. }
  920. vmcase(OP_SUBI) {
  921. TValue *rb = vRB(i);
  922. int ic = GETARG_sC(i);
  923. lua_Number nb;
  924. if (ttisinteger(rb)) {
  925. setivalue(s2v(ra), intop(-, ivalue(rb), ic));
  926. }
  927. else if (tonumberns(rb, nb)) {
  928. setfltvalue(s2v(ra), luai_numsub(L, nb, cast_num(ic)));
  929. }
  930. else
  931. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_SUB));
  932. vmbreak;
  933. }
  934. vmcase(OP_MULI) {
  935. TValue *rb = vRB(i);
  936. int ic = GETARG_sC(i);
  937. lua_Number nb;
  938. if (ttisinteger(rb)) {
  939. setivalue(s2v(ra), intop(*, ivalue(rb), ic));
  940. }
  941. else if (tonumberns(rb, nb)) {
  942. setfltvalue(s2v(ra), luai_nummul(L, nb, cast_num(ic)));
  943. }
  944. else
  945. Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_MUL));
  946. vmbreak;
  947. }
  948. vmcase(OP_MODI) {
  949. TValue *rb = vRB(i);
  950. int ic = GETARG_sC(i);
  951. lua_Number nb;
  952. if (ttisinteger(rb)) {
  953. setivalue(s2v(ra), luaV_mod(L, ivalue(rb), ic));
  954. }
  955. else if (tonumberns(rb, nb)) {
  956. lua_Number m;
  957. lua_Number nc = cast_num(ic);
  958. luai_nummod(L, nb, nc, m);
  959. setfltvalue(s2v(ra), m);
  960. }
  961. else
  962. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_MOD));
  963. vmbreak;
  964. }
  965. vmcase(OP_POWI) {
  966. TValue *rb = vRB(i);
  967. int ic = GETARG_sC(i);
  968. lua_Number nb;
  969. if (tonumberns(rb, nb)) {
  970. lua_Number nc = cast_num(ic);
  971. setfltvalue(s2v(ra), luai_numpow(L, nb, nc));
  972. }
  973. else
  974. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_POW));
  975. vmbreak;
  976. }
  977. vmcase(OP_DIVI) {
  978. TValue *rb = vRB(i);
  979. int ic = GETARG_sC(i);
  980. lua_Number nb;
  981. if (tonumberns(rb, nb)) {
  982. lua_Number nc = cast_num(ic);
  983. setfltvalue(s2v(ra), luai_numdiv(L, nb, nc));
  984. }
  985. else
  986. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_DIV));
  987. vmbreak;
  988. }
  989. vmcase(OP_IDIVI) {
  990. TValue *rb = vRB(i);
  991. int ic = GETARG_sC(i);
  992. lua_Number nb;
  993. if (ttisinteger(rb)) {
  994. setivalue(s2v(ra), luaV_div(L, ivalue(rb), ic));
  995. }
  996. else if (tonumberns(rb, nb)) {
  997. lua_Number nc = cast_num(ic);
  998. setfltvalue(s2v(ra), luai_numdiv(L, nb, nc));
  999. }
  1000. else
  1001. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_IDIV));
  1002. vmbreak;
  1003. }
  1004. vmcase(OP_ADD) {
  1005. TValue *rb = vRB(i);
  1006. TValue *rc = vRC(i);
  1007. lua_Number nb; lua_Number nc;
  1008. if (ttisinteger(rb) && ttisinteger(rc)) {
  1009. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1010. setivalue(s2v(ra), intop(+, ib, ic));
  1011. }
  1012. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1013. setfltvalue(s2v(ra), luai_numadd(L, nb, nc));
  1014. }
  1015. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
  1016. vmbreak;
  1017. }
  1018. vmcase(OP_SUB) {
  1019. TValue *rb = vRB(i);
  1020. TValue *rc = vRC(i);
  1021. lua_Number nb; lua_Number nc;
  1022. if (ttisinteger(rb) && ttisinteger(rc)) {
  1023. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1024. setivalue(s2v(ra), intop(-, ib, ic));
  1025. }
  1026. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1027. setfltvalue(s2v(ra), luai_numsub(L, nb, nc));
  1028. }
  1029. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
  1030. vmbreak;
  1031. }
  1032. vmcase(OP_MUL) {
  1033. TValue *rb = vRB(i);
  1034. TValue *rc = vRC(i);
  1035. lua_Number nb; lua_Number nc;
  1036. if (ttisinteger(rb) && ttisinteger(rc)) {
  1037. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1038. setivalue(s2v(ra), intop(*, ib, ic));
  1039. }
  1040. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1041. setfltvalue(s2v(ra), luai_nummul(L, nb, nc));
  1042. }
  1043. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
  1044. vmbreak;
  1045. }
  1046. vmcase(OP_DIV) { /* float division (always with floats) */
  1047. TValue *rb = vRB(i);
  1048. TValue *rc = vRC(i);
  1049. lua_Number nb; lua_Number nc;
  1050. if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1051. setfltvalue(s2v(ra), luai_numdiv(L, nb, nc));
  1052. }
  1053. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
  1054. vmbreak;
  1055. }
  1056. vmcase(OP_BAND) {
  1057. TValue *rb = vRB(i);
  1058. TValue *rc = vRC(i);
  1059. lua_Integer ib; lua_Integer ic;
  1060. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  1061. setivalue(s2v(ra), intop(&, ib, ic));
  1062. }
  1063. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); }
  1064. vmbreak;
  1065. }
  1066. vmcase(OP_BOR) {
  1067. TValue *rb = vRB(i);
  1068. TValue *rc = vRC(i);
  1069. lua_Integer ib; lua_Integer ic;
  1070. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  1071. setivalue(s2v(ra), intop(|, ib, ic));
  1072. }
  1073. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); }
  1074. vmbreak;
  1075. }
  1076. vmcase(OP_BXOR) {
  1077. TValue *rb = vRB(i);
  1078. TValue *rc = vRC(i);
  1079. lua_Integer ib; lua_Integer ic;
  1080. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  1081. setivalue(s2v(ra), intop(^, ib, ic));
  1082. }
  1083. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); }
  1084. vmbreak;
  1085. }
  1086. vmcase(OP_SHL) {
  1087. TValue *rb = vRB(i);
  1088. TValue *rc = vRC(i);
  1089. lua_Integer ib; lua_Integer ic;
  1090. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  1091. setivalue(s2v(ra), luaV_shiftl(ib, ic));
  1092. }
  1093. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); }
  1094. vmbreak;
  1095. }
  1096. vmcase(OP_SHR) {
  1097. TValue *rb = vRB(i);
  1098. TValue *rc = vRC(i);
  1099. lua_Integer ib; lua_Integer ic;
  1100. if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
  1101. setivalue(s2v(ra), luaV_shiftl(ib, -ic));
  1102. }
  1103. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); }
  1104. vmbreak;
  1105. }
  1106. vmcase(OP_MOD) {
  1107. TValue *rb = vRB(i);
  1108. TValue *rc = vRC(i);
  1109. lua_Number nb; lua_Number nc;
  1110. if (ttisinteger(rb) && ttisinteger(rc)) {
  1111. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1112. setivalue(s2v(ra), luaV_mod(L, ib, ic));
  1113. }
  1114. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1115. lua_Number m;
  1116. luai_nummod(L, nb, nc, m);
  1117. setfltvalue(s2v(ra), m);
  1118. }
  1119. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
  1120. vmbreak;
  1121. }
  1122. vmcase(OP_IDIV) { /* floor division */
  1123. TValue *rb = vRB(i);
  1124. TValue *rc = vRC(i);
  1125. lua_Number nb; lua_Number nc;
  1126. if (ttisinteger(rb) && ttisinteger(rc)) {
  1127. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1128. setivalue(s2v(ra), luaV_div(L, ib, ic));
  1129. }
  1130. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1131. setfltvalue(s2v(ra), luai_numidiv(L, nb, nc));
  1132. }
  1133. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); }
  1134. vmbreak;
  1135. }
  1136. vmcase(OP_POW) {
  1137. TValue *rb = vRB(i);
  1138. TValue *rc = vRC(i);
  1139. lua_Number nb; lua_Number nc;
  1140. if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1141. setfltvalue(s2v(ra), luai_numpow(L, nb, nc));
  1142. }
  1143. else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
  1144. vmbreak;
  1145. }
  1146. vmcase(OP_UNM) {
  1147. TValue *rb = vRB(i);
  1148. lua_Number nb;
  1149. if (ttisinteger(rb)) {
  1150. lua_Integer ib = ivalue(rb);
  1151. setivalue(s2v(ra), intop(-, 0, ib));
  1152. }
  1153. else if (tonumberns(rb, nb)) {
  1154. setfltvalue(s2v(ra), luai_numunm(L, nb));
  1155. }
  1156. else {
  1157. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  1158. }
  1159. vmbreak;
  1160. }
  1161. vmcase(OP_BNOT) {
  1162. TValue *rb = vRB(i);
  1163. lua_Integer ib;
  1164. if (tointeger(rb, &ib)) {
  1165. setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
  1166. }
  1167. else {
  1168. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  1169. }
  1170. vmbreak;
  1171. }
  1172. vmcase(OP_NOT) {
  1173. TValue *rb = vRB(i);
  1174. int res = l_isfalse(rb); /* next assignment may change this value */
  1175. setbvalue(s2v(ra), res);
  1176. vmbreak;
  1177. }
  1178. vmcase(OP_LEN) {
  1179. Protect(luaV_objlen(L, ra, vRB(i)));
  1180. vmbreak;
  1181. }
  1182. vmcase(OP_CONCAT) {
  1183. int b = GETARG_B(i);
  1184. int c = GETARG_C(i);
  1185. StkId rb;
  1186. L->top = base + c + 1; /* mark the end of concat operands */
  1187. Protect(luaV_concat(L, c - b + 1));
  1188. ra = RA(i); /* 'luaV_concat' may invoke TMs and move the stack */
  1189. rb = base + b;
  1190. setobjs2s(L, ra, rb);
  1191. checkGC(L, (ra >= rb ? ra + 1 : rb));
  1192. L->top = ci->top; /* restore top */
  1193. vmbreak;
  1194. }
  1195. vmcase(OP_CLOSE) {
  1196. luaF_close(L, ra);
  1197. vmbreak;
  1198. }
  1199. vmcase(OP_JMP) {
  1200. dojump(ci, i, 0);
  1201. vmbreak;
  1202. }
  1203. vmcase(OP_EQ) {
  1204. TValue *rb = vRB(i);
  1205. TValue *rc = vRC(i);
  1206. Protect(
  1207. if (luaV_equalobj(L, rb, rc) != GETARG_A(i))
  1208. pc++;
  1209. else
  1210. donextjump(ci);
  1211. )
  1212. vmbreak;
  1213. }
  1214. vmcase(OP_LT) {
  1215. TValue *rb = vRB(i);
  1216. TValue *rc = vRC(i);
  1217. int res;
  1218. if (ttisinteger(rb) && ttisinteger(rc))
  1219. res = (ivalue(rb) < ivalue(rc));
  1220. else Protect(
  1221. res = luaV_lessthan(L, rb, rc);
  1222. )
  1223. if (res != GETARG_A(i))
  1224. pc++;
  1225. else
  1226. donextjump(ci);
  1227. vmbreak;
  1228. }
  1229. vmcase(OP_LE) {
  1230. TValue *rb = vRB(i);
  1231. TValue *rc = vRC(i);
  1232. int res;
  1233. if (ttisinteger(rb) && ttisinteger(rc))
  1234. res = (ivalue(rb) <= ivalue(rc));
  1235. else Protect(
  1236. res = luaV_lessequal(L, rb, rc);
  1237. )
  1238. if (res != GETARG_A(i))
  1239. pc++;
  1240. else
  1241. donextjump(ci);
  1242. vmbreak;
  1243. }
  1244. vmcase(OP_TEST) {
  1245. if (GETARG_C(i) ? l_isfalse(s2v(ra)) : !l_isfalse(s2v(ra)))
  1246. pc++;
  1247. else
  1248. donextjump(ci);
  1249. vmbreak;
  1250. }
  1251. vmcase(OP_TESTSET) {
  1252. TValue *rb = vRB(i);
  1253. if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
  1254. pc++;
  1255. else {
  1256. setobj2s(L, ra, rb);
  1257. donextjump(ci);
  1258. }
  1259. vmbreak;
  1260. }
  1261. vmcase(OP_CALL) {
  1262. int b = GETARG_B(i);
  1263. int nresults = GETARG_C(i) - 1;
  1264. int isC;
  1265. if (b != 0) /* fixed number of arguments? */
  1266. L->top = ra + b; /* top signals number of arguments */
  1267. /* else previous instruction set top */
  1268. Protect(isC = luaD_precall(L, ra, nresults));
  1269. if (isC) { /* C function? */
  1270. if (nresults >= 0) /* fixed number of results? */
  1271. L->top = ci->top; /* correct top */
  1272. /* else leave top for next instruction */
  1273. }
  1274. else { /* Lua function */
  1275. ci = L->ci;
  1276. goto newframe; /* restart luaV_execute over new Lua function */
  1277. }
  1278. vmbreak;
  1279. }
  1280. vmcase(OP_TAILCALL) {
  1281. int b = GETARG_B(i);
  1282. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  1283. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  1284. savepc(L);
  1285. if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */
  1286. Protect((void)0); /* update 'base' */
  1287. }
  1288. else {
  1289. /* tail call: put called frame (n) in place of caller one (o) */
  1290. CallInfo *nci = L->ci; /* called frame (new) */
  1291. CallInfo *oci = nci->previous; /* caller frame (old) */
  1292. StkId nfunc = nci->func; /* called function */
  1293. StkId ofunc = oci->func; /* caller function */
  1294. /* last stack slot filled by 'precall' */
  1295. StkId lim = nci->func + 1 + getproto(s2v(nfunc))->numparams;
  1296. int aux;
  1297. /* close all upvalues from previous call */
  1298. if (cl->p->sizep > 0) luaF_close(L, oci->func + 1);
  1299. /* move new frame into old one */
  1300. for (aux = 0; nfunc + aux < lim; aux++)
  1301. setobjs2s(L, ofunc + aux, nfunc + aux);
  1302. oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */
  1303. oci->u.l.savedpc = nci->u.l.savedpc;
  1304. oci->callstatus |= CIST_TAIL; /* function was tail called */
  1305. ci = L->ci = oci; /* remove new frame */
  1306. lua_assert(L->top ==
  1307. oci->func + 1 + getproto(s2v(ofunc))->maxstacksize);
  1308. goto newframe; /* restart luaV_execute over new Lua function */
  1309. }
  1310. vmbreak;
  1311. }
  1312. vmcase(OP_RETURN) {
  1313. int b = GETARG_B(i);
  1314. if (cl->p->sizep > 0) luaF_close(L, base);
  1315. savepc(L);
  1316. b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra)));
  1317. if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */
  1318. return; /* external invocation: return */
  1319. else { /* invocation via reentry: continue execution */
  1320. ci = L->ci;
  1321. if (b) L->top = ci->top;
  1322. lua_assert(isLua(ci));
  1323. lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
  1324. goto newframe; /* restart luaV_execute over new Lua function */
  1325. }
  1326. }
  1327. vmcase(OP_FORLOOP) {
  1328. if (ttisinteger(s2v(ra))) { /* integer loop? */
  1329. lua_Integer step = ivalue(s2v(ra + 2));
  1330. lua_Integer idx = intop(+, ivalue(s2v(ra)), step); /* increment index */
  1331. lua_Integer limit = ivalue(s2v(ra + 1));
  1332. if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
  1333. pc -= GETARG_Bx(i); /* jump back */
  1334. chgivalue(s2v(ra), idx); /* update internal index... */
  1335. setivalue(s2v(ra + 3), idx); /* ...and external index */
  1336. }
  1337. }
  1338. else { /* floating loop */
  1339. lua_Number step = fltvalue(s2v(ra + 2));
  1340. lua_Number limit = fltvalue(s2v(ra + 1));
  1341. lua_Number idx = fltvalue(s2v(ra));
  1342. idx = luai_numadd(L, idx, step); /* inc. index */
  1343. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  1344. : luai_numle(limit, idx)) {
  1345. pc -= GETARG_Bx(i); /* jump back */
  1346. chgfltvalue(s2v(ra), idx); /* update internal index... */
  1347. setfltvalue(s2v(ra + 3), idx); /* ...and external index */
  1348. }
  1349. }
  1350. updatemask(L);
  1351. vmbreak;
  1352. }
  1353. vmcase(OP_FORPREP) {
  1354. TValue *init = s2v(ra);
  1355. TValue *plimit = s2v(ra + 1);
  1356. TValue *pstep = s2v(ra + 2);
  1357. lua_Integer ilimit;
  1358. int stopnow;
  1359. if (ttisinteger(init) && ttisinteger(pstep) &&
  1360. forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
  1361. /* all values are integer */
  1362. lua_Integer initv = (stopnow ? 0 : ivalue(init));
  1363. setivalue(plimit, ilimit);
  1364. setivalue(init, intop(-, initv, ivalue(pstep)));
  1365. }
  1366. else { /* try making all values floats */
  1367. lua_Number ninit; lua_Number nlimit; lua_Number nstep;
  1368. savepc(L); /* in case of errors */
  1369. if (!tonumber(plimit, &nlimit))
  1370. luaG_runerror(L, "'for' limit must be a number");
  1371. setfltvalue(plimit, nlimit);
  1372. if (!tonumber(pstep, &nstep))
  1373. luaG_runerror(L, "'for' step must be a number");
  1374. setfltvalue(pstep, nstep);
  1375. if (!tonumber(init, &ninit))
  1376. luaG_runerror(L, "'for' initial value must be a number");
  1377. setfltvalue(init, luai_numsub(L, ninit, nstep));
  1378. }
  1379. pc += GETARG_Bx(i);
  1380. vmbreak;
  1381. }
  1382. vmcase(OP_TFORCALL) {
  1383. StkId cb = ra + 3; /* call base */
  1384. setobjs2s(L, cb+2, ra+2);
  1385. setobjs2s(L, cb+1, ra+1);
  1386. setobjs2s(L, cb, ra);
  1387. L->top = cb + 3; /* func. + 2 args (state and index) */
  1388. Protect(luaD_call(L, cb, GETARG_C(i)));
  1389. L->top = ci->top;
  1390. i = *(pc++); /* go to next instruction */
  1391. ra = RA(i);
  1392. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  1393. goto l_tforloop;
  1394. }
  1395. vmcase(OP_TFORLOOP) {
  1396. l_tforloop:
  1397. if (!ttisnil(s2v(ra + 1))) { /* continue loop? */
  1398. setobjs2s(L, ra, ra + 1); /* save control variable */
  1399. pc -= GETARG_Bx(i); /* jump back */
  1400. }
  1401. vmbreak;
  1402. }
  1403. vmcase(OP_SETLIST) {
  1404. int n = GETARG_B(i);
  1405. int c = GETARG_C(i);
  1406. unsigned int last;
  1407. Table *h;
  1408. if (n == 0) n = cast_int(L->top - ra) - 1;
  1409. if (c == 0) {
  1410. c = GETARG_Ax(*pc); pc++;
  1411. }
  1412. h = hvalue(s2v(ra));
  1413. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  1414. savepc(L); /* in case of allocation errors */
  1415. if (last > h->sizearray) /* needs more space? */
  1416. luaH_resizearray(L, h, last); /* preallocate it at once */
  1417. for (; n > 0; n--) {
  1418. TValue *val = s2v(ra + n);
  1419. setobj2t(L, &h->array[last - 1], val);
  1420. last--;
  1421. luaC_barrierback(L, h, val);
  1422. }
  1423. L->top = ci->top; /* correct top (in case of previous open call) */
  1424. vmbreak;
  1425. }
  1426. vmcase(OP_CLOSURE) {
  1427. Proto *p = cl->p->p[GETARG_Bx(i)];
  1428. LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  1429. if (ncl == NULL) { /* no match? */
  1430. savepc(L); /* in case of allocation errors */
  1431. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  1432. }
  1433. else
  1434. setclLvalue2s(L, ra, ncl); /* push cashed closure */
  1435. checkGC(L, ra + 1);
  1436. vmbreak;
  1437. }
  1438. vmcase(OP_VARARG) {
  1439. int b = GETARG_B(i) - 1; /* required results */
  1440. TValue *vtab = vRC(i); /* vararg table */
  1441. Protect(luaT_getvarargs(L, vtab, ra, b));
  1442. vmbreak;
  1443. }
  1444. vmcase(OP_EXTRAARG) {
  1445. lua_assert(0);
  1446. vmbreak;
  1447. }
  1448. }
  1449. }
  1450. }
  1451. /* }================================================================== */