lvm.c 56 KB

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