lvm.c 19 KB

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