ltests.c 28 KB

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