lvm.c 55 KB

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