ltests.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. ** $Id: ltests.c,v 2.22 2005/03/23 17:51:11 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. }
  236. else lua_assert(L1->size_ci == 0);
  237. if (L1->stack) {
  238. for (o = L1->stack; o < L1->top; o++)
  239. checkliveness(g, o);
  240. }
  241. else lua_assert(L1->stacksize == 0);
  242. }
  243. static void checkobject (global_State *g, GCObject *o) {
  244. if (isdead(g, o))
  245. /* lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep);*/
  246. { if (!(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep))
  247. printf(">>> %d %s %02x\n", g->gcstate, luaT_typenames[o->gch.tt], o->gch.marked);
  248. }
  249. else {
  250. if (g->gcstate == GCSfinalize)
  251. lua_assert(iswhite(o));
  252. switch (o->gch.tt) {
  253. case LUA_TUPVAL: {
  254. UpVal *uv = gco2uv(o);
  255. lua_assert(uv->v == &uv->u.value); /* must be closed */
  256. lua_assert(!isgray(o)); /* closed upvalues are never gray */
  257. checkvalref(g, o, uv->v);
  258. break;
  259. }
  260. case LUA_TUSERDATA: {
  261. Table *mt = gco2u(o)->metatable;
  262. if (mt) checkobjref(g, o, mt);
  263. break;
  264. }
  265. case LUA_TTABLE: {
  266. checktable(g, gco2h(o));
  267. break;
  268. }
  269. case LUA_TTHREAD: {
  270. checkstack(g, gco2th(o));
  271. break;
  272. }
  273. case LUA_TFUNCTION: {
  274. checkclosure(g, gco2cl(o));
  275. break;
  276. }
  277. case LUA_TPROTO: {
  278. checkproto(g, gco2p(o));
  279. break;
  280. }
  281. default: lua_assert(0);
  282. }
  283. }
  284. }
  285. int lua_checkmemory (lua_State *L) {
  286. global_State *g = G(L);
  287. GCObject *o;
  288. UpVal *uv;
  289. checkstack(g, g->mainthread);
  290. for (o = g->rootgc; o != obj2gco(g->mainthread); o = o->gch.next)
  291. checkobject(g, o);
  292. for (o = o->gch.next; o != NULL; o = o->gch.next) {
  293. lua_assert(o->gch.tt == LUA_TUSERDATA);
  294. checkobject(g, o);
  295. }
  296. for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
  297. lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
  298. lua_assert(uv->v != &uv->u.value); /* must be open */
  299. lua_assert(!isblack(obj2gco(uv))); /* open upvalues are never black */
  300. checkvalref(g, obj2gco(uv), uv->v);
  301. }
  302. return 0;
  303. }
  304. /* }====================================================== */
  305. /*
  306. ** {======================================================
  307. ** Disassembler
  308. ** =======================================================
  309. */
  310. static char *buildop (Proto *p, int pc, char *buff) {
  311. Instruction i = p->code[pc];
  312. OpCode o = GET_OPCODE(i);
  313. const char *name = luaP_opnames[o];
  314. int line = getline(p, pc);
  315. sprintf(buff, "(%4d) %4d - ", line, pc);
  316. switch (getOpMode(o)) {
  317. case iABC:
  318. sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
  319. GETARG_A(i), GETARG_B(i), GETARG_C(i));
  320. break;
  321. case iABx:
  322. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
  323. break;
  324. case iAsBx:
  325. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
  326. break;
  327. }
  328. return buff;
  329. }
  330. #if 0
  331. void luaI_printcode (Proto *pt, int size) {
  332. int pc;
  333. for (pc=0; pc<size; pc++) {
  334. char buff[100];
  335. printf("%s\n", buildop(pt, pc, buff));
  336. }
  337. printf("-------\n");
  338. }
  339. #endif
  340. static int listcode (lua_State *L) {
  341. int pc;
  342. Proto *p;
  343. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  344. 1, "Lua function expected");
  345. p = clvalue(obj_at(L, 1))->l.p;
  346. lua_newtable(L);
  347. setnameval(L, "maxstack", p->maxstacksize);
  348. setnameval(L, "numparams", p->numparams);
  349. for (pc=0; pc<p->sizecode; pc++) {
  350. char buff[100];
  351. lua_pushinteger(L, pc+1);
  352. lua_pushstring(L, buildop(p, pc, buff));
  353. lua_settable(L, -3);
  354. }
  355. return 1;
  356. }
  357. static int listk (lua_State *L) {
  358. Proto *p;
  359. int i;
  360. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  361. 1, "Lua function expected");
  362. p = clvalue(obj_at(L, 1))->l.p;
  363. lua_createtable(L, p->sizek, 0);
  364. for (i=0; i<p->sizek; i++) {
  365. luaA_pushobject(L, p->k+i);
  366. lua_rawseti(L, -2, i+1);
  367. }
  368. return 1;
  369. }
  370. static int listlocals (lua_State *L) {
  371. Proto *p;
  372. int pc = luaL_checkint(L, 2) - 1;
  373. int i = 0;
  374. const char *name;
  375. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  376. 1, "Lua function expected");
  377. p = clvalue(obj_at(L, 1))->l.p;
  378. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  379. lua_pushstring(L, name);
  380. return i-1;
  381. }
  382. /* }====================================================== */
  383. static int get_limits (lua_State *L) {
  384. lua_createtable(L, 0, 5);
  385. setnameval(L, "BITS_INT", LUAI_BITSINT);
  386. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  387. setnameval(L, "MAXVARS", LUAI_MAXVARS);
  388. setnameval(L, "MAXSTACK", MAXSTACK);
  389. setnameval(L, "MAXUPVALUES", LUAI_MAXUPVALUES);
  390. setnameval(L, "NUM_OPCODES", NUM_OPCODES);
  391. return 1;
  392. }
  393. static int mem_query (lua_State *L) {
  394. if (lua_isnone(L, 1)) {
  395. lua_pushinteger(L, memcontrol.total);
  396. lua_pushinteger(L, memcontrol.numblocks);
  397. lua_pushinteger(L, memcontrol.maxmem);
  398. return 3;
  399. }
  400. else {
  401. memcontrol.memlimit = luaL_checkint(L, 1);
  402. return 0;
  403. }
  404. }
  405. static int settrick (lua_State *L) {
  406. Trick = lua_tointeger(L, 1);
  407. return 0;
  408. }
  409. /*static int set_gcstate (lua_State *L) {
  410. static const char *const state[] = {"propagate", "sweep", "finalize"};
  411. return 0;
  412. }*/
  413. static int get_gccolor (lua_State *L) {
  414. TValue *o;
  415. luaL_checkany(L, 1);
  416. o = obj_at(L, 1);
  417. if (!iscollectable(o))
  418. lua_pushstring(L, "no collectable");
  419. else
  420. lua_pushstring(L, iswhite(gcvalue(o)) ? "white" :
  421. isblack(gcvalue(o)) ? "black" : "grey");
  422. return 1;
  423. }
  424. static int gcstate (lua_State *L) {
  425. switch(G(L)->gcstate) {
  426. case GCSpropagate: lua_pushstring(L, "propagate"); break;
  427. case GCSsweepstring: lua_pushstring(L, "sweep strings"); break;
  428. case GCSsweep: lua_pushstring(L, "sweep"); break;
  429. case GCSfinalize: lua_pushstring(L, "finalize"); break;
  430. }
  431. return 1;
  432. }
  433. static int hash_query (lua_State *L) {
  434. if (lua_isnone(L, 2)) {
  435. luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  436. lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
  437. }
  438. else {
  439. TValue *o = obj_at(L, 1);
  440. Table *t;
  441. luaL_checktype(L, 2, LUA_TTABLE);
  442. t = hvalue(obj_at(L, 2));
  443. lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
  444. }
  445. return 1;
  446. }
  447. static int stacklevel (lua_State *L) {
  448. unsigned long a = 0;
  449. lua_pushinteger(L, (L->top - L->stack));
  450. lua_pushinteger(L, (L->stack_last - L->stack));
  451. lua_pushinteger(L, (L->ci - L->base_ci));
  452. lua_pushinteger(L, (L->end_ci - L->base_ci));
  453. lua_pushinteger(L, (unsigned long)&a);
  454. return 5;
  455. }
  456. static int table_query (lua_State *L) {
  457. const Table *t;
  458. int i = luaL_optint(L, 2, -1);
  459. luaL_checktype(L, 1, LUA_TTABLE);
  460. t = hvalue(obj_at(L, 1));
  461. if (i == -1) {
  462. lua_pushinteger(L, t->sizearray);
  463. lua_pushinteger(L, t->node == &luaH_dummynode ? 0 : sizenode(t));
  464. lua_pushinteger(L, t->lastfree - t->node);
  465. }
  466. else if (i < t->sizearray) {
  467. lua_pushinteger(L, i);
  468. luaA_pushobject(L, &t->array[i]);
  469. lua_pushnil(L);
  470. }
  471. else if ((i -= t->sizearray) < sizenode(t)) {
  472. if (!ttisnil(gval(gnode(t, i))) ||
  473. ttisnil(gkey(gnode(t, i))) ||
  474. ttisnumber(gkey(gnode(t, i)))) {
  475. luaA_pushobject(L, key2tval(gnode(t, i)));
  476. }
  477. else
  478. lua_pushliteral(L, "<undef>");
  479. luaA_pushobject(L, gval(gnode(t, i)));
  480. if (gnext(&t->node[i]))
  481. lua_pushinteger(L, gnext(&t->node[i]) - t->node);
  482. else
  483. lua_pushnil(L);
  484. }
  485. return 3;
  486. }
  487. static int string_query (lua_State *L) {
  488. stringtable *tb = &G(L)->strt;
  489. int s = luaL_optint(L, 2, 0) - 1;
  490. if (s==-1) {
  491. lua_pushinteger(L ,tb->nuse);
  492. lua_pushinteger(L ,tb->size);
  493. return 2;
  494. }
  495. else if (s < tb->size) {
  496. GCObject *ts;
  497. int n = 0;
  498. for (ts = tb->hash[s]; ts; ts = ts->gch.next) {
  499. setsvalue2s(L, L->top, gco2ts(ts));
  500. incr_top(L);
  501. n++;
  502. }
  503. return n;
  504. }
  505. return 0;
  506. }
  507. static int tref (lua_State *L) {
  508. int level = lua_gettop(L);
  509. int lock = luaL_optint(L, 2, 1);
  510. luaL_checkany(L, 1);
  511. lua_pushvalue(L, 1);
  512. lua_pushinteger(L, lua_ref(L, lock));
  513. lua_assert(lua_gettop(L) == level+1); /* +1 for result */
  514. return 1;
  515. }
  516. static int getref (lua_State *L) {
  517. int level = lua_gettop(L);
  518. lua_getref(L, luaL_checkint(L, 1));
  519. lua_assert(lua_gettop(L) == level+1);
  520. return 1;
  521. }
  522. static int unref (lua_State *L) {
  523. int level = lua_gettop(L);
  524. lua_unref(L, luaL_checkint(L, 1));
  525. lua_assert(lua_gettop(L) == level);
  526. return 0;
  527. }
  528. static int upvalue (lua_State *L) {
  529. int n = luaL_checkint(L, 2);
  530. luaL_checktype(L, 1, LUA_TFUNCTION);
  531. if (lua_isnone(L, 3)) {
  532. const char *name = lua_getupvalue(L, 1, n);
  533. if (name == NULL) return 0;
  534. lua_pushstring(L, name);
  535. return 2;
  536. }
  537. else {
  538. const char *name = lua_setupvalue(L, 1, n);
  539. lua_pushstring(L, name);
  540. return 1;
  541. }
  542. }
  543. static int newuserdata (lua_State *L) {
  544. size_t size = luaL_checkint(L, 1);
  545. char *p = cast(char *, lua_newuserdata(L, size));
  546. while (size--) *p++ = '\0';
  547. return 1;
  548. }
  549. static int pushuserdata (lua_State *L) {
  550. lua_pushlightuserdata(L, cast(void *, luaL_checkint(L, 1)));
  551. return 1;
  552. }
  553. static int udataval (lua_State *L) {
  554. lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
  555. return 1;
  556. }
  557. static int doonnewstack (lua_State *L) {
  558. lua_State *L1 = lua_newthread(L);
  559. size_t l;
  560. const char *s = luaL_checklstring(L, 1, &l);
  561. int status = luaL_loadbuffer(L1, s, l, s);
  562. if (status == 0)
  563. status = lua_pcall(L1, 0, 0, 0);
  564. lua_pushinteger(L, status);
  565. return 1;
  566. }
  567. static int s2d (lua_State *L) {
  568. lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
  569. return 1;
  570. }
  571. static int d2s (lua_State *L) {
  572. double d = luaL_checknumber(L, 1);
  573. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  574. return 1;
  575. }
  576. static int newstate (lua_State *L) {
  577. void *ud;
  578. lua_Alloc f = lua_getallocf(L, &ud);
  579. lua_State *L1 = lua_newstate(f, ud);
  580. if (L1)
  581. lua_pushinteger(L, (unsigned long)L1);
  582. else
  583. lua_pushnil(L);
  584. return 1;
  585. }
  586. static int loadlib (lua_State *L) {
  587. static const luaL_reg libs[] = {
  588. {"mathlibopen", luaopen_math},
  589. {"strlibopen", luaopen_string},
  590. {"iolibopen", luaopen_io},
  591. {"tablibopen", luaopen_table},
  592. {"dblibopen", luaopen_debug},
  593. {"baselibopen", luaopen_base},
  594. {NULL, NULL}
  595. };
  596. lua_State *L1 = cast(lua_State *,
  597. cast(unsigned long, luaL_checknumber(L, 1)));
  598. lua_pushvalue(L1, LUA_GLOBALSINDEX);
  599. luaL_openlib(L1, NULL, libs, 0);
  600. return 0;
  601. }
  602. static int closestate (lua_State *L) {
  603. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
  604. lua_close(L1);
  605. lua_unlock(L); /* close cannot unlock that */
  606. return 0;
  607. }
  608. static int doremote (lua_State *L) {
  609. lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
  610. size_t lcode;
  611. const char *code = luaL_checklstring(L, 2, &lcode);
  612. int status;
  613. lua_settop(L1, 0);
  614. status = luaL_loadbuffer(L1, code, lcode, code);
  615. if (status == 0)
  616. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  617. if (status != 0) {
  618. lua_pushnil(L);
  619. lua_pushinteger(L, status);
  620. lua_pushstring(L, lua_tostring(L1, -1));
  621. return 3;
  622. }
  623. else {
  624. int i = 0;
  625. while (!lua_isnone(L1, ++i))
  626. lua_pushstring(L, lua_tostring(L1, i));
  627. lua_pop(L1, i-1);
  628. return i-1;
  629. }
  630. }
  631. static int log2_aux (lua_State *L) {
  632. lua_pushinteger(L, luaO_log2(luaL_checkint(L, 1)));
  633. return 1;
  634. }
  635. static int int2fb_aux (lua_State *L) {
  636. int b = luaO_int2fb(luaL_checkint(L, 1));
  637. lua_pushinteger(L, b);
  638. lua_pushinteger(L, luaO_fb2int(b));
  639. return 2;
  640. }
  641. /*
  642. ** {======================================================
  643. ** function to test the API with C. It interprets a kind of assembler
  644. ** language with calls to the API, so the test can be driven by Lua code
  645. ** =======================================================
  646. */
  647. static const char *const delimits = " \t\n,;";
  648. static void skip (const char **pc) {
  649. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  650. }
  651. static int getnum_aux (lua_State *L, const char **pc) {
  652. int res = 0;
  653. int sig = 1;
  654. skip(pc);
  655. if (**pc == '.') {
  656. res = cast(int, lua_tonumber(L, -1));
  657. lua_pop(L, 1);
  658. (*pc)++;
  659. return res;
  660. }
  661. else if (**pc == '-') {
  662. sig = -1;
  663. (*pc)++;
  664. }
  665. while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
  666. return sig*res;
  667. }
  668. static const char *getname_aux (char *buff, const char **pc) {
  669. int i = 0;
  670. skip(pc);
  671. while (**pc != '\0' && !strchr(delimits, **pc))
  672. buff[i++] = *(*pc)++;
  673. buff[i] = '\0';
  674. return buff;
  675. }
  676. static int getindex_aux (lua_State *L, const char **pc) {
  677. skip(pc);
  678. switch (*(*pc)++) {
  679. case 'R': return LUA_REGISTRYINDEX;
  680. case 'G': return LUA_GLOBALSINDEX;
  681. case 'E': return LUA_ENVIRONINDEX;
  682. case 'U': return lua_upvalueindex(getnum_aux(L, pc));
  683. default: (*pc)--; return getnum_aux(L, pc);
  684. }
  685. }
  686. #define EQ(s1) (strcmp(s1, inst) == 0)
  687. #define getnum (getnum_aux(L, &pc))
  688. #define getname (getname_aux(buff, &pc))
  689. #define getindex (getindex_aux(L, &pc))
  690. static int testC (lua_State *L) {
  691. char buff[30];
  692. lua_State *L1;
  693. const char *pc;
  694. if (lua_isnumber(L, 1)) {
  695. L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
  696. pc = luaL_checkstring(L, 2);
  697. }
  698. else {
  699. L1 = L;
  700. pc = luaL_checkstring(L, 1);
  701. }
  702. for (;;) {
  703. const char *inst = getname;
  704. if EQ("") return 0;
  705. else if EQ("isnumber") {
  706. lua_pushinteger(L1, lua_isnumber(L1, getindex));
  707. }
  708. else if EQ("isstring") {
  709. lua_pushinteger(L1, lua_isstring(L1, getindex));
  710. }
  711. else if EQ("istable") {
  712. lua_pushinteger(L1, lua_istable(L1, getindex));
  713. }
  714. else if EQ("iscfunction") {
  715. lua_pushinteger(L1, lua_iscfunction(L1, getindex));
  716. }
  717. else if EQ("isfunction") {
  718. lua_pushinteger(L1, lua_isfunction(L1, getindex));
  719. }
  720. else if EQ("isuserdata") {
  721. lua_pushinteger(L1, lua_isuserdata(L1, getindex));
  722. }
  723. else if EQ("isudataval") {
  724. lua_pushinteger(L1, lua_islightuserdata(L1, getindex));
  725. }
  726. else if EQ("isnil") {
  727. lua_pushinteger(L1, lua_isnil(L1, getindex));
  728. }
  729. else if EQ("isnull") {
  730. lua_pushinteger(L1, lua_isnone(L1, getindex));
  731. }
  732. else if EQ("tonumber") {
  733. lua_pushnumber(L1, lua_tonumber(L1, getindex));
  734. }
  735. else if EQ("tostring") {
  736. const char *s = lua_tostring(L1, getindex);
  737. lua_pushstring(L1, s);
  738. }
  739. else if EQ("objsize") {
  740. lua_pushinteger(L1, lua_objsize(L1, getindex));
  741. }
  742. else if EQ("tocfunction") {
  743. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  744. }
  745. else if EQ("return") {
  746. return getnum;
  747. }
  748. else if EQ("gettop") {
  749. lua_pushinteger(L1, lua_gettop(L1));
  750. }
  751. else if EQ("settop") {
  752. lua_settop(L1, getnum);
  753. }
  754. else if EQ("pop") {
  755. lua_pop(L1, getnum);
  756. }
  757. else if EQ("pushnum") {
  758. lua_pushinteger(L1, getnum);
  759. }
  760. else if EQ("pushstring") {
  761. lua_pushstring(L1, getname);
  762. }
  763. else if EQ("pushnil") {
  764. lua_pushnil(L1);
  765. }
  766. else if EQ("pushbool") {
  767. lua_pushboolean(L1, getnum);
  768. }
  769. else if EQ("newuserdata") {
  770. lua_newuserdata(L1, getnum);
  771. }
  772. else if EQ("tobool") {
  773. lua_pushinteger(L1, lua_toboolean(L1, getindex));
  774. }
  775. else if EQ("pushvalue") {
  776. lua_pushvalue(L1, getindex);
  777. }
  778. else if EQ("pushcclosure") {
  779. lua_pushcclosure(L1, testC, getnum);
  780. }
  781. else if EQ("remove") {
  782. lua_remove(L1, getnum);
  783. }
  784. else if EQ("insert") {
  785. lua_insert(L1, getnum);
  786. }
  787. else if EQ("replace") {
  788. lua_replace(L1, getindex);
  789. }
  790. else if EQ("gettable") {
  791. lua_gettable(L1, getindex);
  792. }
  793. else if EQ("settable") {
  794. lua_settable(L1, getindex);
  795. }
  796. else if EQ("next") {
  797. lua_next(L1, -2);
  798. }
  799. else if EQ("concat") {
  800. lua_concat(L1, getnum);
  801. }
  802. else if EQ("lessthan") {
  803. int a = getindex;
  804. lua_pushboolean(L1, lua_lessthan(L1, a, getindex));
  805. }
  806. else if EQ("equal") {
  807. int a = getindex;
  808. lua_pushboolean(L1, lua_equal(L1, a, getindex));
  809. }
  810. else if EQ("rawcall") {
  811. int narg = getnum;
  812. int nres = getnum;
  813. lua_call(L1, narg, nres);
  814. }
  815. else if EQ("call") {
  816. int narg = getnum;
  817. int nres = getnum;
  818. lua_pcall(L1, narg, nres, 0);
  819. }
  820. else if EQ("loadstring") {
  821. size_t sl;
  822. const char *s = luaL_checklstring(L1, getnum, &sl);
  823. luaL_loadbuffer(L1, s, sl, s);
  824. }
  825. else if EQ("loadfile") {
  826. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  827. }
  828. else if EQ("setmetatable") {
  829. lua_setmetatable(L1, getindex);
  830. }
  831. else if EQ("getmetatable") {
  832. if (lua_getmetatable(L1, getindex) == 0)
  833. lua_pushnil(L1);
  834. }
  835. else if EQ("type") {
  836. lua_pushstring(L1, luaL_typename(L1, getnum));
  837. }
  838. else if EQ("getn") {
  839. int i = getindex;
  840. lua_pushinteger(L1, luaL_getn(L1, i));
  841. }
  842. #ifndef luaL_setn
  843. else if EQ("setn") {
  844. int i = getindex;
  845. int n = cast(int, lua_tonumber(L1, -1));
  846. luaL_setn(L1, i, n);
  847. lua_pop(L1, 1);
  848. }
  849. #endif
  850. else if EQ("throw") {
  851. #if defined(__cplusplus)
  852. static struct X { int x; } x;
  853. throw x;
  854. #else
  855. luaL_error(L1, "C++");
  856. #endif
  857. break;
  858. }
  859. else luaL_error(L, "unknown instruction %s", buff);
  860. }
  861. return 0;
  862. }
  863. /* }====================================================== */
  864. /*
  865. ** {======================================================
  866. ** tests for yield inside hooks
  867. ** =======================================================
  868. */
  869. static void yieldf (lua_State *L, lua_Debug *ar) {
  870. lua_yield(L, 0);
  871. }
  872. static int setyhook (lua_State *L) {
  873. if (lua_isnoneornil(L, 1))
  874. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  875. else {
  876. const char *smask = luaL_checkstring(L, 1);
  877. int count = luaL_optint(L, 2, 0);
  878. int mask = 0;
  879. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  880. if (count > 0) mask |= LUA_MASKCOUNT;
  881. lua_sethook(L, yieldf, mask, count);
  882. }
  883. return 0;
  884. }
  885. static int coresume (lua_State *L) {
  886. int status;
  887. lua_State *co = lua_tothread(L, 1);
  888. luaL_argcheck(L, co, 1, "coroutine expected");
  889. status = lua_resume(co, 0);
  890. if (status != 0) {
  891. lua_pushboolean(L, 0);
  892. lua_insert(L, -2);
  893. return 2; /* return false + error message */
  894. }
  895. else {
  896. lua_pushboolean(L, 1);
  897. return 1;
  898. }
  899. }
  900. /* }====================================================== */
  901. /*
  902. ** {======================================================
  903. ** tests auxlib functions
  904. ** =======================================================
  905. */
  906. static int auxgsub (lua_State *L) {
  907. const char *s1 = luaL_checkstring(L, 1);
  908. const char *s2 = luaL_checkstring(L, 2);
  909. const char *s3 = luaL_checkstring(L, 3);
  910. lua_settop(L, 3);
  911. luaL_gsub(L, s1, s2, s3);
  912. lua_assert(lua_gettop(L) == 4);
  913. return 1;
  914. }
  915. static int auxgetf (lua_State *L) {
  916. const char *s = luaL_checkstring(L, 1);
  917. lua_settop(L, 2);
  918. lua_pushstring(L, luaL_getfield(L, 2, s));
  919. lua_assert(lua_gettop(L) == 4);
  920. return 2;
  921. }
  922. static int auxsetf (lua_State *L) {
  923. const char *s = luaL_checkstring(L, 1);
  924. lua_settop(L, 3);
  925. lua_pushstring(L, luaL_setfield(L, 2, s));
  926. lua_assert(lua_gettop(L) == 3);
  927. return 1;
  928. }
  929. /* }====================================================== */
  930. static const struct luaL_reg tests_funcs[] = {
  931. {"hash", hash_query},
  932. {"limits", get_limits},
  933. {"listcode", listcode},
  934. {"listk", listk},
  935. {"listlocals", listlocals},
  936. {"loadlib", loadlib},
  937. {"stacklevel", stacklevel},
  938. {"querystr", string_query},
  939. {"querytab", table_query},
  940. {"testC", testC},
  941. {"checkmemory", lua_checkmemory},
  942. {"gccolor", get_gccolor},
  943. {"gcstate", gcstate},
  944. {"trick", settrick},
  945. {"ref", tref},
  946. {"getref", getref},
  947. {"unref", unref},
  948. {"d2s", d2s},
  949. {"s2d", s2d},
  950. {"upvalue", upvalue},
  951. {"newuserdata", newuserdata},
  952. {"pushuserdata", pushuserdata},
  953. {"udataval", udataval},
  954. {"doonnewstack", doonnewstack},
  955. {"newstate", newstate},
  956. {"closestate", closestate},
  957. {"doremote", doremote},
  958. {"log2", log2_aux},
  959. {"int2fb", int2fb_aux},
  960. {"totalmem", mem_query},
  961. {"resume", coresume},
  962. {"setyhook", setyhook},
  963. {"gsub", auxgsub},
  964. {"getfield", auxgetf},
  965. {"setfield", auxsetf},
  966. {NULL, NULL}
  967. };
  968. static void fim (void) {
  969. if (!islocked)
  970. lua_close(lua_state);
  971. lua_assert(memcontrol.numblocks == 0);
  972. lua_assert(memcontrol.total == 0);
  973. }
  974. static int l_panic (lua_State *L) {
  975. UNUSED(L);
  976. fprintf(stderr, "unable to recover; exiting\n");
  977. return 0;
  978. }
  979. int luaB_opentests (lua_State *L) {
  980. void *ud;
  981. lua_assert(lua_getallocf(L, &ud) == debug_realloc);
  982. lua_assert(ud == cast(void *, &memcontrol));
  983. lua_atpanic(L, l_panic);
  984. lua_state = L; /* keep first state to be opened */
  985. luaL_openlib(L, "T", tests_funcs, 0);
  986. atexit(fim);
  987. return 0;
  988. }
  989. #undef main
  990. int main (int argc, char *argv[]) {
  991. char *limit = getenv("MEMLIMIT");
  992. if (limit)
  993. memcontrol.memlimit = strtoul(limit, NULL, 10);
  994. return l_main(argc, argv);
  995. }
  996. #endif