lvm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. ** $Id: lvm.c,v 1.90 2000/03/03 14:58:26 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. #define LUA_REENTRANT
  10. #include "lauxlib.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. ** LUA_T_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) != LUA_T_STRING)
  32. return 1;
  33. else {
  34. if (!luaO_str2d(svalue(obj), &nvalue(obj)))
  35. return 2;
  36. ttype(obj) = LUA_T_NUMBER;
  37. return 0;
  38. }
  39. }
  40. int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
  41. if (ttype(obj) != LUA_T_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) = LUA_T_STRING;
  48. return 0;
  49. }
  50. }
  51. void luaV_setn (lua_State *L, Hash *t, int val) {
  52. TObject index, value;
  53. ttype(&index) = LUA_T_STRING; tsvalue(&index) = luaS_new(L, "n");
  54. ttype(&value) = LUA_T_NUMBER; nvalue(&value) = val;
  55. luaH_set(L, t, &index, &value);
  56. }
  57. void luaV_closure (lua_State *L, int nelems) {
  58. if (nelems > 0) {
  59. Closure *c = luaF_newclosure(L, nelems);
  60. c->consts[0] = *(L->top-1);
  61. L->top -= nelems;
  62. while (nelems--)
  63. c->consts[nelems+1] = *(L->top-1+nelems);
  64. ttype(L->top-1) = (ttype(&c->consts[0]) == LUA_T_CPROTO) ?
  65. LUA_T_CCLOSURE : LUA_T_LCLOSURE;
  66. (L->top-1)->value.cl = c;
  67. }
  68. }
  69. /*
  70. ** Function to index a table.
  71. ** Receives the table at top-2 and the index at top-1.
  72. */
  73. void luaV_gettable (lua_State *L, StkId top) {
  74. TObject *table = top-2;
  75. const TObject *im;
  76. if (ttype(table) != LUA_T_ARRAY) { /* not a table, get gettable TM */
  77. im = luaT_getimbyObj(L, table, IM_GETTABLE);
  78. if (ttype(im) == LUA_T_NIL) {
  79. L->top = top;
  80. luaG_indexerror(L, table);
  81. }
  82. }
  83. else { /* object is a table... */
  84. int tg = table->value.a->htag;
  85. im = luaT_getim(L, tg, IM_GETTABLE);
  86. if (ttype(im) == LUA_T_NIL) { /* and does not have a `gettable' TM */
  87. const TObject *h = luaH_get(L, avalue(table), table+1);
  88. if (ttype(h) == LUA_T_NIL &&
  89. (ttype(im=luaT_getim(L, tg, IM_INDEX)) != LUA_T_NIL)) {
  90. /* result is nil and there is an `index' tag method */
  91. L->top = top;
  92. luaD_callTM(L, im, 2, 1); /* calls it */
  93. }
  94. else
  95. *table = *h; /* `push' result into table position */
  96. return;
  97. }
  98. /* else it has a `gettable' TM, go through to next command */
  99. }
  100. /* object is not a table, or it has a `gettable' TM */
  101. L->top = top;
  102. luaD_callTM(L, im, 2, 1);
  103. }
  104. /*
  105. ** Receives table at *t, index at *(t+1) and value at `top'.
  106. */
  107. void luaV_settable (lua_State *L, StkId t, StkId top) {
  108. const TObject *im;
  109. if (ttype(t) != LUA_T_ARRAY) { /* not a table, get `settable' method */
  110. L->top = top;
  111. im = luaT_getimbyObj(L, t, IM_SETTABLE);
  112. if (ttype(im) == LUA_T_NIL)
  113. luaG_indexerror(L, t);
  114. }
  115. else { /* object is a table... */
  116. im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE);
  117. if (ttype(im) == LUA_T_NIL) { /* and does not have a `settable' method */
  118. luaH_set(L, avalue(t), t+1, top-1);
  119. return;
  120. }
  121. /* else it has a `settable' method, go through to next command */
  122. }
  123. /* object is not a table, or it has a `settable' method */
  124. /* prepare arguments and call the tag method */
  125. luaD_checkstack(L, 3);
  126. *(top+2) = *(top-1);
  127. *(top+1) = *(t+1);
  128. *(top) = *t;
  129. *(top-1) = *im;
  130. L->top = top+3;
  131. luaD_call(L, top-1, 0);
  132. }
  133. void luaV_rawsettable (lua_State *L, StkId t) {
  134. if (ttype(t) != LUA_T_ARRAY)
  135. lua_error(L, "indexed expression not a table");
  136. else {
  137. luaH_set(L, avalue(t), t+1, L->top-1);
  138. L->top -= 3;
  139. }
  140. }
  141. void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) {
  142. const TObject *value = &gv->value;
  143. TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
  144. if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
  145. *top = *value; /* default behavior */
  146. else { /* tag method */
  147. luaD_checkstack(L, 3);
  148. *top = *im;
  149. ttype(top+1) = LUA_T_STRING;
  150. tsvalue(top+1) = gv->name; /* global name */
  151. *(top+2) = *value;
  152. L->top = top+3;
  153. luaD_call(L, top, 1);
  154. }
  155. }
  156. void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top) {
  157. const TObject *oldvalue = &gv->value;
  158. const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
  159. if (ttype(im) == LUA_T_NIL) /* is there a tag method? */
  160. gv->value = *(top-1);
  161. else {
  162. luaD_checkstack(L, 3);
  163. *(top+2) = *(top-1); /* new value */
  164. *(top+1) = *oldvalue;
  165. ttype(top) = LUA_T_STRING;
  166. tsvalue(top) = gv->name;
  167. *(top-1) = *im;
  168. L->top = top+3;
  169. luaD_call(L, top-1, 0);
  170. }
  171. }
  172. static void call_binTM (lua_State *L, StkId top, IMS event, const char *msg) {
  173. /* try first operand */
  174. const TObject *im = luaT_getimbyObj(L, top-2, event);
  175. L->top = top;
  176. if (ttype(im) == LUA_T_NIL) {
  177. im = luaT_getimbyObj(L, top-1, event); /* try second operand */
  178. if (ttype(im) == LUA_T_NIL) {
  179. im = luaT_getim(L, 0, event); /* try a `global' method */
  180. if (ttype(im) == LUA_T_NIL)
  181. lua_error(L, msg);
  182. }
  183. }
  184. lua_pushstring(L, luaT_eventname[event]);
  185. luaD_callTM(L, im, 3, 1);
  186. }
  187. static void call_arith (lua_State *L, StkId top, IMS event) {
  188. call_binTM(L, top, event, "unexpected type in arithmetic operation");
  189. }
  190. static int luaV_strcomp (const TaggedString *ls, const TaggedString *rs) {
  191. const char *l = ls->str;
  192. long ll = ls->u.s.len;
  193. const char *r = rs->str;
  194. long lr = rs->u.s.len;
  195. for (;;) {
  196. long temp = strcoll(l, r);
  197. if (temp != 0) return temp;
  198. /* strings are equal up to a '\0' */
  199. temp = strlen(l); /* index of first '\0' in both strings */
  200. if (temp == ll) /* l is finished? */
  201. return (temp == lr) ? 0 : -1; /* l is equal or smaller than r */
  202. else if (temp == lr) /* r is finished? */
  203. return 1; /* l is greater than r (because l is not finished) */
  204. /* both strings longer than temp; go on comparing (after the '\0') */
  205. temp++;
  206. l += temp; ll -= temp; r += temp; lr -= temp;
  207. }
  208. }
  209. int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) {
  210. if (ttype(l) == LUA_T_NUMBER && ttype(r) == LUA_T_NUMBER)
  211. return (nvalue(l) < nvalue(r));
  212. else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING)
  213. return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
  214. else { /* call TM */
  215. luaD_checkstack(L, 2);
  216. *top++ = *l;
  217. *top++ = *r;
  218. call_binTM(L, top, IM_LT, "unexpected type in comparison");
  219. L->top--;
  220. return (ttype(L->top) != LUA_T_NIL);
  221. }
  222. }
  223. #define setbool(o,cond) if (cond) { \
  224. ttype(o) = LUA_T_NUMBER; nvalue(o) = 1.0; } \
  225. else ttype(o) = LUA_T_NIL
  226. static void strconc (lua_State *L, int total, StkId top) {
  227. do {
  228. int n = 2; /* number of elements handled in this pass (at least 2) */
  229. if (tostring(L, top-2) || tostring(L, top-1))
  230. call_binTM(L, top, IM_CONCAT, "unexpected type for concatenation");
  231. else { /* at least two string values; get as many as possible */
  232. long tl = tsvalue(top-2)->u.s.len + tsvalue(top-1)->u.s.len;
  233. char *buffer;
  234. int i;
  235. while (n < total && !tostring(L, top-n-1)) { /* collect total length */
  236. tl += tsvalue(top-n-1)->u.s.len;
  237. n++;
  238. }
  239. buffer = luaL_openspace(L, tl);
  240. tl = 0;
  241. for (i=n; i>0; i--) { /* concat all strings */
  242. long l = tsvalue(top-i)->u.s.len;
  243. memcpy(buffer+tl, tsvalue(top-i)->str, l);
  244. tl += l;
  245. }
  246. tsvalue(top-n) = luaS_newlstr(L, buffer, tl);
  247. }
  248. total -= n-1; /* got `n' strings to create 1 new */
  249. top -= n-1;
  250. } while (total > 1); /* repeat until only 1 result left */
  251. }
  252. void luaV_pack (lua_State *L, StkId firstelem, int nvararg, TObject *tab) {
  253. int i;
  254. Hash *htab;
  255. htab = avalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */
  256. ttype(tab) = LUA_T_ARRAY;
  257. for (i=0; i<nvararg; i++)
  258. luaH_setint(L, htab, i+1, firstelem+i);
  259. luaV_setn(L, htab, nvararg); /* store counter in field `n' */
  260. }
  261. static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
  262. TObject arg;
  263. int nvararg = (L->top-base) - nfixargs;
  264. if (nvararg < 0) {
  265. luaV_pack(L, base, 0, &arg);
  266. luaD_adjusttop(L, base, nfixargs);
  267. }
  268. else {
  269. luaV_pack(L, base+nfixargs, nvararg, &arg);
  270. L->top = base+nfixargs;
  271. }
  272. *L->top++ = arg;
  273. }
  274. /*
  275. ** Executes the given Lua function. Parameters are between [base,top).
  276. ** Returns n such that the the results are between [n,top).
  277. */
  278. StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf,
  279. register StkId base) {
  280. register StkId top; /* keep top local, for performance */
  281. register const Instruction *pc = tf->code;
  282. TaggedString **kstr = tf->kstr;
  283. if (L->callhook)
  284. luaD_callHook(L, base-1, L->callhook, "call");
  285. luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
  286. if (tf->is_vararg) { /* varargs? */
  287. adjust_varargs(L, base, tf->numparams);
  288. luaC_checkGC(L);
  289. }
  290. else
  291. luaD_adjusttop(L, base, tf->numparams);
  292. top = L->top;
  293. for (;;) {
  294. register Instruction i = *pc++;
  295. switch (GET_OPCODE(i)) {
  296. case ENDCODE:
  297. return L->top; /* no results */
  298. case RETCODE:
  299. L->top = top;
  300. return base+GETARG_U(i);
  301. case CALL:
  302. L->top = top;
  303. luaD_call(L, base+GETARG_A(i), GETARG_B(i));
  304. top = L->top;
  305. break;
  306. case TAILCALL:
  307. L->top = top;
  308. luaD_call(L, base+GETARG_A(i), MULT_RET);
  309. return base+GETARG_B(i);
  310. case PUSHNIL: {
  311. int n = GETARG_U(i);
  312. do {
  313. ttype(top++) = LUA_T_NIL;
  314. } while (--n > 0);
  315. break;
  316. }
  317. case POP:
  318. top -= GETARG_U(i);
  319. break;
  320. case PUSHINT:
  321. ttype(top) = LUA_T_NUMBER;
  322. nvalue(top) = (real)GETARG_S(i);
  323. top++;
  324. break;
  325. case PUSHSTRING:
  326. ttype(top) = LUA_T_STRING;
  327. tsvalue(top) = kstr[GETARG_U(i)];
  328. top++;
  329. break;
  330. case PUSHNUM:
  331. ttype(top) = LUA_T_NUMBER;
  332. nvalue(top) = tf->knum[GETARG_U(i)];
  333. top++;
  334. break;
  335. case PUSHNEGNUM:
  336. ttype(top) = LUA_T_NUMBER;
  337. nvalue(top) = -tf->knum[GETARG_U(i)];
  338. top++;
  339. break;
  340. case PUSHUPVALUE:
  341. *top++ = cl->consts[GETARG_U(i)+1];
  342. break;
  343. case PUSHLOCAL:
  344. *top++ = *(base+GETARG_U(i));
  345. break;
  346. case GETGLOBAL:
  347. luaV_getglobal(L, kstr[GETARG_U(i)]->u.s.gv, top);
  348. top++;
  349. break;
  350. case GETTABLE:
  351. luaV_gettable(L, top);
  352. top--;
  353. break;
  354. case GETDOTTED:
  355. ttype(top) = LUA_T_STRING;
  356. tsvalue(top++) = kstr[GETARG_U(i)];
  357. luaV_gettable(L, top);
  358. top--;
  359. break;
  360. case PUSHSELF: {
  361. TObject receiver;
  362. receiver = *(top-1);
  363. ttype(top) = LUA_T_STRING;
  364. tsvalue(top++) = kstr[GETARG_U(i)];
  365. luaV_gettable(L, top);
  366. *(top-1) = receiver;
  367. break;
  368. }
  369. case CREATETABLE:
  370. L->top = top;
  371. luaC_checkGC(L);
  372. avalue(top) = luaH_new(L, GETARG_U(i));
  373. ttype(top) = LUA_T_ARRAY;
  374. top++;
  375. break;
  376. case SETLOCAL:
  377. *(base+GETARG_U(i)) = *(--top);
  378. break;
  379. case SETGLOBAL:
  380. luaV_setglobal(L, kstr[GETARG_U(i)]->u.s.gv, top);
  381. top--;
  382. break;
  383. case SETTABLEPOP:
  384. luaV_settable(L, top-3, top);
  385. top -= 3; /* pop table, index, and value */
  386. break;
  387. case SETTABLE:
  388. luaV_settable(L, top-3-GETARG_U(i), top);
  389. top--; /* pop value */
  390. break;
  391. case SETLIST: {
  392. int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
  393. int n = GETARG_B(i)+1;
  394. Hash *arr = avalue(top-n-1);
  395. L->top = top-n; /* final value of `top' (in case of errors) */
  396. for (; n; n--)
  397. luaH_setint(L, arr, n+aux, --top);
  398. break;
  399. }
  400. case SETMAP: {
  401. int n = GETARG_U(i);
  402. StkId finaltop = top-2*(n+1);
  403. Hash *arr = avalue(finaltop-1);
  404. L->top = finaltop; /* final value of `top' (in case of errors) */
  405. do {
  406. luaH_set(L, arr, top-2, top-1);
  407. top-=2;
  408. } while (n--);
  409. break;
  410. }
  411. case ADDOP:
  412. if (tonumber(top-1) || tonumber(top-2))
  413. call_arith(L, top, IM_ADD);
  414. else
  415. nvalue(top-2) += nvalue(top-1);
  416. top--;
  417. break;
  418. case ADDI:
  419. if (tonumber(top-1)) {
  420. ttype(top) = LUA_T_NUMBER;
  421. nvalue(top) = (real)GETARG_S(i);
  422. call_arith(L, top+1, IM_ADD);
  423. }
  424. else
  425. nvalue(top-1) += (real)GETARG_S(i);
  426. break;
  427. case SUBOP:
  428. if (tonumber(top-1) || tonumber(top-2))
  429. call_arith(L, top, IM_SUB);
  430. else
  431. nvalue(top-2) -= nvalue(top-1);
  432. top--;
  433. break;
  434. case MULTOP:
  435. if (tonumber(top-1) || tonumber(top-2))
  436. call_arith(L, top, IM_MUL);
  437. else
  438. nvalue(top-2) *= nvalue(top-1);
  439. top--;
  440. break;
  441. case DIVOP:
  442. if (tonumber(top-1) || tonumber(top-2))
  443. call_arith(L, top, IM_DIV);
  444. else
  445. nvalue(top-2) /= nvalue(top-1);
  446. top--;
  447. break;
  448. case POWOP:
  449. call_binTM(L, top, IM_POW, "undefined operation");
  450. top--;
  451. break;
  452. case CONCOP: {
  453. int n = GETARG_U(i);
  454. strconc(L, n, top);
  455. top -= n-1;
  456. L->top = top;
  457. luaC_checkGC(L);
  458. break;
  459. }
  460. case MINUSOP:
  461. if (tonumber(top-1)) {
  462. ttype(top) = LUA_T_NIL;
  463. call_arith(L, top+1, IM_UNM);
  464. }
  465. else
  466. nvalue(top-1) = -nvalue(top-1);
  467. break;
  468. case NOTOP:
  469. ttype(top-1) =
  470. (ttype(top-1) == LUA_T_NIL) ? LUA_T_NUMBER : LUA_T_NIL;
  471. nvalue(top-1) = 1;
  472. break;
  473. case IFNEQJMP:
  474. top -= 2;
  475. if (!luaO_equalObj(top, top+1)) pc += GETARG_S(i);
  476. break;
  477. case IFEQJMP:
  478. top -= 2;
  479. if (luaO_equalObj(top, top+1)) pc += GETARG_S(i);
  480. break;
  481. case IFLTJMP:
  482. top -= 2;
  483. if (luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
  484. break;
  485. case IFLEJMP: /* a <= b === !(b<a) */
  486. top -= 2;
  487. if (!luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
  488. break;
  489. case IFGTJMP: /* a > b === (b<a) */
  490. top -= 2;
  491. if (luaV_lessthan(L, top+1, top, top+2)) pc += GETARG_S(i);
  492. break;
  493. case IFGEJMP: /* a >= b === !(a<b) */
  494. top -= 2;
  495. if (!luaV_lessthan(L, top, top+1, top+2)) pc += GETARG_S(i);
  496. break;
  497. case IFTJMP:
  498. if (ttype(--top) != LUA_T_NIL) pc += GETARG_S(i);
  499. break;
  500. case IFFJMP:
  501. if (ttype(--top) == LUA_T_NIL) pc += GETARG_S(i);
  502. break;
  503. case ONTJMP:
  504. if (ttype(top-1) != LUA_T_NIL) pc += GETARG_S(i);
  505. else top--;
  506. break;
  507. case ONFJMP:
  508. if (ttype(top-1) == LUA_T_NIL) pc += GETARG_S(i);
  509. else top--;
  510. break;
  511. case JMP:
  512. pc += GETARG_S(i);
  513. break;
  514. case PUSHNILJMP:
  515. ttype(top++) = LUA_T_NIL;
  516. pc++;
  517. break;
  518. case CLOSURE:
  519. ttype(top) = LUA_T_LPROTO;
  520. tfvalue(top) = tf->kproto[GETARG_A(i)];
  521. L->top = ++top;
  522. luaV_closure(L, GETARG_B(i));
  523. top -= GETARG_B(i);
  524. luaC_checkGC(L);
  525. break;
  526. case SETLINE:
  527. if ((base-1)->ttype != LUA_T_LINE) {
  528. /* open space for LINE value */
  529. int n = top-base;
  530. while (n--) base[n+1] = base[n];
  531. base++;
  532. top++;
  533. (base-1)->ttype = LUA_T_LINE;
  534. }
  535. (base-1)->value.i = GETARG_U(i);
  536. if (L->linehook) {
  537. L->top = top;
  538. luaD_lineHook(L, base-2, GETARG_U(i));
  539. }
  540. break;
  541. }
  542. }
  543. }