lvm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. ** $Id: lvm.c,v 1.126 2000/08/11 16:17:28 roberto Exp roberto $
  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. #include "lua.h"
  10. #include "lapi.h"
  11. #include "lauxlib.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. #ifdef OLD_ANSI
  24. #define strcoll(a,b) strcmp(a,b)
  25. #endif
  26. /*
  27. ** Extra stack size to run a function:
  28. ** TAG_LINE(1), NAME(1), TM calls(3) (plus some extra...)
  29. */
  30. #define EXTRA_STACK 8
  31. int luaV_tonumber (TObject *obj) { /* LUA_NUMBER */
  32. if (ttype(obj) != TAG_STRING)
  33. return 1;
  34. else {
  35. if (!luaO_str2d(svalue(obj), &nvalue(obj)))
  36. return 2;
  37. ttype(obj) = TAG_NUMBER;
  38. return 0;
  39. }
  40. }
  41. int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
  42. if (ttype(obj) != TAG_NUMBER)
  43. return 1;
  44. else {
  45. char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
  46. sprintf(s, "%.16g", (double)nvalue(obj));
  47. tsvalue(obj) = luaS_new(L, s);
  48. ttype(obj) = TAG_STRING;
  49. return 0;
  50. }
  51. }
  52. static void traceexec (lua_State *L, StkId base, StkId top, lua_Hook linehook) {
  53. CallInfo *ci = infovalue(base-1);
  54. int *lineinfo = ci->func->f.l->lineinfo;
  55. int pc = (*ci->pc - 1) - ci->func->f.l->code;
  56. int newline;
  57. if (ci->line == 0) { /* first time? */
  58. ci->line = 1;
  59. ci->refi = 0;
  60. ci->lastpc = pc+1; /* make sure it will call linehook */
  61. }
  62. newline = luaG_getline(lineinfo, pc, ci->line, &ci->refi);
  63. /* calls linehook when enters a new line or jumps back (loop) */
  64. if (newline != ci->line || pc <= ci->lastpc) {
  65. ci->line = newline;
  66. L->top = top;
  67. luaD_lineHook(L, base-2, newline, linehook);
  68. }
  69. ci->lastpc = pc;
  70. }
  71. static Closure *luaV_closure (lua_State *L, lua_Type t, int nelems) {
  72. Closure *c = luaF_newclosure(L, nelems);
  73. L->top -= nelems;
  74. while (nelems--)
  75. c->upvalue[nelems] = *(L->top+nelems);
  76. ttype(L->top) = t;
  77. clvalue(L->top) = c;
  78. incr_top;
  79. return c;
  80. }
  81. void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) {
  82. Closure *cl = luaV_closure(L, TAG_CCLOSURE, nelems);
  83. cl->f.c = c;
  84. }
  85. void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
  86. Closure *cl = luaV_closure(L, TAG_LCLOSURE, nelems);
  87. cl->f.l = l;
  88. }
  89. /*
  90. ** Function to index a table.
  91. ** Receives the table at top-2 and the index at top-1.
  92. */
  93. void luaV_gettable (lua_State *L, StkId top) {
  94. StkId table = top-2;
  95. const TObject *im;
  96. if (ttype(table) != TAG_TABLE) { /* not a table, get gettable TM */
  97. im = luaT_getimbyObj(L, table, IM_GETTABLE);
  98. if (ttype(im) == TAG_NIL) {
  99. L->top = top;
  100. luaG_typeerror(L, table, "index");
  101. }
  102. }
  103. else { /* object is a table... */
  104. int tg = hvalue(table)->htag;
  105. im = luaT_getim(L, tg, IM_GETTABLE);
  106. if (ttype(im) == TAG_NIL) { /* and does not have a `gettable' TM */
  107. const TObject *h = luaH_get(L, hvalue(table), table+1);
  108. if (ttype(h) == TAG_NIL &&
  109. (ttype(im=luaT_getim(L, tg, IM_INDEX)) != TAG_NIL)) {
  110. /* result is nil and there is an `index' tag method */
  111. L->top = top;
  112. luaD_callTM(L, im, 2, 1); /* calls it */
  113. }
  114. else
  115. *table = *h; /* `push' result into table position */
  116. return;
  117. }
  118. /* else it has a `gettable' TM, go through to next command */
  119. }
  120. /* object is not a table, or it has a `gettable' TM */
  121. L->top = top;
  122. luaD_callTM(L, im, 2, 1);
  123. }
  124. /*
  125. ** Receives table at *t, index at *(t+1) and value at `top'.
  126. */
  127. void luaV_settable (lua_State *L, StkId t, StkId top) {
  128. const TObject *im;
  129. if (ttype(t) != TAG_TABLE) { /* not a table, get `settable' method */
  130. L->top = top;
  131. im = luaT_getimbyObj(L, t, IM_SETTABLE);
  132. if (ttype(im) == TAG_NIL)
  133. luaG_typeerror(L, t, "index");
  134. }
  135. else { /* object is a table... */
  136. im = luaT_getim(L, hvalue(t)->htag, IM_SETTABLE);
  137. if (ttype(im) == TAG_NIL) { /* and does not have a `settable' method */
  138. *luaH_set(L, hvalue(t), t+1) = *(top-1);
  139. return;
  140. }
  141. /* else it has a `settable' method, go through to next command */
  142. }
  143. /* object is not a table, or it has a `settable' method */
  144. /* prepare arguments and call the tag method */
  145. luaD_checkstack(L, 3);
  146. *(top+2) = *(top-1);
  147. *(top+1) = *(t+1);
  148. *(top) = *t;
  149. *(top-1) = *im;
  150. L->top = top+3;
  151. luaD_call(L, top-1, 0);
  152. }
  153. void luaV_getglobal (lua_State *L, TString *s, StkId top) {
  154. const TObject *value = luaH_getstr(L->gt, s);
  155. TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
  156. if (ttype(im) == TAG_NIL) /* is there a tag method? */
  157. *top = *value; /* default behavior */
  158. else { /* tag method */
  159. luaD_checkstack(L, 3);
  160. *top = *im;
  161. ttype(top+1) = TAG_STRING;
  162. tsvalue(top+1) = s; /* global name */
  163. *(top+2) = *value;
  164. L->top = top+3;
  165. luaD_call(L, top, 1);
  166. }
  167. }
  168. void luaV_setglobal (lua_State *L, TString *s, StkId top) {
  169. const TObject *oldvalue = luaH_getstr(L->gt, s);
  170. const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
  171. if (ttype(im) == TAG_NIL) { /* is there a tag method? */
  172. if (oldvalue != &luaO_nilobject) {
  173. /* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */
  174. *(TObject *)oldvalue = *(top-1);
  175. }
  176. else {
  177. TObject key;
  178. ttype(&key) = TAG_STRING;
  179. tsvalue(&key) = s;
  180. *luaH_set(L, L->gt, &key) = *(top-1);
  181. }
  182. }
  183. else {
  184. luaD_checkstack(L, 3);
  185. *(top+2) = *(top-1); /* new value */
  186. *(top+1) = *oldvalue;
  187. ttype(top) = TAG_STRING;
  188. tsvalue(top) = s;
  189. *(top-1) = *im;
  190. L->top = top+3;
  191. luaD_call(L, top-1, 0);
  192. }
  193. }
  194. static int call_binTM (lua_State *L, StkId top, IMS event) {
  195. /* try first operand */
  196. const TObject *im = luaT_getimbyObj(L, top-2, event);
  197. L->top = top;
  198. if (ttype(im) == TAG_NIL) {
  199. im = luaT_getimbyObj(L, top-1, event); /* try second operand */
  200. if (ttype(im) == TAG_NIL) {
  201. im = luaT_getim(L, 0, event); /* try a `global' method */
  202. if (ttype(im) == TAG_NIL)
  203. return 0; /* error */
  204. }
  205. }
  206. lua_pushstring(L, luaT_eventname[event]);
  207. luaD_callTM(L, im, 3, 1);
  208. return 1;
  209. }
  210. static void call_arith (lua_State *L, StkId top, IMS event) {
  211. if (!call_binTM(L, top, event))
  212. luaG_binerror(L, top-2, TAG_NUMBER, "perform arithmetic on");
  213. }
  214. static int luaV_strcomp (const TString *ls, const TString *rs) {
  215. const char *l = ls->str;
  216. size_t ll = ls->u.s.len;
  217. const char *r = rs->str;
  218. size_t lr = rs->u.s.len;
  219. for (;;) {
  220. int temp = strcoll(l, r);
  221. if (temp != 0) return temp;
  222. else { /* strings are equal up to a '\0' */
  223. size_t len = strlen(l); /* index of first '\0' in both strings */
  224. if (len == ll) /* l is finished? */
  225. return (len == lr) ? 0 : -1; /* l is equal or smaller than r */
  226. else if (len == lr) /* r is finished? */
  227. return 1; /* l is greater than r (because l is not finished) */
  228. /* both strings longer than `len'; go on comparing (after the '\0') */
  229. len++;
  230. l += len; ll -= len; r += len; lr -= len;
  231. }
  232. }
  233. }
  234. int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) {
  235. if (ttype(l) == TAG_NUMBER && ttype(r) == TAG_NUMBER)
  236. return (nvalue(l) < nvalue(r));
  237. else if (ttype(l) == TAG_STRING && ttype(r) == TAG_STRING)
  238. return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
  239. else { /* call TM */
  240. luaD_checkstack(L, 2);
  241. *top++ = *l;
  242. *top++ = *r;
  243. if (!call_binTM(L, top, IM_LT))
  244. luaG_ordererror(L, top-2);
  245. L->top--;
  246. return (ttype(L->top) != TAG_NIL);
  247. }
  248. }
  249. static void strconc (lua_State *L, int total, StkId top) {
  250. do {
  251. int n = 2; /* number of elements handled in this pass (at least 2) */
  252. if (tostring(L, top-2) || tostring(L, top-1)) {
  253. if (!call_binTM(L, top, IM_CONCAT))
  254. luaG_binerror(L, top-2, TAG_STRING, "concat");
  255. }
  256. else if (tsvalue(top-1)->u.s.len > 0) { /* if len=0, do nothing */
  257. /* at least two string values; get as many as possible */
  258. lint32 tl = (lint32)tsvalue(top-1)->u.s.len +
  259. (lint32)tsvalue(top-2)->u.s.len;
  260. char *buffer;
  261. int i;
  262. while (n < total && !tostring(L, top-n-1)) { /* collect total length */
  263. tl += tsvalue(top-n-1)->u.s.len;
  264. n++;
  265. }
  266. if (tl > MAX_SIZET) lua_error(L, "string size overflow");
  267. buffer = luaL_openspace(L, tl);
  268. tl = 0;
  269. for (i=n; i>0; i--) { /* concat all strings */
  270. size_t l = tsvalue(top-i)->u.s.len;
  271. memcpy(buffer+tl, tsvalue(top-i)->str, l);
  272. tl += l;
  273. }
  274. tsvalue(top-n) = luaS_newlstr(L, buffer, tl);
  275. }
  276. total -= n-1; /* got `n' strings to create 1 new */
  277. top -= n-1;
  278. } while (total > 1); /* repeat until only 1 result left */
  279. }
  280. void luaV_pack (lua_State *L, StkId firstelem, int nvararg, TObject *tab) {
  281. int i;
  282. Hash *htab;
  283. htab = hvalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */
  284. ttype(tab) = TAG_TABLE;
  285. for (i=0; i<nvararg; i++)
  286. *luaH_setint(L, htab, i+1) = *(firstelem+i);
  287. /* store counter in field `n' */
  288. luaH_setstrnum(L, htab, luaS_new(L, "n"), nvararg);
  289. }
  290. static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
  291. TObject arg;
  292. int nvararg = (L->top-base) - nfixargs;
  293. if (nvararg < 0) {
  294. luaV_pack(L, base, 0, &arg);
  295. luaD_adjusttop(L, base, nfixargs);
  296. }
  297. else {
  298. luaV_pack(L, base+nfixargs, nvararg, &arg);
  299. L->top = base+nfixargs;
  300. }
  301. *L->top++ = arg;
  302. }
  303. /*
  304. ** Executes the given Lua function. Parameters are between [base,top).
  305. ** Returns n such that the the results are between [n,top).
  306. */
  307. StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
  308. const Proto *tf = cl->f.l;
  309. StkId top; /* keep top local, for performance */
  310. const Instruction *pc = tf->code;
  311. TString **kstr = tf->kstr;
  312. lua_Hook linehook = L->linehook;
  313. infovalue(base-1)->pc = &pc;
  314. luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
  315. if (tf->is_vararg) { /* varargs? */
  316. adjust_varargs(L, base, tf->numparams);
  317. luaC_checkGC(L);
  318. }
  319. else
  320. luaD_adjusttop(L, base, tf->numparams);
  321. top = L->top;
  322. /* main loop of interpreter */
  323. for (;;) {
  324. const Instruction i = *pc++;
  325. if (linehook)
  326. traceexec(L, base, top, linehook);
  327. switch (GET_OPCODE(i)) {
  328. case OP_END: {
  329. return L->top; /* no results */
  330. }
  331. case OP_RETURN: {
  332. L->top = top;
  333. return base+GETARG_U(i);
  334. }
  335. case OP_CALL: {
  336. L->top = top;
  337. luaD_call(L, base+GETARG_A(i), GETARG_B(i));
  338. top = L->top;
  339. break;
  340. }
  341. case OP_TAILCALL: {
  342. L->top = top;
  343. luaD_call(L, base+GETARG_A(i), MULT_RET);
  344. return base+GETARG_B(i);
  345. }
  346. case OP_PUSHNIL: {
  347. int n = GETARG_U(i);
  348. LUA_ASSERT(n>0, "invalid argument");
  349. do {
  350. ttype(top++) = TAG_NIL;
  351. } while (--n > 0);
  352. break;
  353. }
  354. case OP_POP: {
  355. top -= GETARG_U(i);
  356. break;
  357. }
  358. case OP_PUSHINT: {
  359. ttype(top) = TAG_NUMBER;
  360. nvalue(top) = (Number)GETARG_S(i);
  361. top++;
  362. break;
  363. }
  364. case OP_PUSHSTRING: {
  365. ttype(top) = TAG_STRING;
  366. tsvalue(top) = kstr[GETARG_U(i)];
  367. top++;
  368. break;
  369. }
  370. case OP_PUSHNUM: {
  371. ttype(top) = TAG_NUMBER;
  372. nvalue(top) = tf->knum[GETARG_U(i)];
  373. top++;
  374. break;
  375. }
  376. case OP_PUSHNEGNUM: {
  377. ttype(top) = TAG_NUMBER;
  378. nvalue(top) = -tf->knum[GETARG_U(i)];
  379. top++;
  380. break;
  381. }
  382. case OP_PUSHUPVALUE: {
  383. *top++ = cl->upvalue[GETARG_U(i)];
  384. break;
  385. }
  386. case OP_GETLOCAL: {
  387. *top++ = *(base+GETARG_U(i));
  388. break;
  389. }
  390. case OP_GETGLOBAL: {
  391. luaV_getglobal(L, kstr[GETARG_U(i)], top);
  392. top++;
  393. break;
  394. }
  395. case OP_GETTABLE: {
  396. luaV_gettable(L, top);
  397. top--;
  398. break;
  399. }
  400. case OP_GETDOTTED: {
  401. ttype(top) = TAG_STRING;
  402. tsvalue(top++) = kstr[GETARG_U(i)];
  403. luaV_gettable(L, top);
  404. top--;
  405. break;
  406. }
  407. case OP_GETINDEXED: {
  408. *top++ = *(base+GETARG_U(i));
  409. luaV_gettable(L, top);
  410. top--;
  411. break;
  412. }
  413. case OP_PUSHSELF: {
  414. TObject receiver;
  415. receiver = *(top-1);
  416. ttype(top) = TAG_STRING;
  417. tsvalue(top++) = kstr[GETARG_U(i)];
  418. luaV_gettable(L, top);
  419. *(top-1) = receiver;
  420. break;
  421. }
  422. case OP_CREATETABLE: {
  423. L->top = top;
  424. luaC_checkGC(L);
  425. hvalue(top) = luaH_new(L, GETARG_U(i));
  426. ttype(top) = TAG_TABLE;
  427. top++;
  428. break;
  429. }
  430. case OP_SETLOCAL: {
  431. *(base+GETARG_U(i)) = *(--top);
  432. break;
  433. }
  434. case OP_SETGLOBAL: {
  435. luaV_setglobal(L, kstr[GETARG_U(i)], top);
  436. top--;
  437. break;
  438. }
  439. case OP_SETTABLE: {
  440. luaV_settable(L, top-GETARG_A(i), top);
  441. top -= GETARG_B(i); /* pop values */
  442. break;
  443. }
  444. case OP_SETLIST: {
  445. int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
  446. int n = GETARG_B(i);
  447. Hash *arr = hvalue(top-n-1);
  448. L->top = top-n; /* final value of `top' (in case of errors) */
  449. for (; n; n--)
  450. *luaH_setint(L, arr, n+aux) = *(--top);
  451. break;
  452. }
  453. case OP_SETMAP: {
  454. int n = GETARG_U(i);
  455. StkId finaltop = top-2*n;
  456. Hash *arr = hvalue(finaltop-1);
  457. L->top = finaltop; /* final value of `top' (in case of errors) */
  458. for (; n; n--) {
  459. top-=2;
  460. *luaH_set(L, arr, top) = *(top+1);
  461. }
  462. break;
  463. }
  464. case OP_ADD: {
  465. if (tonumber(top-2) || tonumber(top-1))
  466. call_arith(L, top, IM_ADD);
  467. else
  468. nvalue(top-2) += nvalue(top-1);
  469. top--;
  470. break;
  471. }
  472. case OP_ADDI: {
  473. if (tonumber(top-1)) {
  474. ttype(top) = TAG_NUMBER;
  475. nvalue(top) = (Number)GETARG_S(i);
  476. call_arith(L, top+1, IM_ADD);
  477. }
  478. else
  479. nvalue(top-1) += (Number)GETARG_S(i);
  480. break;
  481. }
  482. case OP_SUB: {
  483. if (tonumber(top-2) || tonumber(top-1))
  484. call_arith(L, top, IM_SUB);
  485. else
  486. nvalue(top-2) -= nvalue(top-1);
  487. top--;
  488. break;
  489. }
  490. case OP_MULT: {
  491. if (tonumber(top-2) || tonumber(top-1))
  492. call_arith(L, top, IM_MUL);
  493. else
  494. nvalue(top-2) *= nvalue(top-1);
  495. top--;
  496. break;
  497. }
  498. case OP_DIV: {
  499. if (tonumber(top-2) || tonumber(top-1))
  500. call_arith(L, top, IM_DIV);
  501. else
  502. nvalue(top-2) /= nvalue(top-1);
  503. top--;
  504. break;
  505. }
  506. case OP_POW: {
  507. if (!call_binTM(L, top, IM_POW))
  508. lua_error(L, "undefined operation");
  509. top--;
  510. break;
  511. }
  512. case OP_CONCAT: {
  513. int n = GETARG_U(i);
  514. strconc(L, n, top);
  515. top -= n-1;
  516. L->top = top;
  517. luaC_checkGC(L);
  518. break;
  519. }
  520. case OP_MINUS: {
  521. if (tonumber(top-1)) {
  522. ttype(top) = TAG_NIL;
  523. call_arith(L, top+1, IM_UNM);
  524. }
  525. else
  526. nvalue(top-1) = -nvalue(top-1);
  527. break;
  528. }
  529. case OP_NOT: {
  530. ttype(top-1) =
  531. (ttype(top-1) == TAG_NIL) ? TAG_NUMBER : TAG_NIL;
  532. nvalue(top-1) = 1;
  533. break;
  534. }
  535. case OP_JMPNE: {
  536. top -= 2;
  537. if (!luaO_equalObj(top, top+1)) pc += GETARG_S(i);
  538. break;
  539. }
  540. case OP_JMPEQ: {
  541. top -= 2;
  542. if (luaO_equalObj(top, top+1)) pc += GETARG_S(i);
  543. break;
  544. }
  545. case OP_JMPLT: {
  546. top -= 2;
  547. if (luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
  548. break;
  549. }
  550. case OP_JMPLE: { /* a <= b === !(b<a) */
  551. top -= 2;
  552. if (!luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
  553. break;
  554. }
  555. case OP_JMPGT: { /* a > b === (b<a) */
  556. top -= 2;
  557. if (luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
  558. break;
  559. }
  560. case OP_JMPGE: { /* a >= b === !(a<b) */
  561. top -= 2;
  562. if (!luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
  563. break;
  564. }
  565. case OP_JMPT: {
  566. if (ttype(--top) != TAG_NIL) pc += GETARG_S(i);
  567. break;
  568. }
  569. case OP_JMPF: {
  570. if (ttype(--top) == TAG_NIL) pc += GETARG_S(i);
  571. break;
  572. }
  573. case OP_JMPONT: {
  574. if (ttype(top-1) != TAG_NIL) pc += GETARG_S(i);
  575. else top--;
  576. break;
  577. }
  578. case OP_JMPONF: {
  579. if (ttype(top-1) == TAG_NIL) pc += GETARG_S(i);
  580. else top--;
  581. break;
  582. }
  583. case OP_JMP: {
  584. pc += GETARG_S(i);
  585. break;
  586. }
  587. case OP_PUSHNILJMP: {
  588. ttype(top++) = TAG_NIL;
  589. pc++;
  590. break;
  591. }
  592. case OP_FORPREP: {
  593. if (tonumber(top-1))
  594. lua_error(L, "`for' step must be a number");
  595. if (tonumber(top-2))
  596. lua_error(L, "`for' limit must be a number");
  597. if (tonumber(top-3))
  598. lua_error(L, "`for' initial value must be a number");
  599. if (nvalue(top-1) > 0 ?
  600. nvalue(top-3) > nvalue(top-2) :
  601. nvalue(top-3) < nvalue(top-2)) { /* `empty' loop? */
  602. top -= 3; /* remove control variables */
  603. pc += GETARG_S(i)+1; /* jump to loop end */
  604. }
  605. break;
  606. }
  607. case OP_FORLOOP: {
  608. LUA_ASSERT(ttype(top-1) == TAG_NUMBER, "invalid step");
  609. LUA_ASSERT(ttype(top-2) == TAG_NUMBER, "invalid limit");
  610. if (ttype(top-3) != TAG_NUMBER)
  611. lua_error(L, "`for' index must be a number");
  612. nvalue(top-3) += nvalue(top-1); /* increment index */
  613. if (nvalue(top-1) > 0 ?
  614. nvalue(top-3) > nvalue(top-2) :
  615. nvalue(top-3) < nvalue(top-2))
  616. top -= 3; /* end loop: remove control variables */
  617. else
  618. pc += GETARG_S(i); /* repeat loop */
  619. break;
  620. }
  621. case OP_LFORPREP: {
  622. if (ttype(top-1) != TAG_TABLE)
  623. lua_error(L, "`for' table must be a table");
  624. top++; /* counter */
  625. L->top = top;
  626. ttype(top-1) = TAG_NUMBER;
  627. nvalue(top-1) = (Number)luaA_next(L, hvalue(top-2), 0); /* counter */
  628. if (nvalue(top-1) == 0) { /* `empty' loop? */
  629. top -= 2; /* remove table and counter */
  630. pc += GETARG_S(i)+1; /* jump to loop end */
  631. }
  632. else {
  633. top += 2; /* index,value */
  634. LUA_ASSERT(top==L->top, "bad top");
  635. }
  636. break;
  637. }
  638. case OP_LFORLOOP: {
  639. int n;
  640. top -= 2; /* remove old index,value */
  641. LUA_ASSERT(ttype(top-2) == TAG_TABLE, "invalid table");
  642. LUA_ASSERT(ttype(top-1) == TAG_NUMBER, "invalid counter");
  643. L->top = top;
  644. n = luaA_next(L, hvalue(top-2), (int)nvalue(top-1));
  645. if (n == 0) /* end loop? */
  646. top -= 2; /* remove table and counter */
  647. else {
  648. nvalue(top-1) = (Number)n;
  649. top += 2; /* new index,value */
  650. pc += GETARG_S(i); /* repeat loop */
  651. }
  652. break;
  653. }
  654. case OP_CLOSURE: {
  655. L->top = top;
  656. luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i));
  657. top = L->top;
  658. luaC_checkGC(L);
  659. break;
  660. }
  661. }
  662. }
  663. }