lvm.c 19 KB

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