lvm.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  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. luaT_callTMres(L, tm, t1, t2, L->top); /* call TM */
  462. return !l_isfalse(s2v(L->top));
  463. }
  464. }
  465. /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
  466. #define tostring(L,o) \
  467. (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
  468. #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0)
  469. /* copy strings in stack from top - n up to top - 1 to buffer */
  470. static void copy2buff (StkId top, int n, char *buff) {
  471. size_t tl = 0; /* size already copied */
  472. do {
  473. size_t l = vslen(s2v(top - n)); /* length of string being copied */
  474. memcpy(buff + tl, svalue(s2v(top - n)), l * sizeof(char));
  475. tl += l;
  476. } while (--n > 0);
  477. }
  478. /*
  479. ** Main operation for concatenation: concat 'total' values in the stack,
  480. ** from 'L->top - total' up to 'L->top - 1'.
  481. */
  482. void luaV_concat (lua_State *L, int total) {
  483. lua_assert(total >= 2);
  484. do {
  485. StkId top = L->top;
  486. int n = 2; /* number of elements handled in this pass (at least 2) */
  487. if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) ||
  488. !tostring(L, s2v(top - 1)))
  489. luaT_tryconcatTM(L);
  490. else if (isemptystr(s2v(top - 1))) /* second operand is empty? */
  491. cast_void(tostring(L, s2v(top - 2))); /* result is first operand */
  492. else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */
  493. setobjs2s(L, top - 2, top - 1); /* result is second op. */
  494. }
  495. else {
  496. /* at least two non-empty string values; get as many as possible */
  497. size_t tl = vslen(s2v(top - 1));
  498. TString *ts;
  499. /* collect total length and number of strings */
  500. for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) {
  501. size_t l = vslen(s2v(top - n - 1));
  502. if (unlikely(l >= (MAX_SIZE/sizeof(char)) - tl))
  503. luaG_runerror(L, "string length overflow");
  504. tl += l;
  505. }
  506. if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */
  507. char buff[LUAI_MAXSHORTLEN];
  508. copy2buff(top, n, buff); /* copy strings to buffer */
  509. ts = luaS_newlstr(L, buff, tl);
  510. }
  511. else { /* long string; copy strings directly to final result */
  512. ts = luaS_createlngstrobj(L, tl);
  513. copy2buff(top, n, getstr(ts));
  514. }
  515. setsvalue2s(L, top - n, ts); /* create result */
  516. }
  517. total -= n-1; /* got 'n' strings to create 1 new */
  518. L->top -= n-1; /* popped 'n' strings and pushed one */
  519. } while (total > 1); /* repeat until only 1 result left */
  520. }
  521. /*
  522. ** Main operation 'ra = #rb'.
  523. */
  524. void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
  525. const TValue *tm;
  526. switch (ttypetag(rb)) {
  527. case LUA_TTABLE: {
  528. Table *h = hvalue(rb);
  529. tm = fasttm(L, h->metatable, TM_LEN);
  530. if (tm) break; /* metamethod? break switch to call it */
  531. setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */
  532. return;
  533. }
  534. case LUA_TSHRSTR: {
  535. setivalue(s2v(ra), tsvalue(rb)->shrlen);
  536. return;
  537. }
  538. case LUA_TLNGSTR: {
  539. setivalue(s2v(ra), tsvalue(rb)->u.lnglen);
  540. return;
  541. }
  542. default: { /* try metamethod */
  543. tm = luaT_gettmbyobj(L, rb, TM_LEN);
  544. if (unlikely(notm(tm))) /* no metamethod? */
  545. luaG_typeerror(L, rb, "get length of");
  546. break;
  547. }
  548. }
  549. luaT_callTMres(L, tm, rb, rb, ra);
  550. }
  551. /*
  552. ** Integer division; return 'm // n', that is, floor(m/n).
  553. ** C division truncates its result (rounds towards zero).
  554. ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
  555. ** otherwise 'floor(q) == trunc(q) - 1'.
  556. */
  557. lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) {
  558. if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
  559. if (n == 0)
  560. luaG_runerror(L, "attempt to divide by zero");
  561. return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */
  562. }
  563. else {
  564. lua_Integer q = m / n; /* perform C division */
  565. if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */
  566. q -= 1; /* correct result for different rounding */
  567. return q;
  568. }
  569. }
  570. /*
  571. ** Integer modulus; return 'm % n'. (Assume that C '%' with
  572. ** negative operands follows C99 behavior. See previous comment
  573. ** about luaV_idiv.)
  574. */
  575. lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
  576. if (unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */
  577. if (n == 0)
  578. luaG_runerror(L, "attempt to perform 'n%%0'");
  579. return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
  580. }
  581. else {
  582. lua_Integer r = m % n;
  583. if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */
  584. r += n; /* correct result for different rounding */
  585. return r;
  586. }
  587. }
  588. /*
  589. ** Float modulus
  590. */
  591. lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) {
  592. lua_Number r;
  593. luai_nummod(L, m, n, r);
  594. return r;
  595. }
  596. /* number of bits in an integer */
  597. #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
  598. /*
  599. ** Shift left operation. (Shift right just negates 'y'.)
  600. */
  601. #define luaV_shiftr(x,y) luaV_shiftl(x,-(y))
  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_MMBIN: case OP_MMBINI: case OP_MMBINK: {
  642. setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top);
  643. break;
  644. }
  645. case OP_UNM: case OP_BNOT: case OP_LEN:
  646. case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
  647. case OP_GETFIELD: case OP_SELF: {
  648. setobjs2s(L, base + GETARG_A(inst), --L->top);
  649. break;
  650. }
  651. case OP_LT: case OP_LE:
  652. case OP_LTI: case OP_LEI:
  653. case OP_GTI: case OP_GEI:
  654. case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */
  655. int res = !l_isfalse(s2v(L->top - 1));
  656. L->top--;
  657. #if defined(LUA_COMPAT_LT_LE)
  658. if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
  659. ci->callstatus ^= CIST_LEQ; /* clear mark */
  660. res = !res; /* negate result */
  661. }
  662. #endif
  663. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  664. if (res != GETARG_k(inst)) /* condition failed? */
  665. ci->u.l.savedpc++; /* skip jump instruction */
  666. break;
  667. }
  668. case OP_CONCAT: {
  669. StkId top = L->top - 1; /* top when 'luaT_tryconcatTM' was called */
  670. int a = GETARG_A(inst); /* first element to concatenate */
  671. int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */
  672. setobjs2s(L, top - 2, top); /* put TM result in proper position */
  673. if (total > 1) { /* are there elements to concat? */
  674. L->top = top - 1; /* top is one after last element (at top-2) */
  675. luaV_concat(L, total); /* concat them (may yield again) */
  676. }
  677. break;
  678. }
  679. default: {
  680. /* only these other opcodes can yield */
  681. lua_assert(op == OP_TFORCALL || op == OP_CALL ||
  682. op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE ||
  683. op == OP_SETI || op == OP_SETFIELD);
  684. break;
  685. }
  686. }
  687. }
  688. /*
  689. ** {==================================================================
  690. ** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute'
  691. ** ===================================================================
  692. */
  693. #define l_addi(L,a,b) intop(+, a, b)
  694. #define l_subi(L,a,b) intop(-, a, b)
  695. #define l_muli(L,a,b) intop(*, a, b)
  696. #define l_band(a,b) intop(&, a, b)
  697. #define l_bor(a,b) intop(|, a, b)
  698. #define l_bxor(a,b) intop(^, a, b)
  699. #define l_lti(a,b) (a < b)
  700. #define l_lei(a,b) (a <= b)
  701. #define l_gti(a,b) (a > b)
  702. #define l_gei(a,b) (a >= b)
  703. /*
  704. ** Auxiliary macro for arithmetic operations over floats and others
  705. ** with immediate operand. 'fop' is the float operation; 'tm' is the
  706. ** corresponding metamethod; 'flip' is true if operands were flipped.
  707. */
  708. #define op_arithfI_aux(L,v1,imm,fop,tm,flip) { \
  709. lua_Number nb; \
  710. if (tonumberns(v1, nb)) { \
  711. lua_Number fimm = cast_num(imm); \
  712. pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \
  713. }}
  714. /*
  715. ** Arithmetic operations over floats and others with immediate operand.
  716. */
  717. #define op_arithfI(L,fop,tm) { \
  718. TValue *v1 = vRB(i); \
  719. int imm = GETARG_sC(i); \
  720. op_arithfI_aux(L, v1, imm, fop, tm, 0); }
  721. /*
  722. ** Arithmetic operations with immediate operands. 'iop' is the integer
  723. ** operation.
  724. */
  725. #define op_arithI(L,iop,fop,tm,flip) { \
  726. TValue *v1 = vRB(i); \
  727. int imm = GETARG_sC(i); \
  728. if (ttisinteger(v1)) { \
  729. lua_Integer iv1 = ivalue(v1); \
  730. pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \
  731. } \
  732. else op_arithfI_aux(L, v1, imm, fop, tm, flip); }
  733. /*
  734. ** Auxiliary function for arithmetic operations over floats and others
  735. ** with two register operands.
  736. */
  737. #define op_arithf_aux(L,v1,v2,fop) { \
  738. lua_Number n1; lua_Number n2; \
  739. if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \
  740. pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \
  741. }}
  742. /*
  743. ** Arithmetic operations over floats and others with register operands.
  744. */
  745. #define op_arithf(L,fop) { \
  746. TValue *v1 = vRB(i); \
  747. TValue *v2 = vRC(i); \
  748. op_arithf_aux(L, v1, v2, fop); }
  749. /*
  750. ** Arithmetic operations with register operands.
  751. */
  752. #define op_arith(L,iop,fop) { \
  753. TValue *v1 = vRB(i); \
  754. TValue *v2 = vRC(i); \
  755. if (ttisinteger(v1) && ttisinteger(v2)) { \
  756. lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \
  757. pc++; setivalue(s2v(ra), iop(L, i1, i2)); \
  758. } \
  759. else op_arithf_aux(L, v1, v2, fop); }
  760. /*
  761. ** Arithmetic operations with K operands.
  762. */
  763. #define op_arithK(L,iop,fop,flip) { \
  764. TValue *v1 = vRB(i); \
  765. TValue *v2 = KC(i); \
  766. if (ttisinteger(v1) && ttisinteger(v2)) { \
  767. lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \
  768. pc++; setivalue(s2v(ra), iop(L, i1, i2)); \
  769. } \
  770. else { \
  771. lua_Number n1; lua_Number n2; \
  772. if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \
  773. pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \
  774. }}}
  775. /*
  776. ** Arithmetic operations with K operands for floats.
  777. */
  778. #define op_arithfK(L,fop) { \
  779. TValue *v1 = vRB(i); \
  780. TValue *v2 = KC(i); \
  781. lua_Number n1; lua_Number n2; \
  782. if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \
  783. pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \
  784. }}
  785. /*
  786. ** Bitwise operations with constant operand.
  787. */
  788. #define op_bitwiseK(L,op) { \
  789. TValue *v1 = vRB(i); \
  790. TValue *v2 = KC(i); \
  791. lua_Integer i1; \
  792. lua_Integer i2 = ivalue(v2); \
  793. if (tointegerns(v1, &i1)) { \
  794. pc++; setivalue(s2v(ra), op(i1, i2)); \
  795. }}
  796. /*
  797. ** Bitwise operations with register operands.
  798. */
  799. #define op_bitwise(L,op) { \
  800. TValue *v1 = vRB(i); \
  801. TValue *v2 = vRC(i); \
  802. lua_Integer i1; lua_Integer i2; \
  803. if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \
  804. pc++; setivalue(s2v(ra), op(i1, i2)); \
  805. }}
  806. /*
  807. ** Order operations with register operands.
  808. */
  809. #define op_order(L,opi,opf,other) { \
  810. int cond; \
  811. TValue *rb = vRB(i); \
  812. if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \
  813. lua_Integer ia = ivalue(s2v(ra)); \
  814. lua_Integer ib = ivalue(rb); \
  815. cond = opi(ia, ib); \
  816. } \
  817. else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \
  818. cond = opf(s2v(ra), rb); \
  819. else \
  820. Protect(cond = other(L, s2v(ra), rb)); \
  821. docondjump(); }
  822. /*
  823. ** Order operations with immediate operand.
  824. */
  825. #define op_orderI(L,opi,opf,inv,tm) { \
  826. int cond; \
  827. int im = GETARG_sB(i); \
  828. if (ttisinteger(s2v(ra))) \
  829. cond = opi(ivalue(s2v(ra)), im); \
  830. else if (ttisfloat(s2v(ra))) { \
  831. lua_Number fa = fltvalue(s2v(ra)); \
  832. lua_Number fim = cast_num(im); \
  833. cond = opf(fa, fim); \
  834. } \
  835. else { \
  836. int isf = GETARG_C(i); \
  837. Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \
  838. } \
  839. docondjump(); }
  840. /* }================================================================== */
  841. /*
  842. ** {==================================================================
  843. ** Function 'luaV_execute': main interpreter loop
  844. ** ===================================================================
  845. */
  846. /*
  847. ** some macros for common tasks in 'luaV_execute'
  848. */
  849. #define RA(i) (base+GETARG_A(i))
  850. #define RB(i) (base+GETARG_B(i))
  851. #define vRB(i) s2v(RB(i))
  852. #define KB(i) (k+GETARG_B(i))
  853. #define RC(i) (base+GETARG_C(i))
  854. #define vRC(i) s2v(RC(i))
  855. #define KC(i) (k+GETARG_C(i))
  856. #define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
  857. #define updatetrap(ci) (trap = ci->u.l.trap)
  858. #define updatebase(ci) (base = ci->func + 1)
  859. #define updatestack(ci) { if (trap) { updatebase(ci); ra = RA(i); } }
  860. /*
  861. ** Execute a jump instruction. The 'updatetrap' allows signals to stop
  862. ** tight loops. (Without it, the local copy of 'trap' could never change.)
  863. */
  864. #define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); }
  865. /* for test instructions, execute the jump instruction that follows it */
  866. #define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); }
  867. /*
  868. ** do a conditional jump: skip next instruction if 'cond' is not what
  869. ** was expected (parameter 'k'), else do next instruction, which must
  870. ** be a jump.
  871. */
  872. #define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci);
  873. /*
  874. ** Correct global 'pc'.
  875. */
  876. #define savepc(L) (ci->u.l.savedpc = pc)
  877. /*
  878. ** Whenever code can raise errors, the global 'pc' and the global
  879. ** 'top' must be correct to report occasional errors.
  880. */
  881. #define savestate(L,ci) (savepc(L), L->top = ci->top)
  882. /*
  883. ** Protect code that, in general, can raise errors, reallocate the
  884. ** stack, and change the hooks.
  885. */
  886. #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci))
  887. /* special version that does not change the top */
  888. #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci))
  889. /*
  890. ** Protect code that will finish the loop (returns) or can only raise
  891. ** errors. (That is, it will not return to the interpreter main loop
  892. ** after changing the stack or hooks.)
  893. */
  894. #define halfProtect(exp) (savestate(L,ci), (exp))
  895. /* idem, but without changing the stack */
  896. #define halfProtectNT(exp) (savepc(L), (exp))
  897. #define checkGC(L,c) \
  898. { luaC_condGC(L, L->top = (c), /* limit of live values */ \
  899. updatetrap(ci)); \
  900. luai_threadyield(L); }
  901. /* fetch an instruction and prepare its execution */
  902. #define vmfetch() { \
  903. if (trap) { /* stack reallocation or hooks? */ \
  904. trap = luaG_traceexec(L, pc); /* handle hooks */ \
  905. updatebase(ci); /* correct stack */ \
  906. } \
  907. i = *(pc++); \
  908. ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \
  909. }
  910. #define vmdispatch(o) switch(o)
  911. #define vmcase(l) case l:
  912. #define vmbreak break
  913. void luaV_execute (lua_State *L, CallInfo *ci) {
  914. LClosure *cl;
  915. TValue *k;
  916. StkId base;
  917. const Instruction *pc;
  918. int trap;
  919. #if LUA_USE_JUMPTABLE
  920. #include "ljumptab.h"
  921. #endif
  922. tailcall:
  923. trap = L->hookmask;
  924. cl = clLvalue(s2v(ci->func));
  925. k = cl->p->k;
  926. pc = ci->u.l.savedpc;
  927. if (trap) {
  928. if (cl->p->is_vararg)
  929. trap = 0; /* hooks will start after VARARGPREP instruction */
  930. else if (pc == cl->p->code) /* first instruction (not resuming)? */
  931. luaD_hookcall(L, ci);
  932. ci->u.l.trap = 1; /* there may be other hooks */
  933. }
  934. base = ci->func + 1;
  935. /* main loop of interpreter */
  936. for (;;) {
  937. Instruction i; /* instruction being executed */
  938. StkId ra; /* instruction's A register */
  939. vmfetch();
  940. lua_assert(base == ci->func + 1);
  941. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  942. /* invalidate top for instructions not expecting it */
  943. lua_assert(isIT(i) || (L->top = base));
  944. vmdispatch (GET_OPCODE(i)) {
  945. vmcase(OP_MOVE) {
  946. setobjs2s(L, ra, RB(i));
  947. vmbreak;
  948. }
  949. vmcase(OP_LOADI) {
  950. lua_Integer b = GETARG_sBx(i);
  951. setivalue(s2v(ra), b);
  952. vmbreak;
  953. }
  954. vmcase(OP_LOADF) {
  955. int b = GETARG_sBx(i);
  956. setfltvalue(s2v(ra), cast_num(b));
  957. vmbreak;
  958. }
  959. vmcase(OP_LOADK) {
  960. TValue *rb = k + GETARG_Bx(i);
  961. setobj2s(L, ra, rb);
  962. vmbreak;
  963. }
  964. vmcase(OP_LOADKX) {
  965. TValue *rb;
  966. rb = k + GETARG_Ax(*pc); pc++;
  967. setobj2s(L, ra, rb);
  968. vmbreak;
  969. }
  970. vmcase(OP_LOADBOOL) {
  971. setbvalue(s2v(ra), GETARG_B(i));
  972. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  973. vmbreak;
  974. }
  975. vmcase(OP_LOADNIL) {
  976. int b = GETARG_B(i);
  977. do {
  978. setnilvalue(s2v(ra++));
  979. } while (b--);
  980. vmbreak;
  981. }
  982. vmcase(OP_GETUPVAL) {
  983. int b = GETARG_B(i);
  984. setobj2s(L, ra, cl->upvals[b]->v);
  985. vmbreak;
  986. }
  987. vmcase(OP_SETUPVAL) {
  988. UpVal *uv = cl->upvals[GETARG_B(i)];
  989. setobj(L, uv->v, s2v(ra));
  990. luaC_barrier(L, uv, s2v(ra));
  991. vmbreak;
  992. }
  993. vmcase(OP_GETTABUP) {
  994. const TValue *slot;
  995. TValue *upval = cl->upvals[GETARG_B(i)]->v;
  996. TValue *rc = KC(i);
  997. TString *key = tsvalue(rc); /* key must be a string */
  998. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  999. setobj2s(L, ra, slot);
  1000. }
  1001. else
  1002. Protect(luaV_finishget(L, upval, rc, ra, slot));
  1003. vmbreak;
  1004. }
  1005. vmcase(OP_GETTABLE) {
  1006. const TValue *slot;
  1007. TValue *rb = vRB(i);
  1008. TValue *rc = vRC(i);
  1009. lua_Unsigned n;
  1010. if (ttisinteger(rc) /* fast track for integers? */
  1011. ? (cast_void(n = ivalue(rc)), luaV_fastgeti(L, rb, n, slot))
  1012. : luaV_fastget(L, rb, rc, slot, luaH_get)) {
  1013. setobj2s(L, ra, slot);
  1014. }
  1015. else
  1016. Protect(luaV_finishget(L, rb, rc, ra, slot));
  1017. vmbreak;
  1018. }
  1019. vmcase(OP_GETI) {
  1020. const TValue *slot;
  1021. TValue *rb = vRB(i);
  1022. int c = GETARG_C(i);
  1023. if (luaV_fastgeti(L, rb, c, slot)) {
  1024. setobj2s(L, ra, slot);
  1025. }
  1026. else {
  1027. TValue key;
  1028. setivalue(&key, c);
  1029. Protect(luaV_finishget(L, rb, &key, ra, slot));
  1030. }
  1031. vmbreak;
  1032. }
  1033. vmcase(OP_GETFIELD) {
  1034. const TValue *slot;
  1035. TValue *rb = vRB(i);
  1036. TValue *rc = KC(i);
  1037. TString *key = tsvalue(rc); /* key must be a string */
  1038. if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) {
  1039. setobj2s(L, ra, slot);
  1040. }
  1041. else
  1042. Protect(luaV_finishget(L, rb, rc, ra, slot));
  1043. vmbreak;
  1044. }
  1045. vmcase(OP_SETTABUP) {
  1046. const TValue *slot;
  1047. TValue *upval = cl->upvals[GETARG_A(i)]->v;
  1048. TValue *rb = KB(i);
  1049. TValue *rc = RKC(i);
  1050. TString *key = tsvalue(rb); /* key must be a string */
  1051. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  1052. luaV_finishfastset(L, upval, slot, rc);
  1053. }
  1054. else
  1055. Protect(luaV_finishset(L, upval, rb, rc, slot));
  1056. vmbreak;
  1057. }
  1058. vmcase(OP_SETTABLE) {
  1059. const TValue *slot;
  1060. TValue *rb = vRB(i); /* key (table is in 'ra') */
  1061. TValue *rc = RKC(i); /* value */
  1062. lua_Unsigned n;
  1063. if (ttisinteger(rb) /* fast track for integers? */
  1064. ? (cast_void(n = ivalue(rb)), luaV_fastgeti(L, s2v(ra), n, slot))
  1065. : luaV_fastget(L, s2v(ra), rb, slot, luaH_get)) {
  1066. luaV_finishfastset(L, s2v(ra), slot, rc);
  1067. }
  1068. else
  1069. Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
  1070. vmbreak;
  1071. }
  1072. vmcase(OP_SETI) {
  1073. const TValue *slot;
  1074. int c = GETARG_B(i);
  1075. TValue *rc = RKC(i);
  1076. if (luaV_fastgeti(L, s2v(ra), c, slot)) {
  1077. luaV_finishfastset(L, s2v(ra), slot, rc);
  1078. }
  1079. else {
  1080. TValue key;
  1081. setivalue(&key, c);
  1082. Protect(luaV_finishset(L, s2v(ra), &key, rc, slot));
  1083. }
  1084. vmbreak;
  1085. }
  1086. vmcase(OP_SETFIELD) {
  1087. const TValue *slot;
  1088. TValue *rb = KB(i);
  1089. TValue *rc = RKC(i);
  1090. TString *key = tsvalue(rb); /* key must be a string */
  1091. if (luaV_fastget(L, s2v(ra), key, slot, luaH_getshortstr)) {
  1092. luaV_finishfastset(L, s2v(ra), slot, rc);
  1093. }
  1094. else
  1095. Protect(luaV_finishset(L, s2v(ra), rb, rc, slot));
  1096. vmbreak;
  1097. }
  1098. vmcase(OP_NEWTABLE) {
  1099. int b = GETARG_B(i); /* log2(hash size) + 1 */
  1100. int c = GETARG_C(i); /* array size */
  1101. Table *t;
  1102. if (b > 0)
  1103. b = 1 << (b - 1); /* size is 2^(b - 1) */
  1104. if (TESTARG_k(i))
  1105. c += GETARG_Ax(*pc) * (MAXARG_C + 1);
  1106. pc++; /* skip extra argument */
  1107. L->top = ra + 1; /* correct top in case of emergency GC */
  1108. t = luaH_new(L); /* memory allocation */
  1109. sethvalue2s(L, ra, t);
  1110. if (b != 0 || c != 0)
  1111. luaH_resize(L, t, c, b); /* idem */
  1112. checkGC(L, ra + 1);
  1113. vmbreak;
  1114. }
  1115. vmcase(OP_SELF) {
  1116. const TValue *slot;
  1117. TValue *rb = vRB(i);
  1118. TValue *rc = RKC(i);
  1119. TString *key = tsvalue(rc); /* key must be a string */
  1120. setobj2s(L, ra + 1, rb);
  1121. if (luaV_fastget(L, rb, key, slot, luaH_getstr)) {
  1122. setobj2s(L, ra, slot);
  1123. }
  1124. else
  1125. Protect(luaV_finishget(L, rb, rc, ra, slot));
  1126. vmbreak;
  1127. }
  1128. vmcase(OP_ADDI) {
  1129. op_arithI(L, l_addi, luai_numadd, TM_ADD, GETARG_k(i));
  1130. vmbreak;
  1131. }
  1132. vmcase(OP_SUBI) {
  1133. op_arithI(L, l_subi, luai_numsub, TM_SUB, 0);
  1134. vmbreak;
  1135. }
  1136. vmcase(OP_MULI) {
  1137. op_arithI(L, l_muli, luai_nummul, TM_MUL, GETARG_k(i));
  1138. vmbreak;
  1139. }
  1140. vmcase(OP_MODI) {
  1141. op_arithI(L, luaV_mod, luaV_modf, TM_MOD, 0);
  1142. vmbreak;
  1143. }
  1144. vmcase(OP_POWI) {
  1145. op_arithfI(L, luai_numpow, TM_POW);
  1146. vmbreak;
  1147. }
  1148. vmcase(OP_DIVI) {
  1149. op_arithfI(L, luai_numdiv, TM_DIV);
  1150. vmbreak;
  1151. }
  1152. vmcase(OP_IDIVI) {
  1153. op_arithI(L, luaV_idiv, luai_numidiv, TM_IDIV, 0);
  1154. vmbreak;
  1155. }
  1156. vmcase(OP_ADDK) {
  1157. op_arithK(L, l_addi, luai_numadd, GETARG_k(i));
  1158. vmbreak;
  1159. }
  1160. vmcase(OP_SUBK) {
  1161. op_arithK(L, l_subi, luai_numsub, 0);
  1162. vmbreak;
  1163. }
  1164. vmcase(OP_MULK) {
  1165. op_arithK(L, l_muli, luai_nummul, GETARG_k(i));
  1166. vmbreak;
  1167. }
  1168. vmcase(OP_MODK) {
  1169. op_arithK(L, luaV_mod, luaV_modf, 0);
  1170. vmbreak;
  1171. }
  1172. vmcase(OP_POWK) {
  1173. op_arithfK(L, luai_numpow);
  1174. vmbreak;
  1175. }
  1176. vmcase(OP_DIVK) {
  1177. op_arithfK(L, luai_numdiv);
  1178. vmbreak;
  1179. }
  1180. vmcase(OP_IDIVK) {
  1181. op_arithK(L, luaV_idiv, luai_numidiv, 0);
  1182. vmbreak;
  1183. }
  1184. vmcase(OP_BANDK) {
  1185. op_bitwiseK(L, l_band);
  1186. vmbreak;
  1187. }
  1188. vmcase(OP_BORK) {
  1189. op_bitwiseK(L, l_bor);
  1190. vmbreak;
  1191. }
  1192. vmcase(OP_BXORK) {
  1193. op_bitwiseK(L, l_bxor);
  1194. vmbreak;
  1195. }
  1196. vmcase(OP_SHRI) {
  1197. TValue *rb = vRB(i);
  1198. int ic = GETARG_sC(i);
  1199. lua_Integer ib;
  1200. if (tointegerns(rb, &ib)) {
  1201. pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic));
  1202. }
  1203. vmbreak;
  1204. }
  1205. vmcase(OP_SHLI) {
  1206. TValue *rb = vRB(i);
  1207. int ic = GETARG_sC(i);
  1208. lua_Integer ib;
  1209. if (tointegerns(rb, &ib)) {
  1210. pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib));
  1211. }
  1212. vmbreak;
  1213. }
  1214. vmcase(OP_ADD) {
  1215. op_arith(L, l_addi, luai_numadd);
  1216. vmbreak;
  1217. }
  1218. vmcase(OP_SUB) {
  1219. op_arith(L, l_subi, luai_numsub);
  1220. vmbreak;
  1221. }
  1222. vmcase(OP_MUL) {
  1223. op_arith(L, l_muli, luai_nummul);
  1224. vmbreak;
  1225. }
  1226. vmcase(OP_MOD) {
  1227. op_arith(L, luaV_mod, luaV_modf);
  1228. vmbreak;
  1229. }
  1230. vmcase(OP_POW) {
  1231. op_arithf(L, luai_numpow);
  1232. vmbreak;
  1233. }
  1234. vmcase(OP_DIV) { /* float division (always with floats) */
  1235. op_arithf(L, luai_numdiv);
  1236. vmbreak;
  1237. }
  1238. vmcase(OP_IDIV) { /* floor division */
  1239. op_arith(L, luaV_idiv, luai_numidiv);
  1240. vmbreak;
  1241. }
  1242. vmcase(OP_BAND) {
  1243. op_bitwise(L, l_band);
  1244. vmbreak;
  1245. }
  1246. vmcase(OP_BOR) {
  1247. op_bitwise(L, l_bor);
  1248. vmbreak;
  1249. }
  1250. vmcase(OP_BXOR) {
  1251. op_bitwise(L, l_bxor);
  1252. vmbreak;
  1253. }
  1254. vmcase(OP_SHR) {
  1255. op_bitwise(L, luaV_shiftr);
  1256. vmbreak;
  1257. }
  1258. vmcase(OP_SHL) {
  1259. op_bitwise(L, luaV_shiftl);
  1260. vmbreak;
  1261. }
  1262. vmcase(OP_MMBIN) {
  1263. Instruction pi = *(pc - 2); /* original arith. expression */
  1264. TValue *rb = vRB(i);
  1265. TMS tm = (TMS)GETARG_C(i);
  1266. StkId result = RA(pi);
  1267. lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR);
  1268. Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm));
  1269. vmbreak;
  1270. }
  1271. vmcase(OP_MMBINI) {
  1272. Instruction pi = *(pc - 2); /* original arith. expression */
  1273. int imm = GETARG_sB(i);
  1274. TMS tm = (TMS)GETARG_C(i);
  1275. int flip = GETARG_k(i);
  1276. StkId result = RA(pi);
  1277. Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm));
  1278. vmbreak;
  1279. }
  1280. vmcase(OP_MMBINK) {
  1281. Instruction pi = *(pc - 2); /* original arith. expression */
  1282. TValue *imm = KB(i);
  1283. TMS tm = (TMS)GETARG_C(i);
  1284. int flip = GETARG_k(i);
  1285. StkId result = RA(pi);
  1286. Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm));
  1287. vmbreak;
  1288. }
  1289. vmcase(OP_UNM) {
  1290. TValue *rb = vRB(i);
  1291. lua_Number nb;
  1292. if (ttisinteger(rb)) {
  1293. lua_Integer ib = ivalue(rb);
  1294. setivalue(s2v(ra), intop(-, 0, ib));
  1295. }
  1296. else if (tonumberns(rb, nb)) {
  1297. setfltvalue(s2v(ra), luai_numunm(L, nb));
  1298. }
  1299. else
  1300. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  1301. vmbreak;
  1302. }
  1303. vmcase(OP_BNOT) {
  1304. TValue *rb = vRB(i);
  1305. lua_Integer ib;
  1306. if (tointegerns(rb, &ib)) {
  1307. setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
  1308. }
  1309. else
  1310. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  1311. vmbreak;
  1312. }
  1313. vmcase(OP_NOT) {
  1314. TValue *rb = vRB(i);
  1315. int nrb = l_isfalse(rb); /* next assignment may change this value */
  1316. setbvalue(s2v(ra), nrb);
  1317. vmbreak;
  1318. }
  1319. vmcase(OP_LEN) {
  1320. Protect(luaV_objlen(L, ra, vRB(i)));
  1321. vmbreak;
  1322. }
  1323. vmcase(OP_CONCAT) {
  1324. int n = GETARG_B(i); /* number of elements to concatenate */
  1325. L->top = ra + n; /* mark the end of concat operands */
  1326. ProtectNT(luaV_concat(L, n));
  1327. checkGC(L, L->top); /* 'luaV_concat' ensures correct top */
  1328. vmbreak;
  1329. }
  1330. vmcase(OP_CLOSE) {
  1331. Protect(luaF_close(L, ra, LUA_OK));
  1332. vmbreak;
  1333. }
  1334. vmcase(OP_TBC) {
  1335. /* create new to-be-closed upvalue */
  1336. halfProtect(luaF_newtbcupval(L, ra));
  1337. vmbreak;
  1338. }
  1339. vmcase(OP_JMP) {
  1340. dojump(ci, i, 0);
  1341. vmbreak;
  1342. }
  1343. vmcase(OP_EQ) {
  1344. int cond;
  1345. TValue *rb = vRB(i);
  1346. Protect(cond = luaV_equalobj(L, s2v(ra), rb));
  1347. docondjump();
  1348. vmbreak;
  1349. }
  1350. vmcase(OP_LT) {
  1351. op_order(L, l_lti, LTnum, lessthanothers);
  1352. vmbreak;
  1353. }
  1354. vmcase(OP_LE) {
  1355. op_order(L, l_lei, LEnum, lessequalothers);
  1356. vmbreak;
  1357. }
  1358. vmcase(OP_EQK) {
  1359. TValue *rb = KB(i);
  1360. /* basic types do not use '__eq'; we can use raw equality */
  1361. int cond = luaV_equalobj(NULL, s2v(ra), rb);
  1362. docondjump();
  1363. vmbreak;
  1364. }
  1365. vmcase(OP_EQI) {
  1366. int cond;
  1367. int im = GETARG_sB(i);
  1368. if (ttisinteger(s2v(ra)))
  1369. cond = (ivalue(s2v(ra)) == im);
  1370. else if (ttisfloat(s2v(ra)))
  1371. cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im));
  1372. else
  1373. cond = 0; /* other types cannot be equal to a number */
  1374. docondjump();
  1375. vmbreak;
  1376. }
  1377. vmcase(OP_LTI) {
  1378. op_orderI(L, l_lti, luai_numlt, 0, TM_LT);
  1379. vmbreak;
  1380. }
  1381. vmcase(OP_LEI) {
  1382. op_orderI(L, l_lei, luai_numle, 0, TM_LE);
  1383. vmbreak;
  1384. }
  1385. vmcase(OP_GTI) {
  1386. op_orderI(L, l_gti, luai_numgt, 1, TM_LT);
  1387. vmbreak;
  1388. }
  1389. vmcase(OP_GEI) {
  1390. op_orderI(L, l_gei, luai_numge, 1, TM_LE);
  1391. vmbreak;
  1392. }
  1393. vmcase(OP_TEST) {
  1394. int cond = !l_isfalse(s2v(ra));
  1395. docondjump();
  1396. vmbreak;
  1397. }
  1398. vmcase(OP_TESTSET) {
  1399. TValue *rb = vRB(i);
  1400. if (l_isfalse(rb) == GETARG_k(i))
  1401. pc++;
  1402. else {
  1403. setobj2s(L, ra, rb);
  1404. donextjump(ci);
  1405. }
  1406. vmbreak;
  1407. }
  1408. vmcase(OP_CALL) {
  1409. int b = GETARG_B(i);
  1410. int nresults = GETARG_C(i) - 1;
  1411. if (b != 0) /* fixed number of arguments? */
  1412. L->top = ra + b; /* top signals number of arguments */
  1413. /* else previous instruction set top */
  1414. ProtectNT(luaD_call(L, ra, nresults));
  1415. vmbreak;
  1416. }
  1417. vmcase(OP_TAILCALL) {
  1418. int b = GETARG_B(i); /* number of arguments + 1 (function) */
  1419. int nparams1 = GETARG_C(i);
  1420. /* delat is virtual 'func' - real 'func' (vararg functions) */
  1421. int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
  1422. if (b != 0)
  1423. L->top = ra + b;
  1424. else /* previous instruction set top */
  1425. b = cast_int(L->top - ra);
  1426. savepc(ci); /* some calls here can raise errors */
  1427. if (TESTARG_k(i)) {
  1428. /* close upvalues from current call; the compiler ensures
  1429. that there are no to-be-closed variables here, so this
  1430. call cannot change the stack */
  1431. luaF_close(L, base, NOCLOSINGMETH);
  1432. lua_assert(base == ci->func + 1);
  1433. }
  1434. if (!ttisfunction(s2v(ra))) { /* not a function? */
  1435. luaD_tryfuncTM(L, ra); /* try '__call' metamethod */
  1436. b++; /* there is now one extra argument */
  1437. }
  1438. if (!ttisLclosure(s2v(ra))) { /* C function? */
  1439. luaD_call(L, ra, LUA_MULTRET); /* call it */
  1440. updatetrap(ci);
  1441. updatestack(ci); /* stack may have been relocated */
  1442. ci->func -= delta;
  1443. luaD_poscall(L, ci, cast_int(L->top - ra));
  1444. return;
  1445. }
  1446. else { /* Lua tail call */
  1447. ci->func -= delta;
  1448. luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
  1449. goto tailcall;
  1450. }
  1451. vmbreak;
  1452. }
  1453. vmcase(OP_RETURN) {
  1454. int n = GETARG_B(i) - 1; /* number of results */
  1455. int nparams1 = GETARG_C(i);
  1456. if (n < 0) /* not fixed? */
  1457. n = cast_int(L->top - ra); /* get what is available */
  1458. savepc(ci);
  1459. if (TESTARG_k(i)) { /* may there be open upvalues? */
  1460. if (L->top < ci->top)
  1461. L->top = ci->top;
  1462. luaF_close(L, base, LUA_OK);
  1463. updatetrap(ci);
  1464. updatestack(ci);
  1465. }
  1466. if (nparams1) /* vararg function? */
  1467. ci->func -= ci->u.l.nextraargs + nparams1;
  1468. L->top = ra + n; /* set call for 'luaD_poscall' */
  1469. luaD_poscall(L, ci, n);
  1470. return;
  1471. }
  1472. vmcase(OP_RETURN0) {
  1473. if (L->hookmask) {
  1474. L->top = ra;
  1475. halfProtectNT(luaD_poscall(L, ci, 0)); /* no hurry... */
  1476. }
  1477. else { /* do the 'poscall' here */
  1478. int nres = ci->nresults;
  1479. L->ci = ci->previous; /* back to caller */
  1480. L->top = base - 1;
  1481. while (nres-- > 0)
  1482. setnilvalue(s2v(L->top++)); /* all results are nil */
  1483. }
  1484. return;
  1485. }
  1486. vmcase(OP_RETURN1) {
  1487. if (L->hookmask) {
  1488. L->top = ra + 1;
  1489. halfProtectNT(luaD_poscall(L, ci, 1)); /* no hurry... */
  1490. }
  1491. else { /* do the 'poscall' here */
  1492. int nres = ci->nresults;
  1493. L->ci = ci->previous; /* back to caller */
  1494. if (nres == 0)
  1495. L->top = base - 1; /* asked for no results */
  1496. else {
  1497. setobjs2s(L, base - 1, ra); /* at least this result */
  1498. L->top = base;
  1499. while (--nres > 0) /* complete missing results */
  1500. setnilvalue(s2v(L->top++));
  1501. }
  1502. }
  1503. return;
  1504. }
  1505. vmcase(OP_FORLOOP) {
  1506. if (ttisinteger(s2v(ra + 2))) { /* integer loop? */
  1507. lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1)));
  1508. if (count > 0) { /* still more iterations? */
  1509. lua_Integer step = ivalue(s2v(ra + 2));
  1510. lua_Integer idx = ivalue(s2v(ra)); /* internal index */
  1511. chgivalue(s2v(ra + 1), count - 1); /* update counter */
  1512. idx = intop(+, idx, step); /* add step to index */
  1513. chgivalue(s2v(ra), idx); /* update internal index */
  1514. setivalue(s2v(ra + 3), idx); /* and control variable */
  1515. pc -= GETARG_Bx(i); /* jump back */
  1516. }
  1517. }
  1518. else { /* floating loop */
  1519. lua_Number step = fltvalue(s2v(ra + 2));
  1520. lua_Number limit = fltvalue(s2v(ra + 1));
  1521. lua_Number idx = fltvalue(s2v(ra));
  1522. idx = luai_numadd(L, idx, step); /* increment index */
  1523. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  1524. : luai_numle(limit, idx)) {
  1525. chgfltvalue(s2v(ra), idx); /* update internal index */
  1526. setfltvalue(s2v(ra + 3), idx); /* and control variable */
  1527. pc -= GETARG_Bx(i); /* jump back */
  1528. }
  1529. }
  1530. updatetrap(ci); /* allows a signal to break the loop */
  1531. vmbreak;
  1532. }
  1533. vmcase(OP_FORPREP) {
  1534. TValue *pinit = s2v(ra);
  1535. TValue *plimit = s2v(ra + 1);
  1536. TValue *pstep = s2v(ra + 2);
  1537. savestate(L, ci); /* in case of errors */
  1538. if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
  1539. lua_Integer init = ivalue(pinit);
  1540. lua_Integer step = ivalue(pstep);
  1541. lua_Integer limit;
  1542. if (step == 0)
  1543. luaG_runerror(L, "'for' step is zero");
  1544. setivalue(s2v(ra + 3), init); /* control variable */
  1545. if (forlimit(L, init, plimit, &limit, step))
  1546. pc += GETARG_Bx(i) + 1; /* skip the loop */
  1547. else { /* prepare loop counter */
  1548. lua_Unsigned count;
  1549. if (step > 0) { /* ascending loop? */
  1550. count = l_castS2U(limit) - l_castS2U(init);
  1551. if (step != 1) /* avoid division in the too common case */
  1552. count /= l_castS2U(step);
  1553. }
  1554. else { /* step < 0; descending loop */
  1555. count = l_castS2U(init) - l_castS2U(limit);
  1556. /* 'step+1' avoids negating 'mininteger' */
  1557. count /= l_castS2U(-(step + 1)) + 1u;
  1558. }
  1559. /* store the counter in place of the limit (which won't be
  1560. needed anymore */
  1561. setivalue(plimit, l_castU2S(count));
  1562. }
  1563. }
  1564. else { /* try making all values floats */
  1565. lua_Number init; lua_Number limit; lua_Number step;
  1566. if (unlikely(!tonumber(plimit, &limit)))
  1567. luaG_forerror(L, plimit, "limit");
  1568. if (unlikely(!tonumber(pstep, &step)))
  1569. luaG_forerror(L, pstep, "step");
  1570. if (unlikely(!tonumber(pinit, &init)))
  1571. luaG_forerror(L, pinit, "initial value");
  1572. if (step == 0)
  1573. luaG_runerror(L, "'for' step is zero");
  1574. if (luai_numlt(0, step) ? luai_numlt(limit, init)
  1575. : luai_numlt(init, limit))
  1576. pc += GETARG_Bx(i) + 1; /* skip the loop */
  1577. else {
  1578. /* make sure internal values are all float */
  1579. setfltvalue(plimit, limit);
  1580. setfltvalue(pstep, step);
  1581. setfltvalue(s2v(ra), init); /* internal index */
  1582. setfltvalue(s2v(ra + 3), init); /* control variable */
  1583. }
  1584. }
  1585. vmbreak;
  1586. }
  1587. vmcase(OP_TFORPREP) {
  1588. /* create to-be-closed upvalue (if needed) */
  1589. halfProtect(luaF_newtbcupval(L, ra + 3));
  1590. pc += GETARG_Bx(i);
  1591. i = *(pc++); /* go to next instruction */
  1592. lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i));
  1593. goto l_tforcall;
  1594. }
  1595. vmcase(OP_TFORCALL) {
  1596. l_tforcall:
  1597. /* 'ra' has the iterator function, 'ra + 1' has the state,
  1598. 'ra + 2' has the control variable, and 'ra + 3' has the
  1599. to-be-closed variable. The call will use the stack after
  1600. these values (starting at 'ra + 4')
  1601. */
  1602. /* push function, state, and control variable */
  1603. memcpy(ra + 4, ra, 3 * sizeof(*ra));
  1604. L->top = ra + 4 + 3;
  1605. ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */
  1606. updatestack(ci); /* stack may have changed */
  1607. i = *(pc++); /* go to next instruction */
  1608. lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
  1609. goto l_tforloop;
  1610. }
  1611. vmcase(OP_TFORLOOP) {
  1612. l_tforloop:
  1613. if (!ttisnil(s2v(ra + 4))) { /* continue loop? */
  1614. setobjs2s(L, ra + 2, ra + 4); /* save control variable */
  1615. pc -= GETARG_Bx(i); /* jump back */
  1616. }
  1617. vmbreak;
  1618. }
  1619. vmcase(OP_SETLIST) {
  1620. int n = GETARG_B(i);
  1621. unsigned int last = GETARG_C(i);
  1622. Table *h = hvalue(s2v(ra));
  1623. if (n == 0)
  1624. n = cast_int(L->top - ra) - 1; /* get up to the top */
  1625. else
  1626. L->top = ci->top; /* correct top in case of emergency GC */
  1627. last += n;
  1628. if (TESTARG_k(i)) {
  1629. last += GETARG_Ax(*pc) * (MAXARG_C + 1);
  1630. pc++;
  1631. }
  1632. if (last > luaH_realasize(h)) /* needs more space? */
  1633. luaH_resizearray(L, h, last); /* preallocate it at once */
  1634. for (; n > 0; n--) {
  1635. TValue *val = s2v(ra + n);
  1636. setobj2t(L, &h->array[last - 1], val);
  1637. last--;
  1638. luaC_barrierback(L, obj2gco(h), val);
  1639. }
  1640. vmbreak;
  1641. }
  1642. vmcase(OP_CLOSURE) {
  1643. Proto *p = cl->p->p[GETARG_Bx(i)];
  1644. halfProtect(pushclosure(L, p, cl->upvals, base, ra));
  1645. checkGC(L, ra + 1);
  1646. vmbreak;
  1647. }
  1648. vmcase(OP_VARARG) {
  1649. int n = GETARG_C(i) - 1; /* required results */
  1650. Protect(luaT_getvarargs(L, ci, ra, n));
  1651. vmbreak;
  1652. }
  1653. vmcase(OP_VARARGPREP) {
  1654. luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p);
  1655. updatetrap(ci);
  1656. if (trap) {
  1657. luaD_hookcall(L, ci);
  1658. L->oldpc = pc + 1; /* next opcode will be seen as a "new" line */
  1659. }
  1660. updatebase(ci); /* function has new base after adjustment */
  1661. vmbreak;
  1662. }
  1663. vmcase(OP_EXTRAARG) {
  1664. lua_assert(0);
  1665. vmbreak;
  1666. }
  1667. }
  1668. }
  1669. }
  1670. /* }================================================================== */