lvm.c 54 KB

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