ltests.c 30 KB

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