lvm.c 56 KB

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