lvm.c 54 KB

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