2
0

lvm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. ** $Id: lvm.c,v 1.135 2000/09/11 17:38:42 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. #define dojump(pc, i) { int d = GETARG_S(i); pc += d; }
  294. /*
  295. ** Executes the given Lua function. Parameters are between [base,top).
  296. ** Returns n such that the the results are between [n,top).
  297. */
  298. StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
  299. const Proto *tf = cl->f.l;
  300. StkId top; /* keep top local, for performance */
  301. const Instruction *pc = tf->code;
  302. TString **kstr = tf->kstr;
  303. lua_Hook linehook = L->linehook;
  304. infovalue(base-1)->pc = &pc;
  305. luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
  306. if (tf->is_vararg) { /* varargs? */
  307. adjust_varargs(L, base, tf->numparams);
  308. luaC_checkGC(L);
  309. }
  310. else
  311. luaD_adjusttop(L, base, tf->numparams);
  312. top = L->top;
  313. /* main loop of interpreter */
  314. for (;;) {
  315. const Instruction i = *pc++;
  316. if (linehook)
  317. traceexec(L, base, top, linehook);
  318. switch (GET_OPCODE(i)) {
  319. case OP_END: {
  320. return L->top; /* no results */
  321. }
  322. case OP_RETURN: {
  323. L->top = top;
  324. return base+GETARG_U(i);
  325. }
  326. case OP_CALL: {
  327. int nres = GETARG_B(i);
  328. if (nres == MULT_RET) nres = LUA_MULTRET;
  329. L->top = top;
  330. luaD_call(L, base+GETARG_A(i), nres);
  331. top = L->top;
  332. break;
  333. }
  334. case OP_TAILCALL: {
  335. L->top = top;
  336. luaD_call(L, base+GETARG_A(i), LUA_MULTRET);
  337. return base+GETARG_B(i);
  338. }
  339. case OP_PUSHNIL: {
  340. int n = GETARG_U(i);
  341. LUA_ASSERT(n>0, "invalid argument");
  342. do {
  343. ttype(top++) = TAG_NIL;
  344. } while (--n > 0);
  345. break;
  346. }
  347. case OP_POP: {
  348. top -= GETARG_U(i);
  349. break;
  350. }
  351. case OP_PUSHINT: {
  352. ttype(top) = TAG_NUMBER;
  353. nvalue(top) = (Number)GETARG_S(i);
  354. top++;
  355. break;
  356. }
  357. case OP_PUSHSTRING: {
  358. ttype(top) = TAG_STRING;
  359. tsvalue(top) = kstr[GETARG_U(i)];
  360. top++;
  361. break;
  362. }
  363. case OP_PUSHNUM: {
  364. ttype(top) = TAG_NUMBER;
  365. nvalue(top) = tf->knum[GETARG_U(i)];
  366. top++;
  367. break;
  368. }
  369. case OP_PUSHNEGNUM: {
  370. ttype(top) = TAG_NUMBER;
  371. nvalue(top) = -tf->knum[GETARG_U(i)];
  372. top++;
  373. break;
  374. }
  375. case OP_PUSHUPVALUE: {
  376. *top++ = cl->upvalue[GETARG_U(i)];
  377. break;
  378. }
  379. case OP_GETLOCAL: {
  380. *top++ = *(base+GETARG_U(i));
  381. break;
  382. }
  383. case OP_GETGLOBAL: {
  384. L->top = top;
  385. *top = *luaV_getglobal(L, kstr[GETARG_U(i)]);
  386. top++;
  387. break;
  388. }
  389. case OP_GETTABLE: {
  390. L->top = top;
  391. top--;
  392. *(top-1) = *luaV_gettable(L, top-1);
  393. break;
  394. }
  395. case OP_GETDOTTED: {
  396. ttype(top) = TAG_STRING;
  397. tsvalue(top) = kstr[GETARG_U(i)];
  398. L->top = top+1;
  399. *(top-1) = *luaV_gettable(L, top-1);
  400. break;
  401. }
  402. case OP_GETINDEXED: {
  403. *top = *(base+GETARG_U(i));
  404. L->top = top+1;
  405. *(top-1) = *luaV_gettable(L, top-1);
  406. break;
  407. }
  408. case OP_PUSHSELF: {
  409. TObject receiver;
  410. receiver = *(top-1);
  411. ttype(top) = TAG_STRING;
  412. tsvalue(top++) = kstr[GETARG_U(i)];
  413. L->top = top;
  414. *(top-2) = *luaV_gettable(L, top-2);
  415. *(top-1) = receiver;
  416. break;
  417. }
  418. case OP_CREATETABLE: {
  419. L->top = top;
  420. luaC_checkGC(L);
  421. hvalue(top) = luaH_new(L, GETARG_U(i));
  422. ttype(top) = TAG_TABLE;
  423. top++;
  424. break;
  425. }
  426. case OP_SETLOCAL: {
  427. *(base+GETARG_U(i)) = *(--top);
  428. break;
  429. }
  430. case OP_SETGLOBAL: {
  431. L->top = top;
  432. luaV_setglobal(L, kstr[GETARG_U(i)]);
  433. top--;
  434. break;
  435. }
  436. case OP_SETTABLE: {
  437. StkId t = top-GETARG_A(i);
  438. L->top = top;
  439. luaV_settable(L, t, t+1);
  440. top -= GETARG_B(i); /* pop values */
  441. break;
  442. }
  443. case OP_SETLIST: {
  444. int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
  445. int n = GETARG_B(i);
  446. Hash *arr = hvalue(top-n-1);
  447. L->top = top-n; /* final value of `top' (in case of errors) */
  448. for (; n; n--)
  449. *luaH_setint(L, arr, n+aux) = *(--top);
  450. break;
  451. }
  452. case OP_SETMAP: {
  453. int n = GETARG_U(i);
  454. StkId finaltop = top-2*n;
  455. Hash *arr = hvalue(finaltop-1);
  456. L->top = finaltop; /* final value of `top' (in case of errors) */
  457. for (; n; n--) {
  458. top-=2;
  459. *luaH_set(L, arr, top) = *(top+1);
  460. }
  461. break;
  462. }
  463. case OP_ADD: {
  464. if (tonumber(top-2) || tonumber(top-1))
  465. call_arith(L, top, IM_ADD);
  466. else
  467. nvalue(top-2) += nvalue(top-1);
  468. top--;
  469. break;
  470. }
  471. case OP_ADDI: {
  472. if (tonumber(top-1)) {
  473. ttype(top) = TAG_NUMBER;
  474. nvalue(top) = (Number)GETARG_S(i);
  475. call_arith(L, top+1, IM_ADD);
  476. }
  477. else
  478. nvalue(top-1) += (Number)GETARG_S(i);
  479. break;
  480. }
  481. case OP_SUB: {
  482. if (tonumber(top-2) || tonumber(top-1))
  483. call_arith(L, top, IM_SUB);
  484. else
  485. nvalue(top-2) -= nvalue(top-1);
  486. top--;
  487. break;
  488. }
  489. case OP_MULT: {
  490. if (tonumber(top-2) || tonumber(top-1))
  491. call_arith(L, top, IM_MUL);
  492. else
  493. nvalue(top-2) *= nvalue(top-1);
  494. top--;
  495. break;
  496. }
  497. case OP_DIV: {
  498. if (tonumber(top-2) || tonumber(top-1))
  499. call_arith(L, top, IM_DIV);
  500. else
  501. nvalue(top-2) /= nvalue(top-1);
  502. top--;
  503. break;
  504. }
  505. case OP_POW: {
  506. if (!call_binTM(L, top, IM_POW))
  507. lua_error(L, "undefined operation");
  508. top--;
  509. break;
  510. }
  511. case OP_CONCAT: {
  512. int n = GETARG_U(i);
  513. luaV_strconc(L, n, top);
  514. top -= n-1;
  515. L->top = top;
  516. luaC_checkGC(L);
  517. break;
  518. }
  519. case OP_MINUS: {
  520. if (tonumber(top-1)) {
  521. ttype(top) = TAG_NIL;
  522. call_arith(L, top+1, IM_UNM);
  523. }
  524. else
  525. nvalue(top-1) = -nvalue(top-1);
  526. break;
  527. }
  528. case OP_NOT: {
  529. ttype(top-1) =
  530. (ttype(top-1) == TAG_NIL) ? TAG_NUMBER : TAG_NIL;
  531. nvalue(top-1) = 1;
  532. break;
  533. }
  534. case OP_JMPNE: {
  535. top -= 2;
  536. if (!luaO_equalObj(top, top+1)) dojump(pc, i);
  537. break;
  538. }
  539. case OP_JMPEQ: {
  540. top -= 2;
  541. if (luaO_equalObj(top, top+1)) dojump(pc, i);
  542. break;
  543. }
  544. case OP_JMPLT: {
  545. top -= 2;
  546. if (luaV_lessthan(L, top, top+1, top+2)) dojump(pc, i);
  547. break;
  548. }
  549. case OP_JMPLE: { /* a <= b === !(b<a) */
  550. top -= 2;
  551. if (!luaV_lessthan(L, top+1, top, top+2)) dojump(pc, i);
  552. break;
  553. }
  554. case OP_JMPGT: { /* a > b === (b<a) */
  555. top -= 2;
  556. if (luaV_lessthan(L, top+1, top, top+2)) dojump(pc, i);
  557. break;
  558. }
  559. case OP_JMPGE: { /* a >= b === !(a<b) */
  560. top -= 2;
  561. if (!luaV_lessthan(L, top, top+1, top+2)) dojump(pc, i);
  562. break;
  563. }
  564. case OP_JMPT: {
  565. if (ttype(--top) != TAG_NIL) dojump(pc, i);
  566. break;
  567. }
  568. case OP_JMPF: {
  569. if (ttype(--top) == TAG_NIL) dojump(pc, i);
  570. break;
  571. }
  572. case OP_JMPONT: {
  573. if (ttype(top-1) == TAG_NIL) top--;
  574. else dojump(pc, i);
  575. break;
  576. }
  577. case OP_JMPONF: {
  578. if (ttype(top-1) != TAG_NIL) top--;
  579. else dojump(pc, i);
  580. break;
  581. }
  582. case OP_JMP: {
  583. dojump(pc, i);
  584. break;
  585. }
  586. case OP_PUSHNILJMP: {
  587. ttype(top++) = TAG_NIL;
  588. pc++;
  589. break;
  590. }
  591. case OP_FORPREP: {
  592. if (tonumber(top-1))
  593. lua_error(L, "`for' step must be a number");
  594. if (tonumber(top-2))
  595. lua_error(L, "`for' limit must be a number");
  596. if (tonumber(top-3))
  597. lua_error(L, "`for' initial value must be a number");
  598. if (nvalue(top-1) > 0 ?
  599. nvalue(top-3) > nvalue(top-2) :
  600. nvalue(top-3) < nvalue(top-2)) { /* `empty' loop? */
  601. top -= 3; /* remove control variables */
  602. dojump(pc, i); /* jump to loop end */
  603. }
  604. break;
  605. }
  606. case OP_FORLOOP: {
  607. LUA_ASSERT(ttype(top-1) == TAG_NUMBER, "invalid step");
  608. LUA_ASSERT(ttype(top-2) == TAG_NUMBER, "invalid limit");
  609. if (ttype(top-3) != TAG_NUMBER)
  610. lua_error(L, "`for' index must be a number");
  611. nvalue(top-3) += nvalue(top-1); /* increment index */
  612. if (nvalue(top-1) > 0 ?
  613. nvalue(top-3) > nvalue(top-2) :
  614. nvalue(top-3) < nvalue(top-2))
  615. top -= 3; /* end loop: remove control variables */
  616. else
  617. dojump(pc, i); /* repeat loop */
  618. break;
  619. }
  620. case OP_LFORPREP: {
  621. Node *node;
  622. if (ttype(top-1) != TAG_TABLE)
  623. lua_error(L, "`for' table must be a table");
  624. node = luaH_next(L, hvalue(top-1), &luaO_nilobject);
  625. if (node == NULL) { /* `empty' loop? */
  626. top--; /* remove table */
  627. dojump(pc, i); /* jump to loop end */
  628. }
  629. else {
  630. top += 2; /* index,value */
  631. *(top-2) = *key(node);
  632. *(top-1) = *val(node);
  633. }
  634. break;
  635. }
  636. case OP_LFORLOOP: {
  637. Node *node;
  638. LUA_ASSERT(ttype(top-3) == TAG_TABLE, "invalid table");
  639. node = luaH_next(L, hvalue(top-3), top-2);
  640. if (node == NULL) /* end loop? */
  641. top -= 3; /* remove table, key, and value */
  642. else {
  643. *(top-2) = *key(node);
  644. *(top-1) = *val(node);
  645. dojump(pc, i); /* repeat loop */
  646. }
  647. break;
  648. }
  649. case OP_CLOSURE: {
  650. L->top = top;
  651. luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i));
  652. top = L->top;
  653. luaC_checkGC(L);
  654. break;
  655. }
  656. }
  657. }
  658. }