ltests.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. 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 table_query (lua_State *L) {
  206. const Table *t;
  207. int i = luaL_opt_int(L, 2, -1);
  208. luaL_check_type(L, 1, LUA_TTABLE);
  209. t = hvalue(luaA_index(L, 1));
  210. if (i == -1) {
  211. lua_pushnumber(L, t->sizearray);
  212. lua_pushnumber(L, sizenode(t));
  213. lua_pushnumber(L, t->firstfree - t->node);
  214. }
  215. else if (i < t->sizearray) {
  216. lua_pushnumber(L, i);
  217. luaA_pushobject(L, &t->array[i]);
  218. lua_pushnil(L);
  219. }
  220. else if ((i -= t->sizearray) < sizenode(t)) {
  221. if (ttype(val(node(t, i))) != LUA_TNIL ||
  222. ttype(key(node(t, i))) == LUA_TNIL ||
  223. ttype(key(node(t, i))) == LUA_TNUMBER) {
  224. luaA_pushobject(L, key(node(t, i)));
  225. }
  226. else
  227. lua_pushstring(L, "<undef>");
  228. luaA_pushobject(L, val(&t->node[i]));
  229. if (t->node[i].next)
  230. lua_pushnumber(L, t->node[i].next - t->node);
  231. else
  232. lua_pushnil(L);
  233. }
  234. return 3;
  235. }
  236. static int string_query (lua_State *L) {
  237. stringtable *tb = &G(L)->strt;
  238. int s = luaL_opt_int(L, 2, 0) - 1;
  239. if (s==-1) {
  240. lua_pushnumber(L ,tb->nuse);
  241. lua_pushnumber(L ,tb->size);
  242. return 2;
  243. }
  244. else if (s < tb->size) {
  245. TString *ts;
  246. int n = 0;
  247. for (ts = tb->hash[s]; ts; ts = ts->tsv.nexthash) {
  248. setsvalue(L->top, ts);
  249. incr_top;
  250. n++;
  251. }
  252. return n;
  253. }
  254. return 0;
  255. }
  256. static int tref (lua_State *L) {
  257. int level = lua_gettop(L);
  258. int lock = luaL_opt_int(L, 2, 1);
  259. luaL_check_any(L, 1);
  260. lua_pushvalue(L, 1);
  261. lua_pushnumber(L, lua_ref(L, lock));
  262. assert(lua_gettop(L) == level+1); /* +1 for result */
  263. return 1;
  264. }
  265. static int getref (lua_State *L) {
  266. int level = lua_gettop(L);
  267. lua_getref(L, luaL_check_int(L, 1));
  268. assert(lua_gettop(L) == level+1);
  269. return 1;
  270. }
  271. static int unref (lua_State *L) {
  272. int level = lua_gettop(L);
  273. lua_unref(L, luaL_check_int(L, 1));
  274. assert(lua_gettop(L) == level);
  275. return 0;
  276. }
  277. static int eventtable (lua_State *L) {
  278. luaL_check_any(L, 1);
  279. if (lua_isnone(L, 2))
  280. lua_geteventtable(L, 1);
  281. else {
  282. lua_settop(L, 2);
  283. luaL_check_type(L, 2, LUA_TTABLE);
  284. lua_seteventtable(L, 1);
  285. }
  286. return 1;
  287. }
  288. static int newuserdata (lua_State *L) {
  289. size_t size = luaL_check_int(L, 1);
  290. char *p = cast(char *, lua_newuserdata(L, size));
  291. while (size--) *p++ = '\0';
  292. return 1;
  293. }
  294. static int newuserdatabox (lua_State *L) {
  295. lua_newuserdatabox(L, cast(void *, luaL_check_int(L, 1)));
  296. return 1;
  297. }
  298. static int udataval (lua_State *L) {
  299. luaL_check_type(L, 1, LUA_TUSERDATA);
  300. lua_pushnumber(L, cast(int, lua_touserdata(L, 1)));
  301. return 1;
  302. }
  303. static int doonnewstack (lua_State *L) {
  304. lua_State *L1 = lua_newthread(L, luaL_check_int(L, 1));
  305. if (L1 == NULL) return 0;
  306. *cast(int **, L1) = &islocked; /* initialize the lock */
  307. lua_dostring(L1, luaL_check_string(L, 2));
  308. lua_pushnumber(L, 1);
  309. lua_close(L1);
  310. return 1;
  311. }
  312. static int s2d (lua_State *L) {
  313. lua_pushnumber(L, *cast(double *, luaL_check_string(L, 1)));
  314. return 1;
  315. }
  316. static int d2s (lua_State *L) {
  317. double d = luaL_check_number(L, 1);
  318. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  319. return 1;
  320. }
  321. static int newstate (lua_State *L) {
  322. lua_State *L1 = lua_open(luaL_check_int(L, 1));
  323. if (L1) {
  324. *cast(int **, L1) = &islocked; /* initialize the lock */
  325. lua_pushnumber(L, (unsigned long)L1);
  326. }
  327. else
  328. lua_pushnil(L);
  329. return 1;
  330. }
  331. static int loadlib (lua_State *L) {
  332. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  333. lua_register(L1, "mathlibopen", lua_mathlibopen);
  334. lua_register(L1, "strlibopen", lua_strlibopen);
  335. lua_register(L1, "iolibopen", lua_iolibopen);
  336. lua_register(L1, "dblibopen", lua_dblibopen);
  337. lua_register(L1, "baselibopen", lua_baselibopen);
  338. return 0;
  339. }
  340. static int closestate (lua_State *L) {
  341. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  342. lua_close(L1);
  343. lua_unlock(L); /* close cannot unlock that */
  344. return 0;
  345. }
  346. static int doremote (lua_State *L) {
  347. lua_State *L1;
  348. const char *code = luaL_check_string(L, 2);
  349. int status;
  350. L1 = cast(lua_State *, cast(unsigned long, luaL_check_number(L, 1)));
  351. status = lua_dostring(L1, code);
  352. if (status != 0) {
  353. lua_pushnil(L);
  354. lua_pushnumber(L, status);
  355. return 2;
  356. }
  357. else {
  358. int i = 0;
  359. while (!lua_isnone(L1, ++i))
  360. lua_pushstring(L, lua_tostring(L1, i));
  361. lua_pop(L1, i-1);
  362. return i-1;
  363. }
  364. }
  365. static int log2_aux (lua_State *L) {
  366. lua_pushnumber(L, luaO_log2(luaL_check_int(L, 1)));
  367. return 1;
  368. }
  369. /*
  370. ** {======================================================
  371. ** function to test the API with C. It interprets a kind of assembler
  372. ** language with calls to the API, so the test can be driven by Lua code
  373. ** =======================================================
  374. */
  375. static const char *const delimits = " \t\n,;";
  376. static void skip (const char **pc) {
  377. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  378. }
  379. static int getnum_aux (lua_State *L, const char **pc) {
  380. int res = 0;
  381. int sig = 1;
  382. skip(pc);
  383. if (**pc == '.') {
  384. res = cast(int, lua_tonumber(L, -1));
  385. lua_pop(L, 1);
  386. (*pc)++;
  387. return res;
  388. }
  389. else if (**pc == '-') {
  390. sig = -1;
  391. (*pc)++;
  392. }
  393. while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
  394. return sig*res;
  395. }
  396. static const char *getname_aux (char *buff, const char **pc) {
  397. int i = 0;
  398. skip(pc);
  399. while (**pc != '\0' && !strchr(delimits, **pc))
  400. buff[i++] = *(*pc)++;
  401. buff[i] = '\0';
  402. return buff;
  403. }
  404. #define EQ(s1) (strcmp(s1, inst) == 0)
  405. #define getnum (getnum_aux(L, &pc))
  406. #define getname (getname_aux(buff, &pc))
  407. static int testC (lua_State *L) {
  408. char buff[30];
  409. const char *pc = luaL_check_string(L, 1);
  410. for (;;) {
  411. const char *inst = getname;
  412. if EQ("") return 0;
  413. else if EQ("isnumber") {
  414. lua_pushnumber(L, lua_isnumber(L, getnum));
  415. }
  416. else if EQ("isstring") {
  417. lua_pushnumber(L, lua_isstring(L, getnum));
  418. }
  419. else if EQ("istable") {
  420. lua_pushnumber(L, lua_istable(L, getnum));
  421. }
  422. else if EQ("iscfunction") {
  423. lua_pushnumber(L, lua_iscfunction(L, getnum));
  424. }
  425. else if EQ("isfunction") {
  426. lua_pushnumber(L, lua_isfunction(L, getnum));
  427. }
  428. else if EQ("isuserdata") {
  429. lua_pushnumber(L, lua_isuserdata(L, getnum));
  430. }
  431. else if EQ("isnil") {
  432. lua_pushnumber(L, lua_isnil(L, getnum));
  433. }
  434. else if EQ("isnull") {
  435. lua_pushnumber(L, lua_isnone(L, getnum));
  436. }
  437. else if EQ("tonumber") {
  438. lua_pushnumber(L, lua_tonumber(L, getnum));
  439. }
  440. else if EQ("tostring") {
  441. const char *s = lua_tostring(L, getnum);
  442. lua_pushstring(L, s);
  443. }
  444. else if EQ("tonumber") {
  445. lua_pushnumber(L, lua_tonumber(L, getnum));
  446. }
  447. else if EQ("strlen") {
  448. lua_pushnumber(L, lua_strlen(L, getnum));
  449. }
  450. else if EQ("tocfunction") {
  451. lua_pushcfunction(L, lua_tocfunction(L, getnum));
  452. }
  453. else if EQ("return") {
  454. return getnum;
  455. }
  456. else if EQ("gettop") {
  457. lua_pushnumber(L, lua_gettop(L));
  458. }
  459. else if EQ("settop") {
  460. lua_settop(L, getnum);
  461. }
  462. else if EQ("pop") {
  463. lua_pop(L, getnum);
  464. }
  465. else if EQ("pushnum") {
  466. lua_pushnumber(L, getnum);
  467. }
  468. else if EQ("pushvalue") {
  469. lua_pushvalue(L, getnum);
  470. }
  471. else if EQ("pushcclosure") {
  472. lua_pushcclosure(L, testC, getnum);
  473. }
  474. else if EQ("pushupvalues") {
  475. lua_pushupvalues(L);
  476. }
  477. else if EQ("remove") {
  478. lua_remove(L, getnum);
  479. }
  480. else if EQ("insert") {
  481. lua_insert(L, getnum);
  482. }
  483. else if EQ("gettable") {
  484. lua_gettable(L, getnum);
  485. }
  486. else if EQ("settable") {
  487. lua_settable(L, getnum);
  488. }
  489. else if EQ("next") {
  490. lua_next(L, -2);
  491. }
  492. else if EQ("concat") {
  493. lua_concat(L, getnum);
  494. }
  495. else if EQ("lessthan") {
  496. int a = getnum;
  497. lua_pushboolean(L, lua_lessthan(L, a, getnum));
  498. }
  499. else if EQ("equal") {
  500. int a = getnum;
  501. lua_pushboolean(L, lua_equal(L, a, getnum));
  502. }
  503. else if EQ("rawcall") {
  504. int narg = getnum;
  505. int nres = getnum;
  506. lua_rawcall(L, narg, nres);
  507. }
  508. else if EQ("call") {
  509. int narg = getnum;
  510. int nres = getnum;
  511. lua_call(L, narg, nres);
  512. }
  513. else if EQ("dostring") {
  514. lua_dostring(L, luaL_check_string(L, getnum));
  515. }
  516. else if EQ("seteventtable") {
  517. lua_seteventtable(L, getnum);
  518. }
  519. else if EQ("geteventtable") {
  520. lua_geteventtable(L, getnum);
  521. }
  522. else if EQ("type") {
  523. lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
  524. }
  525. else luaL_verror(L, "unknown instruction %.30s", buff);
  526. }
  527. return 0;
  528. }
  529. /* }====================================================== */
  530. static const struct luaL_reg tests_funcs[] = {
  531. {"hash", hash_query},
  532. {"limits", get_limits},
  533. {"listcode", listcode},
  534. {"listk", listk},
  535. {"listlocals", listlocals},
  536. {"loadlib", loadlib},
  537. {"querystr", string_query},
  538. {"querytab", table_query},
  539. {"testC", testC},
  540. {"ref", tref},
  541. {"getref", getref},
  542. {"unref", unref},
  543. {"d2s", d2s},
  544. {"s2d", s2d},
  545. {"eventtable", eventtable},
  546. {"newuserdata", newuserdata},
  547. {"newuserdatabox", newuserdatabox},
  548. {"udataval", udataval},
  549. {"doonnewstack", doonnewstack},
  550. {"newstate", newstate},
  551. {"closestate", closestate},
  552. {"doremote", doremote},
  553. {"log2", log2_aux},
  554. {"totalmem", mem_query}
  555. };
  556. void luaB_opentests (lua_State *L) {
  557. *cast(int **, L) = &islocked; /* init lock */
  558. lua_state = L; /* keep first state to be opened */
  559. /* open lib in a new table */
  560. lua_newtable(L);
  561. lua_getglobals(L);
  562. lua_pushvalue(L, -2);
  563. lua_setglobals(L);
  564. luaL_openl(L, tests_funcs); /* open functions inside new table */
  565. lua_setglobals(L); /* restore old table of globals */
  566. lua_setglobal(L, "T"); /* set new table as global T */
  567. }
  568. #endif