lvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. ** $Id: lvm.c,v 1.174 2001/03/07 13:22:55 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. TString **const kstr = tf->kstr;
  288. const lua_Hook linehook = L->linehook;
  289. L->ci->pc = &pc;
  290. if (tf->is_vararg) /* varargs? */
  291. adjust_varargs(L, base, tf->numparams);
  292. luaD_adjusttop(L, base, tf->maxstacksize);
  293. top = base+tf->numparams+tf->is_vararg;
  294. /* main loop of interpreter */
  295. for (;;) {
  296. const Instruction i = *pc++;
  297. lua_assert(L->top == base+tf->maxstacksize);
  298. if (linehook)
  299. traceexec(L, linehook);
  300. switch (GET_OPCODE(i)) {
  301. case OP_RETURN: {
  302. L->top = top;
  303. return base+GETARG_U(i);
  304. }
  305. case OP_CALL: {
  306. int nres = GETARG_B(i);
  307. if (nres == MULT_RET) nres = LUA_MULTRET;
  308. L->top = top;
  309. luaD_call(L, base+GETARG_A(i), nres);
  310. top = L->top;
  311. L->top = base+tf->maxstacksize;
  312. break;
  313. }
  314. case OP_PUSHNIL: {
  315. int n = GETARG_U(i);
  316. lua_assert(n>0);
  317. do {
  318. setnilvalue(top++);
  319. } while (--n > 0);
  320. break;
  321. }
  322. case OP_POP: {
  323. top -= GETARG_U(i);
  324. break;
  325. }
  326. case OP_PUSHINT: {
  327. setnvalue(top, (lua_Number)GETARG_S(i));
  328. top++;
  329. break;
  330. }
  331. case OP_PUSHSTRING: {
  332. setsvalue(top, kstr[GETARG_U(i)]);
  333. top++;
  334. break;
  335. }
  336. case OP_PUSHNUM: {
  337. setnvalue(top, tf->knum[GETARG_U(i)]);
  338. top++;
  339. break;
  340. }
  341. case OP_PUSHNEGNUM: {
  342. setnvalue(top, -tf->knum[GETARG_U(i)]);
  343. top++;
  344. break;
  345. }
  346. case OP_PUSHUPVALUE: {
  347. setobj(top++, &cl->upvalue[GETARG_U(i)]);
  348. break;
  349. }
  350. case OP_GETLOCAL: {
  351. setobj(top++, base+GETARG_U(i));
  352. break;
  353. }
  354. case OP_GETGLOBAL: {
  355. luaV_getglobal(L, kstr[GETARG_U(i)], top);
  356. top++;
  357. break;
  358. }
  359. case OP_GETTABLE: {
  360. top--;
  361. luaV_gettable(L, top-1, top, top-1);
  362. break;
  363. }
  364. case OP_GETDOTTED: {
  365. setsvalue(top, kstr[GETARG_U(i)]);
  366. luaV_gettable(L, top-1, top, top-1);
  367. break;
  368. }
  369. case OP_GETINDEXED: {
  370. luaV_gettable(L, top-1, base+GETARG_U(i), top-1);
  371. break;
  372. }
  373. case OP_PUSHSELF: {
  374. setobj(top, top-1);
  375. setsvalue(top+1, kstr[GETARG_U(i)]);
  376. luaV_gettable(L, top-1, top+1, top-1);
  377. top++;
  378. break;
  379. }
  380. case OP_CREATETABLE: {
  381. luaC_checkGC(L);
  382. sethvalue(top, luaH_new(L, GETARG_U(i)));
  383. top++;
  384. break;
  385. }
  386. case OP_SETLOCAL: {
  387. setobj(base+GETARG_U(i), --top);
  388. break;
  389. }
  390. case OP_SETGLOBAL: {
  391. top--;
  392. luaV_setglobal(L, kstr[GETARG_U(i)], top);
  393. break;
  394. }
  395. case OP_SETTABLE: {
  396. StkId t = top-GETARG_A(i);
  397. luaV_settable(L, t, t+1, top-1);
  398. top -= GETARG_B(i); /* pop values */
  399. break;
  400. }
  401. case OP_SETLIST: {
  402. int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
  403. int n = GETARG_B(i);
  404. Hash *arr = hvalue(top-n-1);
  405. for (; n; n--)
  406. setobj(luaH_setnum(L, arr, n+aux), --top);
  407. break;
  408. }
  409. case OP_SETMAP: {
  410. int n = GETARG_U(i);
  411. Hash *arr = hvalue((top-2*n)-1);
  412. for (; n; n--) {
  413. top-=2;
  414. setobj(luaH_set(L, arr, top), top+1);
  415. }
  416. break;
  417. }
  418. case OP_ADD: {
  419. if (tonumber(top-2) || tonumber(top-1))
  420. call_arith(L, top-2, TM_ADD);
  421. else
  422. nvalue(top-2) += nvalue(top-1);
  423. top--;
  424. break;
  425. }
  426. case OP_ADDI: {
  427. if (tonumber(top-1)) {
  428. setnvalue(top, (lua_Number)GETARG_S(i));
  429. call_arith(L, top-1, TM_ADD);
  430. }
  431. else
  432. nvalue(top-1) += (lua_Number)GETARG_S(i);
  433. break;
  434. }
  435. case OP_SUB: {
  436. if (tonumber(top-2) || tonumber(top-1))
  437. call_arith(L, top-2, TM_SUB);
  438. else
  439. nvalue(top-2) -= nvalue(top-1);
  440. top--;
  441. break;
  442. }
  443. case OP_MULT: {
  444. if (tonumber(top-2) || tonumber(top-1))
  445. call_arith(L, top-2, TM_MUL);
  446. else
  447. nvalue(top-2) *= nvalue(top-1);
  448. top--;
  449. break;
  450. }
  451. case OP_DIV: {
  452. if (tonumber(top-2) || tonumber(top-1))
  453. call_arith(L, top-2, TM_DIV);
  454. else
  455. nvalue(top-2) /= nvalue(top-1);
  456. top--;
  457. break;
  458. }
  459. case OP_POW: {
  460. if (!call_binTM(L, top-2, top-1, top-2, TM_POW))
  461. luaD_error(L, l_s("undefined operation"));
  462. top--;
  463. break;
  464. }
  465. case OP_CONCAT: {
  466. int n = GETARG_U(i);
  467. luaV_strconc(L, n, top);
  468. top -= n-1;
  469. luaC_checkGC(L);
  470. break;
  471. }
  472. case OP_MINUS: {
  473. if (tonumber(top-1)) {
  474. setnilvalue(top);
  475. call_arith(L, top-1, TM_UNM);
  476. }
  477. else
  478. nvalue(top-1) = -nvalue(top-1);
  479. break;
  480. }
  481. case OP_NOT: {
  482. ttype(top-1) =
  483. (ttype(top-1) == LUA_TNIL) ? LUA_TNUMBER : LUA_TNIL;
  484. nvalue(top-1) = 1;
  485. break;
  486. }
  487. case OP_JMPNE: {
  488. top -= 2;
  489. if (!luaO_equalObj(top, top+1)) dojump(pc, i);
  490. break;
  491. }
  492. case OP_JMPEQ: {
  493. top -= 2;
  494. if (luaO_equalObj(top, top+1)) dojump(pc, i);
  495. break;
  496. }
  497. case OP_JMPLT: {
  498. top -= 2;
  499. if (luaV_lessthan(L, top, top+1)) dojump(pc, i);
  500. break;
  501. }
  502. case OP_JMPLE: { /* a <= b === !(b<a) */
  503. top -= 2;
  504. if (!luaV_lessthan(L, top+1, top)) dojump(pc, i);
  505. break;
  506. }
  507. case OP_JMPGT: { /* a > b === (b<a) */
  508. top -= 2;
  509. if (luaV_lessthan(L, top+1, top)) dojump(pc, i);
  510. break;
  511. }
  512. case OP_JMPGE: { /* a >= b === !(a<b) */
  513. top -= 2;
  514. if (!luaV_lessthan(L, top, top+1)) dojump(pc, i);
  515. break;
  516. }
  517. case OP_JMPT: {
  518. if (ttype(--top) != LUA_TNIL) dojump(pc, i);
  519. break;
  520. }
  521. case OP_JMPF: {
  522. if (ttype(--top) == LUA_TNIL) dojump(pc, i);
  523. break;
  524. }
  525. case OP_JMPONT: {
  526. if (ttype(top-1) == LUA_TNIL) top--;
  527. else dojump(pc, i);
  528. break;
  529. }
  530. case OP_JMPONF: {
  531. if (ttype(top-1) != LUA_TNIL) top--;
  532. else dojump(pc, i);
  533. break;
  534. }
  535. case OP_JMP: {
  536. dojump(pc, i);
  537. break;
  538. }
  539. case OP_PUSHNILJMP: {
  540. setnilvalue(top++);
  541. pc++;
  542. break;
  543. }
  544. case OP_FORPREP: {
  545. int jmp = GETARG_S(i);
  546. if (tonumber(top-1))
  547. luaD_error(L, l_s("`for' step must be a number"));
  548. if (tonumber(top-2))
  549. luaD_error(L, l_s("`for' limit must be a number"));
  550. if (tonumber(top-3))
  551. luaD_error(L, l_s("`for' initial value must be a number"));
  552. pc += -jmp; /* `jump' to loop end (delta is negated here) */
  553. goto forloop; /* do not increment index */
  554. }
  555. case OP_FORLOOP: {
  556. lua_assert(ttype(top-1) == LUA_TNUMBER);
  557. lua_assert(ttype(top-2) == LUA_TNUMBER);
  558. if (ttype(top-3) != LUA_TNUMBER)
  559. luaD_error(L, l_s("`for' index must be a number"));
  560. nvalue(top-3) += nvalue(top-1); /* increment index */
  561. forloop:
  562. if (nvalue(top-1) > 0 ?
  563. nvalue(top-3) > nvalue(top-2) :
  564. nvalue(top-3) < nvalue(top-2))
  565. top -= 3; /* end loop: remove control variables */
  566. else
  567. dojump(pc, i); /* repeat loop */
  568. break;
  569. }
  570. case OP_LFORPREP: {
  571. int jmp = GETARG_S(i);
  572. if (ttype(top-1) != LUA_TTABLE)
  573. luaD_error(L, l_s("`for' table must be a table"));
  574. top += 3; /* index,key,value */
  575. setnvalue(top-3, -1); /* initial index */
  576. setnilvalue(top-2);
  577. setnilvalue(top-1);
  578. pc += -jmp; /* `jump' to loop end (delta is negated here) */
  579. /* go through */
  580. }
  581. case OP_LFORLOOP: {
  582. Hash *t = hvalue(top-4);
  583. int n = (int)nvalue(top-3);
  584. lua_assert(ttype(top-3) == LUA_TNUMBER);
  585. lua_assert(ttype(top-4) == LUA_TTABLE);
  586. n = luaH_nexti(t, n);
  587. if (n == -1) /* end loop? */
  588. top -= 4; /* remove table, index, key, and value */
  589. else {
  590. Node *node = node(t, n);
  591. setnvalue(top-3, n); /* index */
  592. setkey2obj(top-2, node);
  593. setobj(top-1, val(node));
  594. dojump(pc, i); /* repeat loop */
  595. }
  596. break;
  597. }
  598. case OP_CLOSURE: {
  599. int nup = GETARG_B(i);
  600. luaC_checkGC(L);
  601. L->top = top;
  602. luaV_Lclosure(L, tf->kproto[GETARG_A(i)], nup);
  603. top -= (nup-1);
  604. L->top = base+tf->maxstacksize;
  605. break;
  606. }
  607. }
  608. }
  609. }