lvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*
  2. ** $Id: lvm.c,v 1.175 2001/03/07 18:09:25 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "lua.h"
  11. #include "lapi.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. int luaV_tonumber (TObject *obj) {
  24. if (ttype(obj) != LUA_TSTRING)
  25. return 1;
  26. else {
  27. if (!luaO_str2d(svalue(obj), &nvalue(obj)))
  28. return 2;
  29. ttype(obj) = LUA_TNUMBER;
  30. return 0;
  31. }
  32. }
  33. int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
  34. if (ttype(obj) != LUA_TNUMBER)
  35. return 1;
  36. else {
  37. l_char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  38. lua_number2str(s, nvalue(obj)); /* convert `s' to number */
  39. setsvalue(obj, luaS_new(L, s));
  40. return 0;
  41. }
  42. }
  43. static void traceexec (lua_State *L, lua_Hook linehook) {
  44. CallInfo *ci = L->ci;
  45. int *lineinfo = ci_func(ci)->f.l->lineinfo;
  46. int pc = (*ci->pc - ci_func(ci)->f.l->code) - 1;
  47. int newline;
  48. if (pc == 0) { /* may be first time? */
  49. ci->line = 1;
  50. ci->refi = 0;
  51. ci->lastpc = pc+1; /* make sure it will call linehook */
  52. }
  53. newline = luaG_getline(lineinfo, pc, ci->line, &ci->refi);
  54. /* calls linehook when enters a new line or jumps back (loop) */
  55. if (newline != ci->line || pc <= ci->lastpc) {
  56. ci->line = newline;
  57. luaD_lineHook(L, newline, linehook);
  58. }
  59. ci->lastpc = pc;
  60. }
  61. static Closure *luaV_closure (lua_State *L, int nelems) {
  62. Closure *c = luaF_newclosure(L, nelems);
  63. L->top -= nelems;
  64. while (nelems--)
  65. setobj(&c->upvalue[nelems], L->top+nelems);
  66. setclvalue(L->top, c);
  67. incr_top;
  68. return c;
  69. }
  70. void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) {
  71. Closure *cl = luaV_closure(L, nelems);
  72. cl->f.c = c;
  73. cl->isC = 1;
  74. }
  75. void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
  76. Closure *cl = luaV_closure(L, nelems);
  77. cl->f.l = l;
  78. cl->isC = 0;
  79. }
  80. static void callTM (lua_State *L, const l_char *fmt, ...) {
  81. va_list argp;
  82. StkId base = L->top;
  83. int has_result = 0;
  84. va_start(argp, fmt);
  85. while (*fmt) {
  86. switch (*fmt++) {
  87. case l_c('c'):
  88. setclvalue(L->top, va_arg(argp, Closure *));
  89. break;
  90. case l_c('o'):
  91. setobj(L->top, va_arg(argp, TObject *));
  92. break;
  93. case l_c('s'):
  94. setsvalue(L->top, va_arg(argp, TString *));
  95. break;
  96. case l_c('r'):
  97. has_result = 1;
  98. continue;
  99. }
  100. incr_top;
  101. }
  102. luaD_call(L, base, has_result);
  103. if (has_result) {
  104. L->top--;
  105. setobj(va_arg(argp, TObject *), L->top);
  106. }
  107. va_end(argp);
  108. }
  109. /*
  110. ** Function to index a table.
  111. ** Receives the table at `t' and the key at the `key'.
  112. ** leaves the result at `res'.
  113. */
  114. void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res) {
  115. Closure *tm;
  116. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  117. int tg = hvalue(t)->htag;
  118. if (tg == LUA_TTABLE || /* with default tag? */
  119. (tm = luaT_gettm(G(L), tg, TM_GETTABLE)) == NULL) { /* or no TM? */
  120. const TObject *h = luaH_get(hvalue(t), key); /* do a primitive get */
  121. /* result is no nil or there is no `index' tag method? */
  122. if (ttype(h) != LUA_TNIL || /* no nil? */
  123. ((tm=luaT_gettm(G(L), tg, TM_INDEX)) == NULL)) { /* or no index TM? */
  124. setobj(res, h); /* default get */
  125. return;
  126. }
  127. }
  128. /* else will call the tag method */
  129. }
  130. else { /* not a table; try a `gettable' tag method */
  131. tm = luaT_gettmbyObj(G(L), t, TM_GETTABLE);
  132. if (tm == NULL) /* no tag method? */
  133. luaG_typeerror(L, t, l_s("index"));
  134. }
  135. callTM(L, l_s("coor"), tm, t, key, res);
  136. }
  137. /*
  138. ** Receives table at `t', key at `key' and value at `val'.
  139. */
  140. void luaV_settable (lua_State *L, StkId t, StkId key, StkId val) {
  141. Closure *tm;
  142. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  143. int tg = hvalue(t)->htag;
  144. if (hvalue(t)->htag == LUA_TTABLE || /* with default tag? */
  145. (tm = luaT_gettm(G(L), tg, TM_SETTABLE)) == NULL) { /* or no TM? */
  146. setobj(luaH_set(L, hvalue(t), key), val); /* do a primitive set */
  147. return;
  148. }
  149. /* else will call the tag method */
  150. }
  151. else { /* not a table; try a `settable' tag method */
  152. tm = luaT_gettmbyObj(G(L), t, TM_SETTABLE);
  153. if (tm == NULL) /* no tag method? */
  154. luaG_typeerror(L, t, l_s("index"));
  155. }
  156. callTM(L, l_s("cooo"), tm, t, key, val);
  157. }
  158. void luaV_getglobal (lua_State *L, TString *name, StkId res) {
  159. const TObject *value = luaH_getstr(L->gt, name);
  160. Closure *tm;
  161. if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */
  162. (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) {
  163. setobj(res, value); /* default behavior */
  164. }
  165. else
  166. callTM(L, l_s("csor"), tm, name, value, res);
  167. }
  168. void luaV_setglobal (lua_State *L, TString *name, StkId val) {
  169. TObject *oldvalue = luaH_setstr(L, L->gt, name);
  170. Closure *tm;
  171. if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */
  172. (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) {
  173. setobj(oldvalue, val); /* raw set */
  174. }
  175. else
  176. callTM(L, l_s("csoo"), tm, name, oldvalue, val);
  177. }
  178. static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
  179. TObject *res, TMS event) {
  180. TString *opname;
  181. Closure *tm = luaT_gettmbyObj(G(L), p1, event); /* try first operand */
  182. if (tm == NULL) {
  183. tm = luaT_gettmbyObj(G(L), p2, event); /* try second operand */
  184. if (tm == NULL) {
  185. tm = luaT_gettm(G(L), 0, event); /* try a `global' method */
  186. if (tm == NULL)
  187. return 0; /* no tag method */
  188. }
  189. }
  190. opname = luaS_new(L, luaT_eventname[event]);
  191. callTM(L, l_s("coosr"), tm, p1, p2, opname, res);
  192. return 1;
  193. }
  194. static void call_arith (lua_State *L, StkId p1, TMS event) {
  195. if (!call_binTM(L, p1, p1+1, p1, event))
  196. luaG_binerror(L, p1, LUA_TNUMBER, l_s("perform arithmetic on"));
  197. }
  198. static int luaV_strlessthan (const TString *ls, const TString *rs) {
  199. const l_char *l = getstr(ls);
  200. size_t ll = ls->len;
  201. const l_char *r = getstr(rs);
  202. size_t lr = rs->len;
  203. for (;;) {
  204. int temp = strcoll(l, r);
  205. if (temp != 0) return (temp < 0);
  206. else { /* strings are equal up to a `\0' */
  207. size_t len = strlen(l); /* index of first `\0' in both strings */
  208. if (len == lr) /* r is finished? */
  209. return 0; /* l is equal or greater than r */
  210. else if (len == ll) /* l is finished? */
  211. return 1; /* l is smaller than r (because r is not finished) */
  212. /* both strings longer than `len'; go on comparing (after the `\0') */
  213. len++;
  214. l += len; ll -= len; r += len; lr -= len;
  215. }
  216. }
  217. }
  218. int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r) {
  219. if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER)
  220. return (nvalue(l) < nvalue(r));
  221. else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING)
  222. return luaV_strlessthan(tsvalue(l), tsvalue(r));
  223. else { /* try TM */
  224. if (!call_binTM(L, l, r, L->top, TM_LT))
  225. luaG_ordererror(L, l, r);
  226. return (ttype(L->top) != LUA_TNIL);
  227. }
  228. }
  229. void luaV_strconc (lua_State *L, int total, StkId top) {
  230. do {
  231. int n = 2; /* number of elements handled in this pass (at least 2) */
  232. if (tostring(L, top-2) || tostring(L, top-1)) {
  233. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  234. luaG_binerror(L, top-2, LUA_TSTRING, l_s("concat"));
  235. }
  236. else if (tsvalue(top-1)->len > 0) { /* if len=0, do nothing */
  237. /* at least two string values; get as many as possible */
  238. lu_mem tl = (lu_mem)tsvalue(top-1)->len + (lu_mem)tsvalue(top-2)->len;
  239. l_char *buffer;
  240. int i;
  241. while (n < total && !tostring(L, top-n-1)) { /* collect total length */
  242. tl += tsvalue(top-n-1)->len;
  243. n++;
  244. }
  245. if (tl > MAX_SIZET) luaD_error(L, l_s("string size overflow"));
  246. buffer = luaO_openspace(L, tl, l_char);
  247. tl = 0;
  248. for (i=n; i>0; i--) { /* concat all strings */
  249. size_t l = tsvalue(top-i)->len;
  250. memcpy(buffer+tl, svalue(top-i), l);
  251. tl += l;
  252. }
  253. setsvalue(top-n, luaS_newlstr(L, buffer, tl));
  254. }
  255. total -= n-1; /* got `n' strings to create 1 new */
  256. top -= n-1;
  257. } while (total > 1); /* repeat until only 1 result left */
  258. }
  259. static void luaV_pack (lua_State *L, StkId firstelem) {
  260. int i;
  261. Hash *htab = luaH_new(L, 0);
  262. TObject *n;
  263. for (i=0; firstelem+i<L->top; i++)
  264. setobj(luaH_setnum(L, htab, i+1), firstelem+i);
  265. /* store counter in field `n' */
  266. n = luaH_setstr(L, htab, luaS_newliteral(L, l_s("n")));
  267. setnvalue(n, i);
  268. L->top = firstelem; /* remove elements from the stack */
  269. sethvalue(L->top, htab);
  270. incr_top;
  271. }
  272. static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
  273. int nvararg = (L->top-base) - nfixargs;
  274. if (nvararg < 0)
  275. luaD_adjusttop(L, base, nfixargs);
  276. luaV_pack(L, base+nfixargs);
  277. }
  278. #define dojump(pc, i) ((pc) += GETARG_S(i))
  279. /*
  280. ** Executes the given Lua function. Parameters are between [base,top).
  281. ** Returns n such that the the results are between [n,top).
  282. */
  283. StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
  284. const Proto *const tf = cl->f.l;
  285. StkId top; /* keep top local, for performance */
  286. const Instruction *pc = tf->code;
  287. const lua_Hook linehook = L->linehook;
  288. L->ci->pc = &pc;
  289. if (tf->is_vararg) /* varargs? */
  290. adjust_varargs(L, base, tf->numparams);
  291. luaD_adjusttop(L, base, tf->maxstacksize);
  292. top = base+tf->numparams+tf->is_vararg;
  293. /* main loop of interpreter */
  294. for (;;) {
  295. const Instruction i = *pc++;
  296. lua_assert(L->top == base+tf->maxstacksize);
  297. if (linehook)
  298. traceexec(L, linehook);
  299. switch (GET_OPCODE(i)) {
  300. case OP_RETURN: {
  301. L->top = top;
  302. return base+GETARG_U(i);
  303. }
  304. case OP_CALL: {
  305. int nres = GETARG_B(i);
  306. if (nres == MULT_RET) nres = LUA_MULTRET;
  307. L->top = top;
  308. luaD_call(L, base+GETARG_A(i), nres);
  309. top = L->top;
  310. L->top = base+tf->maxstacksize;
  311. break;
  312. }
  313. case OP_PUSHNIL: {
  314. int n = GETARG_U(i);
  315. lua_assert(n>0);
  316. do {
  317. setnilvalue(top++);
  318. } while (--n > 0);
  319. break;
  320. }
  321. case OP_POP: {
  322. top -= GETARG_U(i);
  323. break;
  324. }
  325. case OP_PUSHINT: {
  326. setnvalue(top, (lua_Number)GETARG_S(i));
  327. top++;
  328. break;
  329. }
  330. case OP_PUSHSTRING: {
  331. setsvalue(top, tf->kstr[GETARG_U(i)]);
  332. top++;
  333. break;
  334. }
  335. case OP_PUSHNUM: {
  336. setnvalue(top, tf->knum[GETARG_U(i)]);
  337. top++;
  338. break;
  339. }
  340. case OP_PUSHNEGNUM: {
  341. setnvalue(top, -tf->knum[GETARG_U(i)]);
  342. top++;
  343. break;
  344. }
  345. case OP_PUSHUPVALUE: {
  346. setobj(top++, &cl->upvalue[GETARG_U(i)]);
  347. break;
  348. }
  349. case OP_GETLOCAL: {
  350. setobj(top++, base+GETARG_U(i));
  351. break;
  352. }
  353. case OP_GETGLOBAL: {
  354. luaV_getglobal(L, tf->kstr[GETARG_U(i)], top);
  355. top++;
  356. break;
  357. }
  358. case OP_GETTABLE: {
  359. top--;
  360. luaV_gettable(L, top-1, top, top-1);
  361. break;
  362. }
  363. case OP_GETDOTTED: {
  364. setsvalue(top, tf->kstr[GETARG_U(i)]);
  365. luaV_gettable(L, top-1, top, top-1);
  366. break;
  367. }
  368. case OP_GETINDEXED: {
  369. luaV_gettable(L, top-1, base+GETARG_U(i), top-1);
  370. break;
  371. }
  372. case OP_PUSHSELF: {
  373. setobj(top, top-1);
  374. setsvalue(top+1, tf->kstr[GETARG_U(i)]);
  375. luaV_gettable(L, top-1, top+1, top-1);
  376. top++;
  377. break;
  378. }
  379. case OP_CREATETABLE: {
  380. luaC_checkGC(L);
  381. sethvalue(top, luaH_new(L, GETARG_U(i)));
  382. top++;
  383. break;
  384. }
  385. case OP_SETLOCAL: {
  386. setobj(base+GETARG_U(i), --top);
  387. break;
  388. }
  389. case OP_SETGLOBAL: {
  390. top--;
  391. luaV_setglobal(L, tf->kstr[GETARG_U(i)], top);
  392. break;
  393. }
  394. case OP_SETTABLE: {
  395. StkId t = top-GETARG_A(i);
  396. luaV_settable(L, t, t+1, top-1);
  397. top -= GETARG_B(i); /* pop values */
  398. break;
  399. }
  400. case OP_SETLIST: {
  401. int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
  402. int n = GETARG_B(i);
  403. Hash *arr = hvalue(top-n-1);
  404. for (; n; n--)
  405. setobj(luaH_setnum(L, arr, n+aux), --top);
  406. break;
  407. }
  408. case OP_SETMAP: {
  409. int n = GETARG_U(i);
  410. Hash *arr = hvalue((top-2*n)-1);
  411. for (; n; n--) {
  412. top-=2;
  413. setobj(luaH_set(L, arr, top), top+1);
  414. }
  415. break;
  416. }
  417. case OP_ADD: {
  418. if (tonumber(top-2) || tonumber(top-1))
  419. call_arith(L, top-2, TM_ADD);
  420. else
  421. nvalue(top-2) += nvalue(top-1);
  422. top--;
  423. break;
  424. }
  425. case OP_ADDI: {
  426. if (tonumber(top-1)) {
  427. setnvalue(top, (lua_Number)GETARG_S(i));
  428. call_arith(L, top-1, TM_ADD);
  429. }
  430. else
  431. nvalue(top-1) += (lua_Number)GETARG_S(i);
  432. break;
  433. }
  434. case OP_SUB: {
  435. if (tonumber(top-2) || tonumber(top-1))
  436. call_arith(L, top-2, TM_SUB);
  437. else
  438. nvalue(top-2) -= nvalue(top-1);
  439. top--;
  440. break;
  441. }
  442. case OP_MULT: {
  443. if (tonumber(top-2) || tonumber(top-1))
  444. call_arith(L, top-2, TM_MUL);
  445. else
  446. nvalue(top-2) *= nvalue(top-1);
  447. top--;
  448. break;
  449. }
  450. case OP_DIV: {
  451. if (tonumber(top-2) || tonumber(top-1))
  452. call_arith(L, top-2, TM_DIV);
  453. else
  454. nvalue(top-2) /= nvalue(top-1);
  455. top--;
  456. break;
  457. }
  458. case OP_POW: {
  459. if (!call_binTM(L, top-2, top-1, top-2, TM_POW))
  460. luaD_error(L, l_s("undefined operation"));
  461. top--;
  462. break;
  463. }
  464. case OP_CONCAT: {
  465. int n = GETARG_U(i);
  466. luaV_strconc(L, n, top);
  467. top -= n-1;
  468. luaC_checkGC(L);
  469. break;
  470. }
  471. case OP_MINUS: {
  472. if (tonumber(top-1)) {
  473. setnilvalue(top);
  474. call_arith(L, top-1, TM_UNM);
  475. }
  476. else
  477. nvalue(top-1) = -nvalue(top-1);
  478. break;
  479. }
  480. case OP_NOT: {
  481. ttype(top-1) =
  482. (ttype(top-1) == LUA_TNIL) ? LUA_TNUMBER : LUA_TNIL;
  483. nvalue(top-1) = 1;
  484. break;
  485. }
  486. case OP_JMPNE: {
  487. top -= 2;
  488. if (!luaO_equalObj(top, top+1)) dojump(pc, i);
  489. break;
  490. }
  491. case OP_JMPEQ: {
  492. top -= 2;
  493. if (luaO_equalObj(top, top+1)) dojump(pc, i);
  494. break;
  495. }
  496. case OP_JMPLT: {
  497. top -= 2;
  498. if (luaV_lessthan(L, top, top+1)) dojump(pc, i);
  499. break;
  500. }
  501. case OP_JMPLE: { /* a <= b === !(b<a) */
  502. top -= 2;
  503. if (!luaV_lessthan(L, top+1, top)) dojump(pc, i);
  504. break;
  505. }
  506. case OP_JMPGT: { /* a > b === (b<a) */
  507. top -= 2;
  508. if (luaV_lessthan(L, top+1, top)) dojump(pc, i);
  509. break;
  510. }
  511. case OP_JMPGE: { /* a >= b === !(a<b) */
  512. top -= 2;
  513. if (!luaV_lessthan(L, top, top+1)) dojump(pc, i);
  514. break;
  515. }
  516. case OP_JMPT: {
  517. if (ttype(--top) != LUA_TNIL) dojump(pc, i);
  518. break;
  519. }
  520. case OP_JMPF: {
  521. if (ttype(--top) == LUA_TNIL) dojump(pc, i);
  522. break;
  523. }
  524. case OP_JMPONT: {
  525. if (ttype(top-1) == LUA_TNIL) top--;
  526. else dojump(pc, i);
  527. break;
  528. }
  529. case OP_JMPONF: {
  530. if (ttype(top-1) != LUA_TNIL) top--;
  531. else dojump(pc, i);
  532. break;
  533. }
  534. case OP_JMP: {
  535. dojump(pc, i);
  536. break;
  537. }
  538. case OP_PUSHNILJMP: {
  539. setnilvalue(top++);
  540. pc++;
  541. break;
  542. }
  543. case OP_FORPREP: {
  544. int jmp = GETARG_S(i);
  545. if (tonumber(top-1))
  546. luaD_error(L, l_s("`for' step must be a number"));
  547. if (tonumber(top-2))
  548. luaD_error(L, l_s("`for' limit must be a number"));
  549. if (tonumber(top-3))
  550. luaD_error(L, l_s("`for' initial value must be a number"));
  551. pc += -jmp; /* `jump' to loop end (delta is negated here) */
  552. goto forloop; /* do not increment index */
  553. }
  554. case OP_FORLOOP: {
  555. lua_assert(ttype(top-1) == LUA_TNUMBER);
  556. lua_assert(ttype(top-2) == LUA_TNUMBER);
  557. if (ttype(top-3) != LUA_TNUMBER)
  558. luaD_error(L, l_s("`for' index must be a number"));
  559. nvalue(top-3) += nvalue(top-1); /* increment index */
  560. forloop:
  561. if (nvalue(top-1) > 0 ?
  562. nvalue(top-3) > nvalue(top-2) :
  563. nvalue(top-3) < nvalue(top-2))
  564. top -= 3; /* end loop: remove control variables */
  565. else
  566. dojump(pc, i); /* repeat loop */
  567. break;
  568. }
  569. case OP_LFORPREP: {
  570. int jmp = GETARG_S(i);
  571. if (ttype(top-1) != LUA_TTABLE)
  572. luaD_error(L, l_s("`for' table must be a table"));
  573. top += 3; /* index,key,value */
  574. setnvalue(top-3, -1); /* initial index */
  575. setnilvalue(top-2);
  576. setnilvalue(top-1);
  577. pc += -jmp; /* `jump' to loop end (delta is negated here) */
  578. /* go through */
  579. }
  580. case OP_LFORLOOP: {
  581. Hash *t = hvalue(top-4);
  582. int n = (int)nvalue(top-3);
  583. lua_assert(ttype(top-3) == LUA_TNUMBER);
  584. lua_assert(ttype(top-4) == LUA_TTABLE);
  585. n = luaH_nexti(t, n);
  586. if (n == -1) /* end loop? */
  587. top -= 4; /* remove table, index, key, and value */
  588. else {
  589. Node *node = node(t, n);
  590. setnvalue(top-3, n); /* index */
  591. setkey2obj(top-2, node);
  592. setobj(top-1, val(node));
  593. dojump(pc, i); /* repeat loop */
  594. }
  595. break;
  596. }
  597. case OP_CLOSURE: {
  598. int nup = GETARG_B(i);
  599. luaC_checkGC(L);
  600. L->top = top;
  601. luaV_Lclosure(L, tf->kproto[GETARG_A(i)], nup);
  602. top -= (nup-1);
  603. L->top = base+tf->maxstacksize;
  604. break;
  605. }
  606. }
  607. }
  608. }