lvm.c 55 KB

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