ltests.c 26 KB

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