ltests.c 24 KB

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