lvm.c 53 KB

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