ltests.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /*
  2. ** $Id: ltests.c,v 2.52 2008/06/23 16:50:34 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. void *l_Trick = 0;
  31. int islocked = 0;
  32. #define obj_at(L,k) (L->ci->base+(k) - 1)
  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. /*
  43. ** {======================================================================
  44. ** Controlled version for realloc.
  45. ** =======================================================================
  46. */
  47. #define MARK 0x55 /* 01010101 (a nice pattern) */
  48. #ifndef EXTERNMEMCHECK
  49. /* full memory check */
  50. #define HEADER (sizeof(L_Umaxalign)) /* ensures maximum alignment for HEADER */
  51. #define MARKSIZE 16 /* size of marks after each block */
  52. #define blockhead(b) (cast(char *, b) - HEADER)
  53. #define setsize(newblock, size) (*cast(size_t *, newblock) = size)
  54. #define checkblocksize(b, size) (size == (*cast(size_t *, blockhead(b))))
  55. #define fillmem(mem,size) memset(mem, -MARK, size)
  56. #else
  57. /* external memory check: don't do it twice */
  58. #define HEADER 0
  59. #define MARKSIZE 0
  60. #define blockhead(b) (b)
  61. #define setsize(newblock, size) /* empty */
  62. #define checkblocksize(b,size) (1)
  63. #define fillmem(mem,size) /* empty */
  64. #endif
  65. Memcontrol l_memcontrol = {0L, 0L, 0L, 0L};
  66. static void *checkblock (void *block, size_t size) {
  67. void *b = blockhead(block);
  68. int i;
  69. for (i=0;i<MARKSIZE;i++)
  70. lua_assert(*(cast(char *, b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  71. return b;
  72. }
  73. static void freeblock (Memcontrol *mc, void *block, size_t size) {
  74. if (block) {
  75. lua_assert(checkblocksize(block, size));
  76. block = checkblock(block, size);
  77. fillmem(block, size+HEADER+MARKSIZE); /* erase block */
  78. free(block); /* free original block */
  79. mc->numblocks--;
  80. mc->total -= size;
  81. }
  82. }
  83. void *debug_realloc (void *ud, void *block, size_t oldsize, size_t size) {
  84. Memcontrol *mc = cast(Memcontrol *, ud);
  85. lua_assert((oldsize == 0) ? block == NULL :
  86. block && checkblocksize(block, oldsize));
  87. if (mc->memlimit == 0) { /* first time? */
  88. char *limit = getenv("MEMLIMIT"); /* initialize memory limit */
  89. mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX;
  90. }
  91. if (size == 0) {
  92. freeblock(mc, block, oldsize);
  93. return NULL;
  94. }
  95. else if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
  96. return NULL; /* to test memory allocation errors */
  97. else {
  98. void *newblock;
  99. int i;
  100. size_t realsize = HEADER+size+MARKSIZE;
  101. size_t commonsize = (oldsize < size) ? oldsize : size;
  102. if (realsize < size) return NULL; /* overflow! */
  103. newblock = malloc(realsize); /* alloc a new block */
  104. if (newblock == NULL) return NULL;
  105. if (block) {
  106. memcpy(cast(char *, newblock)+HEADER, block, commonsize);
  107. freeblock(mc, block, oldsize); /* erase (and check) old copy */
  108. }
  109. /* initialize new part of the block with something `weird' */
  110. fillmem(cast(char *, newblock)+HEADER+commonsize, size-commonsize);
  111. mc->total += size;
  112. if (mc->total > mc->maxmem)
  113. mc->maxmem = mc->total;
  114. mc->numblocks++;
  115. setsize(newblock, size);
  116. for (i=0;i<MARKSIZE;i++)
  117. *(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
  118. return cast(char *, newblock)+HEADER;
  119. }
  120. }
  121. /* }====================================================================== */
  122. /*
  123. ** {======================================================
  124. ** Functions to check memory consistency
  125. ** =======================================================
  126. */
  127. static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
  128. if (isdead(g,t)) return 0;
  129. if (g->gcstate == GCSpropagate)
  130. return !isblack(f) || !iswhite(t);
  131. else if (g->gcstate == GCSfinalize)
  132. return iswhite(f);
  133. else
  134. return 1;
  135. }
  136. static void printobj (global_State *g, GCObject *o) {
  137. int i = 0;
  138. GCObject *p;
  139. for (p = g->rootgc; p != o && p != NULL; p = gch(p)->next) i++;
  140. if (p == NULL) i = -1;
  141. printf("%d:%s(%p)-%c(%02X)", i, luaT_typenames[gch(o)->tt], (void *)o,
  142. isdead(g,o)?'d':isblack(o)?'b':iswhite(o)?'w':'g', gch(o)->marked);
  143. }
  144. static int testobjref (global_State *g, GCObject *f, GCObject *t) {
  145. int r = testobjref1(g,f,t);
  146. if (!r) {
  147. printf("%d(%02X) - ", g->gcstate, g->currentwhite);
  148. printobj(g, f);
  149. printf("\t-> ");
  150. printobj(g, t);
  151. printf("\n");
  152. }
  153. return r;
  154. }
  155. #define checkobjref(g,f,t) lua_assert(testobjref(g,f,obj2gco(t)))
  156. #define checkvalref(g,f,t) lua_assert(!iscollectable(t) || \
  157. ((ttype(t) == gch((t)->value.gc)->tt) && testobjref(g,f,gcvalue(t))))
  158. static void checktable (global_State *g, Table *h) {
  159. int i;
  160. int weakkey = 0;
  161. int weakvalue = 0;
  162. const TValue *mode;
  163. GCObject *hgc = obj2gco(h);
  164. if (h->metatable)
  165. checkobjref(g, hgc, h->metatable);
  166. mode = gfasttm(g, h->metatable, TM_MODE);
  167. if (mode && ttisstring(mode)) { /* is there a weak mode? */
  168. weakkey = (strchr(svalue(mode), 'k') != NULL);
  169. weakvalue = (strchr(svalue(mode), 'v') != NULL);
  170. }
  171. i = h->sizearray;
  172. while (i--)
  173. checkvalref(g, hgc, &h->array[i]);
  174. i = sizenode(h);
  175. while (i--) {
  176. Node *n = gnode(h, i);
  177. if (!ttisnil(gval(n))) {
  178. lua_assert(!ttisnil(gkey(n)));
  179. checkvalref(g, hgc, gkey(n));
  180. checkvalref(g, hgc, gval(n));
  181. }
  182. }
  183. }
  184. /*
  185. ** All marks are conditional because a GC may happen while the
  186. ** prototype is still being created
  187. */
  188. static void checkproto (global_State *g, Proto *f) {
  189. int i;
  190. GCObject *fgc = obj2gco(f);
  191. if (f->source) checkobjref(g, fgc, f->source);
  192. for (i=0; i<f->sizek; i++) {
  193. if (ttisstring(f->k+i))
  194. checkobjref(g, fgc, rawtsvalue(f->k+i));
  195. }
  196. for (i=0; i<f->sizeupvalues; i++) {
  197. if (f->upvalues[i])
  198. checkobjref(g, fgc, f->upvalues[i]);
  199. }
  200. for (i=0; i<f->sizep; i++) {
  201. if (f->p[i])
  202. checkobjref(g, fgc, f->p[i]);
  203. }
  204. for (i=0; i<f->sizelocvars; i++) {
  205. if (f->locvars[i].varname)
  206. checkobjref(g, fgc, f->locvars[i].varname);
  207. }
  208. }
  209. static void checkclosure (global_State *g, Closure *cl) {
  210. GCObject *clgc = obj2gco(cl);
  211. checkobjref(g, clgc, cl->l.env);
  212. if (cl->c.isC) {
  213. int i;
  214. for (i=0; i<cl->c.nupvalues; i++)
  215. checkvalref(g, clgc, &cl->c.upvalue[i]);
  216. }
  217. else {
  218. int i;
  219. lua_assert(cl->l.nupvalues == cl->l.p->nups);
  220. checkobjref(g, clgc, cl->l.p);
  221. for (i=0; i<cl->l.nupvalues; i++) {
  222. if (cl->l.upvals[i]) {
  223. lua_assert(cl->l.upvals[i]->tt == LUA_TUPVAL);
  224. checkobjref(g, clgc, cl->l.upvals[i]);
  225. }
  226. }
  227. }
  228. }
  229. static void checkstack (global_State *g, lua_State *L1) {
  230. StkId o;
  231. CallInfo *ci;
  232. GCObject *uvo;
  233. lua_assert(!isdead(g, obj2gco(L1)));
  234. for (uvo = L1->openupval; uvo != NULL; uvo = gch(uvo)->next) {
  235. UpVal *uv = gco2uv(uvo);
  236. lua_assert(uv->v != &uv->u.value); /* must be open */
  237. lua_assert(!isblack(uvo)); /* open upvalues cannot be black */
  238. }
  239. checkliveness(g, gt(L1));
  240. if (L1->base_ci) {
  241. for (ci = L1->base_ci; ci <= L1->ci; ci++) {
  242. lua_assert(ci->top <= L1->stack_last);
  243. lua_assert(lua_checkpc(L1, ci));
  244. }
  245. }
  246. else lua_assert(L1->size_ci == 0);
  247. if (L1->stack) {
  248. for (o = L1->stack; o < L1->top; o++)
  249. checkliveness(g, o);
  250. }
  251. else lua_assert(L1->stacksize == 0);
  252. }
  253. static void checkobject (global_State *g, GCObject *o) {
  254. if (isdead(g, o))
  255. /* lua_assert(issweep(g));*/
  256. { if (!issweep(g))
  257. printf(">>> %d %s %02x\n", g->gcstate, luaT_typenames[gch(o)->tt], gch(o)->marked);
  258. }
  259. else {
  260. if (g->gcstate == GCSfinalize)
  261. lua_assert(iswhite(o));
  262. switch (gch(o)->tt) {
  263. case LUA_TUPVAL: {
  264. UpVal *uv = gco2uv(o);
  265. lua_assert(uv->v == &uv->u.value); /* must be closed */
  266. lua_assert(!isgray(o)); /* closed upvalues are never gray */
  267. checkvalref(g, o, uv->v);
  268. break;
  269. }
  270. case LUA_TUSERDATA: {
  271. Table *mt = gco2u(o)->metatable;
  272. if (mt) checkobjref(g, o, mt);
  273. break;
  274. }
  275. case LUA_TTABLE: {
  276. checktable(g, gco2t(o));
  277. break;
  278. }
  279. case LUA_TTHREAD: {
  280. checkstack(g, gco2th(o));
  281. break;
  282. }
  283. case LUA_TFUNCTION: {
  284. checkclosure(g, gco2cl(o));
  285. break;
  286. }
  287. case LUA_TPROTO: {
  288. checkproto(g, gco2p(o));
  289. break;
  290. }
  291. default: lua_assert(0);
  292. }
  293. }
  294. }
  295. int lua_checkpc (lua_State *L, pCallInfo ci) {
  296. if (ci == L->base_ci || !f_isLua(ci)) return 1;
  297. else {
  298. Proto *p = ci_func(ci)->l.p;
  299. if (ci < L->ci)
  300. return p->code <= ci->savedpc && ci->savedpc <= p->code + p->sizecode;
  301. else
  302. return p->code <= L->savedpc && L->savedpc <= p->code + p->sizecode;
  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 = getline(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 = clvalue(obj_at(L, 1))->l.p;
  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 = clvalue(obj_at(L, 1))->l.p;
  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 = clvalue(obj_at(L, 1))->l.p;
  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, "MAXUPVALUES", LUAI_MAXUPVALUES);
  418. setnameval(L, "NUM_OPCODES", NUM_OPCODES);
  419. return 1;
  420. }
  421. static int mem_query (lua_State *L) {
  422. if (lua_isnone(L, 1)) {
  423. lua_pushinteger(L, l_memcontrol.total);
  424. lua_pushinteger(L, l_memcontrol.numblocks);
  425. lua_pushinteger(L, l_memcontrol.maxmem);
  426. return 3;
  427. }
  428. else {
  429. l_memcontrol.memlimit = luaL_checkint(L, 1);
  430. return 0;
  431. }
  432. }
  433. static int settrick (lua_State *L) {
  434. l_Trick = (L->base)->value.gc;
  435. return 0;
  436. }
  437. /*static int set_gcstate (lua_State *L) {
  438. static const char *const state[] = {"propagate", "sweep", "finalize"};
  439. return 0;
  440. }*/
  441. static int get_gccolor (lua_State *L) {
  442. TValue *o;
  443. luaL_checkany(L, 1);
  444. o = obj_at(L, 1);
  445. if (!iscollectable(o))
  446. lua_pushstring(L, "no collectable");
  447. else
  448. lua_pushstring(L, iswhite(gcvalue(o)) ? "white" :
  449. isblack(gcvalue(o)) ? "black" : "grey");
  450. return 1;
  451. }
  452. static int gcstate (lua_State *L) {
  453. switch(G(L)->gcstate) {
  454. case GCSpropagate: lua_pushstring(L, "propagate"); break;
  455. case GCSsweepstring: lua_pushstring(L, "sweep strings"); break;
  456. case GCSsweep: lua_pushstring(L, "sweep"); break;
  457. case GCSfinalize: lua_pushstring(L, "finalize"); break;
  458. default: lua_assert(0);
  459. }
  460. return 1;
  461. }
  462. static int hash_query (lua_State *L) {
  463. if (lua_isnone(L, 2)) {
  464. luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  465. lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
  466. }
  467. else {
  468. TValue *o = obj_at(L, 1);
  469. Table *t;
  470. luaL_checktype(L, 2, LUA_TTABLE);
  471. t = hvalue(obj_at(L, 2));
  472. lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
  473. }
  474. return 1;
  475. }
  476. static int stacklevel (lua_State *L) {
  477. unsigned long a = 0;
  478. lua_pushinteger(L, (L->top - L->stack));
  479. lua_pushinteger(L, (L->stack_last - L->stack));
  480. lua_pushinteger(L, (L->ci - L->base_ci));
  481. lua_pushinteger(L, (L->end_ci - L->base_ci));
  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_pushinteger(L, (unsigned long)L1);
  615. else
  616. lua_pushnil(L);
  617. return 1;
  618. }
  619. static int loadlib (lua_State *L) {
  620. static const luaL_Reg libs[] = {
  621. {"baselibopen", luaopen_base},
  622. {"dblibopen", luaopen_debug},
  623. {"iolibopen", luaopen_io},
  624. {"mathlibopen", luaopen_math},
  625. {"strlibopen", luaopen_string},
  626. {"tablibopen", luaopen_table},
  627. {"packageopen", luaopen_package},
  628. {NULL, NULL}
  629. };
  630. lua_State *L1 = cast(lua_State *,
  631. cast(unsigned long, luaL_checknumber(L, 1)));
  632. lua_pushvalue(L1, LUA_GLOBALSINDEX);
  633. luaL_register(L1, NULL, libs);
  634. return 0;
  635. }
  636. static int closestate (lua_State *L) {
  637. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
  638. lua_close(L1);
  639. return 0;
  640. }
  641. static int doremote (lua_State *L) {
  642. lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
  643. size_t lcode;
  644. const char *code = luaL_checklstring(L, 2, &lcode);
  645. int status;
  646. lua_settop(L1, 0);
  647. status = luaL_loadbuffer(L1, code, lcode, code);
  648. if (status == LUA_OK)
  649. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  650. if (status != LUA_OK) {
  651. lua_pushnil(L);
  652. lua_pushinteger(L, status);
  653. lua_pushstring(L, lua_tostring(L1, -1));
  654. return 3;
  655. }
  656. else {
  657. int i = 0;
  658. while (!lua_isnone(L1, ++i))
  659. lua_pushstring(L, lua_tostring(L1, i));
  660. lua_pop(L1, i-1);
  661. return i-1;
  662. }
  663. }
  664. static int int2fb_aux (lua_State *L) {
  665. int b = luaO_int2fb(luaL_checkint(L, 1));
  666. lua_pushinteger(L, b);
  667. lua_pushinteger(L, luaO_fb2int(b));
  668. return 2;
  669. }
  670. /*
  671. ** {======================================================
  672. ** function to test the API with C. It interprets a kind of assembler
  673. ** language with calls to the API, so the test can be driven by Lua code
  674. ** =======================================================
  675. */
  676. static const char *const delimits = " \t\n,;";
  677. static void skip (const char **pc) {
  678. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  679. }
  680. static int getnum_aux (lua_State *L, const char **pc) {
  681. int res = 0;
  682. int sig = 1;
  683. skip(pc);
  684. if (**pc == '.') {
  685. res = cast_int(lua_tonumber(L, -1));
  686. lua_pop(L, 1);
  687. (*pc)++;
  688. return res;
  689. }
  690. else if (**pc == '-') {
  691. sig = -1;
  692. (*pc)++;
  693. }
  694. while (isdigit(cast_int(**pc))) res = res*10 + (*(*pc)++) - '0';
  695. return sig*res;
  696. }
  697. static const char *getname_aux (char *buff, const char **pc) {
  698. int i = 0;
  699. skip(pc);
  700. while (**pc != '\0' && !strchr(delimits, **pc))
  701. buff[i++] = *(*pc)++;
  702. buff[i] = '\0';
  703. return buff;
  704. }
  705. static int getindex_aux (lua_State *L, const char **pc) {
  706. skip(pc);
  707. switch (*(*pc)++) {
  708. case 'R': return LUA_REGISTRYINDEX;
  709. case 'G': return LUA_GLOBALSINDEX;
  710. case 'E': return LUA_ENVIRONINDEX;
  711. case 'U': return lua_upvalueindex(getnum_aux(L, pc));
  712. default: (*pc)--; return getnum_aux(L, pc);
  713. }
  714. }
  715. #define EQ(s1) (strcmp(s1, inst) == 0)
  716. #define getnum (getnum_aux(L, &pc))
  717. #define getname (getname_aux(buff, &pc))
  718. #define getindex (getindex_aux(L, &pc))
  719. static int testC (lua_State *L) {
  720. char buff[30];
  721. lua_State *L1;
  722. const char *pc;
  723. if (lua_isnumber(L, 1)) {
  724. L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
  725. pc = luaL_checkstring(L, 2);
  726. }
  727. else {
  728. L1 = L;
  729. pc = luaL_checkstring(L, 1);
  730. }
  731. for (;;) {
  732. const char *inst = getname;
  733. if EQ("") return 0;
  734. else if EQ("isnumber") {
  735. lua_pushboolean(L1, lua_isnumber(L1, getindex));
  736. }
  737. else if EQ("isstring") {
  738. lua_pushboolean(L1, lua_isstring(L1, getindex));
  739. }
  740. else if EQ("istable") {
  741. lua_pushboolean(L1, lua_istable(L1, getindex));
  742. }
  743. else if EQ("iscfunction") {
  744. lua_pushboolean(L1, lua_iscfunction(L1, getindex));
  745. }
  746. else if EQ("isfunction") {
  747. lua_pushboolean(L1, lua_isfunction(L1, getindex));
  748. }
  749. else if EQ("isuserdata") {
  750. lua_pushboolean(L1, lua_isuserdata(L1, getindex));
  751. }
  752. else if EQ("isudataval") {
  753. lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
  754. }
  755. else if EQ("isnil") {
  756. lua_pushboolean(L1, lua_isnil(L1, getindex));
  757. }
  758. else if EQ("isnull") {
  759. lua_pushboolean(L1, lua_isnone(L1, getindex));
  760. }
  761. else if EQ("tonumber") {
  762. lua_pushnumber(L1, lua_tonumber(L1, getindex));
  763. }
  764. else if EQ("tostring") {
  765. const char *s = lua_tostring(L1, getindex);
  766. const char *s1 = lua_pushstring(L1, s);
  767. lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0);
  768. }
  769. else if EQ("objsize") {
  770. lua_pushinteger(L1, lua_objlen(L1, getindex));
  771. }
  772. else if EQ("tocfunction") {
  773. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  774. }
  775. else if EQ("return") {
  776. return getnum;
  777. }
  778. else if EQ("gettop") {
  779. lua_pushinteger(L1, lua_gettop(L1));
  780. }
  781. else if EQ("settop") {
  782. lua_settop(L1, getnum);
  783. }
  784. else if EQ("pop") {
  785. lua_pop(L1, getnum);
  786. }
  787. else if EQ("pushnum") {
  788. lua_pushinteger(L1, getnum);
  789. }
  790. else if EQ("pushstring") {
  791. lua_pushstring(L1, getname);
  792. }
  793. else if EQ("pushnil") {
  794. lua_pushnil(L1);
  795. }
  796. else if EQ("pushbool") {
  797. lua_pushboolean(L1, getnum);
  798. }
  799. else if EQ("newtable") {
  800. lua_newtable(L1);
  801. }
  802. else if EQ("newuserdata") {
  803. lua_newuserdata(L1, getnum);
  804. }
  805. else if EQ("tobool") {
  806. lua_pushboolean(L1, lua_toboolean(L1, getindex));
  807. }
  808. else if EQ("pushvalue") {
  809. lua_pushvalue(L1, getindex);
  810. }
  811. else if EQ("pushcclosure") {
  812. lua_pushcclosure(L1, testC, getnum);
  813. }
  814. else if EQ("remove") {
  815. lua_remove(L1, getnum);
  816. }
  817. else if EQ("insert") {
  818. lua_insert(L1, getnum);
  819. }
  820. else if EQ("replace") {
  821. lua_replace(L1, getindex);
  822. }
  823. else if EQ("gettable") {
  824. lua_gettable(L1, getindex);
  825. }
  826. else if EQ("settable") {
  827. lua_settable(L1, getindex);
  828. }
  829. else if EQ("next") {
  830. lua_next(L1, -2);
  831. }
  832. else if EQ("concat") {
  833. lua_concat(L1, getnum);
  834. }
  835. else if EQ("lessthan") {
  836. int a = getindex;
  837. lua_pushboolean(L1, lua_lessthan(L1, a, getindex));
  838. }
  839. else if EQ("equal") {
  840. int a = getindex;
  841. lua_pushboolean(L1, lua_equal(L1, a, getindex));
  842. }
  843. else if EQ("rawcall") {
  844. int narg = getnum;
  845. int nres = getnum;
  846. lua_call(L1, narg, nres);
  847. }
  848. else if EQ("call") {
  849. int narg = getnum;
  850. int nres = getnum;
  851. lua_pcall(L1, narg, nres, 0);
  852. }
  853. else if EQ("loadstring") {
  854. size_t sl;
  855. const char *s = luaL_checklstring(L1, getnum, &sl);
  856. luaL_loadbuffer(L1, s, sl, s);
  857. }
  858. else if EQ("loadfile") {
  859. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  860. }
  861. else if EQ("setmetatable") {
  862. lua_setmetatable(L1, getindex);
  863. }
  864. else if EQ("getmetatable") {
  865. if (lua_getmetatable(L1, getindex) == 0)
  866. lua_pushnil(L1);
  867. }
  868. else if EQ("type") {
  869. lua_pushstring(L1, luaL_typename(L1, getnum));
  870. }
  871. else if EQ("getn") {
  872. int i = getindex;
  873. lua_pushinteger(L1, lua_objlen(L1, i));
  874. }
  875. else if EQ("checkstack") {
  876. if (!lua_checkstack(L1, getnum))
  877. luaL_error(L, "C stack overflow");
  878. }
  879. else if EQ("newmetatable") {
  880. lua_pushboolean(L1, luaL_newmetatable(L1, getname));
  881. }
  882. else if EQ("testudata") {
  883. int i = getindex;
  884. lua_pushboolean(L1, luaL_testudata(L1, i, getname) != NULL);
  885. }
  886. else if EQ("gsub") {
  887. int a = getnum; int b = getnum; int c = getnum;
  888. luaL_gsub(L1, lua_tostring(L1, a),
  889. lua_tostring(L, b),
  890. lua_tostring(L, c));
  891. }
  892. else if EQ("throw") {
  893. #if defined(__cplusplus)
  894. static struct X { int x; } x;
  895. throw x;
  896. #else
  897. luaL_error(L1, "C++");
  898. #endif
  899. break;
  900. }
  901. else luaL_error(L, "unknown instruction %s", buff);
  902. }
  903. return 0;
  904. }
  905. /* }====================================================== */
  906. /*
  907. ** {======================================================
  908. ** tests for yield inside hooks
  909. ** =======================================================
  910. */
  911. static void yieldf (lua_State *L, lua_Debug *ar) {
  912. lua_yield(L, 0);
  913. }
  914. static int setyhook (lua_State *L) {
  915. if (lua_isnoneornil(L, 1))
  916. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  917. else {
  918. const char *smask = luaL_checkstring(L, 1);
  919. int count = luaL_optint(L, 2, 0);
  920. int mask = 0;
  921. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  922. if (count > 0) mask |= LUA_MASKCOUNT;
  923. lua_sethook(L, yieldf, mask, count);
  924. }
  925. return 0;
  926. }
  927. static int coresume (lua_State *L) {
  928. int status;
  929. lua_State *co = lua_tothread(L, 1);
  930. luaL_argcheck(L, co, 1, "coroutine expected");
  931. status = lua_resume(co, 0);
  932. if (status != 0) {
  933. lua_pushboolean(L, 0);
  934. lua_insert(L, -2);
  935. return 2; /* return false + error message */
  936. }
  937. else {
  938. lua_pushboolean(L, 1);
  939. return 1;
  940. }
  941. }
  942. /* }====================================================== */
  943. static const struct luaL_Reg tests_funcs[] = {
  944. {"checkmemory", lua_checkmemory},
  945. {"closestate", closestate},
  946. {"d2s", d2s},
  947. {"doonnewstack", doonnewstack},
  948. {"doremote", doremote},
  949. {"gccolor", get_gccolor},
  950. {"gcstate", gcstate},
  951. {"getref", getref},
  952. {"hash", hash_query},
  953. {"int2fb", int2fb_aux},
  954. {"limits", get_limits},
  955. {"listcode", listcode},
  956. {"listk", listk},
  957. {"listlocals", listlocals},
  958. {"loadlib", loadlib},
  959. {"newstate", newstate},
  960. {"newuserdata", newuserdata},
  961. {"num2int", num2int},
  962. {"pushuserdata", pushuserdata},
  963. {"querystr", string_query},
  964. {"querytab", table_query},
  965. {"ref", tref},
  966. {"resume", coresume},
  967. {"s2d", s2d},
  968. {"setyhook", setyhook},
  969. {"stacklevel", stacklevel},
  970. {"testC", testC},
  971. {"totalmem", mem_query},
  972. {"trick", settrick},
  973. {"udataval", udataval},
  974. {"unref", unref},
  975. {"upvalue", upvalue},
  976. {NULL, NULL}
  977. };
  978. static void checkfinalmem (void) {
  979. lua_assert(l_memcontrol.numblocks == 0);
  980. lua_assert(l_memcontrol.total == 0);
  981. }
  982. int luaB_opentests (lua_State *L) {
  983. void *ud;
  984. atexit(checkfinalmem);
  985. lua_assert(lua_getallocf(L, &ud) == debug_realloc);
  986. lua_assert(ud == cast(void *, &l_memcontrol));
  987. lua_setallocf(L, lua_getallocf(L, NULL), ud);
  988. luaL_register(L, "T", tests_funcs);
  989. return 0;
  990. }
  991. #endif