lvm.c 56 KB

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