ltests.c 28 KB

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