lvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. ** $Id: lvm.c,v 1.35 1998/12/30 13:16:50 roberto Exp roberto $
  3. ** Lua virtual machine
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <limits.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "lauxlib.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lgc.h"
  15. #include "lmem.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 "luadebug.h"
  23. #include "lvm.h"
  24. #ifdef OLD_ANSI
  25. #define strcoll(a,b) strcmp(a,b)
  26. #endif
  27. #define skip_word(pc) (pc+=2)
  28. #define get_word(pc) ((*(pc)<<8)+(*((pc)+1)))
  29. #define next_word(pc) (pc+=2, get_word(pc-2))
  30. /* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */
  31. #define EXTRA_STACK 5
  32. static TaggedString *strconc (TaggedString *l, TaggedString *r) {
  33. long nl = l->u.s.len;
  34. long nr = r->u.s.len;
  35. char *buffer = luaL_openspace(nl+nr);
  36. memcpy(buffer, l->str, nl);
  37. memcpy(buffer+nl, r->str, nr);
  38. return luaS_newlstr(buffer, nl+nr);
  39. }
  40. int luaV_tonumber (TObject *obj) {
  41. /* LUA_NUMBER */
  42. if (ttype(obj) != LUA_T_STRING)
  43. return 1;
  44. else {
  45. double t;
  46. char *e = svalue(obj);
  47. int sig = 1;
  48. while (isspace((unsigned char)*e)) e++;
  49. if (*e == '+') e++;
  50. else if (*e == '-') {
  51. e++;
  52. sig = -1;
  53. }
  54. t = luaO_str2d(e);
  55. if (t<0) return 2;
  56. nvalue(obj) = (real)t*sig;
  57. ttype(obj) = LUA_T_NUMBER;
  58. return 0;
  59. }
  60. }
  61. int luaV_tostring (TObject *obj) {
  62. /* LUA_NUMBER */
  63. if (ttype(obj) != LUA_T_NUMBER)
  64. return 1;
  65. else {
  66. char s[60];
  67. real f = nvalue(obj);
  68. long i;
  69. if ((real)LONG_MIN <= f && f <= (real)LONG_MAX && (real)(i=(long)f) == f)
  70. sprintf(s, "%ld", i);
  71. else
  72. sprintf(s, "%g", (double)nvalue(obj));
  73. tsvalue(obj) = luaS_new(s);
  74. ttype(obj) = LUA_T_STRING;
  75. return 0;
  76. }
  77. }
  78. void luaV_setn (Hash *t, int val) {
  79. TObject index, value;
  80. ttype(&index) = LUA_T_STRING; tsvalue(&index) = luaS_new("n");
  81. ttype(&value) = LUA_T_NUMBER; nvalue(&value) = val;
  82. *(luaH_set(t, &index)) = value;
  83. }
  84. void luaV_closure (int nelems)
  85. {
  86. if (nelems > 0) {
  87. struct Stack *S = &L->stack;
  88. Closure *c = luaF_newclosure(nelems);
  89. c->consts[0] = *(S->top-1);
  90. memcpy(&c->consts[1], S->top-(nelems+1), nelems*sizeof(TObject));
  91. S->top -= nelems;
  92. ttype(S->top-1) = LUA_T_CLOSURE;
  93. (S->top-1)->value.cl = c;
  94. }
  95. }
  96. /*
  97. ** Function to index a table.
  98. ** Receives the table at top-2 and the index at top-1.
  99. */
  100. void luaV_gettable (void) {
  101. struct Stack *S = &L->stack;
  102. TObject *im;
  103. if (ttype(S->top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */
  104. im = luaT_getimbyObj(S->top-2, IM_GETTABLE);
  105. else { /* object is a table... */
  106. int tg = (S->top-2)->value.a->htag;
  107. im = luaT_getim(tg, IM_GETTABLE);
  108. if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */
  109. TObject *h = luaH_get(avalue(S->top-2), S->top-1);
  110. if (ttype(h) != LUA_T_NIL) {
  111. --S->top;
  112. *(S->top-1) = *h;
  113. }
  114. else if (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL)
  115. luaD_callTM(im, 2, 1);
  116. else {
  117. --S->top;
  118. ttype(S->top-1) = LUA_T_NIL;
  119. }
  120. return;
  121. }
  122. /* else it has a "gettable" method, go through to next command */
  123. }
  124. /* object is not a table, or it has a "gettable" method */
  125. if (ttype(im) == LUA_T_NIL)
  126. lua_error("indexed expression not a table");
  127. luaD_callTM(im, 2, 1);
  128. }
  129. /*
  130. ** Function to store indexed based on values at the stack.top
  131. ** deep = 1: "deep L->stack.stack" store (with tag methods)
  132. */
  133. void luaV_settable (TObject *t, int deep) {
  134. struct Stack *S = &L->stack;
  135. TObject *im;
  136. if (ttype(t) != LUA_T_ARRAY) /* not a table, get "settable" method */
  137. im = luaT_getimbyObj(t, IM_SETTABLE);
  138. else { /* object is a table... */
  139. im = luaT_getim(avalue(t)->htag, IM_SETTABLE);
  140. if (ttype(im) == LUA_T_NIL) { /* and does not have a "settable" method */
  141. *(luaH_set(avalue(t), t+1)) = *(S->top-1);
  142. /* if deep, pop only value; otherwise, pop table, index and value */
  143. S->top -= (deep) ? 1 : 3;
  144. return;
  145. }
  146. /* else it has a "settable" method, go through to next command */
  147. }
  148. /* object is not a table, or it has a "settable" method */
  149. if (ttype(im) == LUA_T_NIL)
  150. lua_error("indexed expression not a table");
  151. if (deep) { /* table and index were not on top; copy them */
  152. *(S->top+1) = *(L->stack.top-1);
  153. *(S->top) = *(t+1);
  154. *(S->top-1) = *t;
  155. S->top += 2; /* WARNING: caller must assure stack space */
  156. }
  157. luaD_callTM(im, 3, 0);
  158. }
  159. void luaV_rawsettable (TObject *t) {
  160. if (ttype(t) != LUA_T_ARRAY)
  161. lua_error("indexed expression not a table");
  162. else {
  163. struct Stack *S = &L->stack;
  164. *(luaH_set(avalue(t), t+1)) = *(S->top-1);
  165. S->top -= 3;
  166. }
  167. }
  168. void luaV_getglobal (TaggedString *ts)
  169. {
  170. /* WARNING: caller must assure stack space */
  171. TObject *value = &ts->u.s.globalval;
  172. TObject *im = luaT_getimbyObj(value, IM_GETGLOBAL);
  173. if (ttype(im) == LUA_T_NIL) { /* default behavior */
  174. *L->stack.top++ = *value;
  175. }
  176. else {
  177. struct Stack *S = &L->stack;
  178. ttype(S->top) = LUA_T_STRING;
  179. tsvalue(S->top) = ts;
  180. S->top++;
  181. *S->top++ = *value;
  182. luaD_callTM(im, 2, 1);
  183. }
  184. }
  185. void luaV_setglobal (TaggedString *ts)
  186. {
  187. TObject *oldvalue = &ts->u.s.globalval;
  188. TObject *im = luaT_getimbyObj(oldvalue, IM_SETGLOBAL);
  189. if (ttype(im) == LUA_T_NIL) /* default behavior */
  190. luaS_rawsetglobal(ts, --L->stack.top);
  191. else {
  192. /* WARNING: caller must assure stack space */
  193. struct Stack *S = &L->stack;
  194. TObject newvalue = *(S->top-1);
  195. ttype(S->top-1) = LUA_T_STRING;
  196. tsvalue(S->top-1) = ts;
  197. *S->top++ = *oldvalue;
  198. *S->top++ = newvalue;
  199. luaD_callTM(im, 3, 0);
  200. }
  201. }
  202. static void call_binTM (IMS event, char *msg)
  203. {
  204. TObject *im = luaT_getimbyObj(L->stack.top-2, event);/* try first operand */
  205. if (ttype(im) == LUA_T_NIL) {
  206. im = luaT_getimbyObj(L->stack.top-1, event); /* try second operand */
  207. if (ttype(im) == LUA_T_NIL) {
  208. im = luaT_getim(0, event); /* try a 'global' i.m. */
  209. if (ttype(im) == LUA_T_NIL)
  210. lua_error(msg);
  211. }
  212. }
  213. lua_pushstring(luaT_eventname[event]);
  214. luaD_callTM(im, 3, 1);
  215. }
  216. static void call_arith (IMS event)
  217. {
  218. call_binTM(event, "unexpected type in arithmetic operation");
  219. }
  220. static int strcomp (char *l, long ll, char *r, long lr)
  221. {
  222. for (;;) {
  223. long temp = strcoll(l, r);
  224. if (temp != 0) return temp;
  225. /* strings are equal up to a '\0' */
  226. temp = strlen(l); /* index of first '\0' in both strings */
  227. if (temp == ll) /* l is finished? */
  228. return (temp == lr) ? 0 : -1; /* l is equal or smaller than r */
  229. else if (temp == lr) /* r is finished? */
  230. return 1; /* l is greater than r (because l is not finished) */
  231. /* both strings longer than temp; go on comparing (after the '\0') */
  232. temp++;
  233. l += temp; ll -= temp; r += temp; lr -= temp;
  234. }
  235. }
  236. void luaV_comparison (lua_Type ttype_less, lua_Type ttype_equal,
  237. lua_Type ttype_great, IMS op)
  238. {
  239. struct Stack *S = &L->stack;
  240. TObject *l = S->top-2;
  241. TObject *r = S->top-1;
  242. int result;
  243. if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
  244. result = (nvalue(l) < nvalue(r)) ? -1 : (nvalue(l) == nvalue(r)) ? 0 : 1;
  245. else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
  246. result = strcomp(svalue(l), tsvalue(l)->u.s.len,
  247. svalue(r), tsvalue(r)->u.s.len);
  248. else {
  249. call_binTM(op, "unexpected type in comparison");
  250. return;
  251. }
  252. S->top--;
  253. nvalue(S->top-1) = 1;
  254. ttype(S->top-1) = (result < 0) ? ttype_less :
  255. (result == 0) ? ttype_equal : ttype_great;
  256. }
  257. void luaV_pack (StkId firstel, int nvararg, TObject *tab) {
  258. TObject *firstelem = L->stack.stack+firstel;
  259. int i;
  260. Hash *htab;
  261. if (nvararg < 0) nvararg = 0;
  262. htab = avalue(tab) = luaH_new(nvararg+1); /* +1 for field 'n' */
  263. ttype(tab) = LUA_T_ARRAY;
  264. for (i=0; i<nvararg; i++)
  265. luaH_setint(htab, i+1, firstelem+i);
  266. luaV_setn(htab, nvararg); /* store counter in field "n" */
  267. }
  268. static void adjust_varargs (StkId first_extra_arg)
  269. {
  270. TObject arg;
  271. luaV_pack(first_extra_arg,
  272. (L->stack.top-L->stack.stack)-first_extra_arg, &arg);
  273. luaD_adjusttop(first_extra_arg);
  274. *L->stack.top++ = arg;
  275. }
  276. /*
  277. ** Execute the given opcode, until a RET. Parameters are between
  278. ** [stack+base,top). Returns n such that the the results are between
  279. ** [stack+n,top).
  280. */
  281. StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base)
  282. {
  283. struct Stack *S = &L->stack; /* to optimize */
  284. Byte *pc = tf->code;
  285. TObject *consts = tf->consts;
  286. if (lua_callhook)
  287. luaD_callHook(base, tf, 0);
  288. luaD_checkstack((*pc++)+EXTRA_STACK);
  289. if (*pc < ZEROVARARG)
  290. luaD_adjusttop(base+*(pc++));
  291. else { /* varargs */
  292. luaC_checkGC();
  293. adjust_varargs(base+(*pc++)-ZEROVARARG);
  294. }
  295. for (;;) {
  296. int aux;
  297. switch ((OpCode)(aux = *pc++)) {
  298. case PUSHNIL0:
  299. ttype(S->top++) = LUA_T_NIL;
  300. break;
  301. case PUSHNIL:
  302. aux = *pc++;
  303. do {
  304. ttype(S->top++) = LUA_T_NIL;
  305. } while (aux--);
  306. break;
  307. case PUSHNUMBER:
  308. aux = *pc++; goto pushnumber;
  309. case PUSHNUMBERW:
  310. aux = next_word(pc); goto pushnumber;
  311. case PUSHNUMBER0: case PUSHNUMBER1: case PUSHNUMBER2:
  312. aux -= PUSHNUMBER0;
  313. pushnumber:
  314. ttype(S->top) = LUA_T_NUMBER;
  315. nvalue(S->top) = aux;
  316. S->top++;
  317. break;
  318. case PUSHLOCAL:
  319. aux = *pc++; goto pushlocal;
  320. case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
  321. case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
  322. aux -= PUSHLOCAL0;
  323. pushlocal:
  324. *S->top++ = *((S->stack+base) + aux);
  325. break;
  326. case GETGLOBALW:
  327. aux = next_word(pc); goto getglobal;
  328. case GETGLOBAL:
  329. aux = *pc++; goto getglobal;
  330. case GETGLOBAL0: case GETGLOBAL1: case GETGLOBAL2: case GETGLOBAL3:
  331. case GETGLOBAL4: case GETGLOBAL5: case GETGLOBAL6: case GETGLOBAL7:
  332. aux -= GETGLOBAL0;
  333. getglobal:
  334. luaV_getglobal(tsvalue(&consts[aux]));
  335. break;
  336. case GETTABLE:
  337. luaV_gettable();
  338. break;
  339. case GETDOTTEDW:
  340. aux = next_word(pc); goto getdotted;
  341. case GETDOTTED:
  342. aux = *pc++; goto getdotted;
  343. case GETDOTTED0: case GETDOTTED1: case GETDOTTED2: case GETDOTTED3:
  344. case GETDOTTED4: case GETDOTTED5: case GETDOTTED6: case GETDOTTED7:
  345. aux -= GETDOTTED0;
  346. getdotted:
  347. *S->top++ = consts[aux];
  348. luaV_gettable();
  349. break;
  350. case PUSHSELFW:
  351. aux = next_word(pc); goto pushself;
  352. case PUSHSELF:
  353. aux = *pc++; goto pushself;
  354. case PUSHSELF0: case PUSHSELF1: case PUSHSELF2: case PUSHSELF3:
  355. case PUSHSELF4: case PUSHSELF5: case PUSHSELF6: case PUSHSELF7:
  356. aux -= PUSHSELF0;
  357. pushself: {
  358. TObject receiver = *(S->top-1);
  359. *S->top++ = consts[aux];
  360. luaV_gettable();
  361. *S->top++ = receiver;
  362. break;
  363. }
  364. case PUSHCONSTANTW:
  365. aux = next_word(pc); goto pushconstant;
  366. case PUSHCONSTANT:
  367. aux = *pc++; goto pushconstant;
  368. case PUSHCONSTANT0: case PUSHCONSTANT1: case PUSHCONSTANT2:
  369. case PUSHCONSTANT3: case PUSHCONSTANT4: case PUSHCONSTANT5:
  370. case PUSHCONSTANT6: case PUSHCONSTANT7:
  371. aux -= PUSHCONSTANT0;
  372. pushconstant:
  373. *S->top++ = consts[aux];
  374. break;
  375. case PUSHUPVALUE:
  376. aux = *pc++; goto pushupvalue;
  377. case PUSHUPVALUE0: case PUSHUPVALUE1:
  378. aux -= PUSHUPVALUE0;
  379. pushupvalue:
  380. *S->top++ = cl->consts[aux+1];
  381. break;
  382. case SETLOCAL:
  383. aux = *pc++; goto setlocal;
  384. case SETLOCAL0: case SETLOCAL1: case SETLOCAL2: case SETLOCAL3:
  385. case SETLOCAL4: case SETLOCAL5: case SETLOCAL6: case SETLOCAL7:
  386. aux -= SETLOCAL0;
  387. setlocal:
  388. *((S->stack+base) + aux) = *(--S->top);
  389. break;
  390. case SETGLOBALW:
  391. aux = next_word(pc); goto setglobal;
  392. case SETGLOBAL:
  393. aux = *pc++; goto setglobal;
  394. case SETGLOBAL0: case SETGLOBAL1: case SETGLOBAL2: case SETGLOBAL3:
  395. case SETGLOBAL4: case SETGLOBAL5: case SETGLOBAL6: case SETGLOBAL7:
  396. aux -= SETGLOBAL0;
  397. setglobal:
  398. luaV_setglobal(tsvalue(&consts[aux]));
  399. break;
  400. case SETTABLE0:
  401. luaV_settable(S->top-3, 0);
  402. break;
  403. case SETTABLE:
  404. luaV_settable(S->top-3-(*pc++), 1);
  405. break;
  406. case SETLISTW:
  407. aux = next_word(pc); aux *= LFIELDS_PER_FLUSH; goto setlist;
  408. case SETLIST:
  409. aux = *(pc++) * LFIELDS_PER_FLUSH; goto setlist;
  410. case SETLIST0:
  411. aux = 0;
  412. setlist: {
  413. int n = *(pc++);
  414. TObject *arr = S->top-n-1;
  415. for (; n; n--) {
  416. ttype(S->top) = LUA_T_NUMBER;
  417. nvalue(S->top) = n+aux;
  418. *(luaH_set(avalue(arr), S->top)) = *(S->top-1);
  419. S->top--;
  420. }
  421. break;
  422. }
  423. case SETMAP0:
  424. aux = 0; goto setmap;
  425. case SETMAP:
  426. aux = *pc++;
  427. setmap: {
  428. TObject *arr = S->top-(2*aux)-3;
  429. do {
  430. *(luaH_set(avalue(arr), S->top-2)) = *(S->top-1);
  431. S->top-=2;
  432. } while (aux--);
  433. break;
  434. }
  435. case POP:
  436. aux = *pc++; goto pop;
  437. case POP0: case POP1:
  438. aux -= POP0;
  439. pop:
  440. S->top -= (aux+1);
  441. break;
  442. case CREATEARRAYW:
  443. aux = next_word(pc); goto createarray;
  444. case CREATEARRAY0: case CREATEARRAY1:
  445. aux -= CREATEARRAY0; goto createarray;
  446. case CREATEARRAY:
  447. aux = *pc++;
  448. createarray:
  449. luaC_checkGC();
  450. avalue(S->top) = luaH_new(aux);
  451. ttype(S->top) = LUA_T_ARRAY;
  452. S->top++;
  453. break;
  454. case EQOP: case NEQOP: {
  455. int res = luaO_equalObj(S->top-2, S->top-1);
  456. S->top--;
  457. if (aux == NEQOP) res = !res;
  458. ttype(S->top-1) = res ? LUA_T_NUMBER : LUA_T_NIL;
  459. nvalue(S->top-1) = 1;
  460. break;
  461. }
  462. case LTOP:
  463. luaV_comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, IM_LT);
  464. break;
  465. case LEOP:
  466. luaV_comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, IM_LE);
  467. break;
  468. case GTOP:
  469. luaV_comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, IM_GT);
  470. break;
  471. case GEOP:
  472. luaV_comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, IM_GE);
  473. break;
  474. case ADDOP: {
  475. TObject *l = S->top-2;
  476. TObject *r = S->top-1;
  477. if (tonumber(r) || tonumber(l))
  478. call_arith(IM_ADD);
  479. else {
  480. nvalue(l) += nvalue(r);
  481. --S->top;
  482. }
  483. break;
  484. }
  485. case SUBOP: {
  486. TObject *l = S->top-2;
  487. TObject *r = S->top-1;
  488. if (tonumber(r) || tonumber(l))
  489. call_arith(IM_SUB);
  490. else {
  491. nvalue(l) -= nvalue(r);
  492. --S->top;
  493. }
  494. break;
  495. }
  496. case MULTOP: {
  497. TObject *l = S->top-2;
  498. TObject *r = S->top-1;
  499. if (tonumber(r) || tonumber(l))
  500. call_arith(IM_MUL);
  501. else {
  502. nvalue(l) *= nvalue(r);
  503. --S->top;
  504. }
  505. break;
  506. }
  507. case DIVOP: {
  508. TObject *l = S->top-2;
  509. TObject *r = S->top-1;
  510. if (tonumber(r) || tonumber(l))
  511. call_arith(IM_DIV);
  512. else {
  513. nvalue(l) /= nvalue(r);
  514. --S->top;
  515. }
  516. break;
  517. }
  518. case POWOP:
  519. call_binTM(IM_POW, "undefined operation");
  520. break;
  521. case CONCOP: {
  522. TObject *l = S->top-2;
  523. TObject *r = S->top-1;
  524. if (tostring(l) || tostring(r))
  525. call_binTM(IM_CONCAT, "unexpected type for concatenation");
  526. else {
  527. tsvalue(l) = strconc(tsvalue(l), tsvalue(r));
  528. --S->top;
  529. }
  530. luaC_checkGC();
  531. break;
  532. }
  533. case MINUSOP:
  534. if (tonumber(S->top-1)) {
  535. ttype(S->top) = LUA_T_NIL;
  536. S->top++;
  537. call_arith(IM_UNM);
  538. }
  539. else
  540. nvalue(S->top-1) = - nvalue(S->top-1);
  541. break;
  542. case NOTOP:
  543. ttype(S->top-1) =
  544. (ttype(S->top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  545. nvalue(S->top-1) = 1;
  546. break;
  547. case ONTJMPW:
  548. aux = next_word(pc); goto ontjmp;
  549. case ONTJMP:
  550. aux = *pc++;
  551. ontjmp:
  552. if (ttype(S->top-1) != LUA_T_NIL) pc += aux;
  553. else S->top--;
  554. break;
  555. case ONFJMPW:
  556. aux = next_word(pc); goto onfjmp;
  557. case ONFJMP:
  558. aux = *pc++;
  559. onfjmp:
  560. if (ttype(S->top-1) == LUA_T_NIL) pc += aux;
  561. else S->top--;
  562. break;
  563. case JMPW:
  564. aux = next_word(pc); goto jmp;
  565. case JMP:
  566. aux = *pc++;
  567. jmp:
  568. pc += aux;
  569. break;
  570. case IFFJMPW:
  571. aux = next_word(pc); goto iffjmp;
  572. case IFFJMP:
  573. aux = *pc++;
  574. iffjmp:
  575. if (ttype(--S->top) == LUA_T_NIL) pc += aux;
  576. break;
  577. case IFTUPJMPW:
  578. aux = next_word(pc); goto iftupjmp;
  579. case IFTUPJMP:
  580. aux = *pc++;
  581. iftupjmp:
  582. if (ttype(--S->top) != LUA_T_NIL) pc -= aux;
  583. break;
  584. case IFFUPJMPW:
  585. aux = next_word(pc); goto iffupjmp;
  586. case IFFUPJMP:
  587. aux = *pc++;
  588. iffupjmp:
  589. if (ttype(--S->top) == LUA_T_NIL) pc -= aux;
  590. break;
  591. case CLOSUREW:
  592. aux = next_word(pc); goto closure;
  593. case CLOSURE:
  594. aux = *pc++;
  595. closure:
  596. *S->top++ = consts[aux];
  597. luaV_closure(*pc++);
  598. luaC_checkGC();
  599. break;
  600. case CALLFUNC:
  601. aux = *pc++; goto callfunc;
  602. case CALLFUNC0: case CALLFUNC1:
  603. aux -= CALLFUNC0;
  604. callfunc: {
  605. StkId newBase = (S->top-S->stack)-(*pc++);
  606. luaD_call(newBase, aux);
  607. break;
  608. }
  609. case ENDCODE:
  610. S->top = S->stack + base;
  611. /* goes through */
  612. case RETCODE:
  613. if (lua_callhook)
  614. luaD_callHook(base, NULL, 1);
  615. return (base + ((aux==RETCODE) ? *pc : 0));
  616. case SETLINEW:
  617. aux = next_word(pc); goto setline;
  618. case SETLINE:
  619. aux = *pc++;
  620. setline:
  621. if ((S->stack+base-1)->ttype != LUA_T_LINE) {
  622. /* open space for LINE value */
  623. luaD_openstack((S->top-S->stack)-base);
  624. base++;
  625. (S->stack+base-1)->ttype = LUA_T_LINE;
  626. }
  627. (S->stack+base-1)->value.i = aux;
  628. if (lua_linehook)
  629. luaD_lineHook(aux);
  630. break;
  631. #ifdef DEBUG
  632. default:
  633. LUA_INTERNALERROR("opcode doesn't match");
  634. #endif
  635. }
  636. }
  637. }