ltests.c 27 KB

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