ltests.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /*
  2. ** $Id: ltests.c,v 2.47 2008/02/19 18:55:09 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 l_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 l_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 = gch(p)->next) i++;
  141. if (p == NULL) i = -1;
  142. printf("%d:%s(%p)-%c(%02X)", i, luaT_typenames[gch(o)->tt], (void *)o,
  143. isdead(g,o)?'d':isblack(o)?'b':iswhite(o)?'w':'g', gch(o)->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) == gch((t)->value.gc)->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 = gch(uvo)->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(issweep(g));*/
  257. { if (!issweep(g))
  258. printf(">>> %d %s %02x\n", g->gcstate, luaT_typenames[gch(o)->tt], gch(o)->marked);
  259. }
  260. else {
  261. if (g->gcstate == GCSfinalize)
  262. lua_assert(iswhite(o));
  263. switch (gch(o)->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, gco2t(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 != NULL; o = gch(o)->next)
  312. checkobject(g, o);
  313. for (o = g->tmudata; o != NULL; o = gch(o)->next) {
  314. lua_assert(!isdead(g, o));
  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. case iAx:
  349. sprintf(buff+strlen(buff), "%-12s%4d", name, GETARG_Ax(i));
  350. break;
  351. }
  352. return buff;
  353. }
  354. #if 0
  355. void luaI_printcode (Proto *pt, int size) {
  356. int pc;
  357. for (pc=0; pc<size; pc++) {
  358. char buff[100];
  359. printf("%s\n", buildop(pt, pc, buff));
  360. }
  361. printf("-------\n");
  362. }
  363. #endif
  364. static int listcode (lua_State *L) {
  365. int pc;
  366. Proto *p;
  367. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  368. 1, "Lua function expected");
  369. p = clvalue(obj_at(L, 1))->l.p;
  370. lua_newtable(L);
  371. setnameval(L, "maxstack", p->maxstacksize);
  372. setnameval(L, "numparams", p->numparams);
  373. for (pc=0; pc<p->sizecode; pc++) {
  374. char buff[100];
  375. lua_pushinteger(L, pc+1);
  376. lua_pushstring(L, buildop(p, pc, buff));
  377. lua_settable(L, -3);
  378. }
  379. return 1;
  380. }
  381. static int listk (lua_State *L) {
  382. Proto *p;
  383. int i;
  384. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  385. 1, "Lua function expected");
  386. p = clvalue(obj_at(L, 1))->l.p;
  387. lua_createtable(L, p->sizek, 0);
  388. for (i=0; i<p->sizek; i++) {
  389. pushobject(L, p->k+i);
  390. lua_rawseti(L, -2, i+1);
  391. }
  392. return 1;
  393. }
  394. static int listlocals (lua_State *L) {
  395. Proto *p;
  396. int pc = luaL_checkint(L, 2) - 1;
  397. int i = 0;
  398. const char *name;
  399. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  400. 1, "Lua function expected");
  401. p = clvalue(obj_at(L, 1))->l.p;
  402. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  403. lua_pushstring(L, name);
  404. return i-1;
  405. }
  406. /* }====================================================== */
  407. static int get_limits (lua_State *L) {
  408. lua_createtable(L, 0, 5);
  409. setnameval(L, "BITS_INT", LUAI_BITSINT);
  410. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  411. setnameval(L, "MAXVARS", LUAI_MAXVARS);
  412. setnameval(L, "MAXSTACK", MAXSTACK);
  413. setnameval(L, "MAXUPVALUES", LUAI_MAXUPVALUES);
  414. setnameval(L, "NUM_OPCODES", NUM_OPCODES);
  415. return 1;
  416. }
  417. static int mem_query (lua_State *L) {
  418. if (lua_isnone(L, 1)) {
  419. lua_pushinteger(L, l_memcontrol.total);
  420. lua_pushinteger(L, l_memcontrol.numblocks);
  421. lua_pushinteger(L, l_memcontrol.maxmem);
  422. return 3;
  423. }
  424. else {
  425. l_memcontrol.memlimit = luaL_checkint(L, 1);
  426. return 0;
  427. }
  428. }
  429. static int settrick (lua_State *L) {
  430. l_Trick = lua_tointeger(L, 1);
  431. return 0;
  432. }
  433. /*static int set_gcstate (lua_State *L) {
  434. static const char *const state[] = {"propagate", "sweep", "finalize"};
  435. return 0;
  436. }*/
  437. static int get_gccolor (lua_State *L) {
  438. TValue *o;
  439. luaL_checkany(L, 1);
  440. o = obj_at(L, 1);
  441. if (!iscollectable(o))
  442. lua_pushstring(L, "no collectable");
  443. else
  444. lua_pushstring(L, iswhite(gcvalue(o)) ? "white" :
  445. isblack(gcvalue(o)) ? "black" : "grey");
  446. return 1;
  447. }
  448. static int gcstate (lua_State *L) {
  449. switch(G(L)->gcstate) {
  450. case GCSpropagate: lua_pushstring(L, "propagate"); break;
  451. case GCSsweepstring: lua_pushstring(L, "sweep strings"); break;
  452. case GCSsweeptmu: lua_pushstring(L, "sweep udata with __gc"); break;
  453. case GCSsweep: lua_pushstring(L, "sweep"); break;
  454. case GCSfinalize: lua_pushstring(L, "finalize"); break;
  455. default: lua_assert(0);
  456. }
  457. return 1;
  458. }
  459. static int hash_query (lua_State *L) {
  460. if (lua_isnone(L, 2)) {
  461. luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  462. lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
  463. }
  464. else {
  465. TValue *o = obj_at(L, 1);
  466. Table *t;
  467. luaL_checktype(L, 2, LUA_TTABLE);
  468. t = hvalue(obj_at(L, 2));
  469. lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
  470. }
  471. return 1;
  472. }
  473. static int stacklevel (lua_State *L) {
  474. unsigned long a = 0;
  475. lua_pushinteger(L, (L->top - L->stack));
  476. lua_pushinteger(L, (L->stack_last - L->stack));
  477. lua_pushinteger(L, (L->ci - L->base_ci));
  478. lua_pushinteger(L, (L->end_ci - L->base_ci));
  479. lua_pushinteger(L, (unsigned long)&a);
  480. return 5;
  481. }
  482. static int table_query (lua_State *L) {
  483. const Table *t;
  484. int i = luaL_optint(L, 2, -1);
  485. luaL_checktype(L, 1, LUA_TTABLE);
  486. t = hvalue(obj_at(L, 1));
  487. if (i == -1) {
  488. lua_pushinteger(L, t->sizearray);
  489. lua_pushinteger(L, luaH_isdummy(t->node) ? 0 : sizenode(t));
  490. lua_pushinteger(L, t->lastfree - t->node);
  491. }
  492. else if (i < t->sizearray) {
  493. lua_pushinteger(L, i);
  494. pushobject(L, &t->array[i]);
  495. lua_pushnil(L);
  496. }
  497. else if ((i -= t->sizearray) < sizenode(t)) {
  498. if (!ttisnil(gval(gnode(t, i))) ||
  499. ttisnil(gkey(gnode(t, i))) ||
  500. ttisnumber(gkey(gnode(t, i)))) {
  501. pushobject(L, key2tval(gnode(t, i)));
  502. }
  503. else
  504. lua_pushliteral(L, "<undef>");
  505. pushobject(L, gval(gnode(t, i)));
  506. if (gnext(&t->node[i]))
  507. lua_pushinteger(L, gnext(&t->node[i]) - t->node);
  508. else
  509. lua_pushnil(L);
  510. }
  511. return 3;
  512. }
  513. static int string_query (lua_State *L) {
  514. stringtable *tb = &G(L)->strt;
  515. int s = luaL_optint(L, 2, 0) - 1;
  516. if (s==-1) {
  517. lua_pushinteger(L ,tb->nuse);
  518. lua_pushinteger(L ,tb->size);
  519. return 2;
  520. }
  521. else if (s < tb->size) {
  522. GCObject *ts;
  523. int n = 0;
  524. for (ts = tb->hash[s]; ts; ts = gch(ts)->next) {
  525. setsvalue2s(L, L->top, gco2ts(ts));
  526. incr_top(L);
  527. n++;
  528. }
  529. return n;
  530. }
  531. return 0;
  532. }
  533. static int tref (lua_State *L) {
  534. int level = lua_gettop(L);
  535. int lock = luaL_optint(L, 2, 1);
  536. luaL_checkany(L, 1);
  537. lua_pushvalue(L, 1);
  538. lua_pushinteger(L, lua_ref(L, lock));
  539. lua_assert(lua_gettop(L) == level+1); /* +1 for result */
  540. return 1;
  541. }
  542. static int getref (lua_State *L) {
  543. int level = lua_gettop(L);
  544. lua_getref(L, luaL_checkint(L, 1));
  545. lua_assert(lua_gettop(L) == level+1);
  546. return 1;
  547. }
  548. static int unref (lua_State *L) {
  549. int level = lua_gettop(L);
  550. lua_unref(L, luaL_checkint(L, 1));
  551. lua_assert(lua_gettop(L) == level);
  552. return 0;
  553. }
  554. static int upvalue (lua_State *L) {
  555. int n = luaL_checkint(L, 2);
  556. luaL_checktype(L, 1, LUA_TFUNCTION);
  557. if (lua_isnone(L, 3)) {
  558. const char *name = lua_getupvalue(L, 1, n);
  559. if (name == NULL) return 0;
  560. lua_pushstring(L, name);
  561. return 2;
  562. }
  563. else {
  564. const char *name = lua_setupvalue(L, 1, n);
  565. lua_pushstring(L, name);
  566. return 1;
  567. }
  568. }
  569. static int newuserdata (lua_State *L) {
  570. size_t size = luaL_checkint(L, 1);
  571. char *p = cast(char *, lua_newuserdata(L, size));
  572. while (size--) *p++ = '\0';
  573. return 1;
  574. }
  575. static int pushuserdata (lua_State *L) {
  576. lua_pushlightuserdata(L, cast(void *, luaL_checkint(L, 1)));
  577. return 1;
  578. }
  579. static int udataval (lua_State *L) {
  580. lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
  581. return 1;
  582. }
  583. static int doonnewstack (lua_State *L) {
  584. lua_State *L1 = lua_newthread(L);
  585. size_t l;
  586. const char *s = luaL_checklstring(L, 1, &l);
  587. int status = luaL_loadbuffer(L1, s, l, s);
  588. if (status == LUA_OK)
  589. status = lua_pcall(L1, 0, 0, 0);
  590. lua_pushinteger(L, status);
  591. return 1;
  592. }
  593. static int s2d (lua_State *L) {
  594. lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
  595. return 1;
  596. }
  597. static int d2s (lua_State *L) {
  598. double d = luaL_checknumber(L, 1);
  599. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  600. return 1;
  601. }
  602. static int num2int (lua_State *L) {
  603. lua_pushinteger(L, lua_tointeger(L, 1));
  604. return 1;
  605. }
  606. static int newstate (lua_State *L) {
  607. void *ud;
  608. lua_Alloc f = lua_getallocf(L, &ud);
  609. lua_State *L1 = lua_newstate(f, ud);
  610. if (L1)
  611. lua_pushinteger(L, (unsigned long)L1);
  612. else
  613. lua_pushnil(L);
  614. return 1;
  615. }
  616. static int loadlib (lua_State *L) {
  617. static const luaL_Reg libs[] = {
  618. {"baselibopen", luaopen_base},
  619. {"dblibopen", luaopen_debug},
  620. {"iolibopen", luaopen_io},
  621. {"mathlibopen", luaopen_math},
  622. {"strlibopen", luaopen_string},
  623. {"tablibopen", luaopen_table},
  624. {"packageopen", luaopen_package},
  625. {NULL, NULL}
  626. };
  627. lua_State *L1 = cast(lua_State *,
  628. cast(unsigned long, luaL_checknumber(L, 1)));
  629. lua_pushvalue(L1, LUA_GLOBALSINDEX);
  630. luaL_register(L1, NULL, libs);
  631. return 0;
  632. }
  633. static int closestate (lua_State *L) {
  634. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
  635. lua_close(L1);
  636. return 0;
  637. }
  638. static int doremote (lua_State *L) {
  639. lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
  640. size_t lcode;
  641. const char *code = luaL_checklstring(L, 2, &lcode);
  642. int status;
  643. lua_settop(L1, 0);
  644. status = luaL_loadbuffer(L1, code, lcode, code);
  645. if (status == LUA_OK)
  646. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  647. if (status != LUA_OK) {
  648. lua_pushnil(L);
  649. lua_pushinteger(L, status);
  650. lua_pushstring(L, lua_tostring(L1, -1));
  651. return 3;
  652. }
  653. else {
  654. int i = 0;
  655. while (!lua_isnone(L1, ++i))
  656. lua_pushstring(L, lua_tostring(L1, i));
  657. lua_pop(L1, i-1);
  658. return i-1;
  659. }
  660. }
  661. static int int2fb_aux (lua_State *L) {
  662. int b = luaO_int2fb(luaL_checkint(L, 1));
  663. lua_pushinteger(L, b);
  664. lua_pushinteger(L, luaO_fb2int(b));
  665. return 2;
  666. }
  667. /*
  668. ** {======================================================
  669. ** function to test the API with C. It interprets a kind of assembler
  670. ** language with calls to the API, so the test can be driven by Lua code
  671. ** =======================================================
  672. */
  673. static const char *const delimits = " \t\n,;";
  674. static void skip (const char **pc) {
  675. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  676. }
  677. static int getnum_aux (lua_State *L, const char **pc) {
  678. int res = 0;
  679. int sig = 1;
  680. skip(pc);
  681. if (**pc == '.') {
  682. res = cast_int(lua_tonumber(L, -1));
  683. lua_pop(L, 1);
  684. (*pc)++;
  685. return res;
  686. }
  687. else if (**pc == '-') {
  688. sig = -1;
  689. (*pc)++;
  690. }
  691. while (isdigit(cast_int(**pc))) res = res*10 + (*(*pc)++) - '0';
  692. return sig*res;
  693. }
  694. static const char *getname_aux (char *buff, const char **pc) {
  695. int i = 0;
  696. skip(pc);
  697. while (**pc != '\0' && !strchr(delimits, **pc))
  698. buff[i++] = *(*pc)++;
  699. buff[i] = '\0';
  700. return buff;
  701. }
  702. static int getindex_aux (lua_State *L, const char **pc) {
  703. skip(pc);
  704. switch (*(*pc)++) {
  705. case 'R': return LUA_REGISTRYINDEX;
  706. case 'G': return LUA_GLOBALSINDEX;
  707. case 'E': return LUA_ENVIRONINDEX;
  708. case 'U': return lua_upvalueindex(getnum_aux(L, pc));
  709. default: (*pc)--; return getnum_aux(L, pc);
  710. }
  711. }
  712. #define EQ(s1) (strcmp(s1, inst) == 0)
  713. #define getnum (getnum_aux(L, &pc))
  714. #define getname (getname_aux(buff, &pc))
  715. #define getindex (getindex_aux(L, &pc))
  716. static int testC (lua_State *L) {
  717. char buff[30];
  718. lua_State *L1;
  719. const char *pc;
  720. if (lua_isnumber(L, 1)) {
  721. L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
  722. pc = luaL_checkstring(L, 2);
  723. }
  724. else {
  725. L1 = L;
  726. pc = luaL_checkstring(L, 1);
  727. }
  728. for (;;) {
  729. const char *inst = getname;
  730. if EQ("") return 0;
  731. else if EQ("isnumber") {
  732. lua_pushboolean(L1, lua_isnumber(L1, getindex));
  733. }
  734. else if EQ("isstring") {
  735. lua_pushboolean(L1, lua_isstring(L1, getindex));
  736. }
  737. else if EQ("istable") {
  738. lua_pushboolean(L1, lua_istable(L1, getindex));
  739. }
  740. else if EQ("iscfunction") {
  741. lua_pushboolean(L1, lua_iscfunction(L1, getindex));
  742. }
  743. else if EQ("isfunction") {
  744. lua_pushboolean(L1, lua_isfunction(L1, getindex));
  745. }
  746. else if EQ("isuserdata") {
  747. lua_pushboolean(L1, lua_isuserdata(L1, getindex));
  748. }
  749. else if EQ("isudataval") {
  750. lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
  751. }
  752. else if EQ("isnil") {
  753. lua_pushboolean(L1, lua_isnil(L1, getindex));
  754. }
  755. else if EQ("isnull") {
  756. lua_pushboolean(L1, lua_isnone(L1, getindex));
  757. }
  758. else if EQ("tonumber") {
  759. lua_pushnumber(L1, lua_tonumber(L1, getindex));
  760. }
  761. else if EQ("tostring") {
  762. const char *s = lua_tostring(L1, getindex);
  763. const char *s1 = lua_pushstring(L1, s);
  764. lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0);
  765. }
  766. else if EQ("objsize") {
  767. lua_pushinteger(L1, lua_objlen(L1, getindex));
  768. }
  769. else if EQ("tocfunction") {
  770. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  771. }
  772. else if EQ("return") {
  773. return getnum;
  774. }
  775. else if EQ("gettop") {
  776. lua_pushinteger(L1, lua_gettop(L1));
  777. }
  778. else if EQ("settop") {
  779. lua_settop(L1, getnum);
  780. }
  781. else if EQ("pop") {
  782. lua_pop(L1, getnum);
  783. }
  784. else if EQ("pushnum") {
  785. lua_pushinteger(L1, getnum);
  786. }
  787. else if EQ("pushstring") {
  788. lua_pushstring(L1, getname);
  789. }
  790. else if EQ("pushnil") {
  791. lua_pushnil(L1);
  792. }
  793. else if EQ("pushbool") {
  794. lua_pushboolean(L1, getnum);
  795. }
  796. else if EQ("newtable") {
  797. lua_newtable(L1);
  798. }
  799. else if EQ("newuserdata") {
  800. lua_newuserdata(L1, getnum);
  801. }
  802. else if EQ("tobool") {
  803. lua_pushboolean(L1, lua_toboolean(L1, getindex));
  804. }
  805. else if EQ("pushvalue") {
  806. lua_pushvalue(L1, getindex);
  807. }
  808. else if EQ("pushcclosure") {
  809. lua_pushcclosure(L1, testC, getnum);
  810. }
  811. else if EQ("remove") {
  812. lua_remove(L1, getnum);
  813. }
  814. else if EQ("insert") {
  815. lua_insert(L1, getnum);
  816. }
  817. else if EQ("replace") {
  818. lua_replace(L1, getindex);
  819. }
  820. else if EQ("gettable") {
  821. lua_gettable(L1, getindex);
  822. }
  823. else if EQ("settable") {
  824. lua_settable(L1, getindex);
  825. }
  826. else if EQ("next") {
  827. lua_next(L1, -2);
  828. }
  829. else if EQ("concat") {
  830. lua_concat(L1, getnum);
  831. }
  832. else if EQ("lessthan") {
  833. int a = getindex;
  834. lua_pushboolean(L1, lua_lessthan(L1, a, getindex));
  835. }
  836. else if EQ("equal") {
  837. int a = getindex;
  838. lua_pushboolean(L1, lua_equal(L1, a, getindex));
  839. }
  840. else if EQ("rawcall") {
  841. int narg = getnum;
  842. int nres = getnum;
  843. lua_call(L1, narg, nres);
  844. }
  845. else if EQ("call") {
  846. int narg = getnum;
  847. int nres = getnum;
  848. lua_pcall(L1, narg, nres, 0);
  849. }
  850. else if EQ("loadstring") {
  851. size_t sl;
  852. const char *s = luaL_checklstring(L1, getnum, &sl);
  853. luaL_loadbuffer(L1, s, sl, s);
  854. }
  855. else if EQ("loadfile") {
  856. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  857. }
  858. else if EQ("setmetatable") {
  859. lua_setmetatable(L1, getindex);
  860. }
  861. else if EQ("getmetatable") {
  862. if (lua_getmetatable(L1, getindex) == 0)
  863. lua_pushnil(L1);
  864. }
  865. else if EQ("type") {
  866. lua_pushstring(L1, luaL_typename(L1, getnum));
  867. }
  868. else if EQ("getn") {
  869. int i = getindex;
  870. lua_pushinteger(L1, lua_objlen(L1, i));
  871. }
  872. else if EQ("checkstack") {
  873. if (!lua_checkstack(L1, getnum))
  874. luaL_error(L, "C stack overflow");
  875. }
  876. else if EQ("throw") {
  877. #if defined(__cplusplus)
  878. static struct X { int x; } x;
  879. throw x;
  880. #else
  881. luaL_error(L1, "C++");
  882. #endif
  883. break;
  884. }
  885. else luaL_error(L, "unknown instruction %s", buff);
  886. }
  887. return 0;
  888. }
  889. /* }====================================================== */
  890. /*
  891. ** {======================================================
  892. ** tests for yield inside hooks
  893. ** =======================================================
  894. */
  895. static void yieldf (lua_State *L, lua_Debug *ar) {
  896. lua_yield(L, 0);
  897. }
  898. static int setyhook (lua_State *L) {
  899. if (lua_isnoneornil(L, 1))
  900. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  901. else {
  902. const char *smask = luaL_checkstring(L, 1);
  903. int count = luaL_optint(L, 2, 0);
  904. int mask = 0;
  905. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  906. if (count > 0) mask |= LUA_MASKCOUNT;
  907. lua_sethook(L, yieldf, mask, count);
  908. }
  909. return 0;
  910. }
  911. static int coresume (lua_State *L) {
  912. int status;
  913. lua_State *co = lua_tothread(L, 1);
  914. luaL_argcheck(L, co, 1, "coroutine expected");
  915. status = lua_resume(co, 0);
  916. if (status != 0) {
  917. lua_pushboolean(L, 0);
  918. lua_insert(L, -2);
  919. return 2; /* return false + error message */
  920. }
  921. else {
  922. lua_pushboolean(L, 1);
  923. return 1;
  924. }
  925. }
  926. /* }====================================================== */
  927. /*
  928. ** {======================================================
  929. ** tests auxlib functions
  930. ** =======================================================
  931. */
  932. static int auxgsub (lua_State *L) {
  933. const char *s1 = luaL_checkstring(L, 1);
  934. const char *s2 = luaL_checkstring(L, 2);
  935. const char *s3 = luaL_checkstring(L, 3);
  936. lua_settop(L, 3);
  937. luaL_gsub(L, s1, s2, s3);
  938. lua_assert(lua_gettop(L) == 4);
  939. return 1;
  940. }
  941. /* }====================================================== */
  942. static const struct luaL_Reg tests_funcs[] = {
  943. {"checkmemory", lua_checkmemory},
  944. {"closestate", closestate},
  945. {"d2s", d2s},
  946. {"doonnewstack", doonnewstack},
  947. {"doremote", doremote},
  948. {"gccolor", get_gccolor},
  949. {"gcstate", gcstate},
  950. {"getref", getref},
  951. {"gsub", auxgsub},
  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. lua_state = L; /* keep first state to be opened */
  989. luaL_register(L, "T", tests_funcs);
  990. return 0;
  991. }
  992. #endif