lvm.c 56 KB

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