lvm.c 52 KB

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