lvm.c 55 KB

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