lvm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*
  2. ** $Id: lvm.c,v 1.237 2002/06/12 14:51:31 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "lua.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lgc.h"
  14. #include "lobject.h"
  15. #include "lopcodes.h"
  16. #include "lstate.h"
  17. #include "lstring.h"
  18. #include "ltable.h"
  19. #include "ltm.h"
  20. #include "lvm.h"
  21. /* function to convert a lua_Number to a string */
  22. #ifndef lua_number2str
  23. #include <stdio.h>
  24. #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
  25. #endif
  26. /* limit for table tag-method chains (to avoid loops) */
  27. #define MAXTAGLOOP 10000
  28. static void luaV_checkGC (lua_State *L, StkId top) {
  29. if (G(L)->nblocks >= G(L)->GCthreshold) {
  30. L->top = top; /* limit for active registers */
  31. luaC_collectgarbage(L);
  32. L->top = L->ci->top; /* restore old top position */
  33. }
  34. }
  35. const TObject *luaV_tonumber (const TObject *obj, TObject *n) {
  36. lua_Number num;
  37. if (ttype(obj) == LUA_TNUMBER) return obj;
  38. if (ttype(obj) == LUA_TSTRING && luaO_str2d(svalue(obj), &num)) {
  39. setnvalue(n, num);
  40. return n;
  41. }
  42. else
  43. return NULL;
  44. }
  45. int luaV_tostring (lua_State *L, TObject *obj) {
  46. if (ttype(obj) != LUA_TNUMBER)
  47. return 0;
  48. else {
  49. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  50. lua_number2str(s, nvalue(obj));
  51. setsvalue(obj, luaS_new(L, s));
  52. return 1;
  53. }
  54. }
  55. static void traceexec (lua_State *L) {
  56. CallInfo *ci = L->ci;
  57. Proto *p = ci_func(ci)->l.p;
  58. int newline = getline(p, pcRel(*ci->pc, p));
  59. if (pcRel(*ci->pc, p) == 0) /* tracing may be starting now? */
  60. ci->savedpc = *ci->pc; /* initialize `savedpc' */
  61. /* calls linehook when enters a new line or jumps back (loop) */
  62. if (*ci->pc <= ci->savedpc || newline != getline(p, pcRel(ci->savedpc, p))) {
  63. luaD_lineHook(L, newline);
  64. ci = L->ci; /* previous call may reallocate `ci' */
  65. }
  66. ci->savedpc = *ci->pc;
  67. }
  68. static void callTMres (lua_State *L, const TObject *f,
  69. const TObject *p1, const TObject *p2, TObject *result ) {
  70. ptrdiff_t res = savestack(L, result);
  71. setobj(L->top, f); /* push function */
  72. setobj(L->top+1, p1); /* 1st argument */
  73. setobj(L->top+2, p2); /* 2nd argument */
  74. luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */
  75. L->top += 3;
  76. luaD_call(L, L->top - 3, 1);
  77. result = restorestack(L, res); /* previous call may change stack */
  78. setobj(result, --L->top); /* get result */
  79. }
  80. static void callTM (lua_State *L, const TObject *f,
  81. const TObject *p1, const TObject *p2, const TObject *p3) {
  82. setobj(L->top, f); /* push function */
  83. setobj(L->top+1, p1); /* 1st argument */
  84. setobj(L->top+2, p2); /* 2nd argument */
  85. setobj(L->top+3, p3); /* 3th argument */
  86. luaD_checkstack(L, 4); /* cannot check before (could invalidate p1...p3) */
  87. L->top += 4;
  88. luaD_call(L, L->top - 4, 0);
  89. }
  90. /*
  91. ** Function to index a table.
  92. ** Receives the table at `t' and the key at `key'.
  93. ** leaves the result at `res'.
  94. */
  95. void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res) {
  96. const TObject *tm;
  97. int loop = 0;
  98. init:
  99. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  100. Table *h = hvalue(t);
  101. Table *et = h->metatable;
  102. if ((tm = fasttm(L, et, TM_GETTABLE)) == NULL) { /* no gettable TM? */
  103. const TObject *v = luaH_get(h, key); /* do a primitive get */
  104. if (ttype(v) != LUA_TNIL || /* result is no nil ... */
  105. (tm = fasttm(L, et, TM_INDEX)) == NULL) { /* ... or no index TM? */
  106. setobj(res, v); /* default get */
  107. return;
  108. }
  109. }
  110. /* else will try the tag method */
  111. } else { /* not a table; try a `gettable' tag method */
  112. if (ttype(tm = luaT_gettmbyobj(L, t, TM_GETTABLE)) == LUA_TNIL) {
  113. luaG_typeerror(L, t, "index");
  114. return; /* to avoid warnings */
  115. }
  116. }
  117. if (ttype(tm) == LUA_TFUNCTION)
  118. callTMres(L, tm, t, key, res);
  119. else {
  120. if (++loop == MAXTAGLOOP) luaG_runerror(L, "loop in gettable");
  121. t = tm;
  122. goto init; /* return luaV_gettable(L, tm, key, res); */
  123. }
  124. }
  125. /*
  126. ** Receives table at `t', key at `key' and value at `val'.
  127. */
  128. void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) {
  129. const TObject *tm;
  130. int loop = 0;
  131. init:
  132. if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */
  133. Table *h = hvalue(t);
  134. Table *et = h->metatable;
  135. if ((tm = fasttm(L, et, TM_SETTABLE)) == NULL) { /* no settable TM? */
  136. TObject *oldval = luaH_set(L, h, key); /* do a primitive set */
  137. if (ttype(oldval) != LUA_TNIL || /* result is no nil ... */
  138. (tm = fasttm(L, et, TM_NEWINDEX)) == NULL) { /* ... or no TM? */
  139. setobj(oldval, val);
  140. return;
  141. }
  142. }
  143. /* else will try the tag method */
  144. } else { /* `t' is not a table; try a `settable' tag method */
  145. if (ttype(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)) == LUA_TNIL) {
  146. luaG_typeerror(L, t, "index");
  147. return; /* to avoid warnings */
  148. }
  149. }
  150. if (ttype(tm) == LUA_TFUNCTION)
  151. callTM(L, tm, t, key, val);
  152. else {
  153. if (++loop == MAXTAGLOOP) luaG_runerror(L, "loop in settable");
  154. t = tm;
  155. goto init; /* luaV_settable(L, tm, key, val); */
  156. }
  157. }
  158. static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2,
  159. TObject *res, TMS event) {
  160. const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  161. if (ttype(tm) == LUA_TNIL)
  162. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  163. if (ttype(tm) != LUA_TFUNCTION) return 0;
  164. callTMres(L, tm, p1, p2, res);
  165. return 1;
  166. }
  167. static void call_arith (lua_State *L, StkId p1, const TObject *p2,
  168. StkId res, TMS event) {
  169. if (!call_binTM(L, p1, p2, res, event))
  170. luaG_aritherror(L, p1, p2);
  171. }
  172. static int luaV_strcmp (const TString *ls, const TString *rs) {
  173. const char *l = getstr(ls);
  174. size_t ll = ls->tsv.len;
  175. const char *r = getstr(rs);
  176. size_t lr = rs->tsv.len;
  177. for (;;) {
  178. int temp = strcoll(l, r);
  179. if (temp != 0) return temp;
  180. else { /* strings are equal up to a `\0' */
  181. size_t len = strlen(l); /* index of first `\0' in both strings */
  182. if (len == lr) /* r is finished? */
  183. return (len == ll) ? 0 : 1;
  184. else if (len == ll) /* l is finished? */
  185. return -1; /* l is smaller than r (because r is not finished) */
  186. /* both strings longer than `len'; go on comparing (after the `\0') */
  187. len++;
  188. l += len; ll -= len; r += len; lr -= len;
  189. }
  190. }
  191. }
  192. int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r) {
  193. if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER)
  194. return nvalue(l) < nvalue(r);
  195. else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING)
  196. return luaV_strcmp(tsvalue(l), tsvalue(r)) < 0;
  197. else { /* try TM */
  198. if (!call_binTM(L, l, r, L->top, TM_LT))
  199. luaG_ordererror(L, l, r);
  200. return !l_isfalse(L->top);
  201. }
  202. }
  203. static int luaV_lessequal (lua_State *L, const TObject *l, const TObject *r) {
  204. if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER)
  205. return nvalue(l) <= nvalue(r);
  206. else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING)
  207. return luaV_strcmp(tsvalue(l), tsvalue(r)) <= 0;
  208. else { /* try TM */
  209. if (call_binTM(L, l, r, L->top, TM_LE)) /* first try `le' */
  210. return !l_isfalse(L->top);
  211. else if (!call_binTM(L, r, l, L->top, TM_LT)) /* else try `lt' */
  212. luaG_ordererror(L, l, r);
  213. return l_isfalse(L->top);
  214. }
  215. }
  216. int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2) {
  217. const TObject *tm = NULL;
  218. lua_assert(ttype(t1) == ttype(t2));
  219. switch (ttype(t1)) {
  220. case LUA_TNIL: return 1;
  221. case LUA_TNUMBER: return nvalue(t1) == nvalue(t2);
  222. case LUA_TSTRING: return tsvalue(t1) == tsvalue(t2);
  223. case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  224. case LUA_TUDATAVAL: return pvalue(t1) == pvalue(t2);
  225. case LUA_TFUNCTION: return clvalue(t1) == clvalue(t2);
  226. case LUA_TUSERDATA:
  227. if (uvalue(t1) == uvalue(t2)) return 1;
  228. else if ((tm = fasttm(L, uvalue(t1)->uv.metatable, TM_EQ)) == NULL &&
  229. (tm = fasttm(L, uvalue(t2)->uv.metatable, TM_EQ)) == NULL)
  230. return 0; /* no TM */
  231. else break; /* will try TM */
  232. case LUA_TTABLE:
  233. if (hvalue(t1) == hvalue(t2)) return 1;
  234. else if ((tm = fasttm(L, hvalue(t1)->metatable, TM_EQ)) == NULL &&
  235. (tm = fasttm(L, hvalue(t2)->metatable, TM_EQ)) == NULL)
  236. return 0; /* no TM */
  237. else break; /* will try TM */
  238. }
  239. callTMres(L, tm, t1, t2, L->top); /* call TM */
  240. return !l_isfalse(L->top);
  241. }
  242. void luaV_concat (lua_State *L, int total, int last) {
  243. do {
  244. StkId top = L->ci->base + last + 1;
  245. int n = 2; /* number of elements handled in this pass (at least 2) */
  246. if (!tostring(L, top-2) || !tostring(L, top-1)) {
  247. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  248. luaG_concaterror(L, top-2, top-1);
  249. } else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */
  250. /* at least two string values; get as many as possible */
  251. lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) +
  252. cast(lu_mem, tsvalue(top-2)->tsv.len);
  253. char *buffer;
  254. int i;
  255. while (n < total && tostring(L, top-n-1)) { /* collect total length */
  256. tl += tsvalue(top-n-1)->tsv.len;
  257. n++;
  258. }
  259. if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow");
  260. buffer = luaO_openspace(L, tl, char);
  261. tl = 0;
  262. for (i=n; i>0; i--) { /* concat all strings */
  263. size_t l = tsvalue(top-i)->tsv.len;
  264. memcpy(buffer+tl, svalue(top-i), l);
  265. tl += l;
  266. }
  267. setsvalue(top-n, luaS_newlstr(L, buffer, tl));
  268. }
  269. total -= n-1; /* got `n' strings to create 1 new */
  270. last -= n-1;
  271. } while (total > 1); /* repeat until only 1 result left */
  272. }
  273. static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) {
  274. const TObject *b = rb;
  275. const TObject *c = rc;
  276. TObject tempb, tempc;
  277. if (tonumber(b, &tempb) && tonumber(c, &tempc)) {
  278. TObject f, o;
  279. setsvalue(&o, luaS_newliteral(L, "pow"));
  280. luaV_gettable(L, gt(L), &o, &f);
  281. if (ttype(&f) != LUA_TFUNCTION)
  282. luaG_runerror(L, "`pow' (for `^' operator) is not a function");
  283. callTMres(L, &f, b, c, ra);
  284. }
  285. else
  286. call_arith(L, rb, rc, ra, TM_POW);
  287. }
  288. /*
  289. ** some macros for common tasks in `luaV_execute'
  290. */
  291. #define runtime_check(L, c) { if (!(c)) return 0; }
  292. #define RA(i) (base+GETARG_A(i))
  293. #define RB(i) (base+GETARG_B(i))
  294. #define RC(i) (base+GETARG_C(i))
  295. #define RKC(i) ((GETARG_C(i) < MAXSTACK) ? \
  296. base+GETARG_C(i) : \
  297. k+GETARG_C(i)-MAXSTACK)
  298. #define KBx(i) (k+GETARG_Bx(i))
  299. #define Arith(op, optm) { \
  300. const TObject *b = RB(i); const TObject *c = RKC(i); \
  301. TObject tempb, tempc; \
  302. if (tonumber(b, &tempb) && tonumber(c, &tempc)) { \
  303. setnvalue(ra, nvalue(b) op nvalue(c)); \
  304. } else \
  305. call_arith(L, RB(i), RKC(i), ra, optm); \
  306. }
  307. #define dojump(pc, i) ((pc) += (i))
  308. StkId luaV_execute (lua_State *L) {
  309. StkId base;
  310. LClosure *cl;
  311. TObject *k;
  312. const Instruction *pc;
  313. callentry: /* entry point when calling new functions */
  314. L->ci->pc = &pc;
  315. L->ci->pb = &base;
  316. pc = L->ci->savedpc;
  317. retentry: /* entry point when returning to old functions */
  318. base = L->ci->base;
  319. cl = &clvalue(base - 1)->l;
  320. k = cl->p->k;
  321. /* main loop of interpreter */
  322. for (;;) {
  323. const Instruction i = *pc++;
  324. StkId ra;
  325. if (L->linehook)
  326. traceexec(L);
  327. ra = RA(i);
  328. lua_assert(L->top <= L->stack + L->stacksize && L->top >= L->ci->base);
  329. lua_assert(L->top == L->ci->top ||
  330. GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||
  331. GET_OPCODE(i) == OP_RETURN || GET_OPCODE(i) == OP_SETLISTO);
  332. switch (GET_OPCODE(i)) {
  333. case OP_MOVE: {
  334. setobj(ra, RB(i));
  335. break;
  336. }
  337. case OP_LOADK: {
  338. setobj(ra, KBx(i));
  339. break;
  340. }
  341. case OP_LOADBOOL: {
  342. setbvalue(ra, GETARG_B(i));
  343. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  344. break;
  345. }
  346. case OP_LOADNIL: {
  347. TObject *rb = RB(i);
  348. do {
  349. setnilvalue(rb--);
  350. } while (rb >= ra);
  351. break;
  352. }
  353. case OP_GETUPVAL: {
  354. int b = GETARG_B(i);
  355. setobj(ra, cl->upvals[b]->v);
  356. break;
  357. }
  358. case OP_GETGLOBAL: {
  359. lua_assert(ttype(KBx(i)) == LUA_TSTRING);
  360. luaV_gettable(L, gt(L), KBx(i), ra);
  361. break;
  362. }
  363. case OP_GETTABLE: {
  364. luaV_gettable(L, RB(i), RKC(i), ra);
  365. break;
  366. }
  367. case OP_SETGLOBAL: {
  368. lua_assert(ttype(KBx(i)) == LUA_TSTRING);
  369. luaV_settable(L, gt(L), KBx(i), ra);
  370. break;
  371. }
  372. case OP_SETUPVAL: {
  373. int b = GETARG_B(i);
  374. setobj(cl->upvals[b]->v, ra);
  375. break;
  376. }
  377. case OP_SETTABLE: {
  378. luaV_settable(L, RB(i), RKC(i), ra);
  379. break;
  380. }
  381. case OP_NEWTABLE: {
  382. int b = GETARG_B(i);
  383. if (b > 0) b = twoto(b-1);
  384. sethvalue(ra, luaH_new(L, b, GETARG_C(i)));
  385. luaV_checkGC(L, ra+1);
  386. break;
  387. }
  388. case OP_SELF: {
  389. StkId rb = RB(i);
  390. setobj(ra+1, rb);
  391. luaV_gettable(L, rb, RKC(i), ra);
  392. break;
  393. }
  394. case OP_ADD: {
  395. Arith( + , TM_ADD);
  396. break;
  397. }
  398. case OP_SUB: {
  399. Arith( - , TM_SUB);
  400. break;
  401. }
  402. case OP_MUL: {
  403. Arith( * , TM_MUL);
  404. break;
  405. }
  406. case OP_DIV: {
  407. Arith( / , TM_DIV);
  408. break;
  409. }
  410. case OP_POW: {
  411. powOp(L, ra, RB(i), RKC(i));
  412. break;
  413. }
  414. case OP_UNM: {
  415. const TObject *rb = RB(i);
  416. if (tonumber(rb, ra)) {
  417. setnvalue(ra, -nvalue(rb));
  418. }
  419. else {
  420. TObject temp;
  421. setnilvalue(&temp);
  422. call_arith(L, RB(i), &temp, ra, TM_UNM);
  423. }
  424. break;
  425. }
  426. case OP_NOT: {
  427. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  428. setbvalue(ra, res);
  429. break;
  430. }
  431. case OP_CONCAT: {
  432. int b = GETARG_B(i);
  433. int c = GETARG_C(i);
  434. luaV_concat(L, c-b+1, c); /* may change `base' (and `ra') */
  435. setobj(base+GETARG_A(i), base+b);
  436. luaV_checkGC(L, base+c+1);
  437. break;
  438. }
  439. case OP_JMP: {
  440. dojump(pc, GETARG_sBx(i));
  441. break;
  442. }
  443. case OP_EQ: { /* skip next instruction if test fails */
  444. if (equalobj(L, ra, RKC(i)) != GETARG_B(i)) pc++;
  445. else dojump(pc, GETARG_sBx(*pc) + 1);
  446. break;
  447. }
  448. case OP_LT: {
  449. if (luaV_lessthan(L, ra, RKC(i)) != GETARG_B(i)) pc++;
  450. else dojump(pc, GETARG_sBx(*pc) + 1);
  451. break;
  452. }
  453. case OP_LE: {
  454. if (luaV_lessequal(L, ra, RKC(i)) != GETARG_B(i)) pc++;
  455. else dojump(pc, GETARG_sBx(*pc) + 1);
  456. break;
  457. }
  458. case OP_GT: {
  459. if (luaV_lessthan(L, RKC(i), ra) != GETARG_B(i)) pc++;
  460. else dojump(pc, GETARG_sBx(*pc) + 1);
  461. break;
  462. }
  463. case OP_GE: {
  464. if (luaV_lessequal(L, RKC(i), ra) != GETARG_B(i)) pc++;
  465. else dojump(pc, GETARG_sBx(*pc) + 1);
  466. break;
  467. }
  468. case OP_TEST: {
  469. StkId rc = RKC(i);
  470. if (l_isfalse(rc) == GETARG_B(i)) pc++;
  471. else {
  472. setobj(ra, rc);
  473. dojump(pc, GETARG_sBx(*pc) + 1);
  474. }
  475. break;
  476. }
  477. case OP_CALL: {
  478. StkId firstResult;
  479. int b = GETARG_B(i);
  480. int nresults;
  481. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  482. nresults = GETARG_C(i) - 1;
  483. firstResult = luaD_precall(L, ra);
  484. if (firstResult) {
  485. if (firstResult > L->top) { /* yield? */
  486. (L->ci-1)->savedpc = pc;
  487. return NULL;
  488. }
  489. /* it was a C function (`precall' called it); adjust results */
  490. luaD_poscall(L, nresults, firstResult);
  491. if (nresults >= 0) L->top = L->ci->top;
  492. }
  493. else { /* it is a Lua function: `call' it */
  494. (L->ci-1)->savedpc = pc;
  495. goto callentry;
  496. }
  497. break;
  498. }
  499. case OP_TAILCALL: {
  500. int b = GETARG_B(i);
  501. if (L->openupval) luaF_close(L, base);
  502. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  503. luaD_poscall(L, LUA_MULTRET, ra); /* move down function and args. */
  504. ra = luaD_precall(L, base-1);
  505. if (ra == NULL) goto callentry; /* it is a Lua function */
  506. else if (ra > L->top) return NULL; /* yield??? */
  507. else goto ret;
  508. }
  509. case OP_RETURN: {
  510. int b;
  511. if (L->openupval) luaF_close(L, base);
  512. b = GETARG_B(i);
  513. if (b != 0) L->top = ra+b-1;
  514. lua_assert(L->ci->pc == &pc);
  515. }
  516. ret: {
  517. CallInfo *ci;
  518. ci = L->ci - 1;
  519. if (ci->pc != &pc) /* previous function was running `here'? */
  520. return ra; /* no: return */
  521. else { /* yes: continue its execution */
  522. int nresults;
  523. lua_assert(ttype(ci->base-1) == LUA_TFUNCTION);
  524. pc = ci->savedpc;
  525. lua_assert(GET_OPCODE(*(pc-1)) == OP_CALL);
  526. nresults = GETARG_C(*(pc-1)) - 1;
  527. luaD_poscall(L, nresults, ra);
  528. if (nresults >= 0) L->top = L->ci->top;
  529. goto retentry;
  530. }
  531. }
  532. case OP_FORLOOP: {
  533. lua_Number step, index, limit;
  534. const TObject *plimit = ra+1;
  535. const TObject *pstep = ra+2;
  536. if (ttype(ra) != LUA_TNUMBER)
  537. luaG_runerror(L, "`for' initial value must be a number");
  538. if (!tonumber(plimit, ra+1))
  539. luaG_runerror(L, "`for' limit must be a number");
  540. if (!tonumber(pstep, ra+2))
  541. luaG_runerror(L, "`for' step must be a number");
  542. step = nvalue(pstep);
  543. index = nvalue(ra) + step; /* increment index */
  544. limit = nvalue(plimit);
  545. if (step > 0 ? index <= limit : index >= limit) {
  546. dojump(pc, GETARG_sBx(i)); /* jump back */
  547. chgnvalue(ra, index); /* update index */
  548. }
  549. break;
  550. }
  551. case OP_TFORLOOP: {
  552. setobj(ra+4, ra+2);
  553. setobj(ra+3, ra+1);
  554. setobj(ra+2, ra);
  555. L->top = ra+5;
  556. luaD_call(L, ra+2, GETARG_C(i) + 1);
  557. L->top = L->ci->top;
  558. if (ttype(ra+2) == LUA_TNIL) pc++; /* skip jump (break loop) */
  559. else dojump(pc, GETARG_sBx(*pc) + 1); /* else jump back */
  560. break;
  561. }
  562. case OP_TFORPREP: {
  563. if (ttype(ra) == LUA_TTABLE) {
  564. setobj(ra+1, ra);
  565. setsvalue(ra, luaS_new(L, "next"));
  566. luaV_gettable(L, gt(L), ra, ra);
  567. }
  568. dojump(pc, GETARG_sBx(i));
  569. break;
  570. }
  571. case OP_SETLIST:
  572. case OP_SETLISTO: {
  573. int bc;
  574. int n;
  575. Table *h;
  576. runtime_check(L, ttype(ra) == LUA_TTABLE);
  577. h = hvalue(ra);
  578. bc = GETARG_Bx(i);
  579. if (GET_OPCODE(i) == OP_SETLIST)
  580. n = (bc&(LFIELDS_PER_FLUSH-1)) + 1;
  581. else {
  582. n = L->top - ra - 1;
  583. L->top = L->ci->top;
  584. }
  585. bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */
  586. for (; n > 0; n--)
  587. setobj(luaH_setnum(L, h, bc+n), ra+n);
  588. break;
  589. }
  590. case OP_CLOSE: {
  591. luaF_close(L, ra);
  592. break;
  593. }
  594. case OP_CLOSURE: {
  595. Proto *p;
  596. Closure *ncl;
  597. int nup, j;
  598. p = cl->p->p[GETARG_Bx(i)];
  599. nup = p->nupvalues;
  600. ncl = luaF_newLclosure(L, nup);
  601. ncl->l.p = p;
  602. for (j=0; j<nup; j++, pc++) {
  603. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  604. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  605. else {
  606. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  607. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  608. }
  609. }
  610. setclvalue(ra, ncl);
  611. luaV_checkGC(L, L->top);
  612. break;
  613. }
  614. }
  615. }
  616. }