/script_binding/lua/lua/lvm.c

http://ftk.googlecode.com/ · C · 763 lines · 686 code · 56 blank · 21 comment · 182 complexity · 009df5dea34190295d8b3c79d6910cc3 MD5 · raw file

  1. /*
  2. ** $Id: lvm.c,v 2.63.1.3 2007/12/28 15:32:23 roberto Exp $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #define lvm_c
  10. #define LUA_CORE
  11. #include "lua.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. /* limit for table tag-method chains (to avoid loops) */
  24. #define MAXTAGLOOP 100
  25. const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
  26. lua_Number num;
  27. if (ttisnumber(obj)) return obj;
  28. if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
  29. setnvalue(n, num);
  30. return n;
  31. }
  32. else
  33. return NULL;
  34. }
  35. int luaV_tostring (lua_State *L, StkId obj) {
  36. if (!ttisnumber(obj))
  37. return 0;
  38. else {
  39. char s[LUAI_MAXNUMBER2STR];
  40. lua_Number n = nvalue(obj);
  41. lua_number2str(s, n);
  42. setsvalue2s(L, obj, luaS_new(L, s));
  43. return 1;
  44. }
  45. }
  46. static void traceexec (lua_State *L, const Instruction *pc) {
  47. lu_byte mask = L->hookmask;
  48. const Instruction *oldpc = L->savedpc;
  49. L->savedpc = pc;
  50. if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
  51. resethookcount(L);
  52. luaD_callhook(L, LUA_HOOKCOUNT, -1);
  53. }
  54. if (mask & LUA_MASKLINE) {
  55. Proto *p = ci_func(L->ci)->l.p;
  56. int npc = pcRel(pc, p);
  57. int newline = getline(p, npc);
  58. /* call linehook when enter a new function, when jump back (loop),
  59. or when enter a new line */
  60. if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
  61. luaD_callhook(L, LUA_HOOKLINE, newline);
  62. }
  63. }
  64. static void callTMres (lua_State *L, StkId res, const TValue *f,
  65. const TValue *p1, const TValue *p2) {
  66. ptrdiff_t result = savestack(L, res);
  67. setobj2s(L, L->top, f); /* push function */
  68. setobj2s(L, L->top+1, p1); /* 1st argument */
  69. setobj2s(L, L->top+2, p2); /* 2nd argument */
  70. luaD_checkstack(L, 3);
  71. L->top += 3;
  72. luaD_call(L, L->top - 3, 1);
  73. res = restorestack(L, result);
  74. L->top--;
  75. setobjs2s(L, res, L->top);
  76. }
  77. static void callTM (lua_State *L, const TValue *f, const TValue *p1,
  78. const TValue *p2, const TValue *p3) {
  79. setobj2s(L, L->top, f); /* push function */
  80. setobj2s(L, L->top+1, p1); /* 1st argument */
  81. setobj2s(L, L->top+2, p2); /* 2nd argument */
  82. setobj2s(L, L->top+3, p3); /* 3th argument */
  83. luaD_checkstack(L, 4);
  84. L->top += 4;
  85. luaD_call(L, L->top - 4, 0);
  86. }
  87. void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  88. int loop;
  89. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  90. const TValue *tm;
  91. if (ttistable(t)) { /* `t' is a table? */
  92. Table *h = hvalue(t);
  93. const TValue *res = luaH_get(h, key); /* do a primitive get */
  94. if (!ttisnil(res) || /* result is no nil? */
  95. (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
  96. setobj2s(L, val, res);
  97. return;
  98. }
  99. /* else will try the tag method */
  100. }
  101. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
  102. luaG_typeerror(L, t, "index");
  103. if (ttisfunction(tm)) {
  104. callTMres(L, val, tm, t, key);
  105. return;
  106. }
  107. t = tm; /* else repeat with `tm' */
  108. }
  109. luaG_runerror(L, "loop in gettable");
  110. }
  111. void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
  112. int loop;
  113. for (loop = 0; loop < MAXTAGLOOP; loop++) {
  114. const TValue *tm;
  115. if (ttistable(t)) { /* `t' is a table? */
  116. Table *h = hvalue(t);
  117. TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
  118. if (!ttisnil(oldval) || /* result is no nil? */
  119. (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
  120. setobj2t(L, oldval, val);
  121. luaC_barriert(L, h, val);
  122. return;
  123. }
  124. /* else will try the tag method */
  125. }
  126. else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
  127. luaG_typeerror(L, t, "index");
  128. if (ttisfunction(tm)) {
  129. callTM(L, tm, t, key, val);
  130. return;
  131. }
  132. t = tm; /* else repeat with `tm' */
  133. }
  134. luaG_runerror(L, "loop in settable");
  135. }
  136. static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
  137. StkId res, TMS event) {
  138. const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
  139. if (ttisnil(tm))
  140. tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
  141. if (ttisnil(tm)) return 0;
  142. callTMres(L, res, tm, p1, p2);
  143. return 1;
  144. }
  145. static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
  146. TMS event) {
  147. const TValue *tm1 = fasttm(L, mt1, event);
  148. const TValue *tm2;
  149. if (tm1 == NULL) return NULL; /* no metamethod */
  150. if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
  151. tm2 = fasttm(L, mt2, event);
  152. if (tm2 == NULL) return NULL; /* no metamethod */
  153. if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
  154. return tm1;
  155. return NULL;
  156. }
  157. static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
  158. TMS event) {
  159. const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
  160. const TValue *tm2;
  161. if (ttisnil(tm1)) return -1; /* no metamethod? */
  162. tm2 = luaT_gettmbyobj(L, p2, event);
  163. if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
  164. return -1;
  165. callTMres(L, L->top, tm1, p1, p2);
  166. return !l_isfalse(L->top);
  167. }
  168. static int l_strcmp (const TString *ls, const TString *rs) {
  169. const char *l = getstr(ls);
  170. size_t ll = ls->tsv.len;
  171. const char *r = getstr(rs);
  172. size_t lr = rs->tsv.len;
  173. for (;;) {
  174. int temp = strcoll(l, r);
  175. if (temp != 0) return temp;
  176. else { /* strings are equal up to a `\0' */
  177. size_t len = strlen(l); /* index of first `\0' in both strings */
  178. if (len == lr) /* r is finished? */
  179. return (len == ll) ? 0 : 1;
  180. else if (len == ll) /* l is finished? */
  181. return -1; /* l is smaller than r (because r is not finished) */
  182. /* both strings longer than `len'; go on comparing (after the `\0') */
  183. len++;
  184. l += len; ll -= len; r += len; lr -= len;
  185. }
  186. }
  187. }
  188. int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  189. int res;
  190. if (ttype(l) != ttype(r))
  191. return luaG_ordererror(L, l, r);
  192. else if (ttisnumber(l))
  193. return luai_numlt(nvalue(l), nvalue(r));
  194. else if (ttisstring(l))
  195. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  196. else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
  197. return res;
  198. return luaG_ordererror(L, l, r);
  199. }
  200. static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
  201. int res;
  202. if (ttype(l) != ttype(r))
  203. return luaG_ordererror(L, l, r);
  204. else if (ttisnumber(l))
  205. return luai_numle(nvalue(l), nvalue(r));
  206. else if (ttisstring(l))
  207. return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
  208. else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
  209. return res;
  210. else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
  211. return !res;
  212. return luaG_ordererror(L, l, r);
  213. }
  214. int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
  215. const TValue *tm;
  216. lua_assert(ttype(t1) == ttype(t2));
  217. switch (ttype(t1)) {
  218. case LUA_TNIL: return 1;
  219. case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
  220. case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
  221. case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
  222. case LUA_TUSERDATA: {
  223. if (uvalue(t1) == uvalue(t2)) return 1;
  224. tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
  225. TM_EQ);
  226. break; /* will try TM */
  227. }
  228. case LUA_TTABLE: {
  229. if (hvalue(t1) == hvalue(t2)) return 1;
  230. tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
  231. break; /* will try TM */
  232. }
  233. default: return gcvalue(t1) == gcvalue(t2);
  234. }
  235. if (tm == NULL) return 0; /* no TM? */
  236. callTMres(L, L->top, tm, t1, t2); /* call TM */
  237. return !l_isfalse(L->top);
  238. }
  239. void luaV_concat (lua_State *L, int total, int last) {
  240. do {
  241. StkId top = L->base + last + 1;
  242. int n = 2; /* number of elements handled in this pass (at least 2) */
  243. if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
  244. if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
  245. luaG_concaterror(L, top-2, top-1);
  246. } else if (tsvalue(top-1)->len == 0) /* second op is empty? */
  247. (void)tostring(L, top - 2); /* result is first op (as string) */
  248. else {
  249. /* at least two string values; get as many as possible */
  250. size_t tl = tsvalue(top-1)->len;
  251. char *buffer;
  252. int i;
  253. /* collect total length */
  254. for (n = 1; n < total && tostring(L, top-n-1); n++) {
  255. size_t l = tsvalue(top-n-1)->len;
  256. if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
  257. tl += l;
  258. }
  259. buffer = luaZ_openspace(L, &G(L)->buff, tl);
  260. tl = 0;
  261. for (i=n; i>0; i--) { /* concat all strings */
  262. size_t l = tsvalue(top-i)->len;
  263. memcpy(buffer+tl, svalue(top-i), l);
  264. tl += l;
  265. }
  266. setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
  267. }
  268. total -= n-1; /* got `n' strings to create 1 new */
  269. last -= n-1;
  270. } while (total > 1); /* repeat until only 1 result left */
  271. }
  272. static void Arith (lua_State *L, StkId ra, const TValue *rb,
  273. const TValue *rc, TMS op) {
  274. TValue tempb, tempc;
  275. const TValue *b, *c;
  276. if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
  277. (c = luaV_tonumber(rc, &tempc)) != NULL) {
  278. lua_Number nb = nvalue(b), nc = nvalue(c);
  279. switch (op) {
  280. case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
  281. case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
  282. case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
  283. case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
  284. case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
  285. case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
  286. case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
  287. default: lua_assert(0); break;
  288. }
  289. }
  290. else if (!call_binTM(L, rb, rc, ra, op))
  291. luaG_aritherror(L, rb, rc);
  292. }
  293. /*
  294. ** some macros for common tasks in `luaV_execute'
  295. */
  296. #define runtime_check(L, c) { if (!(c)) break; }
  297. #define RA(i) (base+GETARG_A(i))
  298. /* to be used after possible stack reallocation */
  299. #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
  300. #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
  301. #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
  302. ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
  303. #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
  304. ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
  305. #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
  306. #define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
  307. #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
  308. #define arith_op(op,tm) { \
  309. TValue *rb = RKB(i); \
  310. TValue *rc = RKC(i); \
  311. if (ttisnumber(rb) && ttisnumber(rc)) { \
  312. lua_Number nb = nvalue(rb), nc = nvalue(rc); \
  313. setnvalue(ra, op(nb, nc)); \
  314. } \
  315. else \
  316. Protect(Arith(L, ra, rb, rc, tm)); \
  317. }
  318. void luaV_execute (lua_State *L, int nexeccalls) {
  319. LClosure *cl;
  320. StkId base;
  321. TValue *k;
  322. const Instruction *pc;
  323. reentry: /* entry point */
  324. lua_assert(isLua(L->ci));
  325. pc = L->savedpc;
  326. cl = &clvalue(L->ci->func)->l;
  327. base = L->base;
  328. k = cl->p->k;
  329. /* main loop of interpreter */
  330. for (;;) {
  331. const Instruction i = *pc++;
  332. StkId ra;
  333. if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
  334. (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
  335. traceexec(L, pc);
  336. if (L->status == LUA_YIELD) { /* did hook yield? */
  337. L->savedpc = pc - 1;
  338. return;
  339. }
  340. base = L->base;
  341. }
  342. /* warning!! several calls may realloc the stack and invalidate `ra' */
  343. ra = RA(i);
  344. lua_assert(base == L->base && L->base == L->ci->base);
  345. lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
  346. lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
  347. switch (GET_OPCODE(i)) {
  348. case OP_MOVE: {
  349. setobjs2s(L, ra, RB(i));
  350. continue;
  351. }
  352. case OP_LOADK: {
  353. setobj2s(L, ra, KBx(i));
  354. continue;
  355. }
  356. case OP_LOADBOOL: {
  357. setbvalue(ra, GETARG_B(i));
  358. if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
  359. continue;
  360. }
  361. case OP_LOADNIL: {
  362. TValue *rb = RB(i);
  363. do {
  364. setnilvalue(rb--);
  365. } while (rb >= ra);
  366. continue;
  367. }
  368. case OP_GETUPVAL: {
  369. int b = GETARG_B(i);
  370. setobj2s(L, ra, cl->upvals[b]->v);
  371. continue;
  372. }
  373. case OP_GETGLOBAL: {
  374. TValue g;
  375. TValue *rb = KBx(i);
  376. sethvalue(L, &g, cl->env);
  377. lua_assert(ttisstring(rb));
  378. Protect(luaV_gettable(L, &g, rb, ra));
  379. continue;
  380. }
  381. case OP_GETTABLE: {
  382. Protect(luaV_gettable(L, RB(i), RKC(i), ra));
  383. continue;
  384. }
  385. case OP_SETGLOBAL: {
  386. TValue g;
  387. sethvalue(L, &g, cl->env);
  388. lua_assert(ttisstring(KBx(i)));
  389. Protect(luaV_settable(L, &g, KBx(i), ra));
  390. continue;
  391. }
  392. case OP_SETUPVAL: {
  393. UpVal *uv = cl->upvals[GETARG_B(i)];
  394. setobj(L, uv->v, ra);
  395. luaC_barrier(L, uv, ra);
  396. continue;
  397. }
  398. case OP_SETTABLE: {
  399. Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
  400. continue;
  401. }
  402. case OP_NEWTABLE: {
  403. int b = GETARG_B(i);
  404. int c = GETARG_C(i);
  405. sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
  406. Protect(luaC_checkGC(L));
  407. continue;
  408. }
  409. case OP_SELF: {
  410. StkId rb = RB(i);
  411. setobjs2s(L, ra+1, rb);
  412. Protect(luaV_gettable(L, rb, RKC(i), ra));
  413. continue;
  414. }
  415. case OP_ADD: {
  416. arith_op(luai_numadd, TM_ADD);
  417. continue;
  418. }
  419. case OP_SUB: {
  420. arith_op(luai_numsub, TM_SUB);
  421. continue;
  422. }
  423. case OP_MUL: {
  424. arith_op(luai_nummul, TM_MUL);
  425. continue;
  426. }
  427. case OP_DIV: {
  428. arith_op(luai_numdiv, TM_DIV);
  429. continue;
  430. }
  431. case OP_MOD: {
  432. arith_op(luai_nummod, TM_MOD);
  433. continue;
  434. }
  435. case OP_POW: {
  436. arith_op(luai_numpow, TM_POW);
  437. continue;
  438. }
  439. case OP_UNM: {
  440. TValue *rb = RB(i);
  441. if (ttisnumber(rb)) {
  442. lua_Number nb = nvalue(rb);
  443. setnvalue(ra, luai_numunm(nb));
  444. }
  445. else {
  446. Protect(Arith(L, ra, rb, rb, TM_UNM));
  447. }
  448. continue;
  449. }
  450. case OP_NOT: {
  451. int res = l_isfalse(RB(i)); /* next assignment may change this value */
  452. setbvalue(ra, res);
  453. continue;
  454. }
  455. case OP_LEN: {
  456. const TValue *rb = RB(i);
  457. switch (ttype(rb)) {
  458. case LUA_TTABLE: {
  459. setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
  460. break;
  461. }
  462. case LUA_TSTRING: {
  463. setnvalue(ra, cast_num(tsvalue(rb)->len));
  464. break;
  465. }
  466. default: { /* try metamethod */
  467. Protect(
  468. if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
  469. luaG_typeerror(L, rb, "get length of");
  470. )
  471. }
  472. }
  473. continue;
  474. }
  475. case OP_CONCAT: {
  476. int b = GETARG_B(i);
  477. int c = GETARG_C(i);
  478. Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
  479. setobjs2s(L, RA(i), base+b);
  480. continue;
  481. }
  482. case OP_JMP: {
  483. dojump(L, pc, GETARG_sBx(i));
  484. continue;
  485. }
  486. case OP_EQ: {
  487. TValue *rb = RKB(i);
  488. TValue *rc = RKC(i);
  489. Protect(
  490. if (equalobj(L, rb, rc) == GETARG_A(i))
  491. dojump(L, pc, GETARG_sBx(*pc));
  492. )
  493. pc++;
  494. continue;
  495. }
  496. case OP_LT: {
  497. Protect(
  498. if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
  499. dojump(L, pc, GETARG_sBx(*pc));
  500. )
  501. pc++;
  502. continue;
  503. }
  504. case OP_LE: {
  505. Protect(
  506. if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
  507. dojump(L, pc, GETARG_sBx(*pc));
  508. )
  509. pc++;
  510. continue;
  511. }
  512. case OP_TEST: {
  513. if (l_isfalse(ra) != GETARG_C(i))
  514. dojump(L, pc, GETARG_sBx(*pc));
  515. pc++;
  516. continue;
  517. }
  518. case OP_TESTSET: {
  519. TValue *rb = RB(i);
  520. if (l_isfalse(rb) != GETARG_C(i)) {
  521. setobjs2s(L, ra, rb);
  522. dojump(L, pc, GETARG_sBx(*pc));
  523. }
  524. pc++;
  525. continue;
  526. }
  527. case OP_CALL: {
  528. int b = GETARG_B(i);
  529. int nresults = GETARG_C(i) - 1;
  530. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  531. L->savedpc = pc;
  532. switch (luaD_precall(L, ra, nresults)) {
  533. case PCRLUA: {
  534. nexeccalls++;
  535. goto reentry; /* restart luaV_execute over new Lua function */
  536. }
  537. case PCRC: {
  538. /* it was a C function (`precall' called it); adjust results */
  539. if (nresults >= 0) L->top = L->ci->top;
  540. base = L->base;
  541. continue;
  542. }
  543. default: {
  544. return; /* yield */
  545. }
  546. }
  547. }
  548. case OP_TAILCALL: {
  549. int b = GETARG_B(i);
  550. if (b != 0) L->top = ra+b; /* else previous instruction set top */
  551. L->savedpc = pc;
  552. lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
  553. switch (luaD_precall(L, ra, LUA_MULTRET)) {
  554. case PCRLUA: {
  555. /* tail call: put new frame in place of previous one */
  556. CallInfo *ci = L->ci - 1; /* previous frame */
  557. int aux;
  558. StkId func = ci->func;
  559. StkId pfunc = (ci+1)->func; /* previous function index */
  560. if (L->openupval) luaF_close(L, ci->base);
  561. L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
  562. for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
  563. setobjs2s(L, func+aux, pfunc+aux);
  564. ci->top = L->top = func+aux; /* correct top */
  565. lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
  566. ci->savedpc = L->savedpc;
  567. ci->tailcalls++; /* one more call lost */
  568. L->ci--; /* remove new frame */
  569. goto reentry;
  570. }
  571. case PCRC: { /* it was a C function (`precall' called it) */
  572. base = L->base;
  573. continue;
  574. }
  575. default: {
  576. return; /* yield */
  577. }
  578. }
  579. }
  580. case OP_RETURN: {
  581. int b = GETARG_B(i);
  582. if (b != 0) L->top = ra+b-1;
  583. if (L->openupval) luaF_close(L, base);
  584. L->savedpc = pc;
  585. b = luaD_poscall(L, ra);
  586. if (--nexeccalls == 0) /* was previous function running `here'? */
  587. return; /* no: return */
  588. else { /* yes: continue its execution */
  589. if (b) L->top = L->ci->top;
  590. lua_assert(isLua(L->ci));
  591. lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
  592. goto reentry;
  593. }
  594. }
  595. case OP_FORLOOP: {
  596. lua_Number step = nvalue(ra+2);
  597. lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
  598. lua_Number limit = nvalue(ra+1);
  599. if (luai_numlt(0, step) ? luai_numle(idx, limit)
  600. : luai_numle(limit, idx)) {
  601. dojump(L, pc, GETARG_sBx(i)); /* jump back */
  602. setnvalue(ra, idx); /* update internal index... */
  603. setnvalue(ra+3, idx); /* ...and external index */
  604. }
  605. continue;
  606. }
  607. case OP_FORPREP: {
  608. const TValue *init = ra;
  609. const TValue *plimit = ra+1;
  610. const TValue *pstep = ra+2;
  611. L->savedpc = pc; /* next steps may throw errors */
  612. if (!tonumber(init, ra))
  613. luaG_runerror(L, LUA_QL("for") " initial value must be a number");
  614. else if (!tonumber(plimit, ra+1))
  615. luaG_runerror(L, LUA_QL("for") " limit must be a number");
  616. else if (!tonumber(pstep, ra+2))
  617. luaG_runerror(L, LUA_QL("for") " step must be a number");
  618. setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
  619. dojump(L, pc, GETARG_sBx(i));
  620. continue;
  621. }
  622. case OP_TFORLOOP: {
  623. StkId cb = ra + 3; /* call base */
  624. setobjs2s(L, cb+2, ra+2);
  625. setobjs2s(L, cb+1, ra+1);
  626. setobjs2s(L, cb, ra);
  627. L->top = cb+3; /* func. + 2 args (state and index) */
  628. Protect(luaD_call(L, cb, GETARG_C(i)));
  629. L->top = L->ci->top;
  630. cb = RA(i) + 3; /* previous call may change the stack */
  631. if (!ttisnil(cb)) { /* continue loop? */
  632. setobjs2s(L, cb-1, cb); /* save control variable */
  633. dojump(L, pc, GETARG_sBx(*pc)); /* jump back */
  634. }
  635. pc++;
  636. continue;
  637. }
  638. case OP_SETLIST: {
  639. int n = GETARG_B(i);
  640. int c = GETARG_C(i);
  641. int last;
  642. Table *h;
  643. if (n == 0) {
  644. n = cast_int(L->top - ra) - 1;
  645. L->top = L->ci->top;
  646. }
  647. if (c == 0) c = cast_int(*pc++);
  648. runtime_check(L, ttistable(ra));
  649. h = hvalue(ra);
  650. last = ((c-1)*LFIELDS_PER_FLUSH) + n;
  651. if (last > h->sizearray) /* needs more space? */
  652. luaH_resizearray(L, h, last); /* pre-alloc it at once */
  653. for (; n > 0; n--) {
  654. TValue *val = ra+n;
  655. setobj2t(L, luaH_setnum(L, h, last--), val);
  656. luaC_barriert(L, h, val);
  657. }
  658. continue;
  659. }
  660. case OP_CLOSE: {
  661. luaF_close(L, ra);
  662. continue;
  663. }
  664. case OP_CLOSURE: {
  665. Proto *p;
  666. Closure *ncl;
  667. int nup, j;
  668. p = cl->p->p[GETARG_Bx(i)];
  669. nup = p->nups;
  670. ncl = luaF_newLclosure(L, nup, cl->env);
  671. ncl->l.p = p;
  672. for (j=0; j<nup; j++, pc++) {
  673. if (GET_OPCODE(*pc) == OP_GETUPVAL)
  674. ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
  675. else {
  676. lua_assert(GET_OPCODE(*pc) == OP_MOVE);
  677. ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
  678. }
  679. }
  680. setclvalue(L, ra, ncl);
  681. Protect(luaC_checkGC(L));
  682. continue;
  683. }
  684. case OP_VARARG: {
  685. int b = GETARG_B(i) - 1;
  686. int j;
  687. CallInfo *ci = L->ci;
  688. int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
  689. if (b == LUA_MULTRET) {
  690. Protect(luaD_checkstack(L, n));
  691. ra = RA(i); /* previous call may change the stack */
  692. b = n;
  693. L->top = ra + n;
  694. }
  695. for (j = 0; j < b; j++) {
  696. if (j < n) {
  697. setobjs2s(L, ra + j, ci->base - n + j);
  698. }
  699. else {
  700. setnilvalue(ra + j);
  701. }
  702. }
  703. continue;
  704. }
  705. }
  706. }
  707. }