lvm.c 52 KB

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