lvm.c 57 KB

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