lvm.c 53 KB

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