ltests.c 30 KB

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