lvm.c 18 KB

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