lvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. ** $Id: lvm.c,v 1.176 2001/03/07 18:16:22 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. #define LUA_PRIVATE
  11. #include "lua.h"
  12. #include "lapi.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lfunc.h"
  16. #include "lgc.h"
  17. #include "lobject.h"
  18. #include "lopcodes.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "ltm.h"
  23. #include "lvm.h"
  24. int luaV_tonumber (TObject *obj) {
  25. if (ttype(obj) != LUA_TSTRING)
  26. return 1;
  27. else {
  28. if (!luaO_str2d(svalue(obj), &nvalue(obj)))
  29. return 2;
  30. ttype(obj) = LUA_TNUMBER;
  31. return 0;
  32. }
  33. }
  34. int luaV_tostring (lua_State *L, TObject *obj) {
  35. if (ttype(obj) != LUA_TNUMBER)
  36. return 1;
  37. else {
  38. l_char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  39. lua_number2str(s, nvalue(obj)); /* convert `s' to number */
  40. setsvalue(obj, luaS_new(L, s));
  41. return 0;
  42. }
  43. }
  44. static void traceexec (lua_State *L, lua_Hook linehook) {
  45. CallInfo *ci = L->ci;
  46. int *lineinfo = ci_func(ci)->f.l->lineinfo;
  47. int pc = (*ci->pc - ci_func(ci)->f.l->code) - 1;
  48. int newline;
  49. if (pc == 0) { /* may be first time? */
  50. ci->line = 1;
  51. ci->refi = 0;
  52. ci->lastpc = pc+1; /* make sure it will call linehook */
  53. }
  54. newline = luaG_getline(lineinfo, pc, ci->line, &ci->refi);
  55. /* calls linehook when enters a new line or jumps back (loop) */
  56. if (newline != ci->line || pc <= ci->lastpc) {
  57. ci->line = newline;
  58. luaD_lineHook(L, newline, linehook);
  59. }
  60. ci->lastpc = pc;
  61. }
  62. static Closure *luaV_closure (lua_State *L, int nelems) {
  63. Closure *c = luaF_newclosure(L, nelems);
  64. L->top -= nelems;
  65. while (nelems--)
  66. setobj(&c->upvalue[nelems], L->top+nelems);
  67. setclvalue(L->top, c);
  68. incr_top;
  69. return c;
  70. }
  71. void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) {
  72. Closure *cl = luaV_closure(L, nelems);
  73. cl->f.c = c;
  74. cl->isC = 1;
  75. }
  76. void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
  77. Closure *cl = luaV_closure(L, nelems);
  78. cl->f.l = l;
  79. cl->isC = 0;
  80. }
  81. static void callTM (lua_State *L, const l_char *fmt, ...) {
  82. va_list argp;
  83. StkId base = L->top;
  84. int has_result = 0;
  85. va_start(argp, fmt);
  86. while (*fmt) {
  87. switch (*fmt++) {
  88. case l_c('c'):
  89. setclvalue(L->top, va_arg(argp, Closure *));
  90. break;
  91. case l_c('o'):
  92. setobj(L->top, va_arg(argp, TObject *));
  93. break;
  94. case l_c('s'):
  95. setsvalue(L->top, va_arg(argp, TString *));
  96. break;
  97. case l_c('r'):
  98. has_result = 1;
  99. continue;
  100. }
  101. incr_top;
  102. }
  103. luaD_call(L, base, has_result);
  104. if (has_result) {
  105. L->top--;
  106. setobj(va_arg(argp, TObject *), L->top);
  107. }
  108. va_end(argp);
  109. }
  110. /*
  111. ** Function to index a table.
  112. ** Receives the table at `t' and the key at the `key'.
  113. ** leaves the result at `res'.
  114. */
  115. void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res) {
  116. Closure *tm;
  117. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  118. int tg = hvalue(t)->htag;
  119. if (tg == LUA_TTABLE || /* with default tag? */
  120. (tm = luaT_gettm(G(L), tg, TM_GETTABLE)) == NULL) { /* or no TM? */
  121. const TObject *h = luaH_get(hvalue(t), key); /* do a primitive get */
  122. /* result is no nil or there is no `index' tag method? */
  123. if (ttype(h) != LUA_TNIL || /* no nil? */
  124. ((tm=luaT_gettm(G(L), tg, TM_INDEX)) == NULL)) { /* or no index TM? */
  125. setobj(res, h); /* default get */
  126. return;
  127. }
  128. }
  129. /* else will call the tag method */
  130. }
  131. else { /* not a table; try a `gettable' tag method */
  132. tm = luaT_gettmbyObj(G(L), t, TM_GETTABLE);
  133. if (tm == NULL) /* no tag method? */
  134. luaG_typeerror(L, t, l_s("index"));
  135. }
  136. callTM(L, l_s("coor"), tm, t, key, res);
  137. }
  138. /*
  139. ** Receives table at `t', key at `key' and value at `val'.
  140. */
  141. void luaV_settable (lua_State *L, StkId t, StkId key, StkId val) {
  142. Closure *tm;
  143. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  144. int tg = hvalue(t)->htag;
  145. if (hvalue(t)->htag == LUA_TTABLE || /* with default tag? */
  146. (tm = luaT_gettm(G(L), tg, TM_SETTABLE)) == NULL) { /* or no TM? */
  147. setobj(luaH_set(L, hvalue(t), key), val); /* do a primitive set */
  148. return;
  149. }
  150. /* else will call the tag method */
  151. }
  152. else { /* not a table; try a `settable' tag method */
  153. tm = luaT_gettmbyObj(G(L), t, TM_SETTABLE);
  154. if (tm == NULL) /* no tag method? */
  155. luaG_typeerror(L, t, l_s("index"));
  156. }
  157. callTM(L, l_s("cooo"), tm, t, key, val);
  158. }
  159. void luaV_getglobal (lua_State *L, TString *name, StkId res) {
  160. const TObject *value = luaH_getstr(L->gt, name);
  161. Closure *tm;
  162. if (!HAS_TM_GETGLOBAL(L, ttype(value)) || /* is there a tag method? */
  163. (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) {
  164. setobj(res, value); /* default behavior */
  165. }
  166. else
  167. callTM(L, l_s("csor"), tm, name, value, res);
  168. }
  169. void luaV_setglobal (lua_State *L, TString *name, StkId val) {
  170. TObject *oldvalue = luaH_setstr(L, L->gt, name);
  171. Closure *tm;
  172. if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) || /* no tag methods? */
  173. (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) {
  174. setobj(oldvalue, val); /* raw set */
  175. }
  176. else
  177. callTM(L, l_s("csoo"), tm, name, oldvalue, val);
  178. }
  179. static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
  180. TObject *res, TMS event) {
  181. TString *opname;
  182. Closure *tm = luaT_gettmbyObj(G(L), p1, event); /* try first operand */
  183. if (tm == NULL) {
  184. tm = luaT_gettmbyObj(G(L), p2, event); /* try second operand */
  185. if (tm == NULL) {
  186. tm = luaT_gettm(G(L), 0, event); /* try a `global' method */
  187. if (tm == NULL)
  188. return 0; /* no tag method */
  189. }
  190. }
  191. opname = luaS_new(L, luaT_eventname[event]);
  192. callTM(L, l_s("coosr"), tm, p1, p2, opname, res);
  193. return 1;
  194. }
  195. static void call_arith (lua_State *L, StkId p1, TMS event) {
  196. if (!call_binTM(L, p1, p1+1, p1, event))
  197. luaG_binerror(L, p1, LUA_TNUMBER, l_s("perform arithmetic on"));
  198. }
  199. static int luaV_strlessthan (const TString *ls, const TString *rs) {
  200. const l_char *l = getstr(ls);
  201. size_t ll = ls->len;
  202. const l_char *r = getstr(rs);
  203. size_t lr = rs->len;
  204. for (;;) {
  205. int temp = strcoll(l, r);
  206. if (temp != 0) return (temp < 0);
  207. else { /* strings are equal up to a `\0' */
  208. size_t len = strlen(l); /* index of first `\0' in both strings */
  209. if (len == lr) /* r is finished? */
  210. return 0; /* l is equal or greater than r */
  211. else if (len == ll) /* l is finished? */
  212. return 1; /* l is smaller than r (because r is not finished) */
  213. /* both strings longer than `len'; go on comparing (after the `\0') */
  214. len++;
  215. l += len; ll -= len; r += len; lr -= len;
  216. }
  217. }
  218. }
  219. int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r) {
  220. if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER)
  221. return (nvalue(l) < nvalue(r));
  222. else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING)
  223. return luaV_strlessthan(tsvalue(l), tsvalue(r));
  224. else { /* try TM */
  225. if (!call_binTM(L, l, r, L->top, TM_LT))
  226. luaG_ordererror(L, l, r);
  227. return (ttype(L->top) != LUA_TNIL);
  228. }
  229. }
  230. void luaV_strconc (lua_State *L, int total, StkId top) {
  231. do {
  232. int n = 2; /* number of elements handled in this pass (at least 2) */
  233. if (tostring(L, top-2) || tostring(L, top-1)) {
  234. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  235. luaG_binerror(L, top-2, LUA_TSTRING, l_s("concat"));
  236. }
  237. else if (tsvalue(top-1)->len > 0) { /* if len=0, do nothing */
  238. /* at least two string values; get as many as possible */
  239. lu_mem tl = (lu_mem)tsvalue(top-1)->len + (lu_mem)tsvalue(top-2)->len;
  240. l_char *buffer;
  241. int i;
  242. while (n < total && !tostring(L, top-n-1)) { /* collect total length */
  243. tl += tsvalue(top-n-1)->len;
  244. n++;
  245. }
  246. if (tl > MAX_SIZET) luaD_error(L, l_s("string size overflow"));
  247. buffer = luaO_openspace(L, tl, l_char);
  248. tl = 0;
  249. for (i=n; i>0; i--) { /* concat all strings */
  250. size_t l = tsvalue(top-i)->len;
  251. memcpy(buffer+tl, svalue(top-i), l);
  252. tl += l;
  253. }
  254. setsvalue(top-n, luaS_newlstr(L, buffer, tl));
  255. }
  256. total -= n-1; /* got `n' strings to create 1 new */
  257. top -= n-1;
  258. } while (total > 1); /* repeat until only 1 result left */
  259. }
  260. static void luaV_pack (lua_State *L, StkId firstelem) {
  261. int i;
  262. Hash *htab = luaH_new(L, 0);
  263. TObject *n;
  264. for (i=0; firstelem+i<L->top; i++)
  265. setobj(luaH_setnum(L, htab, i+1), firstelem+i);
  266. /* store counter in field `n' */
  267. n = luaH_setstr(L, htab, luaS_newliteral(L, l_s("n")));
  268. setnvalue(n, i);
  269. L->top = firstelem; /* remove elements from the stack */
  270. sethvalue(L->top, htab);
  271. incr_top;
  272. }
  273. static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
  274. int nvararg = (L->top-base) - nfixargs;
  275. if (nvararg < 0)
  276. luaD_adjusttop(L, base, nfixargs);
  277. luaV_pack(L, base+nfixargs);
  278. }
  279. #define dojump(pc, i) ((pc) += GETARG_S(i))
  280. /*
  281. ** Executes the given Lua function. Parameters are between [base,top).
  282. ** Returns n such that the the results are between [n,top).
  283. */
  284. StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
  285. const Proto *const tf = cl->f.l;
  286. StkId top; /* keep top local, for performance */
  287. const Instruction *pc = tf->code;
  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, tf->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, tf->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, tf->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, tf->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, tf->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. }