lvm.c 56 KB

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