lvm.c 55 KB

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