ltests.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /*
  2. ** $Id: ltests.c,v 2.51 2008/06/13 17:07:10 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 != NULL; o = gch(o)->next)
  311. checkobject(g, o);
  312. for (o = g->tmudata; o != NULL; o = gch(o)->next) {
  313. lua_assert(!isdead(g, o));
  314. checkobject(g, o);
  315. }
  316. for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
  317. lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
  318. lua_assert(uv->v != &uv->u.value); /* must be open */
  319. lua_assert(!isblack(obj2gco(uv))); /* open upvalues are never black */
  320. checkvalref(g, obj2gco(uv), uv->v);
  321. }
  322. return 0;
  323. }
  324. /* }====================================================== */
  325. /*
  326. ** {======================================================
  327. ** Disassembler
  328. ** =======================================================
  329. */
  330. static char *buildop (Proto *p, int pc, char *buff) {
  331. Instruction i = p->code[pc];
  332. OpCode o = GET_OPCODE(i);
  333. const char *name = luaP_opnames[o];
  334. int line = getline(p, pc);
  335. sprintf(buff, "(%4d) %4d - ", line, pc);
  336. switch (getOpMode(o)) {
  337. case iABC:
  338. sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
  339. GETARG_A(i), GETARG_B(i), GETARG_C(i));
  340. break;
  341. case iABx:
  342. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
  343. break;
  344. case iAsBx:
  345. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
  346. break;
  347. case iAx:
  348. sprintf(buff+strlen(buff), "%-12s%4d", name, GETARG_Ax(i));
  349. break;
  350. }
  351. return buff;
  352. }
  353. #if 0
  354. void luaI_printcode (Proto *pt, int size) {
  355. int pc;
  356. for (pc=0; pc<size; pc++) {
  357. char buff[100];
  358. printf("%s\n", buildop(pt, pc, buff));
  359. }
  360. printf("-------\n");
  361. }
  362. #endif
  363. static int listcode (lua_State *L) {
  364. int pc;
  365. Proto *p;
  366. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  367. 1, "Lua function expected");
  368. p = clvalue(obj_at(L, 1))->l.p;
  369. lua_newtable(L);
  370. setnameval(L, "maxstack", p->maxstacksize);
  371. setnameval(L, "numparams", p->numparams);
  372. for (pc=0; pc<p->sizecode; pc++) {
  373. char buff[100];
  374. lua_pushinteger(L, pc+1);
  375. lua_pushstring(L, buildop(p, pc, buff));
  376. lua_settable(L, -3);
  377. }
  378. return 1;
  379. }
  380. static int listk (lua_State *L) {
  381. Proto *p;
  382. int i;
  383. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  384. 1, "Lua function expected");
  385. p = clvalue(obj_at(L, 1))->l.p;
  386. lua_createtable(L, p->sizek, 0);
  387. for (i=0; i<p->sizek; i++) {
  388. pushobject(L, p->k+i);
  389. lua_rawseti(L, -2, i+1);
  390. }
  391. return 1;
  392. }
  393. static int listlocals (lua_State *L) {
  394. Proto *p;
  395. int pc = luaL_checkint(L, 2) - 1;
  396. int i = 0;
  397. const char *name;
  398. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  399. 1, "Lua function expected");
  400. p = clvalue(obj_at(L, 1))->l.p;
  401. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  402. lua_pushstring(L, name);
  403. return i-1;
  404. }
  405. /* }====================================================== */
  406. static int get_limits (lua_State *L) {
  407. lua_createtable(L, 0, 5);
  408. setnameval(L, "BITS_INT", LUAI_BITSINT);
  409. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  410. setnameval(L, "MAXVARS", LUAI_MAXVARS);
  411. setnameval(L, "MAXSTACK", MAXSTACK);
  412. setnameval(L, "MAXUPVALUES", LUAI_MAXUPVALUES);
  413. setnameval(L, "NUM_OPCODES", NUM_OPCODES);
  414. return 1;
  415. }
  416. static int mem_query (lua_State *L) {
  417. if (lua_isnone(L, 1)) {
  418. lua_pushinteger(L, l_memcontrol.total);
  419. lua_pushinteger(L, l_memcontrol.numblocks);
  420. lua_pushinteger(L, l_memcontrol.maxmem);
  421. return 3;
  422. }
  423. else {
  424. l_memcontrol.memlimit = luaL_checkint(L, 1);
  425. return 0;
  426. }
  427. }
  428. static int settrick (lua_State *L) {
  429. l_Trick = (L->base)->value.gc;
  430. return 0;
  431. }
  432. /*static int set_gcstate (lua_State *L) {
  433. static const char *const state[] = {"propagate", "sweep", "finalize"};
  434. return 0;
  435. }*/
  436. static int get_gccolor (lua_State *L) {
  437. TValue *o;
  438. luaL_checkany(L, 1);
  439. o = obj_at(L, 1);
  440. if (!iscollectable(o))
  441. lua_pushstring(L, "no collectable");
  442. else
  443. lua_pushstring(L, iswhite(gcvalue(o)) ? "white" :
  444. isblack(gcvalue(o)) ? "black" : "grey");
  445. return 1;
  446. }
  447. static int gcstate (lua_State *L) {
  448. switch(G(L)->gcstate) {
  449. case GCSpropagate: lua_pushstring(L, "propagate"); break;
  450. case GCSsweepstring: lua_pushstring(L, "sweep strings"); break;
  451. case GCSsweeptmu: lua_pushstring(L, "sweep udata with __gc"); break;
  452. case GCSsweep: lua_pushstring(L, "sweep"); break;
  453. case GCSfinalize: lua_pushstring(L, "finalize"); break;
  454. default: lua_assert(0);
  455. }
  456. return 1;
  457. }
  458. static int hash_query (lua_State *L) {
  459. if (lua_isnone(L, 2)) {
  460. luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  461. lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
  462. }
  463. else {
  464. TValue *o = obj_at(L, 1);
  465. Table *t;
  466. luaL_checktype(L, 2, LUA_TTABLE);
  467. t = hvalue(obj_at(L, 2));
  468. lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
  469. }
  470. return 1;
  471. }
  472. static int stacklevel (lua_State *L) {
  473. unsigned long a = 0;
  474. lua_pushinteger(L, (L->top - L->stack));
  475. lua_pushinteger(L, (L->stack_last - L->stack));
  476. lua_pushinteger(L, (L->ci - L->base_ci));
  477. lua_pushinteger(L, (L->end_ci - L->base_ci));
  478. lua_pushinteger(L, (unsigned long)&a);
  479. return 5;
  480. }
  481. static int table_query (lua_State *L) {
  482. const Table *t;
  483. int i = luaL_optint(L, 2, -1);
  484. luaL_checktype(L, 1, LUA_TTABLE);
  485. t = hvalue(obj_at(L, 1));
  486. if (i == -1) {
  487. lua_pushinteger(L, t->sizearray);
  488. lua_pushinteger(L, luaH_isdummy(t->node) ? 0 : sizenode(t));
  489. lua_pushinteger(L, t->lastfree - t->node);
  490. }
  491. else if (i < t->sizearray) {
  492. lua_pushinteger(L, i);
  493. pushobject(L, &t->array[i]);
  494. lua_pushnil(L);
  495. }
  496. else if ((i -= t->sizearray) < sizenode(t)) {
  497. if (!ttisnil(gval(gnode(t, i))) ||
  498. ttisnil(gkey(gnode(t, i))) ||
  499. ttisnumber(gkey(gnode(t, i)))) {
  500. pushobject(L, key2tval(gnode(t, i)));
  501. }
  502. else
  503. lua_pushliteral(L, "<undef>");
  504. pushobject(L, gval(gnode(t, i)));
  505. if (gnext(&t->node[i]))
  506. lua_pushinteger(L, gnext(&t->node[i]) - t->node);
  507. else
  508. lua_pushnil(L);
  509. }
  510. return 3;
  511. }
  512. static int string_query (lua_State *L) {
  513. stringtable *tb = &G(L)->strt;
  514. int s = luaL_optint(L, 2, 0) - 1;
  515. if (s==-1) {
  516. lua_pushinteger(L ,tb->nuse);
  517. lua_pushinteger(L ,tb->size);
  518. return 2;
  519. }
  520. else if (s < tb->size) {
  521. GCObject *ts;
  522. int n = 0;
  523. for (ts = tb->hash[s]; ts; ts = gch(ts)->next) {
  524. setsvalue2s(L, L->top, gco2ts(ts));
  525. incr_top(L);
  526. n++;
  527. }
  528. return n;
  529. }
  530. return 0;
  531. }
  532. static int tref (lua_State *L) {
  533. int level = lua_gettop(L);
  534. int lock = luaL_optint(L, 2, 1);
  535. luaL_checkany(L, 1);
  536. lua_pushvalue(L, 1);
  537. lua_pushinteger(L, lua_ref(L, lock));
  538. lua_assert(lua_gettop(L) == level+1); /* +1 for result */
  539. return 1;
  540. }
  541. static int getref (lua_State *L) {
  542. int level = lua_gettop(L);
  543. lua_getref(L, luaL_checkint(L, 1));
  544. lua_assert(lua_gettop(L) == level+1);
  545. return 1;
  546. }
  547. static int unref (lua_State *L) {
  548. int level = lua_gettop(L);
  549. lua_unref(L, luaL_checkint(L, 1));
  550. lua_assert(lua_gettop(L) == level);
  551. return 0;
  552. }
  553. static int upvalue (lua_State *L) {
  554. int n = luaL_checkint(L, 2);
  555. luaL_checktype(L, 1, LUA_TFUNCTION);
  556. if (lua_isnone(L, 3)) {
  557. const char *name = lua_getupvalue(L, 1, n);
  558. if (name == NULL) return 0;
  559. lua_pushstring(L, name);
  560. return 2;
  561. }
  562. else {
  563. const char *name = lua_setupvalue(L, 1, n);
  564. lua_pushstring(L, name);
  565. return 1;
  566. }
  567. }
  568. static int newuserdata (lua_State *L) {
  569. size_t size = luaL_checkint(L, 1);
  570. char *p = cast(char *, lua_newuserdata(L, size));
  571. while (size--) *p++ = '\0';
  572. return 1;
  573. }
  574. static int pushuserdata (lua_State *L) {
  575. lua_pushlightuserdata(L, cast(void *, luaL_checkint(L, 1)));
  576. return 1;
  577. }
  578. static int udataval (lua_State *L) {
  579. lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
  580. return 1;
  581. }
  582. static int doonnewstack (lua_State *L) {
  583. lua_State *L1 = lua_newthread(L);
  584. size_t l;
  585. const char *s = luaL_checklstring(L, 1, &l);
  586. int status = luaL_loadbuffer(L1, s, l, s);
  587. if (status == LUA_OK)
  588. status = lua_pcall(L1, 0, 0, 0);
  589. lua_pushinteger(L, status);
  590. return 1;
  591. }
  592. static int s2d (lua_State *L) {
  593. lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
  594. return 1;
  595. }
  596. static int d2s (lua_State *L) {
  597. double d = luaL_checknumber(L, 1);
  598. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  599. return 1;
  600. }
  601. static int num2int (lua_State *L) {
  602. lua_pushinteger(L, lua_tointeger(L, 1));
  603. return 1;
  604. }
  605. static int newstate (lua_State *L) {
  606. void *ud;
  607. lua_Alloc f = lua_getallocf(L, &ud);
  608. lua_State *L1 = lua_newstate(f, ud);
  609. if (L1)
  610. lua_pushinteger(L, (unsigned long)L1);
  611. else
  612. lua_pushnil(L);
  613. return 1;
  614. }
  615. static int loadlib (lua_State *L) {
  616. static const luaL_Reg libs[] = {
  617. {"baselibopen", luaopen_base},
  618. {"dblibopen", luaopen_debug},
  619. {"iolibopen", luaopen_io},
  620. {"mathlibopen", luaopen_math},
  621. {"strlibopen", luaopen_string},
  622. {"tablibopen", luaopen_table},
  623. {"packageopen", luaopen_package},
  624. {NULL, NULL}
  625. };
  626. lua_State *L1 = cast(lua_State *,
  627. cast(unsigned long, luaL_checknumber(L, 1)));
  628. lua_pushvalue(L1, LUA_GLOBALSINDEX);
  629. luaL_register(L1, NULL, libs);
  630. return 0;
  631. }
  632. static int closestate (lua_State *L) {
  633. lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
  634. lua_close(L1);
  635. return 0;
  636. }
  637. static int doremote (lua_State *L) {
  638. lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
  639. size_t lcode;
  640. const char *code = luaL_checklstring(L, 2, &lcode);
  641. int status;
  642. lua_settop(L1, 0);
  643. status = luaL_loadbuffer(L1, code, lcode, code);
  644. if (status == LUA_OK)
  645. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  646. if (status != LUA_OK) {
  647. lua_pushnil(L);
  648. lua_pushinteger(L, status);
  649. lua_pushstring(L, lua_tostring(L1, -1));
  650. return 3;
  651. }
  652. else {
  653. int i = 0;
  654. while (!lua_isnone(L1, ++i))
  655. lua_pushstring(L, lua_tostring(L1, i));
  656. lua_pop(L1, i-1);
  657. return i-1;
  658. }
  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_pushboolean(L1, lua_isnumber(L1, getindex));
  732. }
  733. else if EQ("isstring") {
  734. lua_pushboolean(L1, lua_isstring(L1, getindex));
  735. }
  736. else if EQ("istable") {
  737. lua_pushboolean(L1, lua_istable(L1, getindex));
  738. }
  739. else if EQ("iscfunction") {
  740. lua_pushboolean(L1, lua_iscfunction(L1, getindex));
  741. }
  742. else if EQ("isfunction") {
  743. lua_pushboolean(L1, lua_isfunction(L1, getindex));
  744. }
  745. else if EQ("isuserdata") {
  746. lua_pushboolean(L1, lua_isuserdata(L1, getindex));
  747. }
  748. else if EQ("isudataval") {
  749. lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
  750. }
  751. else if EQ("isnil") {
  752. lua_pushboolean(L1, lua_isnil(L1, getindex));
  753. }
  754. else if EQ("isnull") {
  755. lua_pushboolean(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. const char *s1 = lua_pushstring(L1, s);
  763. lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0);
  764. }
  765. else if EQ("objsize") {
  766. lua_pushinteger(L1, lua_objlen(L1, getindex));
  767. }
  768. else if EQ("tocfunction") {
  769. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  770. }
  771. else if EQ("return") {
  772. return getnum;
  773. }
  774. else if EQ("gettop") {
  775. lua_pushinteger(L1, lua_gettop(L1));
  776. }
  777. else if EQ("settop") {
  778. lua_settop(L1, getnum);
  779. }
  780. else if EQ("pop") {
  781. lua_pop(L1, getnum);
  782. }
  783. else if EQ("pushnum") {
  784. lua_pushinteger(L1, getnum);
  785. }
  786. else if EQ("pushstring") {
  787. lua_pushstring(L1, getname);
  788. }
  789. else if EQ("pushnil") {
  790. lua_pushnil(L1);
  791. }
  792. else if EQ("pushbool") {
  793. lua_pushboolean(L1, getnum);
  794. }
  795. else if EQ("newtable") {
  796. lua_newtable(L1);
  797. }
  798. else if EQ("newuserdata") {
  799. lua_newuserdata(L1, getnum);
  800. }
  801. else if EQ("tobool") {
  802. lua_pushboolean(L1, lua_toboolean(L1, getindex));
  803. }
  804. else if EQ("pushvalue") {
  805. lua_pushvalue(L1, getindex);
  806. }
  807. else if EQ("pushcclosure") {
  808. lua_pushcclosure(L1, testC, getnum);
  809. }
  810. else if EQ("remove") {
  811. lua_remove(L1, getnum);
  812. }
  813. else if EQ("insert") {
  814. lua_insert(L1, getnum);
  815. }
  816. else if EQ("replace") {
  817. lua_replace(L1, getindex);
  818. }
  819. else if EQ("gettable") {
  820. lua_gettable(L1, getindex);
  821. }
  822. else if EQ("settable") {
  823. lua_settable(L1, getindex);
  824. }
  825. else if EQ("next") {
  826. lua_next(L1, -2);
  827. }
  828. else if EQ("concat") {
  829. lua_concat(L1, getnum);
  830. }
  831. else if EQ("lessthan") {
  832. int a = getindex;
  833. lua_pushboolean(L1, lua_lessthan(L1, a, getindex));
  834. }
  835. else if EQ("equal") {
  836. int a = getindex;
  837. lua_pushboolean(L1, lua_equal(L1, a, getindex));
  838. }
  839. else if EQ("rawcall") {
  840. int narg = getnum;
  841. int nres = getnum;
  842. lua_call(L1, narg, nres);
  843. }
  844. else if EQ("call") {
  845. int narg = getnum;
  846. int nres = getnum;
  847. lua_pcall(L1, narg, nres, 0);
  848. }
  849. else if EQ("loadstring") {
  850. size_t sl;
  851. const char *s = luaL_checklstring(L1, getnum, &sl);
  852. luaL_loadbuffer(L1, s, sl, s);
  853. }
  854. else if EQ("loadfile") {
  855. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  856. }
  857. else if EQ("setmetatable") {
  858. lua_setmetatable(L1, getindex);
  859. }
  860. else if EQ("getmetatable") {
  861. if (lua_getmetatable(L1, getindex) == 0)
  862. lua_pushnil(L1);
  863. }
  864. else if EQ("type") {
  865. lua_pushstring(L1, luaL_typename(L1, getnum));
  866. }
  867. else if EQ("getn") {
  868. int i = getindex;
  869. lua_pushinteger(L1, lua_objlen(L1, i));
  870. }
  871. else if EQ("checkstack") {
  872. if (!lua_checkstack(L1, getnum))
  873. luaL_error(L, "C stack overflow");
  874. }
  875. else if EQ("newmetatable") {
  876. lua_pushboolean(L1, luaL_newmetatable(L1, getname));
  877. }
  878. else if EQ("testudata") {
  879. int i = getindex;
  880. lua_pushboolean(L1, luaL_testudata(L1, i, getname) != NULL);
  881. }
  882. else if EQ("gsub") {
  883. int a = getnum; int b = getnum; int c = getnum;
  884. luaL_gsub(L1, lua_tostring(L1, a),
  885. lua_tostring(L, b),
  886. lua_tostring(L, c));
  887. }
  888. else if EQ("throw") {
  889. #if defined(__cplusplus)
  890. static struct X { int x; } x;
  891. throw x;
  892. #else
  893. luaL_error(L1, "C++");
  894. #endif
  895. break;
  896. }
  897. else luaL_error(L, "unknown instruction %s", buff);
  898. }
  899. return 0;
  900. }
  901. /* }====================================================== */
  902. /*
  903. ** {======================================================
  904. ** tests for yield inside hooks
  905. ** =======================================================
  906. */
  907. static void yieldf (lua_State *L, lua_Debug *ar) {
  908. lua_yield(L, 0);
  909. }
  910. static int setyhook (lua_State *L) {
  911. if (lua_isnoneornil(L, 1))
  912. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  913. else {
  914. const char *smask = luaL_checkstring(L, 1);
  915. int count = luaL_optint(L, 2, 0);
  916. int mask = 0;
  917. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  918. if (count > 0) mask |= LUA_MASKCOUNT;
  919. lua_sethook(L, yieldf, mask, count);
  920. }
  921. return 0;
  922. }
  923. static int coresume (lua_State *L) {
  924. int status;
  925. lua_State *co = lua_tothread(L, 1);
  926. luaL_argcheck(L, co, 1, "coroutine expected");
  927. status = lua_resume(co, 0);
  928. if (status != 0) {
  929. lua_pushboolean(L, 0);
  930. lua_insert(L, -2);
  931. return 2; /* return false + error message */
  932. }
  933. else {
  934. lua_pushboolean(L, 1);
  935. return 1;
  936. }
  937. }
  938. /* }====================================================== */
  939. static const struct luaL_Reg tests_funcs[] = {
  940. {"checkmemory", lua_checkmemory},
  941. {"closestate", closestate},
  942. {"d2s", d2s},
  943. {"doonnewstack", doonnewstack},
  944. {"doremote", doremote},
  945. {"gccolor", get_gccolor},
  946. {"gcstate", gcstate},
  947. {"getref", getref},
  948. {"hash", hash_query},
  949. {"int2fb", int2fb_aux},
  950. {"limits", get_limits},
  951. {"listcode", listcode},
  952. {"listk", listk},
  953. {"listlocals", listlocals},
  954. {"loadlib", loadlib},
  955. {"newstate", newstate},
  956. {"newuserdata", newuserdata},
  957. {"num2int", num2int},
  958. {"pushuserdata", pushuserdata},
  959. {"querystr", string_query},
  960. {"querytab", table_query},
  961. {"ref", tref},
  962. {"resume", coresume},
  963. {"s2d", s2d},
  964. {"setyhook", setyhook},
  965. {"stacklevel", stacklevel},
  966. {"testC", testC},
  967. {"totalmem", mem_query},
  968. {"trick", settrick},
  969. {"udataval", udataval},
  970. {"unref", unref},
  971. {"upvalue", upvalue},
  972. {NULL, NULL}
  973. };
  974. static void checkfinalmem (void) {
  975. lua_assert(l_memcontrol.numblocks == 0);
  976. lua_assert(l_memcontrol.total == 0);
  977. }
  978. int luaB_opentests (lua_State *L) {
  979. void *ud;
  980. atexit(checkfinalmem);
  981. lua_assert(lua_getallocf(L, &ud) == debug_realloc);
  982. lua_assert(ud == cast(void *, &l_memcontrol));
  983. lua_setallocf(L, lua_getallocf(L, NULL), ud);
  984. luaL_register(L, "T", tests_funcs);
  985. return 0;
  986. }
  987. #endif