lvm.c 19 KB

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