2
0

ltests.c 28 KB

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