ltests.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /*
  2. ** $Id: ltests.c,v 2.43 2007/06/21 13:48:04 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 = 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, l_memcontrol.total);
  417. lua_pushinteger(L, l_memcontrol.numblocks);
  418. lua_pushinteger(L, l_memcontrol.maxmem);
  419. return 3;
  420. }
  421. else {
  422. l_memcontrol.memlimit = luaL_checkint(L, 1);
  423. return 0;
  424. }
  425. }
  426. static int settrick (lua_State *L) {
  427. l_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 == LUA_OK)
  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 == LUA_OK)
  641. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  642. if (status != LUA_OK) {
  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 int2fb_aux (lua_State *L) {
  657. int b = luaO_int2fb(luaL_checkint(L, 1));
  658. lua_pushinteger(L, b);
  659. lua_pushinteger(L, luaO_fb2int(b));
  660. return 2;
  661. }
  662. /*
  663. ** {======================================================
  664. ** function to test the API with C. It interprets a kind of assembler
  665. ** language with calls to the API, so the test can be driven by Lua code
  666. ** =======================================================
  667. */
  668. static const char *const delimits = " \t\n,;";
  669. static void skip (const char **pc) {
  670. while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  671. }
  672. static int getnum_aux (lua_State *L, const char **pc) {
  673. int res = 0;
  674. int sig = 1;
  675. skip(pc);
  676. if (**pc == '.') {
  677. res = cast_int(lua_tonumber(L, -1));
  678. lua_pop(L, 1);
  679. (*pc)++;
  680. return res;
  681. }
  682. else if (**pc == '-') {
  683. sig = -1;
  684. (*pc)++;
  685. }
  686. while (isdigit(cast_int(**pc))) res = res*10 + (*(*pc)++) - '0';
  687. return sig*res;
  688. }
  689. static const char *getname_aux (char *buff, const char **pc) {
  690. int i = 0;
  691. skip(pc);
  692. while (**pc != '\0' && !strchr(delimits, **pc))
  693. buff[i++] = *(*pc)++;
  694. buff[i] = '\0';
  695. return buff;
  696. }
  697. static int getindex_aux (lua_State *L, const char **pc) {
  698. skip(pc);
  699. switch (*(*pc)++) {
  700. case 'R': return LUA_REGISTRYINDEX;
  701. case 'G': return LUA_GLOBALSINDEX;
  702. case 'E': return LUA_ENVIRONINDEX;
  703. case 'U': return lua_upvalueindex(getnum_aux(L, pc));
  704. default: (*pc)--; return getnum_aux(L, pc);
  705. }
  706. }
  707. #define EQ(s1) (strcmp(s1, inst) == 0)
  708. #define getnum (getnum_aux(L, &pc))
  709. #define getname (getname_aux(buff, &pc))
  710. #define getindex (getindex_aux(L, &pc))
  711. static int testC (lua_State *L) {
  712. char buff[30];
  713. lua_State *L1;
  714. const char *pc;
  715. if (lua_isnumber(L, 1)) {
  716. L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
  717. pc = luaL_checkstring(L, 2);
  718. }
  719. else {
  720. L1 = L;
  721. pc = luaL_checkstring(L, 1);
  722. }
  723. for (;;) {
  724. const char *inst = getname;
  725. if EQ("") return 0;
  726. else if EQ("isnumber") {
  727. lua_pushinteger(L1, lua_isnumber(L1, getindex));
  728. }
  729. else if EQ("isstring") {
  730. lua_pushinteger(L1, lua_isstring(L1, getindex));
  731. }
  732. else if EQ("istable") {
  733. lua_pushinteger(L1, lua_istable(L1, getindex));
  734. }
  735. else if EQ("iscfunction") {
  736. lua_pushinteger(L1, lua_iscfunction(L1, getindex));
  737. }
  738. else if EQ("isfunction") {
  739. lua_pushinteger(L1, lua_isfunction(L1, getindex));
  740. }
  741. else if EQ("isuserdata") {
  742. lua_pushinteger(L1, lua_isuserdata(L1, getindex));
  743. }
  744. else if EQ("isudataval") {
  745. lua_pushinteger(L1, lua_islightuserdata(L1, getindex));
  746. }
  747. else if EQ("isnil") {
  748. lua_pushinteger(L1, lua_isnil(L1, getindex));
  749. }
  750. else if EQ("isnull") {
  751. lua_pushinteger(L1, lua_isnone(L1, getindex));
  752. }
  753. else if EQ("tonumber") {
  754. lua_pushnumber(L1, lua_tonumber(L1, getindex));
  755. }
  756. else if EQ("tostring") {
  757. const char *s = lua_tostring(L1, getindex);
  758. const char *s1 = lua_pushstring(L1, s);
  759. lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0);
  760. }
  761. else if EQ("objsize") {
  762. lua_pushinteger(L1, lua_objlen(L1, getindex));
  763. }
  764. else if EQ("tocfunction") {
  765. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  766. }
  767. else if EQ("return") {
  768. return getnum;
  769. }
  770. else if EQ("gettop") {
  771. lua_pushinteger(L1, lua_gettop(L1));
  772. }
  773. else if EQ("settop") {
  774. lua_settop(L1, getnum);
  775. }
  776. else if EQ("pop") {
  777. lua_pop(L1, getnum);
  778. }
  779. else if EQ("pushnum") {
  780. lua_pushinteger(L1, getnum);
  781. }
  782. else if EQ("pushstring") {
  783. lua_pushstring(L1, getname);
  784. }
  785. else if EQ("pushnil") {
  786. lua_pushnil(L1);
  787. }
  788. else if EQ("pushbool") {
  789. lua_pushboolean(L1, getnum);
  790. }
  791. else if EQ("newuserdata") {
  792. lua_newuserdata(L1, getnum);
  793. }
  794. else if EQ("tobool") {
  795. lua_pushinteger(L1, lua_toboolean(L1, getindex));
  796. }
  797. else if EQ("pushvalue") {
  798. lua_pushvalue(L1, getindex);
  799. }
  800. else if EQ("pushcclosure") {
  801. lua_pushcclosure(L1, testC, getnum);
  802. }
  803. else if EQ("remove") {
  804. lua_remove(L1, getnum);
  805. }
  806. else if EQ("insert") {
  807. lua_insert(L1, getnum);
  808. }
  809. else if EQ("replace") {
  810. lua_replace(L1, getindex);
  811. }
  812. else if EQ("gettable") {
  813. lua_gettable(L1, getindex);
  814. }
  815. else if EQ("settable") {
  816. lua_settable(L1, getindex);
  817. }
  818. else if EQ("next") {
  819. lua_next(L1, -2);
  820. }
  821. else if EQ("concat") {
  822. lua_concat(L1, getnum);
  823. }
  824. else if EQ("lessthan") {
  825. int a = getindex;
  826. lua_pushboolean(L1, lua_lessthan(L1, a, getindex));
  827. }
  828. else if EQ("equal") {
  829. int a = getindex;
  830. lua_pushboolean(L1, lua_equal(L1, a, getindex));
  831. }
  832. else if EQ("rawcall") {
  833. int narg = getnum;
  834. int nres = getnum;
  835. lua_call(L1, narg, nres);
  836. }
  837. else if EQ("call") {
  838. int narg = getnum;
  839. int nres = getnum;
  840. lua_pcall(L1, narg, nres, 0);
  841. }
  842. else if EQ("loadstring") {
  843. size_t sl;
  844. const char *s = luaL_checklstring(L1, getnum, &sl);
  845. luaL_loadbuffer(L1, s, sl, s);
  846. }
  847. else if EQ("loadfile") {
  848. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  849. }
  850. else if EQ("setmetatable") {
  851. lua_setmetatable(L1, getindex);
  852. }
  853. else if EQ("getmetatable") {
  854. if (lua_getmetatable(L1, getindex) == 0)
  855. lua_pushnil(L1);
  856. }
  857. else if EQ("type") {
  858. lua_pushstring(L1, luaL_typename(L1, getnum));
  859. }
  860. else if EQ("getn") {
  861. int i = getindex;
  862. lua_pushinteger(L1, lua_objlen(L1, i));
  863. }
  864. else if EQ("throw") {
  865. #if defined(__cplusplus)
  866. static struct X { int x; } x;
  867. throw x;
  868. #else
  869. luaL_error(L1, "C++");
  870. #endif
  871. break;
  872. }
  873. else luaL_error(L, "unknown instruction %s", buff);
  874. }
  875. return 0;
  876. }
  877. /* }====================================================== */
  878. /*
  879. ** {======================================================
  880. ** tests for yield inside hooks
  881. ** =======================================================
  882. */
  883. static void yieldf (lua_State *L, lua_Debug *ar) {
  884. lua_yield(L, 0);
  885. }
  886. static int setyhook (lua_State *L) {
  887. if (lua_isnoneornil(L, 1))
  888. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  889. else {
  890. const char *smask = luaL_checkstring(L, 1);
  891. int count = luaL_optint(L, 2, 0);
  892. int mask = 0;
  893. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  894. if (count > 0) mask |= LUA_MASKCOUNT;
  895. lua_sethook(L, yieldf, mask, count);
  896. }
  897. return 0;
  898. }
  899. static int coresume (lua_State *L) {
  900. int status;
  901. lua_State *co = lua_tothread(L, 1);
  902. luaL_argcheck(L, co, 1, "coroutine expected");
  903. status = lua_resume(co, 0);
  904. if (status != 0) {
  905. lua_pushboolean(L, 0);
  906. lua_insert(L, -2);
  907. return 2; /* return false + error message */
  908. }
  909. else {
  910. lua_pushboolean(L, 1);
  911. return 1;
  912. }
  913. }
  914. /* }====================================================== */
  915. /*
  916. ** {======================================================
  917. ** tests auxlib functions
  918. ** =======================================================
  919. */
  920. static int auxgsub (lua_State *L) {
  921. const char *s1 = luaL_checkstring(L, 1);
  922. const char *s2 = luaL_checkstring(L, 2);
  923. const char *s3 = luaL_checkstring(L, 3);
  924. lua_settop(L, 3);
  925. luaL_gsub(L, s1, s2, s3);
  926. lua_assert(lua_gettop(L) == 4);
  927. return 1;
  928. }
  929. /* }====================================================== */
  930. static const struct luaL_Reg tests_funcs[] = {
  931. {"checkmemory", lua_checkmemory},
  932. {"closestate", closestate},
  933. {"d2s", d2s},
  934. {"doonnewstack", doonnewstack},
  935. {"doremote", doremote},
  936. {"gccolor", get_gccolor},
  937. {"gcstate", gcstate},
  938. {"getref", getref},
  939. {"gsub", auxgsub},
  940. {"hash", hash_query},
  941. {"int2fb", int2fb_aux},
  942. {"limits", get_limits},
  943. {"listcode", listcode},
  944. {"listk", listk},
  945. {"listlocals", listlocals},
  946. {"loadlib", loadlib},
  947. {"newstate", newstate},
  948. {"newuserdata", newuserdata},
  949. {"num2int", num2int},
  950. {"pushuserdata", pushuserdata},
  951. {"querystr", string_query},
  952. {"querytab", table_query},
  953. {"ref", tref},
  954. {"resume", coresume},
  955. {"s2d", s2d},
  956. {"setyhook", setyhook},
  957. {"stacklevel", stacklevel},
  958. {"testC", testC},
  959. {"totalmem", mem_query},
  960. {"trick", settrick},
  961. {"udataval", udataval},
  962. {"unref", unref},
  963. {"upvalue", upvalue},
  964. {NULL, NULL}
  965. };
  966. static void checkfinalmem (void) {
  967. lua_assert(l_memcontrol.numblocks == 0);
  968. lua_assert(l_memcontrol.total == 0);
  969. }
  970. int luaB_opentests (lua_State *L) {
  971. void *ud;
  972. atexit(checkfinalmem);
  973. lua_assert(lua_getallocf(L, &ud) == debug_realloc);
  974. lua_assert(ud == cast(void *, &l_memcontrol));
  975. lua_setallocf(L, lua_getallocf(L, NULL), ud);
  976. lua_state = L; /* keep first state to be opened */
  977. luaL_register(L, "T", tests_funcs);
  978. return 0;
  979. }
  980. #endif