ltests.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. /*
  2. ** $Id: ltests.c,v 2.88 2010/03/24 13:07:01 roberto Exp roberto $
  3. ** Internal Module for Debugging of the Lua Implementation
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <limits.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define ltests_c
  11. #define LUA_CORE
  12. #include "lua.h"
  13. #include "lapi.h"
  14. #include "lauxlib.h"
  15. #include "lcode.h"
  16. #include "lctype.h"
  17. #include "ldebug.h"
  18. #include "ldo.h"
  19. #include "lfunc.h"
  20. #include "lmem.h"
  21. #include "lopcodes.h"
  22. #include "lstate.h"
  23. #include "lstring.h"
  24. #include "ltable.h"
  25. #include "lualib.h"
  26. /*
  27. ** The whole module only makes sense with LUA_DEBUG on
  28. */
  29. #if defined(LUA_DEBUG)
  30. void *l_Trick = 0;
  31. int islocked = 0;
  32. #define obj_at(L,k) (L->ci->func + (k))
  33. static void setnameval (lua_State *L, const char *name, int val) {
  34. lua_pushstring(L, name);
  35. lua_pushinteger(L, val);
  36. lua_settable(L, -3);
  37. }
  38. static void pushobject (lua_State *L, const TValue *o) {
  39. setobj2s(L, L->top, o);
  40. api_incr_top(L);
  41. }
  42. static int tpanic (lua_State *L) {
  43. fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
  44. lua_tostring(L, -1));
  45. return (exit(EXIT_FAILURE), 0); /* do not return to Lua */
  46. }
  47. /*
  48. ** {======================================================================
  49. ** Controlled version for realloc.
  50. ** =======================================================================
  51. */
  52. #define MARK 0x55 /* 01010101 (a nice pattern) */
  53. #ifndef EXTERNMEMCHECK
  54. /* full memory check */
  55. #define HEADER (sizeof(L_Umaxalign)) /* ensures maximum alignment for HEADER */
  56. #define MARKSIZE 16 /* size of marks after each block */
  57. #define blockhead(b) (cast(char *, b) - HEADER)
  58. #define setsize(newblock, size) (*cast(size_t *, newblock) = size)
  59. #define checkblocksize(b, size) (size == (*cast(size_t *, blockhead(b))))
  60. #define fillmem(mem,size) memset(mem, -MARK, size)
  61. #else
  62. /* external memory check: don't do it twice */
  63. #define HEADER 0
  64. #define MARKSIZE 0
  65. #define blockhead(b) (b)
  66. #define setsize(newblock, size) /* empty */
  67. #define checkblocksize(b,size) (1)
  68. #define fillmem(mem,size) /* empty */
  69. #endif
  70. Memcontrol l_memcontrol = {0L, 0L, 0L, 0L, {0L, 0L, 0L, 0L, 0L}};
  71. static void *checkblock (void *block, size_t size) {
  72. void *b = blockhead(block);
  73. int i;
  74. for (i=0;i<MARKSIZE;i++)
  75. lua_assert(*(cast(char *, b)+HEADER+size+i) == MARK+i); /* corrupted block? */
  76. return b;
  77. }
  78. static void freeblock (Memcontrol *mc, void *block, size_t size) {
  79. if (block) {
  80. lua_assert(checkblocksize(block, size));
  81. block = checkblock(block, size);
  82. fillmem(block, size+HEADER+MARKSIZE); /* erase block */
  83. free(block); /* free original block */
  84. mc->numblocks--;
  85. mc->total -= size;
  86. }
  87. }
  88. void *debug_realloc (void *ud, void *block, size_t oldsize, size_t size) {
  89. Memcontrol *mc = cast(Memcontrol *, ud);
  90. if (block == NULL) {
  91. if (LUA_TSTRING <= oldsize && oldsize <= LUA_TTHREAD)
  92. mc->objcount[oldsize - LUA_TSTRING]++;
  93. oldsize = 0;
  94. }
  95. lua_assert((oldsize == 0) ? block == NULL :
  96. block && checkblocksize(block, oldsize));
  97. if (mc->memlimit == 0) { /* first time? */
  98. char *limit = getenv("MEMLIMIT"); /* initialize memory limit */
  99. mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX;
  100. }
  101. if (size == 0) {
  102. freeblock(mc, block, oldsize);
  103. return NULL;
  104. }
  105. else if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
  106. return NULL; /* to test memory allocation errors */
  107. else {
  108. void *newblock;
  109. int i;
  110. size_t realsize = HEADER+size+MARKSIZE;
  111. size_t commonsize = (oldsize < size) ? oldsize : size;
  112. if (realsize < size) return NULL; /* overflow! */
  113. newblock = malloc(realsize); /* alloc a new block */
  114. if (newblock == NULL) return NULL;
  115. if (block) {
  116. memcpy(cast(char *, newblock)+HEADER, block, commonsize);
  117. freeblock(mc, block, oldsize); /* erase (and check) old copy */
  118. }
  119. /* initialize new part of the block with something `weird' */
  120. fillmem(cast(char *, newblock)+HEADER+commonsize, size-commonsize);
  121. mc->total += size;
  122. if (mc->total > mc->maxmem)
  123. mc->maxmem = mc->total;
  124. mc->numblocks++;
  125. setsize(newblock, size);
  126. for (i=0;i<MARKSIZE;i++)
  127. *(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
  128. return cast(char *, newblock)+HEADER;
  129. }
  130. }
  131. /* }====================================================================== */
  132. /*
  133. ** {======================================================
  134. ** Functions to check memory consistency
  135. ** =======================================================
  136. */
  137. #define issweep(g) (GCSsweepstring <= (g)->gcstate && (g)->gcstate <= GCSsweep)
  138. static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
  139. if (isdead(g,t)) return 0;
  140. if (g->gcstate == GCSpropagate)
  141. return !isblack(f) || !iswhite(t);
  142. else if (g->gcstate == GCSfinalize)
  143. return iswhite(f);
  144. else
  145. return 1;
  146. }
  147. static void printobj (global_State *g, GCObject *o) {
  148. int i = 0;
  149. GCObject *p;
  150. for (p = g->allgc; p != o && p != NULL; p = gch(p)->next) i++;
  151. if (p == NULL) i = -1;
  152. printf("%d:%s(%p)-%c(%02X)", i, typename(gch(o)->tt), (void *)o,
  153. isdead(g,o)?'d':isblack(o)?'b':iswhite(o)?'w':'g', gch(o)->marked);
  154. }
  155. static int testobjref (global_State *g, GCObject *f, GCObject *t) {
  156. int r = testobjref1(g,f,t);
  157. if (!r) {
  158. printf("%d(%02X) - ", g->gcstate, g->currentwhite);
  159. printobj(g, f);
  160. printf("\t-> ");
  161. printobj(g, t);
  162. printf("\n");
  163. }
  164. return r;
  165. }
  166. #define checkobjref(g,f,t) lua_assert(testobjref(g,f,obj2gco(t)))
  167. static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
  168. if (iscollectable(t)) {
  169. lua_assert(righttt(t) && testobjref(g, f, gcvalue(t)));
  170. }
  171. }
  172. static void checktable (global_State *g, Table *h) {
  173. int i;
  174. int weakkey = 0;
  175. int weakvalue = 0;
  176. const TValue *mode;
  177. GCObject *hgc = obj2gco(h);
  178. if (h->metatable)
  179. checkobjref(g, hgc, h->metatable);
  180. mode = gfasttm(g, h->metatable, TM_MODE);
  181. if (mode && ttisstring(mode)) { /* is there a weak mode? */
  182. weakkey = (strchr(svalue(mode), 'k') != NULL);
  183. weakvalue = (strchr(svalue(mode), 'v') != NULL);
  184. }
  185. i = h->sizearray;
  186. while (i--)
  187. checkvalref(g, hgc, &h->array[i]);
  188. i = sizenode(h);
  189. while (i--) {
  190. Node *n = gnode(h, i);
  191. if (!ttisnil(gval(n))) {
  192. lua_assert(!ttisnil(gkey(n)));
  193. checkvalref(g, hgc, gkey(n));
  194. checkvalref(g, hgc, gval(n));
  195. }
  196. }
  197. }
  198. /*
  199. ** All marks are conditional because a GC may happen while the
  200. ** prototype is still being created
  201. */
  202. static void checkproto (global_State *g, Proto *f) {
  203. int i;
  204. GCObject *fgc = obj2gco(f);
  205. if (f->source) checkobjref(g, fgc, f->source);
  206. for (i=0; i<f->sizek; i++) {
  207. if (ttisstring(f->k+i))
  208. checkobjref(g, fgc, rawtsvalue(f->k+i));
  209. }
  210. for (i=0; i<f->sizeupvalues; i++) {
  211. if (f->upvalues[i].name)
  212. checkobjref(g, fgc, f->upvalues[i].name);
  213. }
  214. for (i=0; i<f->sizep; i++) {
  215. if (f->p[i])
  216. checkobjref(g, fgc, f->p[i]);
  217. }
  218. for (i=0; i<f->sizelocvars; i++) {
  219. if (f->locvars[i].varname)
  220. checkobjref(g, fgc, f->locvars[i].varname);
  221. }
  222. }
  223. static void checkclosure (global_State *g, Closure *cl) {
  224. GCObject *clgc = obj2gco(cl);
  225. checkobjref(g, clgc, cl->l.env);
  226. if (cl->c.isC) {
  227. int i;
  228. for (i=0; i<cl->c.nupvalues; i++)
  229. checkvalref(g, clgc, &cl->c.upvalue[i]);
  230. }
  231. else {
  232. int i;
  233. lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
  234. checkobjref(g, clgc, cl->l.p);
  235. for (i=0; i<cl->l.nupvalues; i++) {
  236. if (cl->l.upvals[i]) {
  237. lua_assert(cl->l.upvals[i]->tt == LUA_TUPVAL);
  238. checkobjref(g, clgc, cl->l.upvals[i]);
  239. }
  240. }
  241. }
  242. }
  243. static int lua_checkpc (pCallInfo ci) {
  244. if (!isLua(ci)) return 1;
  245. else {
  246. Proto *p = ci_func(ci)->l.p;
  247. return p->code <= ci->u.l.savedpc &&
  248. ci->u.l.savedpc <= p->code + p->sizecode;
  249. }
  250. }
  251. static void checkstack (global_State *g, lua_State *L1) {
  252. StkId o;
  253. CallInfo *ci;
  254. GCObject *uvo;
  255. lua_assert(!isdead(g, obj2gco(L1)));
  256. for (uvo = L1->openupval; uvo != NULL; uvo = gch(uvo)->next) {
  257. UpVal *uv = gco2uv(uvo);
  258. lua_assert(uv->v != &uv->u.value); /* must be open */
  259. lua_assert(!isblack(uvo)); /* open upvalues cannot be black */
  260. }
  261. for (ci = L1->ci; ci != NULL; ci = ci->previous) {
  262. lua_assert(ci->top <= L1->stack_last);
  263. lua_assert(lua_checkpc(ci));
  264. }
  265. if (L1->stack) {
  266. for (o = L1->stack; o < L1->top; o++)
  267. checkliveness(g, o);
  268. }
  269. else lua_assert(L1->stacksize == 0);
  270. }
  271. static void checkobject (global_State *g, GCObject *o) {
  272. if (isdead(g, o))
  273. lua_assert(issweep(g));
  274. else {
  275. if (g->gcstate == GCSfinalize)
  276. lua_assert(iswhite(o));
  277. switch (gch(o)->tt) {
  278. case LUA_TUPVAL: {
  279. UpVal *uv = gco2uv(o);
  280. lua_assert(uv->v == &uv->u.value); /* must be closed */
  281. lua_assert(!isgray(o)); /* closed upvalues are never gray */
  282. checkvalref(g, o, uv->v);
  283. break;
  284. }
  285. case LUA_TUSERDATA: {
  286. Table *mt = gco2u(o)->metatable;
  287. if (mt) checkobjref(g, o, mt);
  288. break;
  289. }
  290. case LUA_TTABLE: {
  291. checktable(g, gco2t(o));
  292. break;
  293. }
  294. case LUA_TTHREAD: {
  295. checkstack(g, gco2th(o));
  296. break;
  297. }
  298. case LUA_TFUNCTION: {
  299. checkclosure(g, gco2cl(o));
  300. break;
  301. }
  302. case LUA_TPROTO: {
  303. checkproto(g, gco2p(o));
  304. break;
  305. }
  306. default: lua_assert(0);
  307. }
  308. }
  309. }
  310. int lua_checkmemory (lua_State *L) {
  311. global_State *g = G(L);
  312. GCObject *o;
  313. UpVal *uv;
  314. checkliveness(g, &g->l_registry);
  315. lua_assert(!isdead(g, obj2gco(g->l_gt)));
  316. checkstack(g, g->mainthread);
  317. for (o = g->allgc; o != NULL; o = gch(o)->next) {
  318. lua_assert(!testbits(o->gch.marked, bitmask(SEPARATED)));
  319. checkobject(g, o);
  320. }
  321. for (o = g->udgc; o != NULL; o = gch(o)->next) {
  322. lua_assert(gch(o)->tt == LUA_TUSERDATA &&
  323. !isdead(g, o) &&
  324. testbit(o->gch.marked, SEPARATED));
  325. checkobject(g, o);
  326. }
  327. for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
  328. lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
  329. lua_assert(uv->v != &uv->u.value); /* must be open */
  330. lua_assert(!isblack(obj2gco(uv))); /* open upvalues are never black */
  331. checkvalref(g, obj2gco(uv), uv->v);
  332. }
  333. return 0;
  334. }
  335. /* }====================================================== */
  336. /*
  337. ** {======================================================
  338. ** Disassembler
  339. ** =======================================================
  340. */
  341. static char *buildop (Proto *p, int pc, char *buff) {
  342. Instruction i = p->code[pc];
  343. OpCode o = GET_OPCODE(i);
  344. const char *name = luaP_opnames[o];
  345. int line = getfuncline(p, pc);
  346. sprintf(buff, "(%4d) %4d - ", line, pc);
  347. switch (getOpMode(o)) {
  348. case iABC:
  349. sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
  350. GETARG_A(i), GETARG_B(i), GETARG_C(i));
  351. break;
  352. case iABx:
  353. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
  354. break;
  355. case iAsBx:
  356. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
  357. break;
  358. case iAx:
  359. sprintf(buff+strlen(buff), "%-12s%4d", name, GETARG_Ax(i));
  360. break;
  361. }
  362. return buff;
  363. }
  364. #if 0
  365. void luaI_printcode (Proto *pt, int size) {
  366. int pc;
  367. for (pc=0; pc<size; pc++) {
  368. char buff[100];
  369. printf("%s\n", buildop(pt, pc, buff));
  370. }
  371. printf("-------\n");
  372. }
  373. #endif
  374. static int listcode (lua_State *L) {
  375. int pc;
  376. Proto *p;
  377. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  378. 1, "Lua function expected");
  379. p = getproto(obj_at(L, 1));
  380. lua_newtable(L);
  381. setnameval(L, "maxstack", p->maxstacksize);
  382. setnameval(L, "numparams", p->numparams);
  383. for (pc=0; pc<p->sizecode; pc++) {
  384. char buff[100];
  385. lua_pushinteger(L, pc+1);
  386. lua_pushstring(L, buildop(p, pc, buff));
  387. lua_settable(L, -3);
  388. }
  389. return 1;
  390. }
  391. static int listk (lua_State *L) {
  392. Proto *p;
  393. int i;
  394. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  395. 1, "Lua function expected");
  396. p = getproto(obj_at(L, 1));
  397. lua_createtable(L, p->sizek, 0);
  398. for (i=0; i<p->sizek; i++) {
  399. pushobject(L, p->k+i);
  400. lua_rawseti(L, -2, i+1);
  401. }
  402. return 1;
  403. }
  404. static int listlocals (lua_State *L) {
  405. Proto *p;
  406. int pc = luaL_checkint(L, 2) - 1;
  407. int i = 0;
  408. const char *name;
  409. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  410. 1, "Lua function expected");
  411. p = getproto(obj_at(L, 1));
  412. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  413. lua_pushstring(L, name);
  414. return i-1;
  415. }
  416. /* }====================================================== */
  417. static int get_limits (lua_State *L) {
  418. lua_createtable(L, 0, 5);
  419. setnameval(L, "BITS_INT", LUAI_BITSINT);
  420. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  421. setnameval(L, "MAXSTACK", MAXSTACK);
  422. setnameval(L, "NUM_OPCODES", NUM_OPCODES);
  423. return 1;
  424. }
  425. static int mem_query (lua_State *L) {
  426. if (lua_isnone(L, 1)) {
  427. int i;
  428. lua_pushinteger(L, l_memcontrol.total);
  429. lua_pushinteger(L, l_memcontrol.numblocks);
  430. lua_pushinteger(L, l_memcontrol.maxmem);
  431. for (i = 0; i < 5; i++) lua_pushinteger(L, l_memcontrol.objcount[i]);
  432. return 3 + 5;
  433. }
  434. else {
  435. l_memcontrol.memlimit = luaL_checkint(L, 1);
  436. return 0;
  437. }
  438. }
  439. static int settrick (lua_State *L) {
  440. l_Trick = gcvalue(obj_at(L, 1));
  441. return 0;
  442. }
  443. /*static int set_gcstate (lua_State *L) {
  444. static const char *const state[] = {"propagate", "sweep", "finalize"};
  445. return 0;
  446. }*/
  447. static int get_gccolor (lua_State *L) {
  448. TValue *o;
  449. luaL_checkany(L, 1);
  450. o = obj_at(L, 1);
  451. if (!iscollectable(o))
  452. lua_pushstring(L, "no collectable");
  453. else
  454. lua_pushstring(L, iswhite(gcvalue(o)) ? "white" :
  455. isblack(gcvalue(o)) ? "black" : "grey");
  456. return 1;
  457. }
  458. static int gcstate (lua_State *L) {
  459. switch(G(L)->gcstate) {
  460. case GCSpropagate: lua_pushstring(L, "propagate"); break;
  461. case GCSsweepstring: lua_pushstring(L, "sweep strings"); break;
  462. case GCSsweep: lua_pushstring(L, "sweep"); break;
  463. case GCSfinalize: lua_pushstring(L, "finalize"); break;
  464. default: lua_assert(0);
  465. }
  466. return 1;
  467. }
  468. static int hash_query (lua_State *L) {
  469. if (lua_isnone(L, 2)) {
  470. luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  471. lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
  472. }
  473. else {
  474. TValue *o = obj_at(L, 1);
  475. Table *t;
  476. luaL_checktype(L, 2, LUA_TTABLE);
  477. t = hvalue(obj_at(L, 2));
  478. lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
  479. }
  480. return 1;
  481. }
  482. static int stacklevel (lua_State *L) {
  483. unsigned long a = 0;
  484. lua_pushinteger(L, (L->top - L->stack));
  485. lua_pushinteger(L, (L->stack_last - L->stack));
  486. lua_pushinteger(L, (unsigned long)&a);
  487. return 5;
  488. }
  489. static int table_query (lua_State *L) {
  490. const Table *t;
  491. int i = luaL_optint(L, 2, -1);
  492. luaL_checktype(L, 1, LUA_TTABLE);
  493. t = hvalue(obj_at(L, 1));
  494. if (i == -1) {
  495. lua_pushinteger(L, t->sizearray);
  496. lua_pushinteger(L, luaH_isdummy(t->node) ? 0 : sizenode(t));
  497. lua_pushinteger(L, t->lastfree - t->node);
  498. }
  499. else if (i < t->sizearray) {
  500. lua_pushinteger(L, i);
  501. pushobject(L, &t->array[i]);
  502. lua_pushnil(L);
  503. }
  504. else if ((i -= t->sizearray) < sizenode(t)) {
  505. if (!ttisnil(gval(gnode(t, i))) ||
  506. ttisnil(gkey(gnode(t, i))) ||
  507. ttisnumber(gkey(gnode(t, i)))) {
  508. pushobject(L, key2tval(gnode(t, i)));
  509. }
  510. else
  511. lua_pushliteral(L, "<undef>");
  512. pushobject(L, gval(gnode(t, i)));
  513. if (gnext(&t->node[i]))
  514. lua_pushinteger(L, gnext(&t->node[i]) - t->node);
  515. else
  516. lua_pushnil(L);
  517. }
  518. return 3;
  519. }
  520. static int string_query (lua_State *L) {
  521. stringtable *tb = &G(L)->strt;
  522. int s = luaL_optint(L, 2, 0) - 1;
  523. if (s==-1) {
  524. lua_pushinteger(L ,tb->nuse);
  525. lua_pushinteger(L ,tb->size);
  526. return 2;
  527. }
  528. else if (s < tb->size) {
  529. GCObject *ts;
  530. int n = 0;
  531. for (ts = tb->hash[s]; ts; ts = gch(ts)->next) {
  532. setsvalue2s(L, L->top, gco2ts(ts));
  533. incr_top(L);
  534. n++;
  535. }
  536. return n;
  537. }
  538. return 0;
  539. }
  540. static int tref (lua_State *L) {
  541. int level = lua_gettop(L);
  542. int lock = luaL_optint(L, 2, 1);
  543. luaL_checkany(L, 1);
  544. lua_pushvalue(L, 1);
  545. lua_pushinteger(L, lua_ref(L, lock));
  546. lua_assert(lua_gettop(L) == level+1); /* +1 for result */
  547. return 1;
  548. }
  549. static int getref (lua_State *L) {
  550. int level = lua_gettop(L);
  551. lua_getref(L, luaL_checkint(L, 1));
  552. lua_assert(lua_gettop(L) == level+1);
  553. return 1;
  554. }
  555. static int unref (lua_State *L) {
  556. int level = lua_gettop(L);
  557. lua_unref(L, luaL_checkint(L, 1));
  558. lua_assert(lua_gettop(L) == level);
  559. return 0;
  560. }
  561. static int upvalue (lua_State *L) {
  562. int n = luaL_checkint(L, 2);
  563. luaL_checktype(L, 1, LUA_TFUNCTION);
  564. if (lua_isnone(L, 3)) {
  565. const char *name = lua_getupvalue(L, 1, n);
  566. if (name == NULL) return 0;
  567. lua_pushstring(L, name);
  568. return 2;
  569. }
  570. else {
  571. const char *name = lua_setupvalue(L, 1, n);
  572. lua_pushstring(L, name);
  573. return 1;
  574. }
  575. }
  576. static int newuserdata (lua_State *L) {
  577. size_t size = luaL_checkint(L, 1);
  578. char *p = cast(char *, lua_newuserdata(L, size));
  579. while (size--) *p++ = '\0';
  580. return 1;
  581. }
  582. static int pushuserdata (lua_State *L) {
  583. lua_pushlightuserdata(L, cast(void *, luaL_checkint(L, 1)));
  584. return 1;
  585. }
  586. static int udataval (lua_State *L) {
  587. lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
  588. return 1;
  589. }
  590. static int doonnewstack (lua_State *L) {
  591. lua_State *L1 = lua_newthread(L);
  592. size_t l;
  593. const char *s = luaL_checklstring(L, 1, &l);
  594. int status = luaL_loadbuffer(L1, s, l, s);
  595. if (status == LUA_OK)
  596. status = lua_pcall(L1, 0, 0, 0);
  597. lua_pushinteger(L, status);
  598. return 1;
  599. }
  600. static int s2d (lua_State *L) {
  601. lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
  602. return 1;
  603. }
  604. static int d2s (lua_State *L) {
  605. double d = luaL_checknumber(L, 1);
  606. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  607. return 1;
  608. }
  609. static int num2int (lua_State *L) {
  610. lua_pushinteger(L, lua_tointeger(L, 1));
  611. return 1;
  612. }
  613. static int newstate (lua_State *L) {
  614. void *ud;
  615. lua_Alloc f = lua_getallocf(L, &ud);
  616. lua_State *L1 = lua_newstate(f, ud);
  617. if (L1) {
  618. lua_atpanic(L1, tpanic);
  619. lua_pushlightuserdata(L, L1);
  620. }
  621. else
  622. lua_pushnil(L);
  623. return 1;
  624. }
  625. static lua_State *getstate (lua_State *L) {
  626. lua_State *L1 = cast(lua_State *, lua_touserdata(L, 1));
  627. luaL_argcheck(L, L1 != NULL, 1, "state expected");
  628. return L1;
  629. }
  630. static int loadlib (lua_State *L) {
  631. static const luaL_Reg libs[] = {
  632. {"baselibopen", luaopen_base},
  633. {"dblibopen", luaopen_debug},
  634. {"iolibopen", luaopen_io},
  635. {"mathlibopen", luaopen_math},
  636. {"strlibopen", luaopen_string},
  637. {"tablibopen", luaopen_table},
  638. {"packageopen", luaopen_package},
  639. {NULL, NULL}
  640. };
  641. lua_State *L1 = getstate(L);
  642. lua_pushglobaltable(L1);
  643. luaL_register(L1, NULL, libs);
  644. return 0;
  645. }
  646. static int closestate (lua_State *L) {
  647. lua_State *L1 = getstate(L);
  648. lua_close(L1);
  649. return 0;
  650. }
  651. static int doremote (lua_State *L) {
  652. lua_State *L1 = getstate(L);
  653. size_t lcode;
  654. const char *code = luaL_checklstring(L, 2, &lcode);
  655. int status;
  656. lua_settop(L1, 0);
  657. status = luaL_loadbuffer(L1, code, lcode, code);
  658. if (status == LUA_OK)
  659. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  660. if (status != LUA_OK) {
  661. lua_pushnil(L);
  662. lua_pushinteger(L, status);
  663. lua_pushstring(L, lua_tostring(L1, -1));
  664. return 3;
  665. }
  666. else {
  667. int i = 0;
  668. while (!lua_isnone(L1, ++i))
  669. lua_pushstring(L, lua_tostring(L1, i));
  670. lua_pop(L1, i-1);
  671. return i-1;
  672. }
  673. }
  674. static int int2fb_aux (lua_State *L) {
  675. int b = luaO_int2fb(luaL_checkint(L, 1));
  676. lua_pushinteger(L, b);
  677. lua_pushinteger(L, luaO_fb2int(b));
  678. return 2;
  679. }
  680. /*
  681. ** {======================================================
  682. ** function to test the API with C. It interprets a kind of assembler
  683. ** language with calls to the API, so the test can be driven by Lua code
  684. ** =======================================================
  685. */
  686. static void sethookaux (lua_State *L, int mask, int count, const char *code);
  687. static const char *const delimits = " \t\n,;";
  688. static void skip (const char **pc) {
  689. for (;;) {
  690. if (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  691. else if (**pc == '#') {
  692. while (**pc != '\n' && **pc != '\0') (*pc)++;
  693. }
  694. else break;
  695. }
  696. }
  697. static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
  698. int res = 0;
  699. int sig = 1;
  700. skip(pc);
  701. if (**pc == '.') {
  702. res = lua_tointeger(L1, -1);
  703. lua_pop(L1, 1);
  704. (*pc)++;
  705. return res;
  706. }
  707. else if (**pc == '-') {
  708. sig = -1;
  709. (*pc)++;
  710. }
  711. if (!lisdigit(cast(unsigned char, **pc)))
  712. luaL_error(L, "number expected (%s)", *pc);
  713. while (lisdigit(cast(unsigned char, **pc))) res = res*10 + (*(*pc)++) - '0';
  714. return sig*res;
  715. }
  716. static const char *getstring_aux (lua_State *L, char *buff, const char **pc) {
  717. int i = 0;
  718. skip(pc);
  719. if (**pc == '"' || **pc == '\'') { /* quoted string? */
  720. int quote = *(*pc)++;
  721. while (**pc != quote) {
  722. if (**pc == '\0') luaL_error(L, "unfinished string in C script");
  723. buff[i++] = *(*pc)++;
  724. }
  725. (*pc)++;
  726. }
  727. else {
  728. while (**pc != '\0' && !strchr(delimits, **pc))
  729. buff[i++] = *(*pc)++;
  730. }
  731. buff[i] = '\0';
  732. return buff;
  733. }
  734. static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
  735. skip(pc);
  736. switch (*(*pc)++) {
  737. case 'R': return LUA_REGISTRYINDEX;
  738. case 'G': return luaL_error(L, "deprecated index 'G'");
  739. case 'E': return LUA_ENVIRONINDEX;
  740. case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc));
  741. default: (*pc)--; return getnum_aux(L, L1, pc);
  742. }
  743. }
  744. static void pushcode (lua_State *L, int code) {
  745. static const char *const codes[] = {"OK", "YIELD", "ERRRUN",
  746. "ERRSYNTAX", "ERRMEM", "ERRGCMM", "ERRERR"};
  747. lua_pushstring(L, codes[code]);
  748. }
  749. #define EQ(s1) (strcmp(s1, inst) == 0)
  750. #define getnum (getnum_aux(L, L1, &pc))
  751. #define getstring (getstring_aux(L, buff, &pc))
  752. #define getindex (getindex_aux(L, L1, &pc))
  753. static int testC (lua_State *L);
  754. static int Cfunck (lua_State *L);
  755. static int runC (lua_State *L, lua_State *L1, const char *pc) {
  756. char buff[300];
  757. int status = 0;
  758. if (pc == NULL) return luaL_error(L, "attempt to runC null script");
  759. for (;;) {
  760. const char *inst = getstring;
  761. if EQ("") return 0;
  762. else if EQ("isnumber") {
  763. lua_pushboolean(L1, lua_isnumber(L1, getindex));
  764. }
  765. else if EQ("isstring") {
  766. lua_pushboolean(L1, lua_isstring(L1, getindex));
  767. }
  768. else if EQ("istable") {
  769. lua_pushboolean(L1, lua_istable(L1, getindex));
  770. }
  771. else if EQ("iscfunction") {
  772. lua_pushboolean(L1, lua_iscfunction(L1, getindex));
  773. }
  774. else if EQ("isfunction") {
  775. lua_pushboolean(L1, lua_isfunction(L1, getindex));
  776. }
  777. else if EQ("isuserdata") {
  778. lua_pushboolean(L1, lua_isuserdata(L1, getindex));
  779. }
  780. else if EQ("isudataval") {
  781. lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
  782. }
  783. else if EQ("isnil") {
  784. lua_pushboolean(L1, lua_isnil(L1, getindex));
  785. }
  786. else if EQ("isnull") {
  787. lua_pushboolean(L1, lua_isnone(L1, getindex));
  788. }
  789. else if EQ("tonumber") {
  790. lua_pushnumber(L1, lua_tonumber(L1, getindex));
  791. }
  792. else if EQ("tostring") {
  793. const char *s = lua_tostring(L1, getindex);
  794. const char *s1 = lua_pushstring(L1, s);
  795. lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0);
  796. }
  797. else if EQ("objsize") {
  798. lua_pushinteger(L1, lua_rawlen(L1, getindex));
  799. }
  800. else if EQ("len") {
  801. lua_len(L1, getindex);
  802. }
  803. else if EQ("Llen") {
  804. lua_pushinteger(L1, luaL_len(L1, getindex));
  805. }
  806. else if EQ("tocfunction") {
  807. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  808. }
  809. else if EQ("func2udata") {
  810. lua_CFunction func = lua_tocfunction(L1, getindex);
  811. lua_pushlightuserdata(L1, &func);
  812. }
  813. else if EQ("return") {
  814. int n = getnum;
  815. if (L1 != L) {
  816. int i;
  817. for (i = 0; i < n; i++)
  818. lua_pushstring(L, lua_tostring(L1, -(n - i)));
  819. }
  820. return n;
  821. }
  822. else if EQ("gettop") {
  823. lua_pushinteger(L1, lua_gettop(L1));
  824. }
  825. else if EQ("settop") {
  826. lua_settop(L1, getnum);
  827. }
  828. else if EQ("pop") {
  829. lua_pop(L1, getnum);
  830. }
  831. else if EQ("pushnum") {
  832. lua_pushinteger(L1, getnum);
  833. }
  834. else if EQ("pushstring") {
  835. lua_pushstring(L1, getstring);
  836. }
  837. else if EQ("pushnil") {
  838. lua_pushnil(L1);
  839. }
  840. else if EQ("pushbool") {
  841. lua_pushboolean(L1, getnum);
  842. }
  843. else if EQ("newtable") {
  844. lua_newtable(L1);
  845. }
  846. else if EQ("newuserdata") {
  847. lua_newuserdata(L1, getnum);
  848. }
  849. else if EQ("tobool") {
  850. lua_pushboolean(L1, lua_toboolean(L1, getindex));
  851. }
  852. else if EQ("pushvalue") {
  853. lua_pushvalue(L1, getindex);
  854. }
  855. else if EQ("pushcclosure") {
  856. lua_pushcclosure(L1, testC, getnum);
  857. }
  858. else if EQ("pushupvalueindex") {
  859. lua_pushinteger(L1, lua_upvalueindex(getnum));
  860. }
  861. else if EQ("remove") {
  862. lua_remove(L1, getnum);
  863. }
  864. else if EQ("insert") {
  865. lua_insert(L1, getnum);
  866. }
  867. else if EQ("replace") {
  868. lua_replace(L1, getindex);
  869. }
  870. else if EQ("copy") {
  871. int f = getindex;
  872. lua_copy(L1, f, getindex);
  873. }
  874. else if EQ("gettable") {
  875. lua_gettable(L1, getindex);
  876. }
  877. else if EQ("getfield") {
  878. int t = getindex;
  879. lua_getfield(L1, t, getstring);
  880. }
  881. else if EQ("setfield") {
  882. int t = getindex;
  883. lua_setfield(L1, t, getstring);
  884. }
  885. else if EQ("rawgeti") {
  886. int t = getindex;
  887. lua_rawgeti(L1, t, getnum);
  888. }
  889. else if EQ("settable") {
  890. lua_settable(L1, getindex);
  891. }
  892. else if EQ("next") {
  893. lua_next(L1, -2);
  894. }
  895. else if EQ("concat") {
  896. lua_concat(L1, getnum);
  897. }
  898. else if EQ("print") {
  899. int n = getnum;
  900. if (n != 0) {
  901. printf("%s\n", luaL_tolstring(L1, n, NULL));
  902. lua_pop(L1, 1);
  903. }
  904. else {
  905. int i;
  906. n = lua_gettop(L1);
  907. for (i = 1; i <= n; i++) {
  908. printf("%s ", luaL_tolstring(L1, i, NULL));
  909. lua_pop(L1, 1);
  910. }
  911. printf("\n");
  912. }
  913. }
  914. else if EQ("arith") {
  915. static char ops[] = "+-*/%^_";
  916. int op;
  917. skip(&pc);
  918. op = strchr(ops, *pc++) - ops;
  919. lua_arith(L1, op);
  920. }
  921. else if EQ("compare") {
  922. int a = getindex;
  923. int b = getindex;
  924. lua_pushboolean(L1, lua_compare(L1, a, b, getnum));
  925. }
  926. else if EQ("call") {
  927. int narg = getnum;
  928. int nres = getnum;
  929. lua_call(L1, narg, nres);
  930. }
  931. else if EQ("pcall") {
  932. int narg = getnum;
  933. int nres = getnum;
  934. status = lua_pcall(L1, narg, nres, 0);
  935. }
  936. else if EQ("pcallk") {
  937. int narg = getnum;
  938. int nres = getnum;
  939. int i = getindex;
  940. status = lua_pcallk(L1, narg, nres, 0, i, Cfunck);
  941. }
  942. else if EQ("callk") {
  943. int narg = getnum;
  944. int nres = getnum;
  945. int i = getindex;
  946. lua_callk(L1, narg, nres, i, Cfunck);
  947. }
  948. else if EQ("yield") {
  949. return lua_yield(L1, getnum);
  950. }
  951. else if EQ("yieldk") {
  952. int nres = getnum;
  953. int i = getindex;
  954. return lua_yieldk(L1, nres, i, Cfunck);
  955. }
  956. else if EQ("newthread") {
  957. lua_newthread(L1);
  958. }
  959. else if EQ("resume") {
  960. int i = getindex;
  961. status = lua_resume(lua_tothread(L1, i), getnum);
  962. }
  963. else if EQ("pushstatus") {
  964. pushcode(L1, status);
  965. }
  966. else if EQ("xmove") {
  967. int f = getindex;
  968. int t = getindex;
  969. lua_State *fs = (f == 0) ? L1 : lua_tothread(L1, f);
  970. lua_State *ts = (t == 0) ? L1 : lua_tothread(L1, t);
  971. int n = getnum;
  972. if (n == 0) n = lua_gettop(fs);
  973. lua_xmove(fs, ts, n);
  974. }
  975. else if EQ("loadstring") {
  976. size_t sl;
  977. const char *s = luaL_checklstring(L1, getnum, &sl);
  978. luaL_loadbuffer(L1, s, sl, s);
  979. }
  980. else if EQ("loadfile") {
  981. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  982. }
  983. else if EQ("setmetatable") {
  984. lua_setmetatable(L1, getindex);
  985. }
  986. else if EQ("getmetatable") {
  987. if (lua_getmetatable(L1, getindex) == 0)
  988. lua_pushnil(L1);
  989. }
  990. else if EQ("type") {
  991. lua_pushstring(L1, luaL_typename(L1, getnum));
  992. }
  993. else if EQ("append") {
  994. int t = getindex;
  995. int i = lua_rawlen(L1, t);
  996. lua_rawseti(L1, t, i + 1);
  997. }
  998. else if EQ("getctx") {
  999. int i = 0;
  1000. int s = lua_getctx(L1, &i);
  1001. pushcode(L1, s);
  1002. lua_pushinteger(L1, i);
  1003. }
  1004. else if EQ("checkstack") {
  1005. if (!lua_checkstack(L1, getnum))
  1006. luaL_error(L, "C stack overflow");
  1007. }
  1008. else if EQ("newmetatable") {
  1009. lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
  1010. }
  1011. else if EQ("testudata") {
  1012. int i = getindex;
  1013. lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
  1014. }
  1015. else if EQ("gsub") {
  1016. int a = getnum; int b = getnum; int c = getnum;
  1017. luaL_gsub(L1, lua_tostring(L1, a),
  1018. lua_tostring(L1, b),
  1019. lua_tostring(L1, c));
  1020. }
  1021. else if EQ("sethook") {
  1022. int mask = getnum;
  1023. int count = getnum;
  1024. sethookaux(L1, mask, count, getstring);
  1025. }
  1026. else if EQ("throw") {
  1027. #if defined(__cplusplus)
  1028. static struct X { int x; } x;
  1029. throw x;
  1030. #else
  1031. luaL_error(L1, "C++");
  1032. #endif
  1033. break;
  1034. }
  1035. else luaL_error(L, "unknown instruction %s", buff);
  1036. }
  1037. return 0;
  1038. }
  1039. static int testC (lua_State *L) {
  1040. lua_State *L1;
  1041. const char *pc;
  1042. if (lua_isuserdata(L, 1)) {
  1043. L1 = getstate(L);
  1044. pc = luaL_checkstring(L, 2);
  1045. }
  1046. else if (lua_isthread(L, 1)) {
  1047. L1 = lua_tothread(L, 1);
  1048. pc = luaL_checkstring(L, 2);
  1049. }
  1050. else {
  1051. L1 = L;
  1052. pc = luaL_checkstring(L, 1);
  1053. }
  1054. return runC(L, L1, pc);
  1055. }
  1056. static int Cfunc (lua_State *L) {
  1057. return runC(L, L, lua_tostring(L, lua_upvalueindex(1)));
  1058. }
  1059. static int Cfunck (lua_State *L) {
  1060. int i = 0;
  1061. lua_getctx(L, &i);
  1062. return runC(L, L, lua_tostring(L, i));
  1063. }
  1064. static int makeCfunc (lua_State *L) {
  1065. luaL_checkstring(L, 1);
  1066. lua_pushcclosure(L, Cfunc, lua_gettop(L));
  1067. return 1;
  1068. }
  1069. /* }====================================================== */
  1070. /*
  1071. ** {======================================================
  1072. ** tests for C hooks
  1073. ** =======================================================
  1074. */
  1075. /*
  1076. ** C hook that runs the C script stored in registry.C_HOOK[L]
  1077. */
  1078. static void Chook (lua_State *L, lua_Debug *ar) {
  1079. const char *scpt;
  1080. const char *const events [] = {"call", "ret", "line", "count", "tailcall"};
  1081. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK");
  1082. lua_pushlightuserdata(L, L);
  1083. lua_gettable(L, -2); /* get C_HOOK[L] (script saved by sethookaux) */
  1084. scpt = lua_tostring(L, -1); /* not very religious (string will be popped) */
  1085. lua_pop(L, 2); /* remove C_HOOK and script */
  1086. lua_pushstring(L, events[ar->event]); /* may be used by script */
  1087. lua_pushinteger(L, ar->currentline); /* may be used by script */
  1088. runC(L, L, scpt); /* run script from C_HOOK[L] */
  1089. }
  1090. /*
  1091. ** sets registry.C_HOOK[L] = scpt and sets Chook as a hook
  1092. */
  1093. static void sethookaux (lua_State *L, int mask, int count, const char *scpt) {
  1094. if (*scpt == '\0') { /* no script? */
  1095. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1096. return;
  1097. }
  1098. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* get C_HOOK table */
  1099. if (!lua_istable(L, -1)) { /* no hook table? */
  1100. lua_pop(L, 1); /* remove previous value */
  1101. lua_newtable(L); /* create new C_HOOK table */
  1102. lua_pushvalue(L, -1);
  1103. lua_setfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* register it */
  1104. }
  1105. lua_pushlightuserdata(L, L);
  1106. lua_pushstring(L, scpt);
  1107. lua_settable(L, -3); /* C_HOOK[L] = script */
  1108. lua_sethook(L, Chook, mask, count);
  1109. }
  1110. static int sethook (lua_State *L) {
  1111. if (lua_isnoneornil(L, 1))
  1112. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1113. else {
  1114. const char *scpt = luaL_checkstring(L, 1);
  1115. const char *smask = luaL_checkstring(L, 2);
  1116. int count = luaL_optint(L, 3, 0);
  1117. int mask = 0;
  1118. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  1119. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  1120. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  1121. if (count > 0) mask |= LUA_MASKCOUNT;
  1122. sethookaux(L, mask, count, scpt);
  1123. }
  1124. return 0;
  1125. }
  1126. static int coresume (lua_State *L) {
  1127. int status;
  1128. lua_State *co = lua_tothread(L, 1);
  1129. luaL_argcheck(L, co, 1, "coroutine expected");
  1130. status = lua_resume(co, 0);
  1131. if (status != LUA_OK && status != LUA_YIELD) {
  1132. lua_pushboolean(L, 0);
  1133. lua_insert(L, -2);
  1134. return 2; /* return false + error message */
  1135. }
  1136. else {
  1137. lua_pushboolean(L, 1);
  1138. return 1;
  1139. }
  1140. }
  1141. /* }====================================================== */
  1142. static const struct luaL_Reg tests_funcs[] = {
  1143. {"checkmemory", lua_checkmemory},
  1144. {"closestate", closestate},
  1145. {"d2s", d2s},
  1146. {"doonnewstack", doonnewstack},
  1147. {"doremote", doremote},
  1148. {"gccolor", get_gccolor},
  1149. {"gcstate", gcstate},
  1150. {"getref", getref},
  1151. {"hash", hash_query},
  1152. {"int2fb", int2fb_aux},
  1153. {"limits", get_limits},
  1154. {"listcode", listcode},
  1155. {"listk", listk},
  1156. {"listlocals", listlocals},
  1157. {"loadlib", loadlib},
  1158. {"newstate", newstate},
  1159. {"newuserdata", newuserdata},
  1160. {"num2int", num2int},
  1161. {"pushuserdata", pushuserdata},
  1162. {"querystr", string_query},
  1163. {"querytab", table_query},
  1164. {"ref", tref},
  1165. {"resume", coresume},
  1166. {"s2d", s2d},
  1167. {"sethook", sethook},
  1168. {"stacklevel", stacklevel},
  1169. {"testC", testC},
  1170. {"makeCfunc", makeCfunc},
  1171. {"totalmem", mem_query},
  1172. {"trick", settrick},
  1173. {"udataval", udataval},
  1174. {"unref", unref},
  1175. {"upvalue", upvalue},
  1176. {NULL, NULL}
  1177. };
  1178. static void checkfinalmem (void) {
  1179. lua_assert(l_memcontrol.numblocks == 0);
  1180. lua_assert(l_memcontrol.total == 0);
  1181. }
  1182. int luaB_opentests (lua_State *L) {
  1183. void *ud;
  1184. lua_atpanic(L, &tpanic);
  1185. atexit(checkfinalmem);
  1186. lua_assert(lua_getallocf(L, &ud) == debug_realloc);
  1187. lua_assert(ud == cast(void *, &l_memcontrol));
  1188. lua_setallocf(L, lua_getallocf(L, NULL), ud);
  1189. luaL_register(L, "T", tests_funcs);
  1190. return 0;
  1191. }
  1192. #endif