ltests.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. ** $Id: ltests.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
  3. ** Internal Module for Debugging of the Lua Implementation
  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 "lua.h"
  12. #include "lapi.h"
  13. #include "lauxlib.h"
  14. #include "lcode.h"
  15. #include "ldebug.h"
  16. #include "ldo.h"
  17. #include "lfunc.h"
  18. #include "lmem.h"
  19. #include "lopcodes.h"
  20. #include "lstate.h"
  21. #include "lstring.h"
  22. #include "ltable.h"
  23. #include "luadebug.h"
  24. #include "lualib.h"
  25. /*
  26. ** The whole module only makes sense with LUA_DEBUG on
  27. */
  28. #ifdef LUA_DEBUG
  29. static lua_State *lua_state = NULL;
  30. int islocked = 0;
  31. static void setnameval (lua_State *L, const char *name, int val) {
  32. lua_pushstring(L, name);
  33. lua_pushnumber(L, val);
  34. lua_settable(L, -3);
  35. }
  36. /*
  37. ** {======================================================================
  38. ** Controlled version for realloc.
  39. ** =======================================================================
  40. */
  41. /* ensures maximum alignment for HEADER */
  42. #define HEADER (sizeof(union L_Umaxalign))
  43. #define MARKSIZE 32
  44. #define MARK 0x55 /* 01010101 (a nice pattern) */
  45. #define blocksize(b) (cast(size_t *, b) - HEADER/sizeof(size_t))
  46. unsigned long memdebug_numblocks = 0;
  47. unsigned long memdebug_total = 0;
  48. unsigned long memdebug_maxmem = 0;
  49. unsigned long memdebug_memlimit = ULONG_MAX;
  50. static void *checkblock (void *block) {
  51. size_t *b = blocksize(block);
  52. size_t size = *b;
  53. int i;
  54. for (i=0;i<MARKSIZE;i++)
  55. lua_assert(*(cast(char *, b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  56. return b;
  57. }
  58. static void freeblock (void *block) {
  59. if (block) {
  60. size_t size = *blocksize(block);
  61. block = checkblock(block);
  62. memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
  63. free(block); /* free original block */
  64. memdebug_numblocks--;
  65. memdebug_total -= size;
  66. }
  67. }
  68. void *debug_realloc (void *block, size_t oldsize, size_t size) {
  69. lua_assert((oldsize == 0) ? block == NULL : oldsize == *blocksize(block));
  70. if (size == 0) {
  71. freeblock(block);
  72. return NULL;
  73. }
  74. else if (memdebug_total+size-oldsize > memdebug_memlimit)
  75. return NULL; /* to test memory allocation errors */
  76. else {
  77. void *newblock;
  78. int i;
  79. size_t realsize = HEADER+size+MARKSIZE;
  80. if (realsize < size) return NULL; /* overflow! */
  81. newblock = malloc(realsize); /* alloc a new block */
  82. if (newblock == NULL) return NULL;
  83. if (oldsize > size) oldsize = size;
  84. if (block) {
  85. memcpy(cast(char *, newblock)+HEADER, block, oldsize);
  86. freeblock(block); /* erase (and check) old copy */
  87. }
  88. /* initialize new part of the block with something `weird' */
  89. memset(cast(char *, newblock)+HEADER+oldsize, -MARK, size-oldsize);
  90. memdebug_total += size;
  91. if (memdebug_total > memdebug_maxmem)
  92. memdebug_maxmem = memdebug_total;
  93. memdebug_numblocks++;
  94. *cast(size_t *, newblock) = size;
  95. for (i=0;i<MARKSIZE;i++)
  96. *(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
  97. return cast(char *, newblock)+HEADER;
  98. }
  99. }
  100. /* }====================================================================== */
  101. /*
  102. ** {======================================================
  103. ** Disassembler
  104. ** =======================================================
  105. */
  106. static char *buildop (Proto *p, int pc, char *buff) {
  107. Instruction i = p->code[pc];
  108. OpCode o = GET_OPCODE(i);
  109. const char *name = luaP_opnames[o];
  110. sprintf(buff, "%4d - ", pc);
  111. switch (getOpMode(o)) {
  112. case iABC:
  113. sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
  114. GETARG_A(i), GETARG_B(i), GETARG_C(i));
  115. break;
  116. case iABc:
  117. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bc(i));
  118. break;
  119. case iAsBc:
  120. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBc(i));
  121. break;
  122. }
  123. return buff;
  124. }
  125. static int listcode (lua_State *L) {
  126. int pc;
  127. Proto *p;
  128. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  129. 1, "Lua function expected");
  130. p = clvalue(luaA_index(L, 1))->l.p;
  131. lua_newtable(L);
  132. setnameval(L, "maxstack", p->maxstacksize);
  133. setnameval(L, "numparams", p->numparams);
  134. for (pc=0; pc<p->sizecode; pc++) {
  135. char buff[100];
  136. lua_pushnumber(L, pc+1);
  137. lua_pushstring(L, buildop(p, pc, buff));
  138. lua_settable(L, -3);
  139. }
  140. return 1;
  141. }
  142. static int listk (lua_State *L) {
  143. Proto *p;
  144. int i;
  145. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  146. 1, "Lua function expected");
  147. p = clvalue(luaA_index(L, 1))->l.p;
  148. lua_newtable(L);
  149. for (i=0; i<p->sizek; i++) {
  150. lua_pushnumber(L, i+1);
  151. luaA_pushobject(L, p->k+i);
  152. lua_settable(L, -3);
  153. }
  154. return 1;
  155. }
  156. static int listlocals (lua_State *L) {
  157. Proto *p;
  158. int pc = luaL_check_int(L, 2) - 1;
  159. int i = 0;
  160. const char *name;
  161. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  162. 1, "Lua function expected");
  163. p = clvalue(luaA_index(L, 1))->l.p;
  164. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  165. lua_pushstring(L, name);
  166. return i-1;
  167. }
  168. /* }====================================================== */
  169. static int get_limits (lua_State *L) {
  170. lua_newtable(L);
  171. setnameval(L, "BITS_INT", BITS_INT);
  172. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  173. setnameval(L, "MAXLOCALS", MAXLOCALS);
  174. setnameval(L, "MAXPARAMS", MAXPARAMS);
  175. setnameval(L, "MAXSTACK", MAXSTACK);
  176. setnameval(L, "MAXUPVALUES", MAXUPVALUES);
  177. return 1;
  178. }
  179. static int mem_query (lua_State *L) {
  180. if (lua_isnone(L, 1)) {
  181. lua_pushnumber(L, memdebug_total);
  182. lua_pushnumber(L, memdebug_numblocks);
  183. lua_pushnumber(L, memdebug_maxmem);
  184. return 3;
  185. }
  186. else {
  187. memdebug_memlimit = luaL_check_int(L, 1);
  188. return 0;
  189. }
  190. }
  191. static int hash_query (lua_State *L) {
  192. if (lua_isnone(L, 2)) {
  193. luaL_arg_check(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  194. lua_pushnumber(L, tsvalue(luaA_index(L, 1))->tsv.hash);
  195. }
  196. else {
  197. TObject *o = luaA_index(L, 1);
  198. Table *t;
  199. luaL_check_type(L, 2, LUA_TTABLE);
  200. t = hvalue(luaA_index(L, 2));
  201. lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
  202. }
  203. return 1;
  204. }
  205. static int stacklevel (lua_State *L) {
  206. unsigned long a = 0;
  207. lua_pushnumber(L, (int)(L->top - L->stack));
  208. lua_pushnumber(L, (int)(L->stack_last - L->stack));
  209. lua_pushnumber(L, (int)(L->ci - L->base_ci));
  210. lua_pushnumber(L, (int)(L->end_ci - L->base_ci));
  211. lua_pushnumber(L, (unsigned long)&a);
  212. return 5;
  213. }
  214. static int table_query (lua_State *L) {
  215. const Table *t;
  216. int i = luaL_opt_int(L, 2, -1);
  217. luaL_check_type(L, 1, LUA_TTABLE);
  218. t = hvalue(luaA_index(L, 1));
  219. if (i == -1) {
  220. lua_pushnumber(L, t->sizearray);
  221. lua_pushnumber(L, sizenode(t));
  222. lua_pushnumber(L, t->firstfree - t->node);
  223. }
  224. else if (i < t->sizearray) {
  225. lua_pushnumber(L, i);
  226. luaA_pushobject(L, &t->array[i]);
  227. lua_pushnil(L);
  228. }
  229. else if ((i -= t->sizearray) < sizenode(t)) {
  230. if (ttype(val(node(t, i))) != LUA_TNIL ||
  231. ttype(key(node(t, i))) == LUA_TNIL ||
  232. ttype(key(node(t, i))) == LUA_TNUMBER) {
  233. luaA_pushobject(L, key(node(t, i)));
  234. }
  235. else
  236. lua_pushstring(L, "<undef>");
  237. luaA_pushobject(L, val(&t->node[i]));
  238. if (t->node[i].next)
  239. lua_pushnumber(L, t->node[i].next - t->node);
  240. else
  241. lua_pushnil(L);
  242. }
  243. return 3;
  244. }
  245. static int string_query (lua_State *L) {
  246. stringtable *tb = &G(L)->strt;
  247. int s = luaL_opt_int(L, 2, 0) - 1;
  248. if (s==-1) {
  249. lua_pushnumber(L ,tb->nuse);
  250. lua_pushnumber(L ,tb->size);
  251. return 2;
  252. }
  253. else if (s < tb->size) {
  254. TString *ts;
  255. int n = 0;
  256. for (ts = tb->hash[s]; ts; ts = ts->tsv.nexthash) {
  257. setsvalue(L->top, ts);
  258. incr_top(L);
  259. n++;
  260. }
  261. return n;
  262. }
  263. return 0;
  264. }
  265. static int tref (lua_State *L) {
  266. int level = lua_gettop(L);
  267. int lock = luaL_opt_int(L, 2, 1);
  268. luaL_check_any(L, 1);
  269. lua_pushvalue(L, 1);
  270. lua_pushnumber(L, lua_ref(L, lock));
  271. assert(lua_gettop(L) == level+1); /* +1 for result */
  272. return 1;
  273. }
  274. static int getref (lua_State *L) {
  275. int level = lua_gettop(L);
  276. lua_getref(L, luaL_check_int(L, 1));
  277. assert(lua_gettop(L) == level+1);
  278. return 1;
  279. }
  280. static int unref (lua_State *L) {
  281. int level = lua_gettop(L);
  282. lua_unref(L, luaL_check_int(L, 1));
  283. assert(lua_gettop(L) == level);
  284. return 0;
  285. }
  286. static int metatable (lua_State *L) {
  287. luaL_check_any(L, 1);
  288. if (lua_isnone(L, 2))
  289. lua_getmetatable(L, 1);
  290. else {
  291. lua_settop(L, 2);
  292. luaL_check_type(L, 2, LUA_TTABLE);
  293. lua_setmetatable(L, 1);
  294. }
  295. return 1;
  296. }
  297. static int newuserdata (lua_State *L) {
  298. size_t size = luaL_check_int(L, 1);
  299. char *p = cast(char *, lua_newuserdata(L, size));
  300. while (size--) *p++ = '\0';
  301. return 1;
  302. }
  303. static int newuserdatabox (lua_State *L) {
  304. lua_newuserdatabox(L, cast(void *, luaL_check_int(L, 1)));
  305. return 1;
  306. }
  307. static int udataval (lua_State *L) {
  308. luaL_check_type(L, 1, LUA_TUSERDATA);
  309. lua_pushnumber(L, cast(int, lua_touserdata(L, 1)));
  310. return 1;
  311. }
  312. static int doonnewstack (lua_State *L) {
  313. lua_State *L1 = lua_newthread(L, luaL_check_int(L, 1));
  314. lua_dostring(L1, luaL_check_string(L, 2));
  315. lua_pushnumber(L, 1);
  316. lua_closethread(L, L1);
  317. return 1;
  318. }
  319. static int s2d (lua_State *L) {
  320. lua_pushnumber(L, *cast(double *, luaL_check_string(L, 1)));
  321. return 1;
  322. }
  323. static int d2s (lua_State *L) {
  324. double d = luaL_check_number(L, 1);
  325. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  326. return 1;
  327. }
  328. static int newstate (lua_State *L) {
  329. lua_State *L1 = lua_open(luaL_check_int(L, 1));
  330. if (L1) {
  331. *cast(int **, L1) = &islocked; /* initialize the lock */
  332. lua_pushnumber(L, (unsigned long)L1);
  333. }
  334. else
  335. lua_pushnil(L);
  336. return 1;
  337. }
  338. static int loadlib (lua_State *L) {
  339. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  340. lua_register(L1, "mathlibopen", lua_mathlibopen);
  341. lua_register(L1, "strlibopen", lua_strlibopen);
  342. lua_register(L1, "iolibopen", lua_iolibopen);
  343. lua_register(L1, "dblibopen", lua_dblibopen);
  344. lua_register(L1, "baselibopen", lua_baselibopen);
  345. return 0;
  346. }
  347. static int closestate (lua_State *L) {
  348. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  349. lua_close(L1);
  350. lua_unlock(L); /* close cannot unlock that */
  351. return 0;
  352. }
  353. static int doremote (lua_State *L) {
  354. lua_State *L1;
  355. const char *code = luaL_check_string(L, 2);
  356. int status;
  357. L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  358. status = lua_dostring(L1, code);
  359. if (status != 0) {
  360. lua_pushnil(L);
  361. lua_pushnumber(L, status);
  362. return 2;
  363. }
  364. else {
  365. int i = 0;
  366. while (!lua_isnone(L1, ++i))
  367. lua_pushstring(L, lua_tostring(L1, i));
  368. lua_pop(L1, i-1);
  369. return i-1;
  370. }
  371. }
  372. static int log2_aux (lua_State *L) {
  373. lua_pushnumber(L, luaO_log2(luaL_check_int(L, 1)));
  374. return 1;
  375. }
  376. /*
  377. ** {======================================================
  378. ** function to test the API with C. It interprets a kind of assembler
  379. ** language with calls to the API, so the test can be driven by Lua code
  380. ** =======================================================
  381. */
  382. static const char *const delimits = " \t\n,;";
  383. static void skip (const char **pc) {
  384. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  385. }
  386. static int getnum_aux (lua_State *L, const char **pc) {
  387. int res = 0;
  388. int sig = 1;
  389. skip(pc);
  390. if (**pc == '.') {
  391. res = cast(int, lua_tonumber(L, -1));
  392. lua_pop(L, 1);
  393. (*pc)++;
  394. return res;
  395. }
  396. else if (**pc == '-') {
  397. sig = -1;
  398. (*pc)++;
  399. }
  400. while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
  401. return sig*res;
  402. }
  403. static const char *getname_aux (char *buff, const char **pc) {
  404. int i = 0;
  405. skip(pc);
  406. while (**pc != '\0' && !strchr(delimits, **pc))
  407. buff[i++] = *(*pc)++;
  408. buff[i] = '\0';
  409. return buff;
  410. }
  411. #define EQ(s1) (strcmp(s1, inst) == 0)
  412. #define getnum (getnum_aux(L, &pc))
  413. #define getname (getname_aux(buff, &pc))
  414. static int testC (lua_State *L) {
  415. char buff[30];
  416. const char *pc = luaL_check_string(L, 1);
  417. for (;;) {
  418. const char *inst = getname;
  419. if EQ("") return 0;
  420. else if EQ("isnumber") {
  421. lua_pushnumber(L, lua_isnumber(L, getnum));
  422. }
  423. else if EQ("isstring") {
  424. lua_pushnumber(L, lua_isstring(L, getnum));
  425. }
  426. else if EQ("istable") {
  427. lua_pushnumber(L, lua_istable(L, getnum));
  428. }
  429. else if EQ("iscfunction") {
  430. lua_pushnumber(L, lua_iscfunction(L, getnum));
  431. }
  432. else if EQ("isfunction") {
  433. lua_pushnumber(L, lua_isfunction(L, getnum));
  434. }
  435. else if EQ("isuserdata") {
  436. lua_pushnumber(L, lua_isuserdata(L, getnum));
  437. }
  438. else if EQ("isnil") {
  439. lua_pushnumber(L, lua_isnil(L, getnum));
  440. }
  441. else if EQ("isnull") {
  442. lua_pushnumber(L, lua_isnone(L, getnum));
  443. }
  444. else if EQ("tonumber") {
  445. lua_pushnumber(L, lua_tonumber(L, getnum));
  446. }
  447. else if EQ("tostring") {
  448. const char *s = lua_tostring(L, getnum);
  449. lua_pushstring(L, s);
  450. }
  451. else if EQ("tonumber") {
  452. lua_pushnumber(L, lua_tonumber(L, getnum));
  453. }
  454. else if EQ("strlen") {
  455. lua_pushnumber(L, lua_strlen(L, getnum));
  456. }
  457. else if EQ("tocfunction") {
  458. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  459. }
  460. else if EQ("return") {
  461. return getnum;
  462. }
  463. else if EQ("gettop") {
  464. lua_pushnumber(L, lua_gettop(L));
  465. }
  466. else if EQ("settop") {
  467. lua_settop(L, getnum);
  468. }
  469. else if EQ("pop") {
  470. lua_pop(L, getnum);
  471. }
  472. else if EQ("pushnum") {
  473. lua_pushnumber(L, getnum);
  474. }
  475. else if EQ("pushvalue") {
  476. lua_pushvalue(L, getnum);
  477. }
  478. else if EQ("pushcclosure") {
  479. lua_pushcclosure(L, testC, getnum);
  480. }
  481. else if EQ("pushupvalues") {
  482. lua_pushupvalues(L);
  483. }
  484. else if EQ("remove") {
  485. lua_remove(L, getnum);
  486. }
  487. else if EQ("insert") {
  488. lua_insert(L, getnum);
  489. }
  490. else if EQ("gettable") {
  491. lua_gettable(L, getnum);
  492. }
  493. else if EQ("settable") {
  494. lua_settable(L, getnum);
  495. }
  496. else if EQ("next") {
  497. lua_next(L, -2);
  498. }
  499. else if EQ("concat") {
  500. lua_concat(L, getnum);
  501. }
  502. else if EQ("lessthan") {
  503. int a = getnum;
  504. lua_pushboolean(L, lua_lessthan(L, a, getnum));
  505. }
  506. else if EQ("equal") {
  507. int a = getnum;
  508. lua_pushboolean(L, lua_equal(L, a, getnum));
  509. }
  510. else if EQ("rawcall") {
  511. int narg = getnum;
  512. int nres = getnum;
  513. lua_rawcall(L, narg, nres);
  514. }
  515. else if EQ("call") {
  516. int narg = getnum;
  517. int nres = getnum;
  518. lua_call(L, narg, nres);
  519. }
  520. else if EQ("dostring") {
  521. lua_dostring(L, luaL_check_string(L, getnum));
  522. }
  523. else if EQ("setmetatable") {
  524. lua_setmetatable(L, getnum);
  525. }
  526. else if EQ("getmetatable") {
  527. lua_getmetatable(L, getnum);
  528. }
  529. else if EQ("type") {
  530. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  531. }
  532. else luaL_verror(L, "unknown instruction %.30s", buff);
  533. }
  534. return 0;
  535. }
  536. /* }====================================================== */
  537. static const struct luaL_reg tests_funcs[] = {
  538. {"hash", hash_query},
  539. {"limits", get_limits},
  540. {"listcode", listcode},
  541. {"listk", listk},
  542. {"listlocals", listlocals},
  543. {"loadlib", loadlib},
  544. {"stacklevel", stacklevel},
  545. {"querystr", string_query},
  546. {"querytab", table_query},
  547. {"testC", testC},
  548. {"ref", tref},
  549. {"getref", getref},
  550. {"unref", unref},
  551. {"d2s", d2s},
  552. {"s2d", s2d},
  553. {"metatable", metatable},
  554. {"newuserdata", newuserdata},
  555. {"newuserdatabox", newuserdatabox},
  556. {"udataval", udataval},
  557. {"doonnewstack", doonnewstack},
  558. {"newstate", newstate},
  559. {"closestate", closestate},
  560. {"doremote", doremote},
  561. {"log2", log2_aux},
  562. {"totalmem", mem_query}
  563. };
  564. static void fim (void) {
  565. if (!islocked)
  566. lua_close(lua_state);
  567. lua_assert(memdebug_numblocks == 0);
  568. lua_assert(memdebug_total == 0);
  569. }
  570. void luaB_opentests (lua_State *L) {
  571. *cast(int **, L) = &islocked; /* init lock */
  572. lua_state = L; /* keep first state to be opened */
  573. /* open lib in a new table */
  574. lua_newtable(L);
  575. lua_getglobals(L);
  576. lua_pushvalue(L, -2);
  577. lua_setglobals(L);
  578. luaL_openl(L, tests_funcs); /* open functions inside new table */
  579. lua_setglobals(L); /* restore old table of globals */
  580. lua_setglobal(L, "T"); /* set new table as global T */
  581. atexit(fim);
  582. }
  583. #endif