lvm.c 18 KB

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