lvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. ** $Id: lvm.c,v 1.105 2000/05/08 19:32:53 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define LUA_REENTRANT
  10. #include "lapi.h"
  11. #include "lauxlib.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lfunc.h"
  15. #include "lgc.h"
  16. #include "lobject.h"
  17. #include "lopcodes.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "ltm.h"
  22. #include "lvm.h"
  23. #ifdef OLD_ANSI
  24. #define strcoll(a,b) strcmp(a,b)
  25. #endif
  26. /*
  27. ** Extra stack size to run a function:
  28. ** TAG_LINE(1), NAME(1), TM calls(3) (plus some extra...)
  29. */
  30. #define EXTRA_STACK 8
  31. int luaV_tonumber (TObject *obj) { /* LUA_NUMBER */
  32. if (ttype(obj) != TAG_STRING)
  33. return 1;
  34. else {
  35. if (!luaO_str2d(svalue(obj), &nvalue(obj)))
  36. return 2;
  37. ttype(obj) = TAG_NUMBER;
  38. return 0;
  39. }
  40. }
  41. int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
  42. if (ttype(obj) != TAG_NUMBER)
  43. return 1;
  44. else {
  45. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  46. sprintf(s, "%.16g", (double)nvalue(obj));
  47. tsvalue(obj) = luaS_new(L, s);
  48. ttype(obj) = TAG_STRING;
  49. return 0;
  50. }
  51. }
  52. void luaV_setn (lua_State *L, Hash *t, int val) {
  53. TObject index, value;
  54. ttype(&index) = TAG_STRING; tsvalue(&index) = luaS_new(L, "n");
  55. ttype(&value) = TAG_NUMBER; nvalue(&value) = val;
  56. luaH_set(L, t, &index, &value);
  57. }
  58. static Closure *luaV_closure (lua_State *L, lua_Type t, int nelems) {
  59. Closure *c = luaF_newclosure(L, nelems);
  60. L->top -= nelems;
  61. while (nelems--)
  62. c->consts[nelems] = *(L->top+nelems);
  63. ttype(L->top) = t;
  64. clvalue(L->top) = c;
  65. incr_top;
  66. return c;
  67. }
  68. void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) {
  69. Closure *cl = luaV_closure(L, TAG_CCLOSURE, nelems);
  70. cl->f.c = c;
  71. }
  72. void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
  73. Closure *cl = luaV_closure(L, TAG_LCLOSURE, nelems);
  74. cl->f.l = l;
  75. }
  76. /*
  77. ** Function to index a table.
  78. ** Receives the table at top-2 and the index at top-1.
  79. */
  80. void luaV_gettable (lua_State *L, StkId top) {
  81. TObject *table = top-2;
  82. const TObject *im;
  83. if (ttype(table) != TAG_TABLE) { /* not a table, get gettable TM */
  84. im = luaT_getimbyObj(L, table, IM_GETTABLE);
  85. if (ttype(im) == TAG_NIL) {
  86. L->top = top;
  87. luaG_indexerror(L, table);
  88. }
  89. }
  90. else { /* object is a table... */
  91. int tg = table->value.a->htag;
  92. im = luaT_getim(L, tg, IM_GETTABLE);
  93. if (ttype(im) == TAG_NIL) { /* and does not have a `gettable' TM */
  94. const TObject *h = luaH_get(L, avalue(table), table+1);
  95. if (ttype(h) == TAG_NIL &&
  96. (ttype(im=luaT_getim(L, tg, IM_INDEX)) != TAG_NIL)) {
  97. /* result is nil and there is an `index' tag method */
  98. L->top = top;
  99. luaD_callTM(L, im, 2, 1); /* calls it */
  100. }
  101. else
  102. *table = *h; /* `push' result into table position */
  103. return;
  104. }
  105. /* else it has a `gettable' TM, go through to next command */
  106. }
  107. /* object is not a table, or it has a `gettable' TM */
  108. L->top = top;
  109. luaD_callTM(L, im, 2, 1);
  110. }
  111. /*
  112. ** Receives table at *t, index at *(t+1) and value at `top'.
  113. */
  114. void luaV_settable (lua_State *L, StkId t, StkId top) {
  115. const TObject *im;
  116. if (ttype(t) != TAG_TABLE) { /* not a table, get `settable' method */
  117. L->top = top;
  118. im = luaT_getimbyObj(L, t, IM_SETTABLE);
  119. if (ttype(im) == TAG_NIL)
  120. luaG_indexerror(L, t);
  121. }
  122. else { /* object is a table... */
  123. im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE);
  124. if (ttype(im) == TAG_NIL) { /* and does not have a `settable' method */
  125. luaH_set(L, avalue(t), t+1, top-1);
  126. return;
  127. }
  128. /* else it has a `settable' method, go through to next command */
  129. }
  130. /* object is not a table, or it has a `settable' method */
  131. /* prepare arguments and call the tag method */
  132. luaD_checkstack(L, 3);
  133. *(top+2) = *(top-1);
  134. *(top+1) = *(t+1);
  135. *(top) = *t;
  136. *(top-1) = *im;
  137. L->top = top+3;
  138. luaD_call(L, top-1, 0);
  139. }
  140. void luaV_getglobal (lua_State *L, TString *s, StkId top) {
  141. const TObject *value = luaH_getstr(L->gt, s);
  142. TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
  143. if (ttype(im) == TAG_NIL) /* is there a tag method? */
  144. *top = *value; /* default behavior */
  145. else { /* tag method */
  146. luaD_checkstack(L, 3);
  147. *top = *im;
  148. ttype(top+1) = TAG_STRING;
  149. tsvalue(top+1) = s; /* global name */
  150. *(top+2) = *value;
  151. L->top = top+3;
  152. luaD_call(L, top, 1);
  153. }
  154. }
  155. void luaV_setglobal (lua_State *L, TString *s, StkId top) {
  156. const TObject *oldvalue = luaH_getstr(L->gt, s);
  157. const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
  158. if (ttype(im) == TAG_NIL) { /* is there a tag method? */
  159. if (oldvalue != &luaO_nilobject)
  160. *oldvalue = *(top-1);
  161. else {
  162. TObject key;
  163. ttype(&key) = TAG_STRING;
  164. tsvalue(&key) = s;
  165. luaH_set(L, L->gt, &key, top-1);
  166. }
  167. }
  168. else {
  169. luaD_checkstack(L, 3);
  170. *(top+2) = *(top-1); /* new value */
  171. *(top+1) = *oldvalue;
  172. ttype(top) = TAG_STRING;
  173. tsvalue(top) = s;
  174. *(top-1) = *im;
  175. L->top = top+3;
  176. luaD_call(L, top-1, 0);
  177. }
  178. }
  179. static void call_binTM (lua_State *L, StkId top, IMS event, const char *msg) {
  180. /* try first operand */
  181. const TObject *im = luaT_getimbyObj(L, top-2, event);
  182. L->top = top;
  183. if (ttype(im) == TAG_NIL) {
  184. im = luaT_getimbyObj(L, top-1, event); /* try second operand */
  185. if (ttype(im) == TAG_NIL) {
  186. im = luaT_getim(L, 0, event); /* try a `global' method */
  187. if (ttype(im) == TAG_NIL)
  188. lua_error(L, msg);
  189. }
  190. }
  191. lua_pushstring(L, luaT_eventname[event]);
  192. luaD_callTM(L, im, 3, 1);
  193. }
  194. static void call_arith (lua_State *L, StkId top, IMS event) {
  195. call_binTM(L, top, event, "unexpected type in arithmetic operation");
  196. }
  197. static void addK (lua_State *L, StkId top, int k) {
  198. ttype(top) = TAG_NUMBER;
  199. nvalue(top) = (Number)k;
  200. call_arith(L, top+1, IM_ADD);
  201. }
  202. static int luaV_strcomp (const TString *ls, const TString *rs) {
  203. const char *l = ls->str;
  204. long ll = ls->u.s.len;
  205. const char *r = rs->str;
  206. long lr = rs->u.s.len;
  207. for (;;) {
  208. long temp = strcoll(l, r);
  209. if (temp != 0) return temp;
  210. /* strings are equal up to a '\0' */
  211. temp = strlen(l); /* index of first '\0' in both strings */
  212. if (temp == ll) /* l is finished? */
  213. return (temp == lr) ? 0 : -1; /* l is equal or smaller than r */
  214. else if (temp == lr) /* r is finished? */
  215. return 1; /* l is greater than r (because l is not finished) */
  216. /* both strings longer than temp; go on comparing (after the '\0') */
  217. temp++;
  218. l += temp; ll -= temp; r += temp; lr -= temp;
  219. }
  220. }
  221. int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) {
  222. if (ttype(l) == TAG_NUMBER && ttype(r) == TAG_NUMBER)
  223. return (nvalue(l) < nvalue(r));
  224. else if (ttype(l) == TAG_STRING && ttype(r) == TAG_STRING)
  225. return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
  226. else { /* call TM */
  227. luaD_checkstack(L, 2);
  228. *top++ = *l;
  229. *top++ = *r;
  230. call_binTM(L, top, IM_LT, "unexpected type in comparison");
  231. L->top--;
  232. return (ttype(L->top) != TAG_NIL);
  233. }
  234. }
  235. static void strconc (lua_State *L, int total, StkId top) {
  236. do {
  237. int n = 2; /* number of elements handled in this pass (at least 2) */
  238. if (tostring(L, top-2) || tostring(L, top-1))
  239. call_binTM(L, top, IM_CONCAT, "unexpected type for concatenation");
  240. else if (tsvalue(top-1)->u.s.len > 0) { /* if len=0, do nothing */
  241. /* at least two string values; get as many as possible */
  242. long tl = tsvalue(top-1)->u.s.len + tsvalue(top-2)->u.s.len;
  243. char *buffer;
  244. int i;
  245. while (n < total && !tostring(L, top-n-1)) { /* collect total length */
  246. tl += tsvalue(top-n-1)->u.s.len;
  247. n++;
  248. }
  249. buffer = luaL_openspace(L, tl);
  250. tl = 0;
  251. for (i=n; i>0; i--) { /* concat all strings */
  252. long l = tsvalue(top-i)->u.s.len;
  253. memcpy(buffer+tl, tsvalue(top-i)->str, l);
  254. tl += l;
  255. }
  256. tsvalue(top-n) = luaS_newlstr(L, buffer, tl);
  257. }
  258. total -= n-1; /* got `n' strings to create 1 new */
  259. top -= n-1;
  260. } while (total > 1); /* repeat until only 1 result left */
  261. }
  262. void luaV_pack (lua_State *L, StkId firstelem, int nvararg, TObject *tab) {
  263. int i;
  264. Hash *htab;
  265. htab = avalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */
  266. ttype(tab) = TAG_TABLE;
  267. for (i=0; i<nvararg; i++)
  268. luaH_setint(L, htab, i+1, firstelem+i);
  269. luaV_setn(L, htab, nvararg); /* store counter in field `n' */
  270. }
  271. static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
  272. TObject arg;
  273. int nvararg = (L->top-base) - nfixargs;
  274. if (nvararg < 0) {
  275. luaV_pack(L, base, 0, &arg);
  276. luaD_adjusttop(L, base, nfixargs);
  277. }
  278. else {
  279. luaV_pack(L, base+nfixargs, nvararg, &arg);
  280. L->top = base+nfixargs;
  281. }
  282. *L->top++ = arg;
  283. }
  284. /*
  285. ** Executes the given Lua function. Parameters are between [base,top).
  286. ** Returns n such that the the results are between [n,top).
  287. */
  288. StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
  289. const Proto *tf = cl->f.l;
  290. StkId top; /* keep top local, for performance */
  291. const Instruction *pc = tf->code;
  292. TString **kstr = tf->kstr;
  293. luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
  294. if (tf->is_vararg) { /* varargs? */
  295. adjust_varargs(L, base, tf->numparams);
  296. luaC_checkGC(L);
  297. }
  298. else
  299. luaD_adjusttop(L, base, tf->numparams);
  300. top = L->top;
  301. for (;;) {
  302. Instruction i = *pc++;
  303. switch (GET_OPCODE(i)) {
  304. case OP_END:
  305. return L->top; /* no results */
  306. case OP_RETURN:
  307. L->top = top;
  308. return base+GETARG_U(i);
  309. case OP_CALL:
  310. L->top = top;
  311. luaD_call(L, base+GETARG_A(i), GETARG_B(i));
  312. top = L->top;
  313. break;
  314. case OP_TAILCALL:
  315. L->top = top;
  316. luaD_call(L, base+GETARG_A(i), MULT_RET);
  317. return base+GETARG_B(i);
  318. case OP_PUSHNIL: {
  319. int n = GETARG_U(i);
  320. LUA_ASSERT(L, n>0, "invalid argument");
  321. do {
  322. ttype(top++) = TAG_NIL;
  323. } while (--n > 0);
  324. break;
  325. }
  326. case OP_POP:
  327. top -= GETARG_U(i);
  328. break;
  329. case OP_PUSHINT:
  330. ttype(top) = TAG_NUMBER;
  331. nvalue(top) = (Number)GETARG_S(i);
  332. top++;
  333. break;
  334. case OP_PUSHSTRING:
  335. ttype(top) = TAG_STRING;
  336. tsvalue(top) = kstr[GETARG_U(i)];
  337. top++;
  338. break;
  339. case OP_PUSHNUM:
  340. ttype(top) = TAG_NUMBER;
  341. nvalue(top) = tf->knum[GETARG_U(i)];
  342. top++;
  343. break;
  344. case OP_PUSHNEGNUM:
  345. ttype(top) = TAG_NUMBER;
  346. nvalue(top) = -tf->knum[GETARG_U(i)];
  347. top++;
  348. break;
  349. case OP_PUSHUPVALUE:
  350. *top++ = cl->consts[GETARG_U(i)];
  351. break;
  352. case OP_GETLOCAL:
  353. *top++ = *(base+GETARG_U(i));
  354. break;
  355. case OP_GETGLOBAL:
  356. luaV_getglobal(L, kstr[GETARG_U(i)], top);
  357. top++;
  358. break;
  359. case OP_GETTABLE:
  360. luaV_gettable(L, top);
  361. top--;
  362. break;
  363. case OP_GETDOTTED:
  364. ttype(top) = TAG_STRING;
  365. tsvalue(top++) = kstr[GETARG_U(i)];
  366. luaV_gettable(L, top);
  367. top--;
  368. break;
  369. case OP_GETINDEXED:
  370. *top++ = *(base+GETARG_U(i));
  371. luaV_gettable(L, top);
  372. top--;
  373. break;
  374. case OP_PUSHSELF: {
  375. TObject receiver;
  376. receiver = *(top-1);
  377. ttype(top) = TAG_STRING;
  378. tsvalue(top++) = kstr[GETARG_U(i)];
  379. luaV_gettable(L, top);
  380. *(top-1) = receiver;
  381. break;
  382. }
  383. case OP_CREATETABLE:
  384. L->top = top;
  385. luaC_checkGC(L);
  386. avalue(top) = luaH_new(L, GETARG_U(i));
  387. ttype(top) = TAG_TABLE;
  388. top++;
  389. break;
  390. case OP_SETLOCAL:
  391. *(base+GETARG_U(i)) = *(--top);
  392. break;
  393. case OP_SETGLOBAL:
  394. luaV_setglobal(L, kstr[GETARG_U(i)], top);
  395. top--;
  396. break;
  397. case OP_SETTABLE:
  398. luaV_settable(L, top-GETARG_A(i), top);
  399. top -= GETARG_B(i); /* pop values */
  400. break;
  401. case OP_SETLIST: {
  402. int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
  403. int n = GETARG_B(i)+1;
  404. Hash *arr = avalue(top-n-1);
  405. L->top = top-n; /* final value of `top' (in case of errors) */
  406. for (; n; n--)
  407. luaH_setint(L, arr, n+aux, --top);
  408. break;
  409. }
  410. case OP_SETMAP: {
  411. int n = GETARG_U(i);
  412. StkId finaltop = top-2*(n+1);
  413. Hash *arr = avalue(finaltop-1);
  414. L->top = finaltop; /* final value of `top' (in case of errors) */
  415. do {
  416. luaH_set(L, arr, top-2, top-1);
  417. top-=2;
  418. } while (n--);
  419. break;
  420. }
  421. case OP_ADD:
  422. if (tonumber(top-1) || tonumber(top-2))
  423. call_arith(L, top, IM_ADD);
  424. else
  425. nvalue(top-2) += nvalue(top-1);
  426. top--;
  427. break;
  428. case OP_ADDI:
  429. if (tonumber(top-1))
  430. addK(L, top, GETARG_S(i));
  431. else
  432. nvalue(top-1) += (Number)GETARG_S(i);
  433. break;
  434. case OP_SUB:
  435. if (tonumber(top-1) || tonumber(top-2))
  436. call_arith(L, top, IM_SUB);
  437. else
  438. nvalue(top-2) -= nvalue(top-1);
  439. top--;
  440. break;
  441. case OP_MULT:
  442. if (tonumber(top-1) || tonumber(top-2))
  443. call_arith(L, top, IM_MUL);
  444. else
  445. nvalue(top-2) *= nvalue(top-1);
  446. top--;
  447. break;
  448. case OP_DIV:
  449. if (tonumber(top-1) || tonumber(top-2))
  450. call_arith(L, top, IM_DIV);
  451. else
  452. nvalue(top-2) /= nvalue(top-1);
  453. top--;
  454. break;
  455. case OP_POW:
  456. call_binTM(L, top, IM_POW, "undefined operation");
  457. top--;
  458. break;
  459. case OP_CONCAT: {
  460. int n = GETARG_U(i);
  461. strconc(L, n, top);
  462. top -= n-1;
  463. L->top = top;
  464. luaC_checkGC(L);
  465. break;
  466. }
  467. case OP_MINUS:
  468. if (tonumber(top-1)) {
  469. ttype(top) = TAG_NIL;
  470. call_arith(L, top+1, IM_UNM);
  471. }
  472. else
  473. nvalue(top-1) = -nvalue(top-1);
  474. break;
  475. case OP_NOT:
  476. ttype(top-1) =
  477. (ttype(top-1) == TAG_NIL) ? TAG_NUMBER : TAG_NIL;
  478. nvalue(top-1) = 1;
  479. break;
  480. case OP_JMPNE:
  481. top -= 2;
  482. if (!luaO_equalObj(top, top+1)) pc += GETARG_S(i);
  483. break;
  484. case OP_JMPEQ:
  485. top -= 2;
  486. if (luaO_equalObj(top, top+1)) pc += GETARG_S(i);
  487. break;
  488. case OP_JMPLT:
  489. top -= 2;
  490. if (luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
  491. break;
  492. case OP_JMPLE: /* a <= b === !(b<a) */
  493. top -= 2;
  494. if (!luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
  495. break;
  496. case OP_JMPGT: /* a > b === (b<a) */
  497. top -= 2;
  498. if (luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
  499. break;
  500. case OP_JMPGE: /* a >= b === !(a<b) */
  501. top -= 2;
  502. if (!luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
  503. break;
  504. case OP_JMPT:
  505. if (ttype(--top) != TAG_NIL) pc += GETARG_S(i);
  506. break;
  507. case OP_JMPF:
  508. if (ttype(--top) == TAG_NIL) pc += GETARG_S(i);
  509. break;
  510. case OP_JMPONT:
  511. if (ttype(top-1) != TAG_NIL) pc += GETARG_S(i);
  512. else top--;
  513. break;
  514. case OP_JMPONF:
  515. if (ttype(top-1) == TAG_NIL) pc += GETARG_S(i);
  516. else top--;
  517. break;
  518. case OP_JMP:
  519. pc += GETARG_S(i);
  520. break;
  521. case OP_PUSHNILJMP:
  522. ttype(top++) = TAG_NIL;
  523. pc++;
  524. break;
  525. case OP_FORPREP:
  526. if (tonumber(top-1))
  527. lua_error(L, "`for' step must be a number");
  528. if (tonumber(top-2))
  529. lua_error(L, "`for' limit must be a number");
  530. if (tonumber(top-3))
  531. lua_error(L, "`for' initial value must be a number");
  532. nvalue(top-3) -= nvalue(top-1); /* to be undone by first FORLOOP */
  533. pc += GETARG_S(i);
  534. break;
  535. case OP_FORLOOP: {
  536. Number step = nvalue(top-1);
  537. Number limit = nvalue(top-2);
  538. Number index;
  539. LUA_ASSERT(L, ttype(top-1) == TAG_NUMBER, "invalid step");
  540. LUA_ASSERT(L, ttype(top-2) == TAG_NUMBER, "invalid limit");
  541. if (ttype(top-3) != TAG_NUMBER)
  542. lua_error(L, "`for' index must be a number");
  543. index = nvalue(top-3)+step;
  544. if ((step>0) ? index>limit : index<limit)
  545. top -= 3; /* end loop: remove control variables */
  546. else {
  547. nvalue(top-3) = index;
  548. pc += GETARG_S(i);
  549. }
  550. break;
  551. }
  552. case OP_LFORPREP: {
  553. if (ttype(top-1) != TAG_TABLE)
  554. lua_error(L, "`for' table must be a table");
  555. top += 3; /* counter + index,value */
  556. ttype(top-3) = TAG_NUMBER;
  557. nvalue(top-3) = 0.0; /* counter */
  558. ttype(top-2) = ttype(top-1) = TAG_NIL;
  559. pc += GETARG_S(i);
  560. break;
  561. }
  562. case OP_LFORLOOP: {
  563. int n;
  564. top -= 2; /* remove old index,value */
  565. LUA_ASSERT(L, ttype(top-2) == TAG_TABLE, "invalid table");
  566. LUA_ASSERT(L, ttype(top-1) == TAG_NUMBER, "invalid counter");
  567. L->top = top;
  568. n = luaA_next(L, avalue(top-2), (int)nvalue(top-1));
  569. if (n == 0) /* end loop? */
  570. top -= 2; /* remove table and counter */
  571. else {
  572. nvalue(top-1) = (Number)n;
  573. top += 2; /* new index,value */
  574. pc += GETARG_S(i); /* repeat loop */
  575. }
  576. break;
  577. }
  578. case OP_CLOSURE:
  579. L->top = top;
  580. luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i));
  581. top = L->top;
  582. luaC_checkGC(L);
  583. break;
  584. case OP_SETLINE:
  585. if ((base-1)->ttype != TAG_LINE) {
  586. /* open space for LINE value */
  587. int n = top-base;
  588. while (n--) base[n+1] = base[n];
  589. base++;
  590. top++;
  591. (base-1)->ttype = TAG_LINE;
  592. }
  593. (base-1)->value.i = GETARG_U(i);
  594. if (L->linehook) {
  595. L->top = top;
  596. luaD_lineHook(L, base-2, GETARG_U(i));
  597. }
  598. break;
  599. }
  600. }
  601. }