2
0

lvm.c 18 KB

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