lvm.c 55 KB

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