lvm.c 18 KB

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