lvm.c 56 KB

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