lvm.c 43 KB

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