lvm.c 19 KB

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