lvm.c 51 KB

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