lvm.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  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_ADDK) {
  1133. op_arithK(L, l_addi, luai_numadd, GETARG_k(i));
  1134. vmbreak;
  1135. }
  1136. vmcase(OP_SUBK) {
  1137. op_arithK(L, l_subi, luai_numsub, 0);
  1138. vmbreak;
  1139. }
  1140. vmcase(OP_MULK) {
  1141. op_arithK(L, l_muli, luai_nummul, GETARG_k(i));
  1142. vmbreak;
  1143. }
  1144. vmcase(OP_MODK) {
  1145. op_arithK(L, luaV_mod, luaV_modf, 0);
  1146. vmbreak;
  1147. }
  1148. vmcase(OP_POWK) {
  1149. op_arithfK(L, luai_numpow);
  1150. vmbreak;
  1151. }
  1152. vmcase(OP_DIVK) {
  1153. op_arithfK(L, luai_numdiv);
  1154. vmbreak;
  1155. }
  1156. vmcase(OP_IDIVK) {
  1157. op_arithK(L, luaV_idiv, luai_numidiv, 0);
  1158. vmbreak;
  1159. }
  1160. vmcase(OP_BANDK) {
  1161. op_bitwiseK(L, l_band);
  1162. vmbreak;
  1163. }
  1164. vmcase(OP_BORK) {
  1165. op_bitwiseK(L, l_bor);
  1166. vmbreak;
  1167. }
  1168. vmcase(OP_BXORK) {
  1169. op_bitwiseK(L, l_bxor);
  1170. vmbreak;
  1171. }
  1172. vmcase(OP_SHRI) {
  1173. TValue *rb = vRB(i);
  1174. int ic = GETARG_sC(i);
  1175. lua_Integer ib;
  1176. if (tointegerns(rb, &ib)) {
  1177. pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic));
  1178. }
  1179. vmbreak;
  1180. }
  1181. vmcase(OP_SHLI) {
  1182. TValue *rb = vRB(i);
  1183. int ic = GETARG_sC(i);
  1184. lua_Integer ib;
  1185. if (tointegerns(rb, &ib)) {
  1186. pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib));
  1187. }
  1188. vmbreak;
  1189. }
  1190. vmcase(OP_ADD) {
  1191. op_arith(L, l_addi, luai_numadd);
  1192. vmbreak;
  1193. }
  1194. vmcase(OP_SUB) {
  1195. op_arith(L, l_subi, luai_numsub);
  1196. vmbreak;
  1197. }
  1198. vmcase(OP_MUL) {
  1199. op_arith(L, l_muli, luai_nummul);
  1200. vmbreak;
  1201. }
  1202. vmcase(OP_MOD) {
  1203. op_arith(L, luaV_mod, luaV_modf);
  1204. vmbreak;
  1205. }
  1206. vmcase(OP_POW) {
  1207. op_arithf(L, luai_numpow);
  1208. vmbreak;
  1209. }
  1210. vmcase(OP_DIV) { /* float division (always with floats) */
  1211. op_arithf(L, luai_numdiv);
  1212. vmbreak;
  1213. }
  1214. vmcase(OP_IDIV) { /* floor division */
  1215. op_arith(L, luaV_idiv, luai_numidiv);
  1216. vmbreak;
  1217. }
  1218. vmcase(OP_BAND) {
  1219. op_bitwise(L, l_band);
  1220. vmbreak;
  1221. }
  1222. vmcase(OP_BOR) {
  1223. op_bitwise(L, l_bor);
  1224. vmbreak;
  1225. }
  1226. vmcase(OP_BXOR) {
  1227. op_bitwise(L, l_bxor);
  1228. vmbreak;
  1229. }
  1230. vmcase(OP_SHR) {
  1231. op_bitwise(L, luaV_shiftr);
  1232. vmbreak;
  1233. }
  1234. vmcase(OP_SHL) {
  1235. op_bitwise(L, luaV_shiftl);
  1236. vmbreak;
  1237. }
  1238. vmcase(OP_MMBIN) {
  1239. Instruction pi = *(pc - 2); /* original arith. expression */
  1240. TValue *rb = vRB(i);
  1241. TMS tm = (TMS)GETARG_C(i);
  1242. StkId result = RA(pi);
  1243. lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR);
  1244. Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm));
  1245. vmbreak;
  1246. }
  1247. vmcase(OP_MMBINI) {
  1248. Instruction pi = *(pc - 2); /* original arith. expression */
  1249. int imm = GETARG_sB(i);
  1250. TMS tm = (TMS)GETARG_C(i);
  1251. int flip = GETARG_k(i);
  1252. StkId result = RA(pi);
  1253. Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm));
  1254. vmbreak;
  1255. }
  1256. vmcase(OP_MMBINK) {
  1257. Instruction pi = *(pc - 2); /* original arith. expression */
  1258. TValue *imm = KB(i);
  1259. TMS tm = (TMS)GETARG_C(i);
  1260. int flip = GETARG_k(i);
  1261. StkId result = RA(pi);
  1262. Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm));
  1263. vmbreak;
  1264. }
  1265. vmcase(OP_UNM) {
  1266. TValue *rb = vRB(i);
  1267. lua_Number nb;
  1268. if (ttisinteger(rb)) {
  1269. lua_Integer ib = ivalue(rb);
  1270. setivalue(s2v(ra), intop(-, 0, ib));
  1271. }
  1272. else if (tonumberns(rb, nb)) {
  1273. setfltvalue(s2v(ra), luai_numunm(L, nb));
  1274. }
  1275. else
  1276. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  1277. vmbreak;
  1278. }
  1279. vmcase(OP_BNOT) {
  1280. TValue *rb = vRB(i);
  1281. lua_Integer ib;
  1282. if (tointegerns(rb, &ib)) {
  1283. setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib));
  1284. }
  1285. else
  1286. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  1287. vmbreak;
  1288. }
  1289. vmcase(OP_NOT) {
  1290. TValue *rb = vRB(i);
  1291. int nrb = l_isfalse(rb); /* next assignment may change this value */
  1292. setbvalue(s2v(ra), nrb);
  1293. vmbreak;
  1294. }
  1295. vmcase(OP_LEN) {
  1296. Protect(luaV_objlen(L, ra, vRB(i)));
  1297. vmbreak;
  1298. }
  1299. vmcase(OP_CONCAT) {
  1300. int n = GETARG_B(i); /* number of elements to concatenate */
  1301. L->top = ra + n; /* mark the end of concat operands */
  1302. ProtectNT(luaV_concat(L, n));
  1303. checkGC(L, L->top); /* 'luaV_concat' ensures correct top */
  1304. vmbreak;
  1305. }
  1306. vmcase(OP_CLOSE) {
  1307. Protect(luaF_close(L, ra, LUA_OK));
  1308. vmbreak;
  1309. }
  1310. vmcase(OP_TBC) {
  1311. /* create new to-be-closed upvalue */
  1312. halfProtect(luaF_newtbcupval(L, ra));
  1313. vmbreak;
  1314. }
  1315. vmcase(OP_JMP) {
  1316. dojump(ci, i, 0);
  1317. vmbreak;
  1318. }
  1319. vmcase(OP_EQ) {
  1320. int cond;
  1321. TValue *rb = vRB(i);
  1322. Protect(cond = luaV_equalobj(L, s2v(ra), rb));
  1323. docondjump();
  1324. vmbreak;
  1325. }
  1326. vmcase(OP_LT) {
  1327. op_order(L, l_lti, LTnum, lessthanothers);
  1328. vmbreak;
  1329. }
  1330. vmcase(OP_LE) {
  1331. op_order(L, l_lei, LEnum, lessequalothers);
  1332. vmbreak;
  1333. }
  1334. vmcase(OP_EQK) {
  1335. TValue *rb = KB(i);
  1336. /* basic types do not use '__eq'; we can use raw equality */
  1337. int cond = luaV_equalobj(NULL, s2v(ra), rb);
  1338. docondjump();
  1339. vmbreak;
  1340. }
  1341. vmcase(OP_EQI) {
  1342. int cond;
  1343. int im = GETARG_sB(i);
  1344. if (ttisinteger(s2v(ra)))
  1345. cond = (ivalue(s2v(ra)) == im);
  1346. else if (ttisfloat(s2v(ra)))
  1347. cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im));
  1348. else
  1349. cond = 0; /* other types cannot be equal to a number */
  1350. docondjump();
  1351. vmbreak;
  1352. }
  1353. vmcase(OP_LTI) {
  1354. op_orderI(L, l_lti, luai_numlt, 0, TM_LT);
  1355. vmbreak;
  1356. }
  1357. vmcase(OP_LEI) {
  1358. op_orderI(L, l_lei, luai_numle, 0, TM_LE);
  1359. vmbreak;
  1360. }
  1361. vmcase(OP_GTI) {
  1362. op_orderI(L, l_gti, luai_numgt, 1, TM_LT);
  1363. vmbreak;
  1364. }
  1365. vmcase(OP_GEI) {
  1366. op_orderI(L, l_gei, luai_numge, 1, TM_LE);
  1367. vmbreak;
  1368. }
  1369. vmcase(OP_TEST) {
  1370. int cond = !l_isfalse(s2v(ra));
  1371. docondjump();
  1372. vmbreak;
  1373. }
  1374. vmcase(OP_TESTSET) {
  1375. TValue *rb = vRB(i);
  1376. if (l_isfalse(rb) == GETARG_k(i))
  1377. pc++;
  1378. else {
  1379. setobj2s(L, ra, rb);
  1380. donextjump(ci);
  1381. }
  1382. vmbreak;
  1383. }
  1384. vmcase(OP_CALL) {
  1385. int b = GETARG_B(i);
  1386. int nresults = GETARG_C(i) - 1;
  1387. if (b != 0) /* fixed number of arguments? */
  1388. L->top = ra + b; /* top signals number of arguments */
  1389. /* else previous instruction set top */
  1390. ProtectNT(luaD_call(L, ra, nresults));
  1391. vmbreak;
  1392. }
  1393. vmcase(OP_TAILCALL) {
  1394. int b = GETARG_B(i); /* number of arguments + 1 (function) */
  1395. int nparams1 = GETARG_C(i);
  1396. /* delat is virtual 'func' - real 'func' (vararg functions) */
  1397. int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0;
  1398. if (b != 0)
  1399. L->top = ra + b;
  1400. else /* previous instruction set top */
  1401. b = cast_int(L->top - ra);
  1402. savepc(ci); /* some calls here can raise errors */
  1403. if (TESTARG_k(i)) {
  1404. /* close upvalues from current call; the compiler ensures
  1405. that there are no to-be-closed variables here, so this
  1406. call cannot change the stack */
  1407. luaF_close(L, base, NOCLOSINGMETH);
  1408. lua_assert(base == ci->func + 1);
  1409. }
  1410. if (!ttisfunction(s2v(ra))) { /* not a function? */
  1411. luaD_tryfuncTM(L, ra); /* try '__call' metamethod */
  1412. b++; /* there is now one extra argument */
  1413. }
  1414. if (!ttisLclosure(s2v(ra))) { /* C function? */
  1415. luaD_call(L, ra, LUA_MULTRET); /* call it */
  1416. updatetrap(ci);
  1417. updatestack(ci); /* stack may have been relocated */
  1418. ci->func -= delta;
  1419. luaD_poscall(L, ci, cast_int(L->top - ra));
  1420. return;
  1421. }
  1422. else { /* Lua tail call */
  1423. ci->func -= delta;
  1424. luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
  1425. goto tailcall;
  1426. }
  1427. vmbreak;
  1428. }
  1429. vmcase(OP_RETURN) {
  1430. int n = GETARG_B(i) - 1; /* number of results */
  1431. int nparams1 = GETARG_C(i);
  1432. if (n < 0) /* not fixed? */
  1433. n = cast_int(L->top - ra); /* get what is available */
  1434. savepc(ci);
  1435. if (TESTARG_k(i)) { /* may there be open upvalues? */
  1436. if (L->top < ci->top)
  1437. L->top = ci->top;
  1438. luaF_close(L, base, LUA_OK);
  1439. updatetrap(ci);
  1440. updatestack(ci);
  1441. }
  1442. if (nparams1) /* vararg function? */
  1443. ci->func -= ci->u.l.nextraargs + nparams1;
  1444. L->top = ra + n; /* set call for 'luaD_poscall' */
  1445. luaD_poscall(L, ci, n);
  1446. return;
  1447. }
  1448. vmcase(OP_RETURN0) {
  1449. if (L->hookmask) {
  1450. L->top = ra;
  1451. halfProtectNT(luaD_poscall(L, ci, 0)); /* no hurry... */
  1452. }
  1453. else { /* do the 'poscall' here */
  1454. int nres = ci->nresults;
  1455. L->ci = ci->previous; /* back to caller */
  1456. L->top = base - 1;
  1457. while (nres-- > 0)
  1458. setnilvalue(s2v(L->top++)); /* all results are nil */
  1459. }
  1460. return;
  1461. }
  1462. vmcase(OP_RETURN1) {
  1463. if (L->hookmask) {
  1464. L->top = ra + 1;
  1465. halfProtectNT(luaD_poscall(L, ci, 1)); /* no hurry... */
  1466. }
  1467. else { /* do the 'poscall' here */
  1468. int nres = ci->nresults;
  1469. L->ci = ci->previous; /* back to caller */
  1470. if (nres == 0)
  1471. L->top = base - 1; /* asked for no results */
  1472. else {
  1473. setobjs2s(L, base - 1, ra); /* at least this result */
  1474. L->top = base;
  1475. while (--nres > 0) /* complete missing results */
  1476. setnilvalue(s2v(L->top++));
  1477. }
  1478. }
  1479. return;
  1480. }
  1481. vmcase(OP_FORLOOP) {
  1482. if (ttisinteger(s2v(ra + 2))) { /* integer loop? */
  1483. lua_Unsigned count = l_castS2U(ivalue(s2v(ra + 1)));
  1484. if (count > 0) { /* still more iterations? */
  1485. lua_Integer step = ivalue(s2v(ra + 2));
  1486. lua_Integer idx = ivalue(s2v(ra)); /* internal index */
  1487. chgivalue(s2v(ra + 1), count - 1); /* update counter */
  1488. idx = intop(+, idx, step); /* add step to index */
  1489. chgivalue(s2v(ra), idx); /* update internal index */
  1490. setivalue(s2v(ra + 3), idx); /* and control variable */
  1491. pc -= GETARG_Bx(i); /* jump back */
  1492. }
  1493. }
  1494. else { /* floating loop */
  1495. lua_Number step = fltvalue(s2v(ra + 2));
  1496. lua_Number limit = fltvalue(s2v(ra + 1));
  1497. lua_Number idx = fltvalue(s2v(ra));
  1498. idx = luai_numadd(L, idx, step); /* increment index */
  1499. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  1500. : luai_numle(limit, idx)) {
  1501. chgfltvalue(s2v(ra), idx); /* update internal index */
  1502. setfltvalue(s2v(ra + 3), idx); /* and control variable */
  1503. pc -= GETARG_Bx(i); /* jump back */
  1504. }
  1505. }
  1506. updatetrap(ci); /* allows a signal to break the loop */
  1507. vmbreak;
  1508. }
  1509. vmcase(OP_FORPREP) {
  1510. TValue *pinit = s2v(ra);
  1511. TValue *plimit = s2v(ra + 1);
  1512. TValue *pstep = s2v(ra + 2);
  1513. savestate(L, ci); /* in case of errors */
  1514. if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */
  1515. lua_Integer init = ivalue(pinit);
  1516. lua_Integer step = ivalue(pstep);
  1517. lua_Integer limit;
  1518. if (step == 0)
  1519. luaG_runerror(L, "'for' step is zero");
  1520. setivalue(s2v(ra + 3), init); /* control variable */
  1521. if (forlimit(L, init, plimit, &limit, step))
  1522. pc += GETARG_Bx(i) + 1; /* skip the loop */
  1523. else { /* prepare loop counter */
  1524. lua_Unsigned count;
  1525. if (step > 0) { /* ascending loop? */
  1526. count = l_castS2U(limit) - l_castS2U(init);
  1527. if (step != 1) /* avoid division in the too common case */
  1528. count /= l_castS2U(step);
  1529. }
  1530. else { /* step < 0; descending loop */
  1531. count = l_castS2U(init) - l_castS2U(limit);
  1532. /* 'step+1' avoids negating 'mininteger' */
  1533. count /= l_castS2U(-(step + 1)) + 1u;
  1534. }
  1535. /* store the counter in place of the limit (which won't be
  1536. needed anymore */
  1537. setivalue(plimit, l_castU2S(count));
  1538. }
  1539. }
  1540. else { /* try making all values floats */
  1541. lua_Number init; lua_Number limit; lua_Number step;
  1542. if (unlikely(!tonumber(plimit, &limit)))
  1543. luaG_forerror(L, plimit, "limit");
  1544. if (unlikely(!tonumber(pstep, &step)))
  1545. luaG_forerror(L, pstep, "step");
  1546. if (unlikely(!tonumber(pinit, &init)))
  1547. luaG_forerror(L, pinit, "initial value");
  1548. if (step == 0)
  1549. luaG_runerror(L, "'for' step is zero");
  1550. if (luai_numlt(0, step) ? luai_numlt(limit, init)
  1551. : luai_numlt(init, limit))
  1552. pc += GETARG_Bx(i) + 1; /* skip the loop */
  1553. else {
  1554. /* make sure internal values are all float */
  1555. setfltvalue(plimit, limit);
  1556. setfltvalue(pstep, step);
  1557. setfltvalue(s2v(ra), init); /* internal index */
  1558. setfltvalue(s2v(ra + 3), init); /* control variable */
  1559. }
  1560. }
  1561. vmbreak;
  1562. }
  1563. vmcase(OP_TFORPREP) {
  1564. /* create to-be-closed upvalue (if needed) */
  1565. halfProtect(luaF_newtbcupval(L, ra + 3));
  1566. pc += GETARG_Bx(i);
  1567. i = *(pc++); /* go to next instruction */
  1568. lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i));
  1569. goto l_tforcall;
  1570. }
  1571. vmcase(OP_TFORCALL) {
  1572. l_tforcall:
  1573. /* 'ra' has the iterator function, 'ra + 1' has the state,
  1574. 'ra + 2' has the control variable, and 'ra + 3' has the
  1575. to-be-closed variable. The call will use the stack after
  1576. these values (starting at 'ra + 4')
  1577. */
  1578. /* push function, state, and control variable */
  1579. memcpy(ra + 4, ra, 3 * sizeof(*ra));
  1580. L->top = ra + 4 + 3;
  1581. ProtectNT(luaD_call(L, ra + 4, GETARG_C(i))); /* do the call */
  1582. updatestack(ci); /* stack may have changed */
  1583. i = *(pc++); /* go to next instruction */
  1584. lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i));
  1585. goto l_tforloop;
  1586. }
  1587. vmcase(OP_TFORLOOP) {
  1588. l_tforloop:
  1589. if (!ttisnil(s2v(ra + 4))) { /* continue loop? */
  1590. setobjs2s(L, ra + 2, ra + 4); /* save control variable */
  1591. pc -= GETARG_Bx(i); /* jump back */
  1592. }
  1593. vmbreak;
  1594. }
  1595. vmcase(OP_SETLIST) {
  1596. int n = GETARG_B(i);
  1597. unsigned int last = GETARG_C(i);
  1598. Table *h = hvalue(s2v(ra));
  1599. if (n == 0)
  1600. n = cast_int(L->top - ra) - 1; /* get up to the top */
  1601. else
  1602. L->top = ci->top; /* correct top in case of emergency GC */
  1603. last += n;
  1604. if (TESTARG_k(i)) {
  1605. last += GETARG_Ax(*pc) * (MAXARG_C + 1);
  1606. pc++;
  1607. }
  1608. if (last > luaH_realasize(h)) /* needs more space? */
  1609. luaH_resizearray(L, h, last); /* preallocate it at once */
  1610. for (; n > 0; n--) {
  1611. TValue *val = s2v(ra + n);
  1612. setobj2t(L, &h->array[last - 1], val);
  1613. last--;
  1614. luaC_barrierback(L, obj2gco(h), val);
  1615. }
  1616. vmbreak;
  1617. }
  1618. vmcase(OP_CLOSURE) {
  1619. Proto *p = cl->p->p[GETARG_Bx(i)];
  1620. halfProtect(pushclosure(L, p, cl->upvals, base, ra));
  1621. checkGC(L, ra + 1);
  1622. vmbreak;
  1623. }
  1624. vmcase(OP_VARARG) {
  1625. int n = GETARG_C(i) - 1; /* required results */
  1626. Protect(luaT_getvarargs(L, ci, ra, n));
  1627. vmbreak;
  1628. }
  1629. vmcase(OP_VARARGPREP) {
  1630. luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p);
  1631. updatetrap(ci);
  1632. if (trap) {
  1633. luaD_hookcall(L, ci);
  1634. L->oldpc = pc + 1; /* next opcode will be seen as a "new" line */
  1635. }
  1636. updatebase(ci); /* function has new base after adjustment */
  1637. vmbreak;
  1638. }
  1639. vmcase(OP_EXTRAARG) {
  1640. lua_assert(0);
  1641. vmbreak;
  1642. }
  1643. }
  1644. }
  1645. }
  1646. /* }================================================================== */