lvm.c 42 KB

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