lvm.c 53 KB

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