lvm.c 48 KB

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