lvm.c 18 KB

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