lvm.c 42 KB

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