ltests.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /*
  2. ** $Id: ltests.c,v 2.23 2005/03/28 17:17:53 roberto Exp roberto $
  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. #define ltests_c
  12. #define LUA_CORE
  13. #include "lua.h"
  14. #include "lapi.h"
  15. #include "lauxlib.h"
  16. #include "lcode.h"
  17. #include "ldebug.h"
  18. #include "ldo.h"
  19. #include "lfunc.h"
  20. #include "lmem.h"
  21. #include "lopcodes.h"
  22. #include "lstate.h"
  23. #include "lstring.h"
  24. #include "ltable.h"
  25. #include "lualib.h"
  26. /*
  27. ** The whole module only makes sense with LUA_DEBUG on
  28. */
  29. #if defined(LUA_DEBUG)
  30. int Trick = 0;
  31. static lua_State *lua_state = NULL;
  32. int islocked = 0;
  33. #define obj_at(L,k) (L->ci->base+(k) - 1)
  34. static void setnameval (lua_State *L, const char *name, int val) {
  35. lua_pushstring(L, name);
  36. lua_pushinteger(L, val);
  37. lua_settable(L, -3);
  38. }
  39. /*
  40. ** {======================================================================
  41. ** Controlled version for realloc.
  42. ** =======================================================================
  43. */
  44. #define MARK 0x55 /* 01010101 (a nice pattern) */
  45. #ifndef EXTERNMEMCHECK
  46. /* full memory check */
  47. #define HEADER (sizeof(L_Umaxalign)) /* ensures maximum alignment for HEADER */
  48. #define MARKSIZE 16 /* size of marks after each block */
  49. #define blockhead(b) (cast(char *, b) - HEADER)
  50. #define setsize(newblock, size) (*cast(size_t *, newblock) = size)
  51. #define checkblocksize(b, size) (size == (*cast(size_t *, blockhead(b))))
  52. #define fillmem(mem,size) memset(mem, -MARK, size)
  53. #else
  54. /* external memory check: don't do it twice */
  55. #define HEADER 0
  56. #define MARKSIZE 0
  57. #define blockhead(b) (b)
  58. #define setsize(newblock, size) /* empty */
  59. #define checkblocksize(b,size) (1)
  60. #define fillmem(mem,size) /* empty */
  61. #endif
  62. Memcontrol memcontrol = {0L, 0L, 0L, ULONG_MAX};
  63. static void *checkblock (void *block, size_t size) {
  64. void *b = blockhead(block);
  65. int i;
  66. for (i=0;i<MARKSIZE;i++)
  67. lua_assert(*(cast(char *, b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  68. return b;
  69. }
  70. static void freeblock (Memcontrol *mc, void *block, size_t size) {
  71. if (block) {
  72. lua_assert(checkblocksize(block, size));
  73. block = checkblock(block, size);
  74. fillmem(block, size+HEADER+MARKSIZE); /* erase block */
  75. free(block); /* free original block */
  76. mc->numblocks--;
  77. mc->total -= size;
  78. }
  79. }
  80. void *debug_realloc (void *ud, void *block, size_t oldsize, size_t size) {
  81. Memcontrol *mc = cast(Memcontrol *, ud);
  82. lua_assert(oldsize == 0 || checkblocksize(block, oldsize));
  83. if (size == 0) {
  84. freeblock(mc, block, oldsize);
  85. return NULL;
  86. }
  87. else if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
  88. return NULL; /* to test memory allocation errors */
  89. else {
  90. void *newblock;
  91. int i;
  92. size_t realsize = HEADER+size+MARKSIZE;
  93. size_t commonsize = (oldsize < size) ? oldsize : size;
  94. if (realsize < size) return NULL; /* overflow! */
  95. newblock = malloc(realsize); /* alloc a new block */
  96. if (newblock == NULL) return NULL;
  97. if (block) {
  98. memcpy(cast(char *, newblock)+HEADER, block, commonsize);
  99. freeblock(mc, block, oldsize); /* erase (and check) old copy */
  100. }
  101. /* initialize new part of the block with something `weird' */
  102. fillmem(cast(char *, newblock)+HEADER+commonsize, size-commonsize);
  103. mc->total += size;
  104. if (mc->total > mc->maxmem)
  105. mc->maxmem = mc->total;
  106. mc->numblocks++;
  107. setsize(newblock, size);
  108. for (i=0;i<MARKSIZE;i++)
  109. *(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
  110. return cast(char *, newblock)+HEADER;
  111. }
  112. }
  113. /* }====================================================================== */
  114. /*
  115. ** {======================================================
  116. ** Functions to check memory consistency
  117. ** =======================================================
  118. */
  119. static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
  120. if (isdead(g,t)) return 0;
  121. if (g->gcstate == GCSpropagate)
  122. return !isblack(f) || !iswhite(t);
  123. else if (g->gcstate == GCSfinalize)
  124. return iswhite(f);
  125. else
  126. return 1;
  127. }
  128. static void printobj (global_State *g, GCObject *o) {
  129. int i = 0;
  130. GCObject *p;
  131. for (p = g->rootgc; p != o && p != NULL; p = p->gch.next) i++;
  132. if (p == NULL) i = -1;
  133. printf("%d:%s(%p)-%c(%02X)", i, luaT_typenames[o->gch.tt], (void *)o,
  134. isdead(g,o)?'d':isblack(o)?'b':iswhite(o)?'w':'g', o->gch.marked);
  135. }
  136. static int testobjref (global_State *g, GCObject *f, GCObject *t) {
  137. int r = testobjref1(g,f,t);
  138. if (!r) {
  139. printf("%d(%02X) - ", g->gcstate, g->currentwhite);
  140. printobj(g, f);
  141. printf("\t-> ");
  142. printobj(g, t);
  143. printf("\n");
  144. }
  145. return r;
  146. }
  147. #define checkobjref(g,f,t) lua_assert(testobjref(g,f,obj2gco(t)))
  148. #define checkvalref(g,f,t) lua_assert(!iscollectable(t) || \
  149. ((ttype(t) == (t)->value.gc->gch.tt) && testobjref(g,f,gcvalue(t))))
  150. static void checktable (global_State *g, Table *h) {
  151. int i;
  152. int weakkey = 0;
  153. int weakvalue = 0;
  154. const TValue *mode;
  155. GCObject *hgc = obj2gco(h);
  156. if (h->metatable)
  157. checkobjref(g, hgc, h->metatable);
  158. mode = gfasttm(g, h->metatable, TM_MODE);
  159. if (mode && ttisstring(mode)) { /* is there a weak mode? */
  160. weakkey = (strchr(svalue(mode), 'k') != NULL);
  161. weakvalue = (strchr(svalue(mode), 'v') != NULL);
  162. }
  163. i = h->sizearray;
  164. while (i--)
  165. checkvalref(g, hgc, &h->array[i]);
  166. i = sizenode(h);
  167. while (i--) {
  168. Node *n = gnode(h, i);
  169. if (!ttisnil(gval(n))) {
  170. lua_assert(!ttisnil(gkey(n)));
  171. checkvalref(g, hgc, gkey(n));
  172. checkvalref(g, hgc, gval(n));
  173. }
  174. }
  175. }
  176. /*
  177. ** All marks are conditional because a GC may happen while the
  178. ** prototype is still being created
  179. */
  180. static void checkproto (global_State *g, Proto *f) {
  181. int i;
  182. GCObject *fgc = obj2gco(f);
  183. if (f->source) checkobjref(g, fgc, f->source);
  184. for (i=0; i<f->sizek; i++) {
  185. if (ttisstring(f->k+i))
  186. checkobjref(g, fgc, rawtsvalue(f->k+i));
  187. }
  188. for (i=0; i<f->sizeupvalues; i++) {
  189. if (f->upvalues[i])
  190. checkobjref(g, fgc, f->upvalues[i]);
  191. }
  192. for (i=0; i<f->sizep; i++) {
  193. if (f->p[i])
  194. checkobjref(g, fgc, f->p[i]);
  195. }
  196. for (i=0; i<f->sizelocvars; i++) {
  197. if (f->locvars[i].varname)
  198. checkobjref(g, fgc, f->locvars[i].varname);
  199. }
  200. }
  201. static void checkclosure (global_State *g, Closure *cl) {
  202. GCObject *clgc = obj2gco(cl);
  203. checkobjref(g, clgc, cl->l.env);
  204. if (cl->c.isC) {
  205. int i;
  206. for (i=0; i<cl->c.nupvalues; i++)
  207. checkvalref(g, clgc, &cl->c.upvalue[i]);
  208. }
  209. else {
  210. int i;
  211. lua_assert(cl->l.nupvalues == cl->l.p->nups);
  212. checkobjref(g, clgc, cl->l.p);
  213. for (i=0; i<cl->l.nupvalues; i++) {
  214. if (cl->l.upvals[i]) {
  215. lua_assert(cl->l.upvals[i]->tt == LUA_TUPVAL);
  216. checkobjref(g, clgc, cl->l.upvals[i]);
  217. }
  218. }
  219. }
  220. }
  221. static void checkstack (global_State *g, lua_State *L1) {
  222. StkId o;
  223. CallInfo *ci;
  224. GCObject *uvo;
  225. lua_assert(!isdead(g, obj2gco(L1)));
  226. for (uvo = L1->openupval; uvo != NULL; uvo = uvo->gch.next) {
  227. UpVal *uv = gco2uv(uvo);
  228. lua_assert(uv->v != &uv->u.value); /* must be open */
  229. lua_assert(!isblack(uvo)); /* open upvalues cannot be black */
  230. }
  231. checkliveness(g, gt(L1));
  232. if (L1->base_ci) {
  233. for (ci = L1->base_ci; ci <= L1->ci; ci++) {
  234. lua_assert(ci->top <= L1->stack_last);
  235. lua_assert(lua_checkpc(L1, ci));
  236. }
  237. }
  238. else lua_assert(L1->size_ci == 0);
  239. if (L1->stack) {
  240. for (o = L1->stack; o < L1->top; o++)
  241. checkliveness(g, o);
  242. }
  243. else lua_assert(L1->stacksize == 0);
  244. }
  245. static void checkobject (global_State *g, GCObject *o) {
  246. if (isdead(g, o))
  247. /* lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep);*/
  248. { if (!(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep))
  249. printf(">>> %d %s %02x\n", g->gcstate, luaT_typenames[o->gch.tt], o->gch.marked);
  250. }
  251. else {
  252. if (g->gcstate == GCSfinalize)
  253. lua_assert(iswhite(o));
  254. switch (o->gch.tt) {
  255. case LUA_TUPVAL: {
  256. UpVal *uv = gco2uv(o);
  257. lua_assert(uv->v == &uv->u.value); /* must be closed */
  258. lua_assert(!isgray(o)); /* closed upvalues are never gray */
  259. checkvalref(g, o, uv->v);
  260. break;
  261. }
  262. case LUA_TUSERDATA: {
  263. Table *mt = gco2u(o)->metatable;
  264. if (mt) checkobjref(g, o, mt);
  265. break;
  266. }
  267. case LUA_TTABLE: {
  268. checktable(g, gco2h(o));
  269. break;
  270. }
  271. case LUA_TTHREAD: {
  272. checkstack(g, gco2th(o));
  273. break;
  274. }
  275. case LUA_TFUNCTION: {
  276. checkclosure(g, gco2cl(o));
  277. break;
  278. }
  279. case LUA_TPROTO: {
  280. checkproto(g, gco2p(o));
  281. break;
  282. }
  283. default: lua_assert(0);
  284. }
  285. }
  286. }
  287. int lua_checkpc (lua_State *L, pCallInfo ci) {
  288. if (ci == L->base_ci || !f_isLua(ci)) return 1;
  289. else {
  290. Proto *p = ci_func(ci)->l.p;
  291. if (ci < L->ci)
  292. return p->code <= ci->savedpc && ci->savedpc <= p->code + p->sizecode;
  293. else
  294. return p->code <= L->savedpc && L->savedpc <= p->code + p->sizecode;
  295. }
  296. }
  297. int lua_checkmemory (lua_State *L) {
  298. global_State *g = G(L);
  299. GCObject *o;
  300. UpVal *uv;
  301. checkstack(g, g->mainthread);
  302. for (o = g->rootgc; o != obj2gco(g->mainthread); o = o->gch.next)
  303. checkobject(g, o);
  304. for (o = o->gch.next; o != NULL; o = o->gch.next) {
  305. lua_assert(o->gch.tt == LUA_TUSERDATA);
  306. checkobject(g, o);
  307. }
  308. for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
  309. lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
  310. lua_assert(uv->v != &uv->u.value); /* must be open */
  311. lua_assert(!isblack(obj2gco(uv))); /* open upvalues are never black */
  312. checkvalref(g, obj2gco(uv), uv->v);
  313. }
  314. return 0;
  315. }
  316. /* }====================================================== */
  317. /*
  318. ** {======================================================
  319. ** Disassembler
  320. ** =======================================================
  321. */
  322. static char *buildop (Proto *p, int pc, char *buff) {
  323. Instruction i = p->code[pc];
  324. OpCode o = GET_OPCODE(i);
  325. const char *name = luaP_opnames[o];
  326. int line = getline(p, pc);
  327. sprintf(buff, "(%4d) %4d - ", line, pc);
  328. switch (getOpMode(o)) {
  329. case iABC:
  330. sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
  331. GETARG_A(i), GETARG_B(i), GETARG_C(i));
  332. break;
  333. case iABx:
  334. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
  335. break;
  336. case iAsBx:
  337. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
  338. break;
  339. }
  340. return buff;
  341. }
  342. #if 0
  343. void luaI_printcode (Proto *pt, int size) {
  344. int pc;
  345. for (pc=0; pc<size; pc++) {
  346. char buff[100];
  347. printf("%s\n", buildop(pt, pc, buff));
  348. }
  349. printf("-------\n");
  350. }
  351. #endif
  352. static int listcode (lua_State *L) {
  353. int pc;
  354. Proto *p;
  355. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  356. 1, "Lua function expected");
  357. p = clvalue(obj_at(L, 1))->l.p;
  358. lua_newtable(L);
  359. setnameval(L, "maxstack", p->maxstacksize);
  360. setnameval(L, "numparams", p->numparams);
  361. for (pc=0; pc<p->sizecode; pc++) {
  362. char buff[100];
  363. lua_pushinteger(L, pc+1);
  364. lua_pushstring(L, buildop(p, pc, buff));
  365. lua_settable(L, -3);
  366. }
  367. return 1;
  368. }
  369. static int listk (lua_State *L) {
  370. Proto *p;
  371. int i;
  372. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  373. 1, "Lua function expected");
  374. p = clvalue(obj_at(L, 1))->l.p;
  375. lua_createtable(L, p->sizek, 0);
  376. for (i=0; i<p->sizek; i++) {
  377. luaA_pushobject(L, p->k+i);
  378. lua_rawseti(L, -2, i+1);
  379. }
  380. return 1;
  381. }
  382. static int listlocals (lua_State *L) {
  383. Proto *p;
  384. int pc = luaL_checkint(L, 2) - 1;
  385. int i = 0;
  386. const char *name;
  387. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  388. 1, "Lua function expected");
  389. p = clvalue(obj_at(L, 1))->l.p;
  390. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  391. lua_pushstring(L, name);
  392. return i-1;
  393. }
  394. /* }====================================================== */
  395. static int get_limits (lua_State *L) {
  396. lua_createtable(L, 0, 5);
  397. setnameval(L, "BITS_INT", LUAI_BITSINT);
  398. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  399. setnameval(L, "MAXVARS", LUAI_MAXVARS);
  400. setnameval(L, "MAXSTACK", MAXSTACK);
  401. setnameval(L, "MAXUPVALUES", LUAI_MAXUPVALUES);
  402. setnameval(L, "NUM_OPCODES", NUM_OPCODES);
  403. return 1;
  404. }
  405. static int mem_query (lua_State *L) {
  406. if (lua_isnone(L, 1)) {
  407. lua_pushinteger(L, memcontrol.total);
  408. lua_pushinteger(L, memcontrol.numblocks);
  409. lua_pushinteger(L, memcontrol.maxmem);
  410. return 3;
  411. }
  412. else {
  413. memcontrol.memlimit = luaL_checkint(L, 1);
  414. return 0;
  415. }
  416. }
  417. static int settrick (lua_State *L) {
  418. Trick = lua_tointeger(L, 1);
  419. return 0;
  420. }
  421. /*static int set_gcstate (lua_State *L) {
  422. static const char *const state[] = {"propagate", "sweep", "finalize"};
  423. return 0;
  424. }*/
  425. static int get_gccolor (lua_State *L) {
  426. TValue *o;
  427. luaL_checkany(L, 1);
  428. o = obj_at(L, 1);
  429. if (!iscollectable(o))
  430. lua_pushstring(L, "no collectable");
  431. else
  432. lua_pushstring(L, iswhite(gcvalue(o)) ? "white" :
  433. isblack(gcvalue(o)) ? "black" : "grey");
  434. return 1;
  435. }
  436. static int gcstate (lua_State *L) {
  437. switch(G(L)->gcstate) {
  438. case GCSpropagate: lua_pushstring(L, "propagate"); break;
  439. case GCSsweepstring: lua_pushstring(L, "sweep strings"); break;
  440. case GCSsweep: lua_pushstring(L, "sweep"); break;
  441. case GCSfinalize: lua_pushstring(L, "finalize"); break;
  442. }
  443. return 1;
  444. }
  445. static int hash_query (lua_State *L) {
  446. if (lua_isnone(L, 2)) {
  447. luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  448. lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
  449. }
  450. else {
  451. TValue *o = obj_at(L, 1);
  452. Table *t;
  453. luaL_checktype(L, 2, LUA_TTABLE);
  454. t = hvalue(obj_at(L, 2));
  455. lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
  456. }
  457. return 1;
  458. }
  459. static int stacklevel (lua_State *L) {
  460. unsigned long a = 0;
  461. lua_pushinteger(L, (L->top - L->stack));
  462. lua_pushinteger(L, (L->stack_last - L->stack));
  463. lua_pushinteger(L, (L->ci - L->base_ci));
  464. lua_pushinteger(L, (L->end_ci - L->base_ci));
  465. lua_pushinteger(L, (unsigned long)&a);
  466. return 5;
  467. }
  468. static int table_query (lua_State *L) {
  469. const Table *t;
  470. int i = luaL_optint(L, 2, -1);
  471. luaL_checktype(L, 1, LUA_TTABLE);
  472. t = hvalue(obj_at(L, 1));
  473. if (i == -1) {
  474. lua_pushinteger(L, t->sizearray);
  475. lua_pushinteger(L, t->node == &luaH_dummynode ? 0 : sizenode(t));
  476. lua_pushinteger(L, t->lastfree - t->node);
  477. }
  478. else if (i < t->sizearray) {
  479. lua_pushinteger(L, i);
  480. luaA_pushobject(L, &t->array[i]);
  481. lua_pushnil(L);
  482. }
  483. else if ((i -= t->sizearray) < sizenode(t)) {
  484. if (!ttisnil(gval(gnode(t, i))) ||
  485. ttisnil(gkey(gnode(t, i))) ||
  486. ttisnumber(gkey(gnode(t, i)))) {
  487. luaA_pushobject(L, key2tval(gnode(t, i)));
  488. }
  489. else
  490. lua_pushliteral(L, "<undef>");
  491. luaA_pushobject(L, gval(gnode(t, i)));
  492. if (gnext(&t->node[i]))
  493. lua_pushinteger(L, gnext(&t->node[i]) - t->node);
  494. else
  495. lua_pushnil(L);
  496. }
  497. return 3;
  498. }
  499. static int string_query (lua_State *L) {
  500. stringtable *tb = &G(L)->strt;
  501. int s = luaL_optint(L, 2, 0) - 1;
  502. if (s==-1) {
  503. lua_pushinteger(L ,tb->nuse);
  504. lua_pushinteger(L ,tb->size);
  505. return 2;
  506. }
  507. else if (s < tb->size) {
  508. GCObject *ts;
  509. int n = 0;
  510. for (ts = tb->hash[s]; ts; ts = ts->gch.next) {
  511. setsvalue2s(L, L->top, gco2ts(ts));
  512. incr_top(L);
  513. n++;
  514. }
  515. return n;
  516. }
  517. return 0;
  518. }
  519. static int tref (lua_State *L) {
  520. int level = lua_gettop(L);
  521. int lock = luaL_optint(L, 2, 1);
  522. luaL_checkany(L, 1);
  523. lua_pushvalue(L, 1);
  524. lua_pushinteger(L, lua_ref(L, lock));
  525. lua_assert(lua_gettop(L) == level+1); /* +1 for result */
  526. return 1;
  527. }
  528. static int getref (lua_State *L) {
  529. int level = lua_gettop(L);
  530. lua_getref(L, luaL_checkint(L, 1));
  531. lua_assert(lua_gettop(L) == level+1);
  532. return 1;
  533. }
  534. static int unref (lua_State *L) {
  535. int level = lua_gettop(L);
  536. lua_unref(L, luaL_checkint(L, 1));
  537. lua_assert(lua_gettop(L) == level);
  538. return 0;
  539. }
  540. static int upvalue (lua_State *L) {
  541. int n = luaL_checkint(L, 2);
  542. luaL_checktype(L, 1, LUA_TFUNCTION);
  543. if (lua_isnone(L, 3)) {
  544. const char *name = lua_getupvalue(L, 1, n);
  545. if (name == NULL) return 0;
  546. lua_pushstring(L, name);
  547. return 2;
  548. }
  549. else {
  550. const char *name = lua_setupvalue(L, 1, n);
  551. lua_pushstring(L, name);
  552. return 1;
  553. }
  554. }
  555. static int newuserdata (lua_State *L) {
  556. size_t size = luaL_checkint(L, 1);
  557. char *p = cast(char *, lua_newuserdata(L, size));
  558. while (size--) *p++ = '\0';
  559. return 1;
  560. }
  561. static int pushuserdata (lua_State *L) {
  562. lua_pushlightuserdata(L, cast(void *, luaL_checkint(L, 1)));
  563. return 1;
  564. }
  565. static int udataval (lua_State *L) {
  566. lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
  567. return 1;
  568. }
  569. static int doonnewstack (lua_State *L) {
  570. lua_State *L1 = lua_newthread(L);
  571. size_t l;
  572. const char *s = luaL_checklstring(L, 1, &l);
  573. int status = luaL_loadbuffer(L1, s, l, s);
  574. if (status == 0)
  575. status = lua_pcall(L1, 0, 0, 0);
  576. lua_pushinteger(L, status);
  577. return 1;
  578. }
  579. static int s2d (lua_State *L) {
  580. lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
  581. return 1;
  582. }
  583. static int d2s (lua_State *L) {
  584. double d = luaL_checknumber(L, 1);
  585. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  586. return 1;
  587. }
  588. static int newstate (lua_State *L) {
  589. void *ud;
  590. lua_Alloc f = lua_getallocf(L, &ud);
  591. lua_State *L1 = lua_newstate(f, ud);
  592. if (L1)
  593. lua_pushinteger(L, (unsigned long)L1);
  594. else
  595. lua_pushnil(L);
  596. return 1;
  597. }
  598. static int loadlib (lua_State *L) {
  599. static const luaL_reg libs[] = {
  600. {"mathlibopen", luaopen_math},
  601. {"strlibopen", luaopen_string},
  602. {"iolibopen", luaopen_io},
  603. {"tablibopen", luaopen_table},
  604. {"dblibopen", luaopen_debug},
  605. {"baselibopen", luaopen_base},
  606. {NULL, NULL}
  607. };
  608. lua_State *L1 = cast(lua_State *,
  609. cast(unsigned long, luaL_checknumber(L, 1)));
  610. lua_pushvalue(L1, LUA_GLOBALSINDEX);
  611. luaL_openlib(L1, NULL, libs, 0);
  612. return 0;
  613. }
  614. static int closestate (lua_State *L) {
  615. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
  616. lua_close(L1);
  617. lua_unlock(L); /* close cannot unlock that */
  618. return 0;
  619. }
  620. static int doremote (lua_State *L) {
  621. lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
  622. size_t lcode;
  623. const char *code = luaL_checklstring(L, 2, &lcode);
  624. int status;
  625. lua_settop(L1, 0);
  626. status = luaL_loadbuffer(L1, code, lcode, code);
  627. if (status == 0)
  628. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  629. if (status != 0) {
  630. lua_pushnil(L);
  631. lua_pushinteger(L, status);
  632. lua_pushstring(L, lua_tostring(L1, -1));
  633. return 3;
  634. }
  635. else {
  636. int i = 0;
  637. while (!lua_isnone(L1, ++i))
  638. lua_pushstring(L, lua_tostring(L1, i));
  639. lua_pop(L1, i-1);
  640. return i-1;
  641. }
  642. }
  643. static int log2_aux (lua_State *L) {
  644. lua_pushinteger(L, luaO_log2(luaL_checkint(L, 1)));
  645. return 1;
  646. }
  647. static int int2fb_aux (lua_State *L) {
  648. int b = luaO_int2fb(luaL_checkint(L, 1));
  649. lua_pushinteger(L, b);
  650. lua_pushinteger(L, luaO_fb2int(b));
  651. return 2;
  652. }
  653. /*
  654. ** {======================================================
  655. ** function to test the API with C. It interprets a kind of assembler
  656. ** language with calls to the API, so the test can be driven by Lua code
  657. ** =======================================================
  658. */
  659. static const char *const delimits = " \t\n,;";
  660. static void skip (const char **pc) {
  661. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  662. }
  663. static int getnum_aux (lua_State *L, const char **pc) {
  664. int res = 0;
  665. int sig = 1;
  666. skip(pc);
  667. if (**pc == '.') {
  668. res = cast(int, lua_tonumber(L, -1));
  669. lua_pop(L, 1);
  670. (*pc)++;
  671. return res;
  672. }
  673. else if (**pc == '-') {
  674. sig = -1;
  675. (*pc)++;
  676. }
  677. while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
  678. return sig*res;
  679. }
  680. static const char *getname_aux (char *buff, const char **pc) {
  681. int i = 0;
  682. skip(pc);
  683. while (**pc != '\0' && !strchr(delimits, **pc))
  684. buff[i++] = *(*pc)++;
  685. buff[i] = '\0';
  686. return buff;
  687. }
  688. static int getindex_aux (lua_State *L, const char **pc) {
  689. skip(pc);
  690. switch (*(*pc)++) {
  691. case 'R': return LUA_REGISTRYINDEX;
  692. case 'G': return LUA_GLOBALSINDEX;
  693. case 'E': return LUA_ENVIRONINDEX;
  694. case 'U': return lua_upvalueindex(getnum_aux(L, pc));
  695. default: (*pc)--; return getnum_aux(L, pc);
  696. }
  697. }
  698. #define EQ(s1) (strcmp(s1, inst) == 0)
  699. #define getnum (getnum_aux(L, &pc))
  700. #define getname (getname_aux(buff, &pc))
  701. #define getindex (getindex_aux(L, &pc))
  702. static int testC (lua_State *L) {
  703. char buff[30];
  704. lua_State *L1;
  705. const char *pc;
  706. if (lua_isnumber(L, 1)) {
  707. L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
  708. pc = luaL_checkstring(L, 2);
  709. }
  710. else {
  711. L1 = L;
  712. pc = luaL_checkstring(L, 1);
  713. }
  714. for (;;) {
  715. const char *inst = getname;
  716. if EQ("") return 0;
  717. else if EQ("isnumber") {
  718. lua_pushinteger(L1, lua_isnumber(L1, getindex));
  719. }
  720. else if EQ("isstring") {
  721. lua_pushinteger(L1, lua_isstring(L1, getindex));
  722. }
  723. else if EQ("istable") {
  724. lua_pushinteger(L1, lua_istable(L1, getindex));
  725. }
  726. else if EQ("iscfunction") {
  727. lua_pushinteger(L1, lua_iscfunction(L1, getindex));
  728. }
  729. else if EQ("isfunction") {
  730. lua_pushinteger(L1, lua_isfunction(L1, getindex));
  731. }
  732. else if EQ("isuserdata") {
  733. lua_pushinteger(L1, lua_isuserdata(L1, getindex));
  734. }
  735. else if EQ("isudataval") {
  736. lua_pushinteger(L1, lua_islightuserdata(L1, getindex));
  737. }
  738. else if EQ("isnil") {
  739. lua_pushinteger(L1, lua_isnil(L1, getindex));
  740. }
  741. else if EQ("isnull") {
  742. lua_pushinteger(L1, lua_isnone(L1, getindex));
  743. }
  744. else if EQ("tonumber") {
  745. lua_pushnumber(L1, lua_tonumber(L1, getindex));
  746. }
  747. else if EQ("tostring") {
  748. const char *s = lua_tostring(L1, getindex);
  749. lua_pushstring(L1, s);
  750. }
  751. else if EQ("objsize") {
  752. lua_pushinteger(L1, lua_objsize(L1, getindex));
  753. }
  754. else if EQ("tocfunction") {
  755. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  756. }
  757. else if EQ("return") {
  758. return getnum;
  759. }
  760. else if EQ("gettop") {
  761. lua_pushinteger(L1, lua_gettop(L1));
  762. }
  763. else if EQ("settop") {
  764. lua_settop(L1, getnum);
  765. }
  766. else if EQ("pop") {
  767. lua_pop(L1, getnum);
  768. }
  769. else if EQ("pushnum") {
  770. lua_pushinteger(L1, getnum);
  771. }
  772. else if EQ("pushstring") {
  773. lua_pushstring(L1, getname);
  774. }
  775. else if EQ("pushnil") {
  776. lua_pushnil(L1);
  777. }
  778. else if EQ("pushbool") {
  779. lua_pushboolean(L1, getnum);
  780. }
  781. else if EQ("newuserdata") {
  782. lua_newuserdata(L1, getnum);
  783. }
  784. else if EQ("tobool") {
  785. lua_pushinteger(L1, lua_toboolean(L1, getindex));
  786. }
  787. else if EQ("pushvalue") {
  788. lua_pushvalue(L1, getindex);
  789. }
  790. else if EQ("pushcclosure") {
  791. lua_pushcclosure(L1, testC, getnum);
  792. }
  793. else if EQ("remove") {
  794. lua_remove(L1, getnum);
  795. }
  796. else if EQ("insert") {
  797. lua_insert(L1, getnum);
  798. }
  799. else if EQ("replace") {
  800. lua_replace(L1, getindex);
  801. }
  802. else if EQ("gettable") {
  803. lua_gettable(L1, getindex);
  804. }
  805. else if EQ("settable") {
  806. lua_settable(L1, getindex);
  807. }
  808. else if EQ("next") {
  809. lua_next(L1, -2);
  810. }
  811. else if EQ("concat") {
  812. lua_concat(L1, getnum);
  813. }
  814. else if EQ("lessthan") {
  815. int a = getindex;
  816. lua_pushboolean(L1, lua_lessthan(L1, a, getindex));
  817. }
  818. else if EQ("equal") {
  819. int a = getindex;
  820. lua_pushboolean(L1, lua_equal(L1, a, getindex));
  821. }
  822. else if EQ("rawcall") {
  823. int narg = getnum;
  824. int nres = getnum;
  825. lua_call(L1, narg, nres);
  826. }
  827. else if EQ("call") {
  828. int narg = getnum;
  829. int nres = getnum;
  830. lua_pcall(L1, narg, nres, 0);
  831. }
  832. else if EQ("loadstring") {
  833. size_t sl;
  834. const char *s = luaL_checklstring(L1, getnum, &sl);
  835. luaL_loadbuffer(L1, s, sl, s);
  836. }
  837. else if EQ("loadfile") {
  838. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  839. }
  840. else if EQ("setmetatable") {
  841. lua_setmetatable(L1, getindex);
  842. }
  843. else if EQ("getmetatable") {
  844. if (lua_getmetatable(L1, getindex) == 0)
  845. lua_pushnil(L1);
  846. }
  847. else if EQ("type") {
  848. lua_pushstring(L1, luaL_typename(L1, getnum));
  849. }
  850. else if EQ("getn") {
  851. int i = getindex;
  852. lua_pushinteger(L1, luaL_getn(L1, i));
  853. }
  854. #ifndef luaL_setn
  855. else if EQ("setn") {
  856. int i = getindex;
  857. int n = cast(int, lua_tonumber(L1, -1));
  858. luaL_setn(L1, i, n);
  859. lua_pop(L1, 1);
  860. }
  861. #endif
  862. else if EQ("throw") {
  863. #if defined(__cplusplus)
  864. static struct X { int x; } x;
  865. throw x;
  866. #else
  867. luaL_error(L1, "C++");
  868. #endif
  869. break;
  870. }
  871. else luaL_error(L, "unknown instruction %s", buff);
  872. }
  873. return 0;
  874. }
  875. /* }====================================================== */
  876. /*
  877. ** {======================================================
  878. ** tests for yield inside hooks
  879. ** =======================================================
  880. */
  881. static void yieldf (lua_State *L, lua_Debug *ar) {
  882. lua_yield(L, 0);
  883. }
  884. static int setyhook (lua_State *L) {
  885. if (lua_isnoneornil(L, 1))
  886. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  887. else {
  888. const char *smask = luaL_checkstring(L, 1);
  889. int count = luaL_optint(L, 2, 0);
  890. int mask = 0;
  891. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  892. if (count > 0) mask |= LUA_MASKCOUNT;
  893. lua_sethook(L, yieldf, mask, count);
  894. }
  895. return 0;
  896. }
  897. static int coresume (lua_State *L) {
  898. int status;
  899. lua_State *co = lua_tothread(L, 1);
  900. luaL_argcheck(L, co, 1, "coroutine expected");
  901. status = lua_resume(co, 0);
  902. if (status != 0) {
  903. lua_pushboolean(L, 0);
  904. lua_insert(L, -2);
  905. return 2; /* return false + error message */
  906. }
  907. else {
  908. lua_pushboolean(L, 1);
  909. return 1;
  910. }
  911. }
  912. /* }====================================================== */
  913. /*
  914. ** {======================================================
  915. ** tests auxlib functions
  916. ** =======================================================
  917. */
  918. static int auxgsub (lua_State *L) {
  919. const char *s1 = luaL_checkstring(L, 1);
  920. const char *s2 = luaL_checkstring(L, 2);
  921. const char *s3 = luaL_checkstring(L, 3);
  922. lua_settop(L, 3);
  923. luaL_gsub(L, s1, s2, s3);
  924. lua_assert(lua_gettop(L) == 4);
  925. return 1;
  926. }
  927. static int auxgetf (lua_State *L) {
  928. const char *s = luaL_checkstring(L, 1);
  929. lua_settop(L, 2);
  930. lua_pushstring(L, luaL_getfield(L, 2, s));
  931. lua_assert(lua_gettop(L) == 4);
  932. return 2;
  933. }
  934. static int auxsetf (lua_State *L) {
  935. const char *s = luaL_checkstring(L, 1);
  936. lua_settop(L, 3);
  937. lua_pushstring(L, luaL_setfield(L, 2, s));
  938. lua_assert(lua_gettop(L) == 3);
  939. return 1;
  940. }
  941. /* }====================================================== */
  942. static const struct luaL_reg tests_funcs[] = {
  943. {"hash", hash_query},
  944. {"limits", get_limits},
  945. {"listcode", listcode},
  946. {"listk", listk},
  947. {"listlocals", listlocals},
  948. {"loadlib", loadlib},
  949. {"stacklevel", stacklevel},
  950. {"querystr", string_query},
  951. {"querytab", table_query},
  952. {"testC", testC},
  953. {"checkmemory", lua_checkmemory},
  954. {"gccolor", get_gccolor},
  955. {"gcstate", gcstate},
  956. {"trick", settrick},
  957. {"ref", tref},
  958. {"getref", getref},
  959. {"unref", unref},
  960. {"d2s", d2s},
  961. {"s2d", s2d},
  962. {"upvalue", upvalue},
  963. {"newuserdata", newuserdata},
  964. {"pushuserdata", pushuserdata},
  965. {"udataval", udataval},
  966. {"doonnewstack", doonnewstack},
  967. {"newstate", newstate},
  968. {"closestate", closestate},
  969. {"doremote", doremote},
  970. {"log2", log2_aux},
  971. {"int2fb", int2fb_aux},
  972. {"totalmem", mem_query},
  973. {"resume", coresume},
  974. {"setyhook", setyhook},
  975. {"gsub", auxgsub},
  976. {"getfield", auxgetf},
  977. {"setfield", auxsetf},
  978. {NULL, NULL}
  979. };
  980. static void fim (void) {
  981. if (!islocked)
  982. lua_close(lua_state);
  983. lua_assert(memcontrol.numblocks == 0);
  984. lua_assert(memcontrol.total == 0);
  985. }
  986. static int l_panic (lua_State *L) {
  987. UNUSED(L);
  988. fprintf(stderr, "unable to recover; exiting\n");
  989. return 0;
  990. }
  991. int luaB_opentests (lua_State *L) {
  992. void *ud;
  993. lua_assert(lua_getallocf(L, &ud) == debug_realloc);
  994. lua_assert(ud == cast(void *, &memcontrol));
  995. lua_atpanic(L, l_panic);
  996. lua_state = L; /* keep first state to be opened */
  997. luaL_openlib(L, "T", tests_funcs, 0);
  998. atexit(fim);
  999. return 0;
  1000. }
  1001. #undef main
  1002. int main (int argc, char *argv[]) {
  1003. char *limit = getenv("MEMLIMIT");
  1004. if (limit)
  1005. memcontrol.memlimit = strtoul(limit, NULL, 10);
  1006. return l_main(argc, argv);
  1007. }
  1008. #endif