lvm.c 18 KB

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