lvm.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. /*
  2. ** $Id: lvm.c,v 2.351 2018/03/16 14:21:20 roberto Exp roberto $
  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 (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 'luaH_emptyobject' if there is no such
  197. ** entry. (The value at 'slot' must be empty, otherwise 'luaV_fastget'
  198. ** 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 (slot == luaH_emptyobject) /* 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 (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 (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 (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_div (lua_State *L, lua_Integer m, lua_Integer n) {
  556. if (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_div.)
  572. */
  573. lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
  574. if (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 && (m ^ n) < 0) /* 'm/n' would be non-integer negative? */
  582. r += n; /* correct result for different rounding */
  583. return r;
  584. }
  585. }
  586. /* number of bits in an integer */
  587. #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
  588. /*
  589. ** Shift left operation. (Shift right just negates 'y'.)
  590. */
  591. lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
  592. if (y < 0) { /* shift right? */
  593. if (y <= -NBITS) return 0;
  594. else return intop(>>, x, -y);
  595. }
  596. else { /* shift left */
  597. if (y >= NBITS) return 0;
  598. else return intop(<<, x, y);
  599. }
  600. }
  601. /*
  602. ** check whether cached closure in prototype 'p' may be reused, that is,
  603. ** whether there is a cached closure with the same upvalues needed by
  604. ** new closure to be created.
  605. */
  606. static LClosure *getcached (Proto *p, UpVal **encup, StkId base) {
  607. LClosure *c = p->cache;
  608. if (c != NULL) { /* is there a cached closure? */
  609. int nup = p->sizeupvalues;
  610. Upvaldesc *uv = p->upvalues;
  611. int i;
  612. for (i = 0; i < nup; i++) { /* check whether it has right upvalues */
  613. TValue *v = uv[i].instack ? s2v(base + uv[i].idx) : encup[uv[i].idx]->v;
  614. if (c->upvals[i]->v != v)
  615. return NULL; /* wrong upvalue; cannot reuse closure */
  616. }
  617. p->cachemiss = 0; /* got a hit */
  618. }
  619. return c; /* return cached closure (or NULL if no cached closure) */
  620. }
  621. /*
  622. ** create a new Lua closure, push it in the stack, and initialize
  623. ** its upvalues. ???
  624. */
  625. static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
  626. StkId ra) {
  627. int nup = p->sizeupvalues;
  628. Upvaldesc *uv = p->upvalues;
  629. int i;
  630. LClosure *ncl = luaF_newLclosure(L, nup);
  631. ncl->p = p;
  632. setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */
  633. for (i = 0; i < nup; i++) { /* fill in its upvalues */
  634. if (uv[i].instack) /* upvalue refers to local variable? */
  635. ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
  636. else /* get upvalue from enclosing function */
  637. ncl->upvals[i] = encup[uv[i].idx];
  638. /* new closure is white, so we do not need a barrier here */
  639. }
  640. if (p->cachemiss >= MAXMISS) /* too many missings? */
  641. p->cache = NULL; /* give up cache */
  642. else {
  643. p->cache = ncl; /* save it on cache for reuse */
  644. luaC_protobarrier(L, p, ncl);
  645. p->cachemiss++;
  646. }
  647. }
  648. /*
  649. ** finish execution of an opcode interrupted by a yield
  650. */
  651. void luaV_finishOp (lua_State *L) {
  652. CallInfo *ci = L->ci;
  653. StkId base = ci->func + 1;
  654. Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */
  655. OpCode op = GET_OPCODE(inst);
  656. switch (op) { /* finish its execution */
  657. case OP_ADDI: case OP_SUBI:
  658. case OP_MULI: case OP_DIVI: case OP_IDIVI:
  659. case OP_MODI: case OP_POWI:
  660. case OP_ADD: case OP_SUB:
  661. case OP_MUL: case OP_DIV: case OP_IDIV:
  662. case OP_BANDK: case OP_BORK: case OP_BXORK:
  663. case OP_BAND: case OP_BOR: case OP_BXOR:
  664. case OP_SHRI: case OP_SHL: case OP_SHR:
  665. case OP_MOD: case OP_POW:
  666. case OP_UNM: case OP_BNOT: case OP_LEN:
  667. case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
  668. case OP_GETFIELD: case OP_SELF: {
  669. setobjs2s(L, base + GETARG_A(inst), --L->top);
  670. break;
  671. }
  672. case OP_LT: case OP_LE:
  673. case OP_LTI: case OP_LEI:
  674. case OP_GTI: case OP_GEI:
  675. case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */
  676. int res = !l_isfalse(s2v(L->top - 1));
  677. L->top--;
  678. if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
  679. ci->callstatus ^= CIST_LEQ; /* clear mark */
  680. res = !res; /* negate result */
  681. }
  682. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  683. if (res != GETARG_k(inst)) /* condition failed? */
  684. ci->u.l.savedpc++; /* skip jump instruction */
  685. break;
  686. }
  687. case OP_CONCAT: {
  688. StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */
  689. int a = GETARG_A(inst); /* first element to concatenate */
  690. int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */
  691. setobjs2s(L, top - 2, top); /* put TM result in proper position */
  692. if (total > 1) { /* are there elements to concat? */
  693. L->top = top - 1; /* top is one after last element (at top-2) */
  694. luaV_concat(L, total); /* concat them (may yield again) */
  695. }
  696. break;
  697. }
  698. case OP_TFORCALL: case OP_CALL: case OP_TAILCALL:
  699. case OP_SETTABUP: case OP_SETTABLE:
  700. case OP_SETI: case OP_SETFIELD:
  701. break;
  702. default: lua_assert(0);
  703. }
  704. }
  705. /*
  706. ** {==================================================================
  707. ** Function 'luaV_execute': main interpreter loop
  708. ** ===================================================================
  709. */
  710. /*
  711. ** some macros for common tasks in 'luaV_execute'
  712. */
  713. #define RA(i) (base+GETARG_A(i))
  714. #define RB(i) (base+GETARG_B(i))
  715. #define vRB(i) s2v(RB(i))
  716. #define KB(i) (k+GETARG_B(i))
  717. #define RC(i) (base+GETARG_C(i))
  718. #define vRC(i) s2v(RC(i))
  719. #define KC(i) (k+GETARG_C(i))
  720. #define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
  721. #define updatetrap(ci) (trap = ci->u.l.trap)
  722. #define updatebase(ci) (base = ci->func + 1)
  723. /*
  724. ** Execute a jump instruction. The 'updatetrap' allows signals to stop
  725. ** tight loops. (Without it, the local copy of 'trap' could never change.)
  726. */
  727. #define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); }
  728. /* for test instructions, execute the jump instruction that follows it */
  729. #define donextjump(ci) { i = *pc; dojump(ci, i, 1); }
  730. /*
  731. ** do a conditional jump: skip next instruction if 'cond' is not what
  732. ** was expected (parameter 'k'), else do next instruction, which must
  733. ** be a jump.
  734. */
  735. #define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci);
  736. /*
  737. ** Correct global 'pc'.
  738. */
  739. #define savepc(L) (ci->u.l.savedpc = pc)
  740. /*
  741. ** Whenever code can raise errors, the global 'pc' and the global
  742. ** 'top' must be correct to report occasional errors.
  743. */
  744. #define savestate(L,ci) (savepc(L), L->top = ci->top)
  745. /*
  746. ** Protect code that, in general, can raise errors, reallocate the
  747. ** stack, and change the hooks.
  748. */
  749. #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci))
  750. /* special version that does not change the top */
  751. #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci))
  752. /*
  753. ** Protect code that will finish the loop (returns).
  754. */
  755. #define halfProtect(exp) (savepc(L), (exp))
  756. #define checkGC(L,c) \
  757. { luaC_condGC(L, L->top = (c), /* limit of live values */ \
  758. updatetrap(ci)); \
  759. luai_threadyield(L); }
  760. /* fetch an instruction and prepare its execution */
  761. #define vmfetch() { \
  762. i = *(pc++); \
  763. if (trap) { \
  764. if (!(L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT))) \
  765. trap = ci->u.l.trap = 0; /* no need to stop again */ \
  766. else { savepc(L); luaG_traceexec(L); } \
  767. updatebase(ci); /* the trap may be just for that */ \
  768. } \
  769. ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \
  770. vra = s2v(ra); \
  771. }
  772. #define vmdispatch(o) switch(o)
  773. #define vmcase(l) case l:
  774. #define vmbreak break
  775. void luaV_execute (lua_State *L, CallInfo *ci) {
  776. LClosure *cl;
  777. TValue *k;
  778. StkId base;
  779. const Instruction *pc;
  780. int trap;
  781. #if LUA_USE_JUMPTABLE
  782. #include "ljumptab.h"
  783. #endif
  784. tailcall:
  785. trap = L->hookmask;
  786. cl = clLvalue(s2v(ci->func));
  787. k = cl->p->k;
  788. pc = ci->u.l.savedpc;
  789. if (trap) {
  790. if (cl->p->is_vararg)
  791. trap = 0; /* hooks will start with PREPVARARG instruction */
  792. else if (pc == cl->p->code) /* first instruction (not resuming)? */
  793. luaD_hookcall(L, ci);
  794. ci->u.l.trap = 1; /* there may be other hooks */
  795. }
  796. base = ci->func + 1;
  797. /* main loop of interpreter */
  798. for (;;) {
  799. int cond; /* flag for conditional jumps */
  800. Instruction i; /* instruction being executed */
  801. StkId ra; /* instruction's A register */
  802. TValue *vra; /* corresponding value */
  803. vmfetch();
  804. lua_assert(base == ci->func + 1);
  805. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  806. lua_assert(ci->top < L->stack + L->stacksize);
  807. vmdispatch (GET_OPCODE(i)) {
  808. vmcase(OP_MOVE) {
  809. setobjs2s(L, ra, RB(i));
  810. vmbreak;
  811. }
  812. vmcase(OP_LOADK) {
  813. TValue *rb = k + GETARG_Bx(i);
  814. setobj2s(L, ra, rb);
  815. vmbreak;
  816. }
  817. vmcase(OP_LOADI) {
  818. lua_Integer b = GETARG_sBx(i);
  819. setivalue(vra, b);
  820. vmbreak;
  821. }
  822. vmcase(OP_LOADF) {
  823. int b = GETARG_sBx(i);
  824. setfltvalue(vra, cast_num(b));
  825. vmbreak;
  826. }
  827. vmcase(OP_LOADKX) {
  828. TValue *rb;
  829. rb = k + GETARG_Ax(*pc); pc++;
  830. setobj2s(L, ra, rb);
  831. vmbreak;
  832. }
  833. vmcase(OP_LOADBOOL) {
  834. setbvalue(vra, GETARG_B(i));
  835. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  836. vmbreak;
  837. }
  838. vmcase(OP_LOADNIL) {
  839. int b = GETARG_B(i);
  840. do {
  841. setnilvalue(s2v(ra++));
  842. } while (b--);
  843. vmbreak;
  844. }
  845. vmcase(OP_GETUPVAL) {
  846. int b = GETARG_B(i);
  847. setobj2s(L, ra, cl->upvals[b]->v);
  848. vmbreak;
  849. }
  850. vmcase(OP_SETUPVAL) {
  851. UpVal *uv = cl->upvals[GETARG_B(i)];
  852. setobj(L, uv->v, vra);
  853. luaC_barrier(L, uv, vra);
  854. vmbreak;
  855. }
  856. vmcase(OP_GETTABUP) {
  857. const TValue *slot;
  858. TValue *upval = cl->upvals[GETARG_B(i)]->v;
  859. TValue *rc = KC(i);
  860. TString *key = tsvalue(rc); /* key must be a string */
  861. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  862. setobj2s(L, ra, slot);
  863. }
  864. else
  865. Protect(luaV_finishget(L, upval, rc, ra, slot));
  866. vmbreak;
  867. }
  868. vmcase(OP_GETTABLE) {
  869. const TValue *slot;
  870. TValue *rb = vRB(i);
  871. TValue *rc = vRC(i);
  872. lua_Unsigned n;
  873. if (ttisinteger(rc) /* fast track for integers? */
  874. ? (n = ivalue(rc), luaV_fastgeti(L, rb, n, slot))
  875. : luaV_fastget(L, rb, rc, slot, luaH_get)) {
  876. setobj2s(L, ra, slot);
  877. }
  878. else
  879. Protect(luaV_finishget(L, rb, rc, ra, slot));
  880. vmbreak;
  881. }
  882. vmcase(OP_GETI) {
  883. const TValue *slot;
  884. TValue *rb = vRB(i);
  885. int c = GETARG_C(i);
  886. if (luaV_fastgeti(L, rb, c, slot)) {
  887. setobj2s(L, ra, slot);
  888. }
  889. else {
  890. TValue key;
  891. setivalue(&key, c);
  892. Protect(luaV_finishget(L, rb, &key, ra, slot));
  893. }
  894. vmbreak;
  895. }
  896. vmcase(OP_GETFIELD) {
  897. const TValue *slot;
  898. TValue *rb = vRB(i);
  899. TValue *rc = KC(i);
  900. TString *key = tsvalue(rc); /* key must be a string */
  901. if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) {
  902. setobj2s(L, ra, slot);
  903. }
  904. else
  905. Protect(luaV_finishget(L, rb, rc, ra, slot));
  906. vmbreak;
  907. }
  908. vmcase(OP_SETTABUP) {
  909. const TValue *slot;
  910. TValue *upval = cl->upvals[GETARG_A(i)]->v;
  911. TValue *rb = KB(i);
  912. TValue *rc = RKC(i);
  913. TString *key = tsvalue(rb); /* key must be a string */
  914. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  915. luaV_finishfastset(L, upval, slot, rc);
  916. }
  917. else
  918. Protect(luaV_finishset(L, upval, rb, rc, slot));
  919. vmbreak;
  920. }
  921. vmcase(OP_SETTABLE) {
  922. const TValue *slot;
  923. TValue *rb = vRB(i); /* key (table is in 'ra') */
  924. TValue *rc = RKC(i); /* value */
  925. lua_Unsigned n;
  926. if (ttisinteger(rb) /* fast track for integers? */
  927. ? (n = ivalue(rb), luaV_fastgeti(L, vra, n, slot))
  928. : luaV_fastget(L, vra, rb, slot, luaH_get)) {
  929. luaV_finishfastset(L, vra, slot, rc);
  930. }
  931. else
  932. Protect(luaV_finishset(L, vra, rb, rc, slot));
  933. vmbreak;
  934. }
  935. vmcase(OP_SETI) {
  936. const TValue *slot;
  937. int c = GETARG_B(i);
  938. TValue *rc = RKC(i);
  939. if (luaV_fastgeti(L, vra, c, slot)) {
  940. luaV_finishfastset(L, vra, slot, rc);
  941. }
  942. else {
  943. TValue key;
  944. setivalue(&key, c);
  945. Protect(luaV_finishset(L, vra, &key, rc, slot));
  946. }
  947. vmbreak;
  948. }
  949. vmcase(OP_SETFIELD) {
  950. const TValue *slot;
  951. TValue *rb = KB(i);
  952. TValue *rc = RKC(i);
  953. TString *key = tsvalue(rb); /* key must be a string */
  954. if (luaV_fastget(L, vra, key, slot, luaH_getshortstr)) {
  955. luaV_finishfastset(L, vra, slot, rc);
  956. }
  957. else
  958. Protect(luaV_finishset(L, vra, rb, rc, slot));
  959. vmbreak;
  960. }
  961. vmcase(OP_NEWTABLE) {
  962. int b = GETARG_B(i);
  963. int c = GETARG_C(i);
  964. Table *t;
  965. L->top = ci->top; /* correct top in case of GC */
  966. t = luaH_new(L); /* memory allocation */
  967. sethvalue2s(L, ra, t);
  968. if (b != 0 || c != 0)
  969. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); /* idem */
  970. checkGC(L, ra + 1);
  971. vmbreak;
  972. }
  973. vmcase(OP_SELF) {
  974. const TValue *slot;
  975. TValue *rb = vRB(i);
  976. TValue *rc = RKC(i);
  977. TString *key = tsvalue(rc); /* key must be a string */
  978. setobj2s(L, ra + 1, rb);
  979. if (luaV_fastget(L, rb, key, slot, luaH_getstr)) {
  980. setobj2s(L, ra, slot);
  981. }
  982. else
  983. Protect(luaV_finishget(L, rb, rc, ra, slot));
  984. vmbreak;
  985. }
  986. vmcase(OP_ADDI) {
  987. TValue *rb = vRB(i);
  988. int ic = GETARG_sC(i);
  989. lua_Number nb;
  990. if (ttisinteger(rb)) {
  991. setivalue(vra, intop(+, ivalue(rb), ic));
  992. }
  993. else if (tonumberns(rb, nb)) {
  994. setfltvalue(vra, luai_numadd(L, nb, cast_num(ic)));
  995. }
  996. else
  997. Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_ADD));
  998. vmbreak;
  999. }
  1000. vmcase(OP_SUBI) {
  1001. TValue *rb = vRB(i);
  1002. int ic = GETARG_sC(i);
  1003. lua_Number nb;
  1004. if (ttisinteger(rb)) {
  1005. setivalue(vra, intop(-, ivalue(rb), ic));
  1006. }
  1007. else if (tonumberns(rb, nb)) {
  1008. setfltvalue(vra, luai_numsub(L, nb, cast_num(ic)));
  1009. }
  1010. else
  1011. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_SUB));
  1012. vmbreak;
  1013. }
  1014. vmcase(OP_MULI) {
  1015. TValue *rb = vRB(i);
  1016. int ic = GETARG_sC(i);
  1017. lua_Number nb;
  1018. if (ttisinteger(rb)) {
  1019. setivalue(vra, intop(*, ivalue(rb), ic));
  1020. }
  1021. else if (tonumberns(rb, nb)) {
  1022. setfltvalue(vra, luai_nummul(L, nb, cast_num(ic)));
  1023. }
  1024. else
  1025. Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_MUL));
  1026. vmbreak;
  1027. }
  1028. vmcase(OP_MODI) {
  1029. TValue *rb = vRB(i);
  1030. int ic = GETARG_sC(i);
  1031. lua_Number nb;
  1032. if (ttisinteger(rb)) {
  1033. setivalue(vra, luaV_mod(L, ivalue(rb), ic));
  1034. }
  1035. else if (tonumberns(rb, nb)) {
  1036. lua_Number m;
  1037. lua_Number nc = cast_num(ic);
  1038. luai_nummod(L, nb, nc, m);
  1039. setfltvalue(vra, m);
  1040. }
  1041. else
  1042. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_MOD));
  1043. vmbreak;
  1044. }
  1045. vmcase(OP_POWI) {
  1046. TValue *rb = vRB(i);
  1047. int ic = GETARG_sC(i);
  1048. lua_Number nb;
  1049. if (tonumberns(rb, nb)) {
  1050. lua_Number nc = cast_num(ic);
  1051. setfltvalue(vra, luai_numpow(L, nb, nc));
  1052. }
  1053. else
  1054. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_POW));
  1055. vmbreak;
  1056. }
  1057. vmcase(OP_DIVI) {
  1058. TValue *rb = vRB(i);
  1059. int ic = GETARG_sC(i);
  1060. lua_Number nb;
  1061. if (tonumberns(rb, nb)) {
  1062. lua_Number nc = cast_num(ic);
  1063. setfltvalue(vra, luai_numdiv(L, nb, nc));
  1064. }
  1065. else
  1066. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_DIV));
  1067. vmbreak;
  1068. }
  1069. vmcase(OP_IDIVI) {
  1070. TValue *rb = vRB(i);
  1071. int ic = GETARG_sC(i);
  1072. lua_Number nb;
  1073. if (ttisinteger(rb)) {
  1074. setivalue(vra, luaV_div(L, ivalue(rb), ic));
  1075. }
  1076. else if (tonumberns(rb, nb)) {
  1077. lua_Number nc = cast_num(ic);
  1078. setfltvalue(vra, luai_numdiv(L, nb, nc));
  1079. }
  1080. else
  1081. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_IDIV));
  1082. vmbreak;
  1083. }
  1084. vmcase(OP_ADD) {
  1085. TValue *rb = vRB(i);
  1086. TValue *rc = vRC(i);
  1087. lua_Number nb; lua_Number nc;
  1088. if (ttisinteger(rb) && ttisinteger(rc)) {
  1089. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1090. setivalue(vra, intop(+, ib, ic));
  1091. }
  1092. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1093. setfltvalue(vra, luai_numadd(L, nb, nc));
  1094. }
  1095. else
  1096. Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD));
  1097. vmbreak;
  1098. }
  1099. vmcase(OP_SUB) {
  1100. TValue *rb = vRB(i);
  1101. TValue *rc = vRC(i);
  1102. lua_Number nb; lua_Number nc;
  1103. if (ttisinteger(rb) && ttisinteger(rc)) {
  1104. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1105. setivalue(vra, intop(-, ib, ic));
  1106. }
  1107. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1108. setfltvalue(vra, luai_numsub(L, nb, nc));
  1109. }
  1110. else
  1111. Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB));
  1112. vmbreak;
  1113. }
  1114. vmcase(OP_MUL) {
  1115. TValue *rb = vRB(i);
  1116. TValue *rc = vRC(i);
  1117. lua_Number nb; lua_Number nc;
  1118. if (ttisinteger(rb) && ttisinteger(rc)) {
  1119. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1120. setivalue(vra, intop(*, ib, ic));
  1121. }
  1122. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1123. setfltvalue(vra, luai_nummul(L, nb, nc));
  1124. }
  1125. else
  1126. Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL));
  1127. vmbreak;
  1128. }
  1129. vmcase(OP_DIV) { /* float division (always with floats) */
  1130. TValue *rb = vRB(i);
  1131. TValue *rc = vRC(i);
  1132. lua_Number nb; lua_Number nc;
  1133. if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1134. setfltvalue(vra, luai_numdiv(L, nb, nc));
  1135. }
  1136. else
  1137. Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV));
  1138. vmbreak;
  1139. }
  1140. vmcase(OP_BANDK) {
  1141. TValue *p1 = vRB(i);
  1142. TValue *p2 = KC(i);
  1143. lua_Integer i1;
  1144. if (tointegerns(p1, &i1)) {
  1145. setivalue(vra, intop(&, i1, ivalue(p2)));
  1146. }
  1147. else
  1148. Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BAND));
  1149. vmbreak;
  1150. }
  1151. vmcase(OP_BORK) {
  1152. TValue *p1 = vRB(i);
  1153. TValue *p2 = KC(i);
  1154. lua_Integer i1;
  1155. if (tointegerns(p1, &i1)) {
  1156. setivalue(vra, intop(|, i1, ivalue(p2)));
  1157. }
  1158. else
  1159. Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BOR));
  1160. vmbreak;
  1161. }
  1162. vmcase(OP_BXORK) {
  1163. TValue *p1 = vRB(i);
  1164. TValue *p2 = KC(i);
  1165. lua_Integer i1;
  1166. if (tointegerns(p1, &i1)) {
  1167. setivalue(vra, intop(^, i1, ivalue(p2)));
  1168. }
  1169. else
  1170. Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BXOR));
  1171. vmbreak;
  1172. }
  1173. vmcase(OP_BAND) {
  1174. TValue *rb = vRB(i);
  1175. TValue *rc = vRC(i);
  1176. lua_Integer ib; lua_Integer ic;
  1177. if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
  1178. setivalue(vra, intop(&, ib, ic));
  1179. }
  1180. else
  1181. Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND));
  1182. vmbreak;
  1183. }
  1184. vmcase(OP_BOR) {
  1185. TValue *rb = vRB(i);
  1186. TValue *rc = vRC(i);
  1187. lua_Integer ib; lua_Integer ic;
  1188. if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
  1189. setivalue(vra, intop(|, ib, ic));
  1190. }
  1191. else
  1192. Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR));
  1193. vmbreak;
  1194. }
  1195. vmcase(OP_BXOR) {
  1196. TValue *rb = vRB(i);
  1197. TValue *rc = vRC(i);
  1198. lua_Integer ib; lua_Integer ic;
  1199. if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
  1200. setivalue(vra, intop(^, ib, ic));
  1201. }
  1202. else
  1203. Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR));
  1204. vmbreak;
  1205. }
  1206. vmcase(OP_SHRI) {
  1207. TValue *rb = vRB(i);
  1208. int ic = GETARG_sC(i);
  1209. lua_Integer ib;
  1210. if (tointegerns(rb, &ib)) {
  1211. setivalue(vra, luaV_shiftl(ib, -ic));
  1212. }
  1213. else {
  1214. TMS ev = TM_SHR;
  1215. if (TESTARG_k(i)) {
  1216. ic = -ic; ev = TM_SHL;
  1217. }
  1218. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, ev));
  1219. }
  1220. vmbreak;
  1221. }
  1222. vmcase(OP_SHLI) {
  1223. TValue *rb = vRB(i);
  1224. int ic = GETARG_sC(i);
  1225. lua_Integer ib;
  1226. if (tointegerns(rb, &ib)) {
  1227. setivalue(vra, luaV_shiftl(ic, ib));
  1228. }
  1229. else
  1230. Protect(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL));
  1231. vmbreak;
  1232. }
  1233. vmcase(OP_SHL) {
  1234. TValue *rb = vRB(i);
  1235. TValue *rc = vRC(i);
  1236. lua_Integer ib; lua_Integer ic;
  1237. if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
  1238. setivalue(vra, luaV_shiftl(ib, ic));
  1239. }
  1240. else
  1241. Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL));
  1242. vmbreak;
  1243. }
  1244. vmcase(OP_SHR) {
  1245. TValue *rb = vRB(i);
  1246. TValue *rc = vRC(i);
  1247. lua_Integer ib; lua_Integer ic;
  1248. if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
  1249. setivalue(vra, luaV_shiftl(ib, -ic));
  1250. }
  1251. else
  1252. Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR));
  1253. vmbreak;
  1254. }
  1255. vmcase(OP_MOD) {
  1256. TValue *rb = vRB(i);
  1257. TValue *rc = vRC(i);
  1258. lua_Number nb; lua_Number nc;
  1259. if (ttisinteger(rb) && ttisinteger(rc)) {
  1260. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1261. setivalue(vra, luaV_mod(L, ib, ic));
  1262. }
  1263. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1264. lua_Number m;
  1265. luai_nummod(L, nb, nc, m);
  1266. setfltvalue(vra, m);
  1267. }
  1268. else
  1269. Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD));
  1270. vmbreak;
  1271. }
  1272. vmcase(OP_IDIV) { /* floor division */
  1273. TValue *rb = vRB(i);
  1274. TValue *rc = vRC(i);
  1275. lua_Number nb; lua_Number nc;
  1276. if (ttisinteger(rb) && ttisinteger(rc)) {
  1277. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1278. setivalue(vra, luaV_div(L, ib, ic));
  1279. }
  1280. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1281. setfltvalue(vra, luai_numidiv(L, nb, nc));
  1282. }
  1283. else
  1284. Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV));
  1285. vmbreak;
  1286. }
  1287. vmcase(OP_POW) {
  1288. TValue *rb = vRB(i);
  1289. TValue *rc = vRC(i);
  1290. lua_Number nb; lua_Number nc;
  1291. if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1292. setfltvalue(vra, luai_numpow(L, nb, nc));
  1293. }
  1294. else
  1295. Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW));
  1296. vmbreak;
  1297. }
  1298. vmcase(OP_UNM) {
  1299. TValue *rb = vRB(i);
  1300. lua_Number nb;
  1301. if (ttisinteger(rb)) {
  1302. lua_Integer ib = ivalue(rb);
  1303. setivalue(vra, intop(-, 0, ib));
  1304. }
  1305. else if (tonumberns(rb, nb)) {
  1306. setfltvalue(vra, luai_numunm(L, nb));
  1307. }
  1308. else
  1309. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  1310. vmbreak;
  1311. }
  1312. vmcase(OP_BNOT) {
  1313. TValue *rb = vRB(i);
  1314. lua_Integer ib;
  1315. if (tointegerns(rb, &ib)) {
  1316. setivalue(vra, intop(^, ~l_castS2U(0), ib));
  1317. }
  1318. else
  1319. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  1320. vmbreak;
  1321. }
  1322. vmcase(OP_NOT) {
  1323. TValue *rb = vRB(i);
  1324. int nrb = l_isfalse(rb); /* next assignment may change this value */
  1325. setbvalue(vra, nrb);
  1326. vmbreak;
  1327. }
  1328. vmcase(OP_LEN) {
  1329. Protect(luaV_objlen(L, ra, vRB(i)));
  1330. vmbreak;
  1331. }
  1332. vmcase(OP_CONCAT) {
  1333. int n = GETARG_B(i); /* number of elements to concatenate */
  1334. L->top = ra + n; /* mark the end of concat operands */
  1335. ProtectNT(luaV_concat(L, n));
  1336. checkGC(L, L->top); /* 'luaV_concat' ensures correct top */
  1337. vmbreak;
  1338. }
  1339. vmcase(OP_CLOSE) {
  1340. luaF_close(L, ra);
  1341. vmbreak;
  1342. }
  1343. vmcase(OP_JMP) {
  1344. dojump(ci, i, 0);
  1345. vmbreak;
  1346. }
  1347. vmcase(OP_EQ) {
  1348. TValue *rb = vRB(i);
  1349. Protect(cond = luaV_equalobj(L, vra, rb));
  1350. docondjump();
  1351. vmbreak;
  1352. }
  1353. vmcase(OP_LT) {
  1354. TValue *rb = vRB(i);
  1355. if (ttisinteger(vra) && ttisinteger(rb))
  1356. cond = (ivalue(vra) < ivalue(rb));
  1357. else if (ttisnumber(vra) && ttisnumber(rb))
  1358. cond = LTnum(vra, rb);
  1359. else
  1360. Protect(cond = lessthanothers(L, vra, rb));
  1361. docondjump();
  1362. vmbreak;
  1363. }
  1364. vmcase(OP_LE) {
  1365. TValue *rb = vRB(i);
  1366. if (ttisinteger(vra) && ttisinteger(rb))
  1367. cond = (ivalue(vra) <= ivalue(rb));
  1368. else if (ttisnumber(vra) && ttisnumber(rb))
  1369. cond = LEnum(vra, rb);
  1370. else
  1371. Protect(cond = lessequalothers(L, vra, rb));
  1372. docondjump();
  1373. vmbreak;
  1374. }
  1375. vmcase(OP_EQK) {
  1376. TValue *rb = KB(i);
  1377. /* basic types do not use '__eq'; we can use raw equality */
  1378. cond = luaV_equalobj(NULL, vra, rb);
  1379. docondjump();
  1380. vmbreak;
  1381. }
  1382. vmcase(OP_EQI) {
  1383. int im = GETARG_sB(i);
  1384. if (ttisinteger(vra))
  1385. cond = (ivalue(vra) == im);
  1386. else if (ttisfloat(vra))
  1387. cond = luai_numeq(fltvalue(vra), cast_num(im));
  1388. else
  1389. cond = 0; /* other types cannot be equal to a number */
  1390. docondjump();
  1391. vmbreak;
  1392. }
  1393. vmcase(OP_LTI) {
  1394. int im = GETARG_sB(i);
  1395. if (ttisinteger(vra))
  1396. cond = (ivalue(vra) < im);
  1397. else if (ttisfloat(vra))
  1398. cond = luai_numlt(fltvalue(vra), cast_num(im));
  1399. else
  1400. Protect(cond = luaT_callorderiTM(L, vra, im, 0, TM_LT));
  1401. docondjump();
  1402. vmbreak;
  1403. }
  1404. vmcase(OP_LEI) {
  1405. int im = GETARG_sB(i);
  1406. if (ttisinteger(vra))
  1407. cond = (ivalue(vra) <= im);
  1408. else if (ttisfloat(vra))
  1409. cond = luai_numle(fltvalue(vra), cast_num(im));
  1410. else
  1411. Protect(cond = luaT_callorderiTM(L, vra, im, 0, TM_LE));
  1412. docondjump();
  1413. vmbreak;
  1414. }
  1415. vmcase(OP_GTI) {
  1416. int im = GETARG_sB(i);
  1417. if (ttisinteger(vra))
  1418. cond = (im < ivalue(vra));
  1419. else if (ttisfloat(vra))
  1420. cond = luai_numlt(cast_num(im), fltvalue(vra));
  1421. else
  1422. Protect(cond = luaT_callorderiTM(L, vra, im, 1, TM_LT));
  1423. docondjump();
  1424. vmbreak;
  1425. }
  1426. vmcase(OP_GEI) {
  1427. int im = GETARG_sB(i);
  1428. if (ttisinteger(vra))
  1429. cond = (im <= ivalue(vra));
  1430. else if (ttisfloat(vra))
  1431. cond = luai_numle(cast_num(im), fltvalue(vra));
  1432. else
  1433. Protect(cond = luaT_callorderiTM(L, vra, im, 1, TM_LE));
  1434. docondjump();
  1435. vmbreak;
  1436. }
  1437. vmcase(OP_TEST) {
  1438. cond = !l_isfalse(vra);
  1439. docondjump();
  1440. vmbreak;
  1441. }
  1442. vmcase(OP_TESTSET) {
  1443. TValue *rb = vRB(i);
  1444. if (l_isfalse(rb) == GETARG_k(i))
  1445. pc++;
  1446. else {
  1447. setobj2s(L, ra, rb);
  1448. donextjump(ci);
  1449. }
  1450. vmbreak;
  1451. }
  1452. vmcase(OP_UNDEF) {
  1453. TValue *rb = vRB(i);
  1454. luaT_keydef(L, vra, rb, 1);
  1455. vmbreak;
  1456. }
  1457. vmcase(OP_ISDEF) {
  1458. TValue *rb = vRB(i);
  1459. TValue *rc = vRC(i);
  1460. int res;
  1461. Protect(res = luaT_keydef(L, rb, rc, 0));
  1462. setbvalue(vra, res == GETARG_k(i));
  1463. vmbreak;
  1464. }
  1465. vmcase(OP_CALL) {
  1466. int b = GETARG_B(i);
  1467. int nresults = GETARG_C(i) - 1;
  1468. if (b != 0) /* fixed number of arguments? */
  1469. L->top = ra + b; /* top signals number of arguments */
  1470. /* else previous instruction set top */
  1471. ProtectNT(luaD_call(L, ra, nresults));
  1472. vmbreak;
  1473. }
  1474. vmcase(OP_TAILCALL) {
  1475. int b = GETARG_B(i); /* number of arguments + 1 (function) */
  1476. int delta = 0; /* virtual 'func' - real 'func' (vararg functions) */
  1477. if (b != 0)
  1478. L->top = ra + b;
  1479. else /* previous instruction set top */
  1480. b = cast_int(L->top - ra);
  1481. savepc(ci);
  1482. if (TESTARG_k(i)) {
  1483. int nparams1 = GETARG_C(i);
  1484. if (nparams1) /* vararg function? */
  1485. delta = ci->u.l.nextraargs + nparams1;
  1486. luaF_close(L, base); /* close upvalues from current call */
  1487. }
  1488. if (!ttisfunction(vra)) { /* not a function? */
  1489. luaD_tryfuncTM(L, ra); /* try '__call' metamethod */
  1490. b++; /* there is now one extra argument */
  1491. }
  1492. if (!ttisLclosure(vra)) { /* C function? */
  1493. luaD_call(L, ra, LUA_MULTRET); /* call it */
  1494. updatetrap(ci);
  1495. if (trap) { /* stack may have been relocated */
  1496. updatebase(ci);
  1497. ra = RA(i);
  1498. }
  1499. ci->func -= delta;
  1500. luaD_poscall(L, ci, ra, cast_int(L->top - ra));
  1501. return;
  1502. }
  1503. else { /* Lua tail call */
  1504. ci->func -= delta;
  1505. luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
  1506. goto tailcall;
  1507. }
  1508. vmbreak;
  1509. }
  1510. vmcase(OP_RETURN) {
  1511. int b = GETARG_B(i);
  1512. int n = (b != 0 ? b - 1 : cast_int(L->top - ra));
  1513. if (TESTARG_k(i)) {
  1514. int nparams1 = GETARG_C(i);
  1515. if (nparams1) /* vararg function? */
  1516. ci->func -= ci->u.l.nextraargs + nparams1;
  1517. luaF_close(L, base); /* there may be open upvalues */
  1518. }
  1519. halfProtect(luaD_poscall(L, ci, ra, n));
  1520. return;
  1521. }
  1522. vmcase(OP_RETURN0) {
  1523. if (L->hookmask)
  1524. halfProtect(luaD_poscall(L, ci, ra, 0)); /* no hurry... */
  1525. else {
  1526. int nres = ci->nresults;
  1527. L->ci = ci->previous; /* back to caller */
  1528. L->top = base - 1;
  1529. while (nres-- > 0)
  1530. setnilvalue(s2v(L->top++)); /* all results are nil */
  1531. }
  1532. return;
  1533. }
  1534. vmcase(OP_RETURN1) {
  1535. if (L->hookmask)
  1536. halfProtect(luaD_poscall(L, ci, ra, 1)); /* no hurry... */
  1537. else {
  1538. int nres = ci->nresults;
  1539. L->ci = ci->previous; /* back to caller */
  1540. if (nres == 0)
  1541. L->top = base - 1; /* asked for no results */
  1542. else {
  1543. setobjs2s(L, base - 1, ra); /* at least this result */
  1544. L->top = base;
  1545. while (--nres > 0) /* complete missing results */
  1546. setnilvalue(s2v(L->top++));
  1547. }
  1548. }
  1549. return;
  1550. }
  1551. vmcase(OP_FORLOOP1) {
  1552. lua_Integer idx = intop(+, ivalue(vra), 1); /* increment index */
  1553. lua_Integer limit = ivalue(s2v(ra + 1));
  1554. if (idx <= limit) {
  1555. pc -= GETARG_Bx(i); /* jump back */
  1556. chgivalue(vra, idx); /* update internal index... */
  1557. setivalue(s2v(ra + 3), idx); /* ...and external index */
  1558. }
  1559. updatetrap(ci);
  1560. vmbreak;
  1561. }
  1562. vmcase(OP_FORPREP1) {
  1563. TValue *init = vra;
  1564. TValue *plimit = s2v(ra + 1);
  1565. lua_Integer ilimit, initv;
  1566. int stopnow;
  1567. if (!forlimit(plimit, &ilimit, 1, &stopnow)) {
  1568. savestate(L, ci); /* for the error message */
  1569. luaG_runerror(L, "'for' limit must be a number");
  1570. }
  1571. initv = (stopnow ? 0 : ivalue(init));
  1572. setivalue(plimit, ilimit);
  1573. setivalue(init, intop(-, initv, 1));
  1574. pc += GETARG_Bx(i);
  1575. vmbreak;
  1576. }
  1577. vmcase(OP_FORLOOP) {
  1578. if (ttisinteger(vra)) { /* integer loop? */
  1579. lua_Integer step = ivalue(s2v(ra + 2));
  1580. lua_Integer idx = intop(+, ivalue(vra), step); /* increment index */
  1581. lua_Integer limit = ivalue(s2v(ra + 1));
  1582. if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
  1583. pc -= GETARG_Bx(i); /* jump back */
  1584. chgivalue(vra, idx); /* update internal index... */
  1585. setivalue(s2v(ra + 3), idx); /* ...and external index */
  1586. }
  1587. }
  1588. else { /* floating loop */
  1589. lua_Number step = fltvalue(s2v(ra + 2));
  1590. lua_Number limit = fltvalue(s2v(ra + 1));
  1591. lua_Number idx = fltvalue(vra);
  1592. idx = luai_numadd(L, idx, step); /* inc. index */
  1593. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  1594. : luai_numle(limit, idx)) {
  1595. pc -= GETARG_Bx(i); /* jump back */
  1596. chgfltvalue(vra, idx); /* update internal index... */
  1597. setfltvalue(s2v(ra + 3), idx); /* ...and external index */
  1598. }
  1599. }
  1600. updatetrap(ci);
  1601. vmbreak;
  1602. }
  1603. vmcase(OP_FORPREP) {
  1604. TValue *init = vra;
  1605. TValue *plimit = s2v(ra + 1);
  1606. TValue *pstep = s2v(ra + 2);
  1607. lua_Integer ilimit;
  1608. int stopnow;
  1609. if (ttisinteger(init) && ttisinteger(pstep) &&
  1610. forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
  1611. /* all values are integer */
  1612. lua_Integer initv = (stopnow ? 0 : ivalue(init));
  1613. setivalue(plimit, ilimit);
  1614. setivalue(init, intop(-, initv, ivalue(pstep)));
  1615. }
  1616. else { /* try making all values floats */
  1617. lua_Number ninit; lua_Number nlimit; lua_Number nstep;
  1618. savestate(L, ci); /* in case of errors */
  1619. if (!tonumber(plimit, &nlimit))
  1620. luaG_runerror(L, "'for' limit must be a number");
  1621. setfltvalue(plimit, nlimit);
  1622. if (!tonumber(pstep, &nstep))
  1623. luaG_runerror(L, "'for' step must be a number");
  1624. setfltvalue(pstep, nstep);
  1625. if (!tonumber(init, &ninit))
  1626. luaG_runerror(L, "'for' initial value must be a number");
  1627. setfltvalue(init, luai_numsub(L, ninit, nstep));
  1628. }
  1629. pc += GETARG_Bx(i);
  1630. vmbreak;
  1631. }
  1632. vmcase(OP_TFORCALL) {
  1633. StkId cb = ra + 3; /* call base */
  1634. setobjs2s(L, cb+2, ra+2);
  1635. setobjs2s(L, cb+1, ra+1);
  1636. setobjs2s(L, cb, ra);
  1637. L->top = cb + 3; /* func. + 2 args (state and index) */
  1638. Protect(luaD_call(L, cb, GETARG_C(i)));
  1639. if (trap) /* keep 'base' correct for next instruction */
  1640. updatebase(ci);
  1641. i = *(pc++); /* go to next instruction */
  1642. ra = RA(i); /* get its 'ra' */
  1643. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  1644. goto l_tforloop;
  1645. }
  1646. vmcase(OP_TFORLOOP) {
  1647. l_tforloop:
  1648. if (!ttisnil(s2v(ra + 1))) { /* continue loop? */
  1649. setobjs2s(L, ra, ra + 1); /* save control variable */
  1650. pc -= GETARG_Bx(i); /* jump back */
  1651. }
  1652. vmbreak;
  1653. }
  1654. vmcase(OP_SETLIST) {
  1655. int n = GETARG_B(i);
  1656. int c = GETARG_C(i);
  1657. unsigned int last;
  1658. Table *h;
  1659. if (n == 0)
  1660. n = cast_int(L->top - ra) - 1;
  1661. else
  1662. L->top = ci->top; /* correct top in case of GC */
  1663. if (c == 0) {
  1664. c = GETARG_Ax(*pc); pc++;
  1665. }
  1666. h = hvalue(vra);
  1667. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  1668. if (last > h->sizearray) /* needs more space? */
  1669. luaH_resizearray(L, h, last); /* preallocate it at once */
  1670. for (; n > 0; n--) {
  1671. TValue *val = s2v(ra + n);
  1672. setobj2t(L, &h->array[last - 1], val);
  1673. last--;
  1674. luaC_barrierback(L, obj2gco(h), val);
  1675. }
  1676. vmbreak;
  1677. }
  1678. vmcase(OP_CLOSURE) {
  1679. Proto *p = cl->p->p[GETARG_Bx(i)];
  1680. LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  1681. if (ncl == NULL) { /* no match? */
  1682. savestate(L, ci); /* in case of allocation errors */
  1683. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  1684. }
  1685. else
  1686. setclLvalue2s(L, ra, ncl); /* push cashed closure */
  1687. checkGC(L, ra + 1);
  1688. vmbreak;
  1689. }
  1690. vmcase(OP_VARARG) {
  1691. int n = GETARG_C(i) - 1; /* required results */
  1692. ProtectNT(luaT_getvarargs(L, ci, ra, n));
  1693. vmbreak;
  1694. }
  1695. vmcase(OP_PREPVARARG) {
  1696. luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p);
  1697. updatetrap(ci);
  1698. if (trap) {
  1699. luaD_hookcall(L, ci);
  1700. L->oldpc = pc + 1; /* next opcode will be seen as a "new" line */
  1701. }
  1702. updatebase(ci); /* function has new base after adjustment */
  1703. vmbreak;
  1704. }
  1705. vmcase(OP_EXTRAARG) {
  1706. lua_assert(0);
  1707. vmbreak;
  1708. }
  1709. }
  1710. }
  1711. }
  1712. /* }================================================================== */