lvm.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. /*
  2. ** $Id: lvm.c,v 2.338 2018/02/07 15:18:04 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 a 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_BANDK: case OP_BORK: case OP_BXORK:
  623. case OP_BAND: case OP_BOR: case OP_BXOR:
  624. case OP_SHRI: case OP_SHL: case OP_SHR:
  625. case OP_MOD: case OP_POW:
  626. case OP_UNM: case OP_BNOT: case OP_LEN:
  627. case OP_GETTABUP: case OP_GETTABLE: case OP_GETI:
  628. case OP_GETFIELD: case OP_SELF: {
  629. setobjs2s(L, base + GETARG_A(inst), --L->top);
  630. break;
  631. }
  632. case OP_LT: case OP_LE:
  633. case OP_LTI: case OP_LEI:
  634. case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */
  635. int res = !l_isfalse(s2v(L->top - 1));
  636. L->top--;
  637. if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */
  638. lua_assert(op == OP_LE ||
  639. (op == OP_LTI && GETARG_C(inst)) ||
  640. (op == OP_LEI && !GETARG_C(inst)));
  641. ci->callstatus ^= CIST_LEQ; /* clear mark */
  642. res = !res; /* negate result */
  643. }
  644. lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
  645. if (GETARG_C(inst)) res = !res;
  646. if (res != GETARG_k(inst)) /* condition failed? */
  647. ci->u.l.savedpc++; /* skip jump instruction */
  648. break;
  649. }
  650. case OP_CONCAT: {
  651. StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */
  652. int a = GETARG_A(inst); /* first element to concatenate */
  653. int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */
  654. setobjs2s(L, top - 2, top); /* put TM result in proper position */
  655. if (total > 1) { /* are there elements to concat? */
  656. L->top = top - 1; /* top is one after last element (at top-2) */
  657. luaV_concat(L, total); /* concat them (may yield again) */
  658. }
  659. break;
  660. }
  661. case OP_TFORCALL: case OP_CALL: case OP_TAILCALL:
  662. case OP_SETTABUP: case OP_SETTABLE:
  663. case OP_SETI: case OP_SETFIELD:
  664. break;
  665. default: lua_assert(0);
  666. }
  667. }
  668. /*
  669. ** {==================================================================
  670. ** Function 'luaV_execute': main interpreter loop
  671. ** ===================================================================
  672. */
  673. /*
  674. ** some macros for common tasks in 'luaV_execute'
  675. */
  676. #define RA(i) (base+GETARG_A(i))
  677. #define RB(i) (base+GETARG_B(i))
  678. #define vRB(i) s2v(RB(i))
  679. #define KB(i) (k+GETARG_B(i))
  680. #define RC(i) (base+GETARG_C(i))
  681. #define vRC(i) s2v(RC(i))
  682. #define KC(i) (k+GETARG_C(i))
  683. #define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i)))
  684. #define updatetrap(ci) (trap = ci->u.l.trap)
  685. #define updatebase(ci) (base = ci->func + 1)
  686. /*
  687. ** Execute a jump instruction. The 'updatetrap' allows signals to stop
  688. ** tight loops. (Without it, the local copy of 'trap' could never change.)
  689. */
  690. #define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); }
  691. /* for test instructions, execute the jump instruction that follows it */
  692. #define donextjump(ci) { i = *pc; dojump(ci, i, 1); }
  693. /*
  694. ** Correct global 'pc'.
  695. */
  696. #define savepc(L) (ci->u.l.savedpc = pc)
  697. /*
  698. ** Whenever code can raise errors, the global 'pc' and the global
  699. ** 'top' must be correct to report occasional errors.
  700. */
  701. #define savestate(L,ci) (savepc(L), L->top = ci->top)
  702. /*
  703. ** Protect code that, in general, can raise errors, reallocate the
  704. ** stack, and change the hooks.
  705. */
  706. #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci))
  707. /* special version that does not change the top */
  708. #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci))
  709. /*
  710. ** Protect code that will finish the loop (returns).
  711. */
  712. #define halfProtect(exp) (savepc(L), (exp))
  713. #define checkGC(L,c) \
  714. { luaC_condGC(L, L->top = (c), /* limit of live values */ \
  715. updatetrap(ci)); \
  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. vra = s2v(ra); \
  728. }
  729. #define vmdispatch(o) switch(o)
  730. #define vmcase(l) case l:
  731. #define vmbreak break
  732. void luaV_execute (lua_State *L, CallInfo *ci) {
  733. LClosure *cl;
  734. TValue *k;
  735. StkId base;
  736. const Instruction *pc;
  737. int trap;
  738. tailcall:
  739. trap = L->hookmask;
  740. cl = clLvalue(s2v(ci->func));
  741. k = cl->p->k;
  742. pc = ci->u.l.savedpc;
  743. if (trap) {
  744. if (cl->p->is_vararg)
  745. trap = 0; /* hooks will start with PREPVARARG instruction */
  746. else if (pc == cl->p->code) /* first instruction (not resuming)? */
  747. luaD_hookcall(L, ci);
  748. ci->u.l.trap = 1; /* there may be other hooks */
  749. }
  750. base = ci->func + 1;
  751. /* main loop of interpreter */
  752. for (;;) {
  753. int cond; /* flag for conditional jumps */
  754. Instruction i; /* instruction being executed */
  755. StkId ra; /* instruction's A register */
  756. TValue *vra; /* corresponding value */
  757. vmfetch();
  758. lua_assert(base == ci->func + 1);
  759. lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
  760. lua_assert(ci->top < L->stack + L->stacksize);
  761. vmdispatch (GET_OPCODE(i)) {
  762. vmcase(OP_MOVE) {
  763. setobjs2s(L, ra, RB(i));
  764. vmbreak;
  765. }
  766. vmcase(OP_LOADK) {
  767. TValue *rb = k + GETARG_Bx(i);
  768. setobj2s(L, ra, rb);
  769. vmbreak;
  770. }
  771. vmcase(OP_LOADI) {
  772. lua_Integer b = GETARG_sBx(i);
  773. setivalue(vra, b);
  774. vmbreak;
  775. }
  776. vmcase(OP_LOADF) {
  777. int b = GETARG_sBx(i);
  778. setfltvalue(vra, cast_num(b));
  779. vmbreak;
  780. }
  781. vmcase(OP_LOADKX) {
  782. TValue *rb;
  783. rb = k + GETARG_Ax(*pc); pc++;
  784. setobj2s(L, ra, rb);
  785. vmbreak;
  786. }
  787. vmcase(OP_LOADBOOL) {
  788. setbvalue(vra, GETARG_B(i));
  789. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  790. vmbreak;
  791. }
  792. vmcase(OP_LOADNIL) {
  793. int b = GETARG_B(i);
  794. do {
  795. setnilvalue(s2v(ra++));
  796. } while (b--);
  797. vmbreak;
  798. }
  799. vmcase(OP_GETUPVAL) {
  800. int b = GETARG_B(i);
  801. setobj2s(L, ra, cl->upvals[b]->v);
  802. vmbreak;
  803. }
  804. vmcase(OP_SETUPVAL) {
  805. UpVal *uv = cl->upvals[GETARG_B(i)];
  806. setobj(L, uv->v, vra);
  807. luaC_barrier(L, uv, vra);
  808. vmbreak;
  809. }
  810. vmcase(OP_GETTABUP) {
  811. const TValue *slot;
  812. TValue *upval = cl->upvals[GETARG_B(i)]->v;
  813. TValue *rc = KC(i);
  814. TString *key = tsvalue(rc); /* key must be a string */
  815. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  816. setobj2s(L, ra, slot);
  817. }
  818. else
  819. Protect(luaV_finishget(L, upval, rc, ra, slot));
  820. vmbreak;
  821. }
  822. vmcase(OP_GETTABLE) {
  823. const TValue *slot;
  824. TValue *rb = vRB(i);
  825. TValue *rc = vRC(i);
  826. lua_Unsigned n;
  827. if (ttisinteger(rc) /* fast track for integers? */
  828. ? (n = ivalue(rc), luaV_fastgeti(L, rb, n, slot))
  829. : luaV_fastget(L, rb, rc, slot, luaH_get)) {
  830. setobj2s(L, ra, slot);
  831. }
  832. else
  833. Protect(luaV_finishget(L, rb, rc, ra, slot));
  834. vmbreak;
  835. }
  836. vmcase(OP_GETI) {
  837. const TValue *slot;
  838. TValue *rb = vRB(i);
  839. int c = GETARG_C(i);
  840. if (luaV_fastgeti(L, rb, c, slot)) {
  841. setobj2s(L, ra, slot);
  842. }
  843. else {
  844. TValue key;
  845. setivalue(&key, c);
  846. Protect(luaV_finishget(L, rb, &key, ra, slot));
  847. }
  848. vmbreak;
  849. }
  850. vmcase(OP_GETFIELD) {
  851. const TValue *slot;
  852. TValue *rb = vRB(i);
  853. TValue *rc = KC(i);
  854. TString *key = tsvalue(rc); /* key must be a string */
  855. if (luaV_fastget(L, rb, key, slot, luaH_getshortstr)) {
  856. setobj2s(L, ra, slot);
  857. }
  858. else
  859. Protect(luaV_finishget(L, rb, rc, ra, slot));
  860. vmbreak;
  861. }
  862. vmcase(OP_SETTABUP) {
  863. const TValue *slot;
  864. TValue *upval = cl->upvals[GETARG_A(i)]->v;
  865. TValue *rb = KB(i);
  866. TValue *rc = RKC(i);
  867. TString *key = tsvalue(rb); /* key must be a string */
  868. if (luaV_fastget(L, upval, key, slot, luaH_getshortstr)) {
  869. luaV_finishfastset(L, upval, slot, rc);
  870. }
  871. else
  872. Protect(luaV_finishset(L, upval, rb, rc, slot));
  873. vmbreak;
  874. }
  875. vmcase(OP_SETTABLE) {
  876. const TValue *slot;
  877. TValue *rb = vRB(i); /* key (table is in 'ra') */
  878. TValue *rc = RKC(i); /* value */
  879. lua_Unsigned n;
  880. if (ttisinteger(rb) /* fast track for integers? */
  881. ? (n = ivalue(rb), luaV_fastgeti(L, vra, n, slot))
  882. : luaV_fastget(L, vra, rb, slot, luaH_get)) {
  883. luaV_finishfastset(L, vra, slot, rc);
  884. }
  885. else
  886. Protect(luaV_finishset(L, vra, rb, rc, slot));
  887. vmbreak;
  888. }
  889. vmcase(OP_SETI) {
  890. const TValue *slot;
  891. int c = GETARG_B(i);
  892. TValue *rc = RKC(i);
  893. if (luaV_fastgeti(L, vra, c, slot)) {
  894. luaV_finishfastset(L, vra, slot, rc);
  895. }
  896. else {
  897. TValue key;
  898. setivalue(&key, c);
  899. Protect(luaV_finishset(L, vra, &key, rc, slot));
  900. }
  901. vmbreak;
  902. }
  903. vmcase(OP_SETFIELD) {
  904. const TValue *slot;
  905. TValue *rb = KB(i);
  906. TValue *rc = RKC(i);
  907. TString *key = tsvalue(rb); /* key must be a string */
  908. if (luaV_fastget(L, vra, key, slot, luaH_getshortstr)) {
  909. luaV_finishfastset(L, vra, slot, rc);
  910. }
  911. else
  912. Protect(luaV_finishset(L, vra, rb, rc, slot));
  913. vmbreak;
  914. }
  915. vmcase(OP_NEWTABLE) {
  916. int b = GETARG_B(i);
  917. int c = GETARG_C(i);
  918. Table *t;
  919. L->top = ci->top; /* correct top in case of GC */
  920. t = luaH_new(L); /* memory allocation */
  921. sethvalue2s(L, ra, t);
  922. if (b != 0 || c != 0)
  923. luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); /* idem */
  924. checkGC(L, ra + 1);
  925. vmbreak;
  926. }
  927. vmcase(OP_SELF) {
  928. const TValue *slot;
  929. TValue *rb = vRB(i);
  930. TValue *rc = RKC(i);
  931. TString *key = tsvalue(rc); /* key must be a string */
  932. setobj2s(L, ra + 1, rb);
  933. if (luaV_fastget(L, rb, key, slot, luaH_getstr)) {
  934. setobj2s(L, ra, slot);
  935. }
  936. else
  937. Protect(luaV_finishget(L, rb, rc, ra, slot));
  938. vmbreak;
  939. }
  940. vmcase(OP_ADDI) {
  941. TValue *rb = vRB(i);
  942. int ic = GETARG_sC(i);
  943. lua_Number nb;
  944. if (ttisinteger(rb)) {
  945. setivalue(vra, intop(+, ivalue(rb), ic));
  946. }
  947. else if (tonumberns(rb, nb)) {
  948. setfltvalue(vra, luai_numadd(L, nb, cast_num(ic)));
  949. }
  950. else
  951. Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_ADD));
  952. vmbreak;
  953. }
  954. vmcase(OP_SUBI) {
  955. TValue *rb = vRB(i);
  956. int ic = GETARG_sC(i);
  957. lua_Number nb;
  958. if (ttisinteger(rb)) {
  959. setivalue(vra, intop(-, ivalue(rb), ic));
  960. }
  961. else if (tonumberns(rb, nb)) {
  962. setfltvalue(vra, luai_numsub(L, nb, cast_num(ic)));
  963. }
  964. else
  965. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_SUB));
  966. vmbreak;
  967. }
  968. vmcase(OP_MULI) {
  969. TValue *rb = vRB(i);
  970. int ic = GETARG_sC(i);
  971. lua_Number nb;
  972. if (ttisinteger(rb)) {
  973. setivalue(vra, intop(*, ivalue(rb), ic));
  974. }
  975. else if (tonumberns(rb, nb)) {
  976. setfltvalue(vra, luai_nummul(L, nb, cast_num(ic)));
  977. }
  978. else
  979. Protect(luaT_trybiniTM(L, rb, ic, GETARG_k(i), ra, TM_MUL));
  980. vmbreak;
  981. }
  982. vmcase(OP_MODI) {
  983. TValue *rb = vRB(i);
  984. int ic = GETARG_sC(i);
  985. lua_Number nb;
  986. if (ttisinteger(rb)) {
  987. setivalue(vra, luaV_mod(L, ivalue(rb), ic));
  988. }
  989. else if (tonumberns(rb, nb)) {
  990. lua_Number m;
  991. lua_Number nc = cast_num(ic);
  992. luai_nummod(L, nb, nc, m);
  993. setfltvalue(vra, m);
  994. }
  995. else
  996. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_MOD));
  997. vmbreak;
  998. }
  999. vmcase(OP_POWI) {
  1000. TValue *rb = vRB(i);
  1001. int ic = GETARG_sC(i);
  1002. lua_Number nb;
  1003. if (tonumberns(rb, nb)) {
  1004. lua_Number nc = cast_num(ic);
  1005. setfltvalue(vra, luai_numpow(L, nb, nc));
  1006. }
  1007. else
  1008. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_POW));
  1009. vmbreak;
  1010. }
  1011. vmcase(OP_DIVI) {
  1012. TValue *rb = vRB(i);
  1013. int ic = GETARG_sC(i);
  1014. lua_Number nb;
  1015. if (tonumberns(rb, nb)) {
  1016. lua_Number nc = cast_num(ic);
  1017. setfltvalue(vra, luai_numdiv(L, nb, nc));
  1018. }
  1019. else
  1020. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_DIV));
  1021. vmbreak;
  1022. }
  1023. vmcase(OP_IDIVI) {
  1024. TValue *rb = vRB(i);
  1025. int ic = GETARG_sC(i);
  1026. lua_Number nb;
  1027. if (ttisinteger(rb)) {
  1028. setivalue(vra, luaV_div(L, ivalue(rb), ic));
  1029. }
  1030. else if (tonumberns(rb, nb)) {
  1031. lua_Number nc = cast_num(ic);
  1032. setfltvalue(vra, luai_numdiv(L, nb, nc));
  1033. }
  1034. else
  1035. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, TM_IDIV));
  1036. vmbreak;
  1037. }
  1038. vmcase(OP_ADD) {
  1039. TValue *rb = vRB(i);
  1040. TValue *rc = vRC(i);
  1041. lua_Number nb; lua_Number nc;
  1042. if (ttisinteger(rb) && ttisinteger(rc)) {
  1043. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1044. setivalue(vra, intop(+, ib, ic));
  1045. }
  1046. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1047. setfltvalue(vra, luai_numadd(L, nb, nc));
  1048. }
  1049. else
  1050. Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD));
  1051. vmbreak;
  1052. }
  1053. vmcase(OP_SUB) {
  1054. TValue *rb = vRB(i);
  1055. TValue *rc = vRC(i);
  1056. lua_Number nb; lua_Number nc;
  1057. if (ttisinteger(rb) && ttisinteger(rc)) {
  1058. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1059. setivalue(vra, intop(-, ib, ic));
  1060. }
  1061. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1062. setfltvalue(vra, luai_numsub(L, nb, nc));
  1063. }
  1064. else
  1065. Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB));
  1066. vmbreak;
  1067. }
  1068. vmcase(OP_MUL) {
  1069. TValue *rb = vRB(i);
  1070. TValue *rc = vRC(i);
  1071. lua_Number nb; lua_Number nc;
  1072. if (ttisinteger(rb) && ttisinteger(rc)) {
  1073. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1074. setivalue(vra, intop(*, ib, ic));
  1075. }
  1076. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1077. setfltvalue(vra, luai_nummul(L, nb, nc));
  1078. }
  1079. else
  1080. Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL));
  1081. vmbreak;
  1082. }
  1083. vmcase(OP_DIV) { /* float division (always with floats) */
  1084. TValue *rb = vRB(i);
  1085. TValue *rc = vRC(i);
  1086. lua_Number nb; lua_Number nc;
  1087. if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1088. setfltvalue(vra, luai_numdiv(L, nb, nc));
  1089. }
  1090. else
  1091. Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV));
  1092. vmbreak;
  1093. }
  1094. vmcase(OP_BANDK) {
  1095. TValue *p1 = vRB(i);
  1096. TValue *p2 = KC(i);
  1097. lua_Integer i1;
  1098. if (tointegerns(p1, &i1)) {
  1099. setivalue(vra, intop(&, i1, ivalue(p2)));
  1100. }
  1101. else
  1102. Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BAND));
  1103. vmbreak;
  1104. }
  1105. vmcase(OP_BORK) {
  1106. TValue *p1 = vRB(i);
  1107. TValue *p2 = KC(i);
  1108. lua_Integer i1;
  1109. if (tointegerns(p1, &i1)) {
  1110. setivalue(vra, intop(|, i1, ivalue(p2)));
  1111. }
  1112. else
  1113. Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BOR));
  1114. vmbreak;
  1115. }
  1116. vmcase(OP_BXORK) {
  1117. TValue *p1 = vRB(i);
  1118. TValue *p2 = KC(i);
  1119. lua_Integer i1;
  1120. if (tointegerns(p1, &i1)) {
  1121. setivalue(vra, intop(^, i1, ivalue(p2)));
  1122. }
  1123. else
  1124. Protect(luaT_trybinassocTM(L, p1, p2, ra, TESTARG_k(i), TM_BXOR));
  1125. vmbreak;
  1126. }
  1127. vmcase(OP_BAND) {
  1128. TValue *rb = vRB(i);
  1129. TValue *rc = vRC(i);
  1130. lua_Integer ib; lua_Integer ic;
  1131. if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
  1132. setivalue(vra, intop(&, ib, ic));
  1133. }
  1134. else
  1135. Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND));
  1136. vmbreak;
  1137. }
  1138. vmcase(OP_BOR) {
  1139. TValue *rb = vRB(i);
  1140. TValue *rc = vRC(i);
  1141. lua_Integer ib; lua_Integer ic;
  1142. if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
  1143. setivalue(vra, intop(|, ib, ic));
  1144. }
  1145. else
  1146. Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR));
  1147. vmbreak;
  1148. }
  1149. vmcase(OP_BXOR) {
  1150. TValue *rb = vRB(i);
  1151. TValue *rc = vRC(i);
  1152. lua_Integer ib; lua_Integer ic;
  1153. if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
  1154. setivalue(vra, intop(^, ib, ic));
  1155. }
  1156. else
  1157. Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR));
  1158. vmbreak;
  1159. }
  1160. vmcase(OP_SHRI) {
  1161. TValue *rb = vRB(i);
  1162. int ic = GETARG_sC(i);
  1163. lua_Integer ib;
  1164. if (tointegerns(rb, &ib)) {
  1165. setivalue(vra, luaV_shiftl(ib, -ic));
  1166. }
  1167. else {
  1168. TMS ev = TM_SHR;
  1169. if (TESTARG_k(i)) {
  1170. ic = -ic; ev = TM_SHL;
  1171. }
  1172. Protect(luaT_trybiniTM(L, rb, ic, 0, ra, ev));
  1173. }
  1174. vmbreak;
  1175. }
  1176. vmcase(OP_SHLI) {
  1177. TValue *rb = vRB(i);
  1178. int ic = GETARG_sC(i);
  1179. lua_Integer ib;
  1180. if (tointegerns(rb, &ib)) {
  1181. setivalue(vra, luaV_shiftl(ic, ib));
  1182. }
  1183. else
  1184. Protect(luaT_trybiniTM(L, rb, ic, 1, ra, TM_SHL));
  1185. vmbreak;
  1186. }
  1187. vmcase(OP_SHL) {
  1188. TValue *rb = vRB(i);
  1189. TValue *rc = vRC(i);
  1190. lua_Integer ib; lua_Integer ic;
  1191. if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
  1192. setivalue(vra, luaV_shiftl(ib, ic));
  1193. }
  1194. else
  1195. Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL));
  1196. vmbreak;
  1197. }
  1198. vmcase(OP_SHR) {
  1199. TValue *rb = vRB(i);
  1200. TValue *rc = vRC(i);
  1201. lua_Integer ib; lua_Integer ic;
  1202. if (tointegerns(rb, &ib) && tointegerns(rc, &ic)) {
  1203. setivalue(vra, luaV_shiftl(ib, -ic));
  1204. }
  1205. else
  1206. Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR));
  1207. vmbreak;
  1208. }
  1209. vmcase(OP_MOD) {
  1210. TValue *rb = vRB(i);
  1211. TValue *rc = vRC(i);
  1212. lua_Number nb; lua_Number nc;
  1213. if (ttisinteger(rb) && ttisinteger(rc)) {
  1214. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1215. setivalue(vra, luaV_mod(L, ib, ic));
  1216. }
  1217. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1218. lua_Number m;
  1219. luai_nummod(L, nb, nc, m);
  1220. setfltvalue(vra, m);
  1221. }
  1222. else
  1223. Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD));
  1224. vmbreak;
  1225. }
  1226. vmcase(OP_IDIV) { /* floor division */
  1227. TValue *rb = vRB(i);
  1228. TValue *rc = vRC(i);
  1229. lua_Number nb; lua_Number nc;
  1230. if (ttisinteger(rb) && ttisinteger(rc)) {
  1231. lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
  1232. setivalue(vra, luaV_div(L, ib, ic));
  1233. }
  1234. else if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1235. setfltvalue(vra, luai_numidiv(L, nb, nc));
  1236. }
  1237. else
  1238. Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV));
  1239. vmbreak;
  1240. }
  1241. vmcase(OP_POW) {
  1242. TValue *rb = vRB(i);
  1243. TValue *rc = vRC(i);
  1244. lua_Number nb; lua_Number nc;
  1245. if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
  1246. setfltvalue(vra, luai_numpow(L, nb, nc));
  1247. }
  1248. else
  1249. Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW));
  1250. vmbreak;
  1251. }
  1252. vmcase(OP_UNM) {
  1253. TValue *rb = vRB(i);
  1254. lua_Number nb;
  1255. if (ttisinteger(rb)) {
  1256. lua_Integer ib = ivalue(rb);
  1257. setivalue(vra, intop(-, 0, ib));
  1258. }
  1259. else if (tonumberns(rb, nb)) {
  1260. setfltvalue(vra, luai_numunm(L, nb));
  1261. }
  1262. else
  1263. Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
  1264. vmbreak;
  1265. }
  1266. vmcase(OP_BNOT) {
  1267. TValue *rb = vRB(i);
  1268. lua_Integer ib;
  1269. if (tointegerns(rb, &ib)) {
  1270. setivalue(vra, intop(^, ~l_castS2U(0), ib));
  1271. }
  1272. else
  1273. Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
  1274. vmbreak;
  1275. }
  1276. vmcase(OP_NOT) {
  1277. TValue *rb = vRB(i);
  1278. int nrb = l_isfalse(rb); /* next assignment may change this value */
  1279. setbvalue(vra, nrb);
  1280. vmbreak;
  1281. }
  1282. vmcase(OP_LEN) {
  1283. Protect(luaV_objlen(L, ra, vRB(i)));
  1284. vmbreak;
  1285. }
  1286. vmcase(OP_CONCAT) {
  1287. int n = GETARG_B(i); /* number of elements to concatenate */
  1288. L->top = ra + n; /* mark the end of concat operands */
  1289. ProtectNT(luaV_concat(L, n));
  1290. checkGC(L, L->top); /* 'luaV_concat' ensures correct top */
  1291. vmbreak;
  1292. }
  1293. vmcase(OP_CLOSE) {
  1294. luaF_close(L, ra);
  1295. vmbreak;
  1296. }
  1297. vmcase(OP_JMP) {
  1298. dojump(ci, i, 0);
  1299. vmbreak;
  1300. }
  1301. vmcase(OP_EQ) {
  1302. TValue *rb = vRB(i);
  1303. Protect(cond = luaV_equalobj(L, vra, rb));
  1304. goto condjump;
  1305. }
  1306. vmcase(OP_LT) {
  1307. TValue *rb = vRB(i);
  1308. if (ttisinteger(vra) && ttisinteger(rb))
  1309. cond = (ivalue(vra) < ivalue(rb));
  1310. else if (ttisnumber(vra) && ttisnumber(rb))
  1311. cond = LTnum(vra, rb);
  1312. else
  1313. Protect(cond = lessthanothers(L, vra, rb));
  1314. goto condjump;
  1315. }
  1316. vmcase(OP_LE) {
  1317. TValue *rb = vRB(i);
  1318. if (ttisinteger(vra) && ttisinteger(rb))
  1319. cond = (ivalue(vra) <= ivalue(rb));
  1320. else if (ttisnumber(vra) && ttisnumber(rb))
  1321. cond = LEnum(vra, rb);
  1322. else
  1323. Protect(cond = lessequalothers(L, vra, rb));
  1324. goto condjump;
  1325. }
  1326. vmcase(OP_EQK) {
  1327. TValue *rb = KB(i);
  1328. /* basic types do not use '__eq'; we can use raw equality */
  1329. cond = luaV_equalobj(NULL, vra, rb);
  1330. goto condjump;
  1331. }
  1332. vmcase(OP_EQI) {
  1333. int im = GETARG_sB(i);
  1334. if (ttisinteger(vra))
  1335. cond = (ivalue(vra) == im);
  1336. else if (ttisfloat(vra))
  1337. cond = luai_numeq(fltvalue(vra), cast_num(im));
  1338. else
  1339. cond = 0; /* other types cannot be equal to a number */
  1340. goto condjump;
  1341. }
  1342. vmcase(OP_LTI) {
  1343. int im = GETARG_sB(i);
  1344. if (ttisinteger(vra))
  1345. cond = (ivalue(vra) < im);
  1346. else if (ttisfloat(vra)) {
  1347. lua_Number f = fltvalue(vra);
  1348. cond = (!luai_numisnan(f)) ? luai_numlt(f, cast_num(im))
  1349. : GETARG_C(i); /* NaN */
  1350. }
  1351. else
  1352. Protect(cond = luaT_callorderiTM(L, vra, im, GETARG_C(i), TM_LT));
  1353. goto condjump;
  1354. }
  1355. vmcase(OP_LEI) {
  1356. int im = GETARG_sB(i);
  1357. if (ttisinteger(vra))
  1358. cond = (ivalue(vra) <= im);
  1359. else if (ttisfloat(vra)) {
  1360. lua_Number f = fltvalue(vra);
  1361. cond = (!luai_numisnan(f)) ? luai_numle(f, cast_num(im))
  1362. : GETARG_C(i); /* NaN? */
  1363. }
  1364. else
  1365. Protect(cond = luaT_callorderiTM(L, vra, im, GETARG_C(i), TM_LE));
  1366. goto condjump;
  1367. }
  1368. vmcase(OP_TEST) {
  1369. cond = !l_isfalse(vra);
  1370. condjump:
  1371. if (cond != GETARG_k(i))
  1372. pc++; /* skip next jump */
  1373. else
  1374. donextjump(ci);
  1375. vmbreak;
  1376. }
  1377. vmcase(OP_TESTSET) {
  1378. TValue *rb = vRB(i);
  1379. if (l_isfalse(rb) == GETARG_k(i))
  1380. pc++;
  1381. else {
  1382. setobj2s(L, ra, rb);
  1383. donextjump(ci);
  1384. }
  1385. vmbreak;
  1386. }
  1387. vmcase(OP_CALL) {
  1388. int b = GETARG_B(i);
  1389. int nresults = GETARG_C(i) - 1;
  1390. if (b != 0) /* fixed number of arguments? */
  1391. L->top = ra + b; /* top signals number of arguments */
  1392. /* else previous instruction set top */
  1393. ProtectNT(luaD_call(L, ra, nresults));
  1394. vmbreak;
  1395. }
  1396. vmcase(OP_TAILCALL) {
  1397. int b = GETARG_B(i); /* number of arguments + 1 (function) */
  1398. if (b != 0)
  1399. L->top = ra + b;
  1400. else /* previous instruction set top */
  1401. b = cast_int(L->top - ra);
  1402. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  1403. if (!ttisfunction(vra)) { /* not a function? */
  1404. ProtectNT(luaD_tryfuncTM(L, ra)); /* try '__call' metamethod */
  1405. b++; /* there is now one extra argument */
  1406. }
  1407. if (TESTARG_k(i))
  1408. luaF_close(L, base); /* close upvalues from current call */
  1409. if (!ttisLclosure(vra)) { /* C function? */
  1410. ProtectNT(luaD_call(L, ra, LUA_MULTRET)); /* call it */
  1411. }
  1412. else { /* Lua tail call */
  1413. if (cl->p->is_vararg)
  1414. ci->func -= cl->p->numparams + ci->u.l.nextraargs + 1;
  1415. luaD_pretailcall(L, ci, ra, b); /* prepare call frame */
  1416. goto tailcall;
  1417. }
  1418. vmbreak;
  1419. }
  1420. vmcase(OP_RETURN) {
  1421. int b = GETARG_B(i);
  1422. int n = (b != 0 ? b - 1 : cast_int(L->top - ra));
  1423. if (TESTARG_k(i))
  1424. luaF_close(L, base);
  1425. halfProtect(luaD_poscall(L, ci, ra, n));
  1426. return;
  1427. }
  1428. vmcase(OP_RETVARARG) {
  1429. int b = GETARG_B(i);
  1430. int nparams = GETARG_C(i);
  1431. int nres = (b != 0 ? b - 1 : cast_int(L->top - ra));
  1432. int delta = ci->u.l.nextraargs + nparams + 2;
  1433. if (TESTARG_k(i))
  1434. luaF_close(L, base);
  1435. savepc(L);
  1436. /* code similar to 'luaD_poscall', but with a delta */
  1437. if (L->hookmask) {
  1438. luaD_rethook(L, ci);
  1439. if (ci->u.l.trap) {
  1440. updatebase(ci);
  1441. ra = RA(i);
  1442. }
  1443. }
  1444. L->ci = ci->previous; /* back to caller */
  1445. luaD_moveresults(L, ra, base - delta, nres, ci->nresults);
  1446. return;
  1447. }
  1448. vmcase(OP_RETURN0) {
  1449. if (TESTARG_k(i))
  1450. luaF_close(L, base);
  1451. if (L->hookmask)
  1452. halfProtect(luaD_poscall(L, ci, ra, 0)); /* no hurry... */
  1453. else {
  1454. int nres = ci->nresults;
  1455. L->ci = ci->previous; /* back to caller */
  1456. L->top = base - 1;
  1457. while (nres-- > 0)
  1458. setnilvalue(s2v(L->top++)); /* all results are nil */
  1459. }
  1460. return;
  1461. }
  1462. vmcase(OP_RETURN1) {
  1463. if (TESTARG_k(i))
  1464. luaF_close(L, base);
  1465. if (L->hookmask)
  1466. halfProtect(luaD_poscall(L, ci, ra, 1)); /* no hurry... */
  1467. else {
  1468. int nres = ci->nresults;
  1469. L->ci = ci->previous; /* back to caller */
  1470. if (nres == 0)
  1471. L->top = base - 1; /* asked for no results */
  1472. else {
  1473. setobjs2s(L, base - 1, ra); /* at least this result */
  1474. L->top = base;
  1475. while (--nres > 0) /* complete missing results */
  1476. setnilvalue(s2v(L->top++));
  1477. }
  1478. }
  1479. return;
  1480. }
  1481. vmcase(OP_FORLOOP1) {
  1482. lua_Integer idx = intop(+, ivalue(vra), 1); /* increment index */
  1483. lua_Integer limit = ivalue(s2v(ra + 1));
  1484. if (idx <= limit) {
  1485. pc -= GETARG_Bx(i); /* jump back */
  1486. chgivalue(vra, idx); /* update internal index... */
  1487. setivalue(s2v(ra + 3), idx); /* ...and external index */
  1488. }
  1489. updatetrap(ci);
  1490. vmbreak;
  1491. }
  1492. vmcase(OP_FORPREP1) {
  1493. TValue *init = vra;
  1494. TValue *plimit = s2v(ra + 1);
  1495. lua_Integer ilimit, initv;
  1496. int stopnow;
  1497. if (!forlimit(plimit, &ilimit, 1, &stopnow)) {
  1498. savestate(L, ci); /* for the error message */
  1499. luaG_runerror(L, "'for' limit must be a number");
  1500. }
  1501. initv = (stopnow ? 0 : ivalue(init));
  1502. setivalue(plimit, ilimit);
  1503. setivalue(init, intop(-, initv, 1));
  1504. pc += GETARG_Bx(i);
  1505. vmbreak;
  1506. }
  1507. vmcase(OP_FORLOOP) {
  1508. if (ttisinteger(vra)) { /* integer loop? */
  1509. lua_Integer step = ivalue(s2v(ra + 2));
  1510. lua_Integer idx = intop(+, ivalue(vra), step); /* increment index */
  1511. lua_Integer limit = ivalue(s2v(ra + 1));
  1512. if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
  1513. pc -= GETARG_Bx(i); /* jump back */
  1514. chgivalue(vra, idx); /* update internal index... */
  1515. setivalue(s2v(ra + 3), idx); /* ...and external index */
  1516. }
  1517. }
  1518. else { /* floating loop */
  1519. lua_Number step = fltvalue(s2v(ra + 2));
  1520. lua_Number limit = fltvalue(s2v(ra + 1));
  1521. lua_Number idx = fltvalue(vra);
  1522. idx = luai_numadd(L, idx, step); /* inc. index */
  1523. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  1524. : luai_numle(limit, idx)) {
  1525. pc -= GETARG_Bx(i); /* jump back */
  1526. chgfltvalue(vra, idx); /* update internal index... */
  1527. setfltvalue(s2v(ra + 3), idx); /* ...and external index */
  1528. }
  1529. }
  1530. updatetrap(ci);
  1531. vmbreak;
  1532. }
  1533. vmcase(OP_FORPREP) {
  1534. TValue *init = vra;
  1535. TValue *plimit = s2v(ra + 1);
  1536. TValue *pstep = s2v(ra + 2);
  1537. lua_Integer ilimit;
  1538. int stopnow;
  1539. if (ttisinteger(init) && ttisinteger(pstep) &&
  1540. forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
  1541. /* all values are integer */
  1542. lua_Integer initv = (stopnow ? 0 : ivalue(init));
  1543. setivalue(plimit, ilimit);
  1544. setivalue(init, intop(-, initv, ivalue(pstep)));
  1545. }
  1546. else { /* try making all values floats */
  1547. lua_Number ninit; lua_Number nlimit; lua_Number nstep;
  1548. savestate(L, ci); /* in case of errors */
  1549. if (!tonumber(plimit, &nlimit))
  1550. luaG_runerror(L, "'for' limit must be a number");
  1551. setfltvalue(plimit, nlimit);
  1552. if (!tonumber(pstep, &nstep))
  1553. luaG_runerror(L, "'for' step must be a number");
  1554. setfltvalue(pstep, nstep);
  1555. if (!tonumber(init, &ninit))
  1556. luaG_runerror(L, "'for' initial value must be a number");
  1557. setfltvalue(init, luai_numsub(L, ninit, nstep));
  1558. }
  1559. pc += GETARG_Bx(i);
  1560. vmbreak;
  1561. }
  1562. vmcase(OP_TFORCALL) {
  1563. StkId cb = ra + 3; /* call base */
  1564. setobjs2s(L, cb+2, ra+2);
  1565. setobjs2s(L, cb+1, ra+1);
  1566. setobjs2s(L, cb, ra);
  1567. L->top = cb + 3; /* func. + 2 args (state and index) */
  1568. Protect(luaD_call(L, cb, GETARG_C(i)));
  1569. if (trap) /* keep 'base' correct for next instruction */
  1570. updatebase(ci);
  1571. i = *(pc++); /* go to next instruction */
  1572. ra = RA(i); /* get its 'ra' */
  1573. lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
  1574. goto l_tforloop;
  1575. }
  1576. vmcase(OP_TFORLOOP) {
  1577. l_tforloop:
  1578. if (!ttisnil(s2v(ra + 1))) { /* continue loop? */
  1579. setobjs2s(L, ra, ra + 1); /* save control variable */
  1580. pc -= GETARG_Bx(i); /* jump back */
  1581. }
  1582. vmbreak;
  1583. }
  1584. vmcase(OP_SETLIST) {
  1585. int n = GETARG_B(i);
  1586. int c = GETARG_C(i);
  1587. unsigned int last;
  1588. Table *h;
  1589. if (n == 0)
  1590. n = cast_int(L->top - ra) - 1;
  1591. else
  1592. L->top = ci->top; /* correct top in case of GC */
  1593. if (c == 0) {
  1594. c = GETARG_Ax(*pc); pc++;
  1595. }
  1596. h = hvalue(vra);
  1597. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  1598. if (last > h->sizearray) /* needs more space? */
  1599. luaH_resizearray(L, h, last); /* preallocate it at once */
  1600. for (; n > 0; n--) {
  1601. TValue *val = s2v(ra + n);
  1602. setobj2t(L, &h->array[last - 1], val);
  1603. last--;
  1604. luaC_barrierback(L, h, val);
  1605. }
  1606. vmbreak;
  1607. }
  1608. vmcase(OP_CLOSURE) {
  1609. Proto *p = cl->p->p[GETARG_Bx(i)];
  1610. LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */
  1611. if (ncl == NULL) { /* no match? */
  1612. savestate(L, ci); /* in case of allocation errors */
  1613. pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
  1614. }
  1615. else
  1616. setclLvalue2s(L, ra, ncl); /* push cashed closure */
  1617. checkGC(L, ra + 1);
  1618. vmbreak;
  1619. }
  1620. vmcase(OP_VARARG) {
  1621. int n = GETARG_C(i) - 1; /* required results */
  1622. ProtectNT(luaT_getvarargs(L, ci, ra, n));
  1623. vmbreak;
  1624. }
  1625. vmcase(OP_PREPVARARG) {
  1626. luaT_adjustvarargs(L, GETARG_A(i), ci);
  1627. updatetrap(ci);
  1628. if (trap) {
  1629. luaD_hookcall(L, ci);
  1630. L->oldpc = pc + 1; /* next opcode will be seen as a new line */
  1631. }
  1632. updatebase(ci);
  1633. vmbreak;
  1634. }
  1635. vmcase(OP_EXTRAARG) {
  1636. lua_assert(0);
  1637. vmbreak;
  1638. }
  1639. }
  1640. }
  1641. }
  1642. /* }================================================================== */