ltests.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /*
  2. ** $Id: ltests.c,v 2.90 2010/03/26 20:58:11 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. if (cl->c.isC) {
  226. int i;
  227. for (i=0; i<cl->c.nupvalues; i++)
  228. checkvalref(g, clgc, &cl->c.upvalue[i]);
  229. }
  230. else {
  231. int i;
  232. lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
  233. checkobjref(g, clgc, cl->l.p);
  234. for (i=0; i<cl->l.nupvalues; i++) {
  235. if (cl->l.upvals[i]) {
  236. lua_assert(cl->l.upvals[i]->tt == LUA_TUPVAL);
  237. checkobjref(g, clgc, cl->l.upvals[i]);
  238. }
  239. }
  240. }
  241. }
  242. static int lua_checkpc (pCallInfo ci) {
  243. if (!isLua(ci)) return 1;
  244. else {
  245. Proto *p = ci_func(ci)->l.p;
  246. return p->code <= ci->u.l.savedpc &&
  247. ci->u.l.savedpc <= p->code + p->sizecode;
  248. }
  249. }
  250. static void checkstack (global_State *g, lua_State *L1) {
  251. StkId o;
  252. CallInfo *ci;
  253. GCObject *uvo;
  254. lua_assert(!isdead(g, obj2gco(L1)));
  255. for (uvo = L1->openupval; uvo != NULL; uvo = gch(uvo)->next) {
  256. UpVal *uv = gco2uv(uvo);
  257. lua_assert(uv->v != &uv->u.value); /* must be open */
  258. lua_assert(!isblack(uvo)); /* open upvalues cannot be black */
  259. }
  260. for (ci = L1->ci; ci != NULL; ci = ci->previous) {
  261. lua_assert(ci->top <= L1->stack_last);
  262. lua_assert(lua_checkpc(ci));
  263. }
  264. if (L1->stack) {
  265. for (o = L1->stack; o < L1->top; o++)
  266. checkliveness(g, o);
  267. }
  268. else lua_assert(L1->stacksize == 0);
  269. }
  270. static void checkobject (global_State *g, GCObject *o) {
  271. if (isdead(g, o))
  272. lua_assert(issweep(g));
  273. else {
  274. if (g->gcstate == GCSfinalize)
  275. lua_assert(iswhite(o));
  276. switch (gch(o)->tt) {
  277. case LUA_TUPVAL: {
  278. UpVal *uv = gco2uv(o);
  279. lua_assert(uv->v == &uv->u.value); /* must be closed */
  280. lua_assert(!isgray(o)); /* closed upvalues are never gray */
  281. checkvalref(g, o, uv->v);
  282. break;
  283. }
  284. case LUA_TUSERDATA: {
  285. Table *mt = gco2u(o)->metatable;
  286. if (mt) checkobjref(g, o, mt);
  287. break;
  288. }
  289. case LUA_TTABLE: {
  290. checktable(g, gco2t(o));
  291. break;
  292. }
  293. case LUA_TTHREAD: {
  294. checkstack(g, gco2th(o));
  295. break;
  296. }
  297. case LUA_TFUNCTION: {
  298. checkclosure(g, gco2cl(o));
  299. break;
  300. }
  301. case LUA_TPROTO: {
  302. checkproto(g, gco2p(o));
  303. break;
  304. }
  305. default: lua_assert(0);
  306. }
  307. }
  308. }
  309. int lua_checkmemory (lua_State *L) {
  310. global_State *g = G(L);
  311. GCObject *o;
  312. UpVal *uv;
  313. checkliveness(g, &g->l_registry);
  314. checkstack(g, g->mainthread);
  315. for (o = g->allgc; o != NULL; o = gch(o)->next) {
  316. lua_assert(!testbits(o->gch.marked, bitmask(SEPARATED)));
  317. checkobject(g, o);
  318. }
  319. for (o = g->udgc; o != NULL; o = gch(o)->next) {
  320. lua_assert(gch(o)->tt == LUA_TUSERDATA &&
  321. !isdead(g, o) &&
  322. testbit(o->gch.marked, SEPARATED));
  323. checkobject(g, o);
  324. }
  325. for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {
  326. lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);
  327. lua_assert(uv->v != &uv->u.value); /* must be open */
  328. lua_assert(!isblack(obj2gco(uv))); /* open upvalues are never black */
  329. checkvalref(g, obj2gco(uv), uv->v);
  330. }
  331. return 0;
  332. }
  333. /* }====================================================== */
  334. /*
  335. ** {======================================================
  336. ** Disassembler
  337. ** =======================================================
  338. */
  339. static char *buildop (Proto *p, int pc, char *buff) {
  340. Instruction i = p->code[pc];
  341. OpCode o = GET_OPCODE(i);
  342. const char *name = luaP_opnames[o];
  343. int line = getfuncline(p, pc);
  344. sprintf(buff, "(%4d) %4d - ", line, pc);
  345. switch (getOpMode(o)) {
  346. case iABC:
  347. sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
  348. GETARG_A(i), GETARG_B(i), GETARG_C(i));
  349. break;
  350. case iABx:
  351. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
  352. break;
  353. case iAsBx:
  354. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
  355. break;
  356. case iAx:
  357. sprintf(buff+strlen(buff), "%-12s%4d", name, GETARG_Ax(i));
  358. break;
  359. }
  360. return buff;
  361. }
  362. #if 0
  363. void luaI_printcode (Proto *pt, int size) {
  364. int pc;
  365. for (pc=0; pc<size; pc++) {
  366. char buff[100];
  367. printf("%s\n", buildop(pt, pc, buff));
  368. }
  369. printf("-------\n");
  370. }
  371. #endif
  372. static int listcode (lua_State *L) {
  373. int pc;
  374. Proto *p;
  375. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  376. 1, "Lua function expected");
  377. p = getproto(obj_at(L, 1));
  378. lua_newtable(L);
  379. setnameval(L, "maxstack", p->maxstacksize);
  380. setnameval(L, "numparams", p->numparams);
  381. for (pc=0; pc<p->sizecode; pc++) {
  382. char buff[100];
  383. lua_pushinteger(L, pc+1);
  384. lua_pushstring(L, buildop(p, pc, buff));
  385. lua_settable(L, -3);
  386. }
  387. return 1;
  388. }
  389. static int listk (lua_State *L) {
  390. Proto *p;
  391. int i;
  392. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  393. 1, "Lua function expected");
  394. p = getproto(obj_at(L, 1));
  395. lua_createtable(L, p->sizek, 0);
  396. for (i=0; i<p->sizek; i++) {
  397. pushobject(L, p->k+i);
  398. lua_rawseti(L, -2, i+1);
  399. }
  400. return 1;
  401. }
  402. static int listlocals (lua_State *L) {
  403. Proto *p;
  404. int pc = luaL_checkint(L, 2) - 1;
  405. int i = 0;
  406. const char *name;
  407. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  408. 1, "Lua function expected");
  409. p = getproto(obj_at(L, 1));
  410. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  411. lua_pushstring(L, name);
  412. return i-1;
  413. }
  414. /* }====================================================== */
  415. static int get_limits (lua_State *L) {
  416. lua_createtable(L, 0, 5);
  417. setnameval(L, "BITS_INT", LUAI_BITSINT);
  418. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  419. setnameval(L, "MAXSTACK", MAXSTACK);
  420. setnameval(L, "NUM_OPCODES", NUM_OPCODES);
  421. return 1;
  422. }
  423. static int mem_query (lua_State *L) {
  424. if (lua_isnone(L, 1)) {
  425. int i;
  426. lua_pushinteger(L, l_memcontrol.total);
  427. lua_pushinteger(L, l_memcontrol.numblocks);
  428. lua_pushinteger(L, l_memcontrol.maxmem);
  429. for (i = 0; i < 5; i++) lua_pushinteger(L, l_memcontrol.objcount[i]);
  430. return 3 + 5;
  431. }
  432. else {
  433. l_memcontrol.memlimit = luaL_checkint(L, 1);
  434. return 0;
  435. }
  436. }
  437. static int settrick (lua_State *L) {
  438. l_Trick = gcvalue(obj_at(L, 1));
  439. return 0;
  440. }
  441. /*static int set_gcstate (lua_State *L) {
  442. static const char *const state[] = {"propagate", "sweep", "finalize"};
  443. return 0;
  444. }*/
  445. static int get_gccolor (lua_State *L) {
  446. TValue *o;
  447. luaL_checkany(L, 1);
  448. o = obj_at(L, 1);
  449. if (!iscollectable(o))
  450. lua_pushstring(L, "no collectable");
  451. else
  452. lua_pushstring(L, iswhite(gcvalue(o)) ? "white" :
  453. isblack(gcvalue(o)) ? "black" : "grey");
  454. return 1;
  455. }
  456. static int gcstate (lua_State *L) {
  457. switch(G(L)->gcstate) {
  458. case GCSpropagate: lua_pushstring(L, "propagate"); break;
  459. case GCSsweepstring: lua_pushstring(L, "sweep strings"); break;
  460. case GCSsweep: lua_pushstring(L, "sweep"); break;
  461. case GCSfinalize: lua_pushstring(L, "finalize"); break;
  462. default: lua_assert(0);
  463. }
  464. return 1;
  465. }
  466. static int hash_query (lua_State *L) {
  467. if (lua_isnone(L, 2)) {
  468. luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  469. lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
  470. }
  471. else {
  472. TValue *o = obj_at(L, 1);
  473. Table *t;
  474. luaL_checktype(L, 2, LUA_TTABLE);
  475. t = hvalue(obj_at(L, 2));
  476. lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
  477. }
  478. return 1;
  479. }
  480. static int stacklevel (lua_State *L) {
  481. unsigned long a = 0;
  482. lua_pushinteger(L, (L->top - L->stack));
  483. lua_pushinteger(L, (L->stack_last - L->stack));
  484. lua_pushinteger(L, (unsigned long)&a);
  485. return 5;
  486. }
  487. static int table_query (lua_State *L) {
  488. const Table *t;
  489. int i = luaL_optint(L, 2, -1);
  490. luaL_checktype(L, 1, LUA_TTABLE);
  491. t = hvalue(obj_at(L, 1));
  492. if (i == -1) {
  493. lua_pushinteger(L, t->sizearray);
  494. lua_pushinteger(L, luaH_isdummy(t->node) ? 0 : sizenode(t));
  495. lua_pushinteger(L, t->lastfree - t->node);
  496. }
  497. else if (i < t->sizearray) {
  498. lua_pushinteger(L, i);
  499. pushobject(L, &t->array[i]);
  500. lua_pushnil(L);
  501. }
  502. else if ((i -= t->sizearray) < sizenode(t)) {
  503. if (!ttisnil(gval(gnode(t, i))) ||
  504. ttisnil(gkey(gnode(t, i))) ||
  505. ttisnumber(gkey(gnode(t, i)))) {
  506. pushobject(L, key2tval(gnode(t, i)));
  507. }
  508. else
  509. lua_pushliteral(L, "<undef>");
  510. pushobject(L, gval(gnode(t, i)));
  511. if (gnext(&t->node[i]))
  512. lua_pushinteger(L, gnext(&t->node[i]) - t->node);
  513. else
  514. lua_pushnil(L);
  515. }
  516. return 3;
  517. }
  518. static int string_query (lua_State *L) {
  519. stringtable *tb = &G(L)->strt;
  520. int s = luaL_optint(L, 2, 0) - 1;
  521. if (s==-1) {
  522. lua_pushinteger(L ,tb->nuse);
  523. lua_pushinteger(L ,tb->size);
  524. return 2;
  525. }
  526. else if (s < tb->size) {
  527. GCObject *ts;
  528. int n = 0;
  529. for (ts = tb->hash[s]; ts; ts = gch(ts)->next) {
  530. setsvalue2s(L, L->top, gco2ts(ts));
  531. incr_top(L);
  532. n++;
  533. }
  534. return n;
  535. }
  536. return 0;
  537. }
  538. static int tref (lua_State *L) {
  539. int level = lua_gettop(L);
  540. int lock = luaL_optint(L, 2, 1);
  541. luaL_checkany(L, 1);
  542. lua_pushvalue(L, 1);
  543. lua_pushinteger(L, lua_ref(L, lock));
  544. lua_assert(lua_gettop(L) == level+1); /* +1 for result */
  545. return 1;
  546. }
  547. static int getref (lua_State *L) {
  548. int level = lua_gettop(L);
  549. lua_getref(L, luaL_checkint(L, 1));
  550. lua_assert(lua_gettop(L) == level+1);
  551. return 1;
  552. }
  553. static int unref (lua_State *L) {
  554. int level = lua_gettop(L);
  555. lua_unref(L, luaL_checkint(L, 1));
  556. lua_assert(lua_gettop(L) == level);
  557. return 0;
  558. }
  559. static int upvalue (lua_State *L) {
  560. int n = luaL_checkint(L, 2);
  561. luaL_checktype(L, 1, LUA_TFUNCTION);
  562. if (lua_isnone(L, 3)) {
  563. const char *name = lua_getupvalue(L, 1, n);
  564. if (name == NULL) return 0;
  565. lua_pushstring(L, name);
  566. return 2;
  567. }
  568. else {
  569. const char *name = lua_setupvalue(L, 1, n);
  570. lua_pushstring(L, name);
  571. return 1;
  572. }
  573. }
  574. static int newuserdata (lua_State *L) {
  575. size_t size = luaL_checkint(L, 1);
  576. char *p = cast(char *, lua_newuserdata(L, size));
  577. while (size--) *p++ = '\0';
  578. return 1;
  579. }
  580. static int pushuserdata (lua_State *L) {
  581. lua_pushlightuserdata(L, cast(void *, luaL_checkint(L, 1)));
  582. return 1;
  583. }
  584. static int udataval (lua_State *L) {
  585. lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
  586. return 1;
  587. }
  588. static int doonnewstack (lua_State *L) {
  589. lua_State *L1 = lua_newthread(L);
  590. size_t l;
  591. const char *s = luaL_checklstring(L, 1, &l);
  592. int status = luaL_loadbuffer(L1, s, l, s);
  593. if (status == LUA_OK)
  594. status = lua_pcall(L1, 0, 0, 0);
  595. lua_pushinteger(L, status);
  596. return 1;
  597. }
  598. static int s2d (lua_State *L) {
  599. lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
  600. return 1;
  601. }
  602. static int d2s (lua_State *L) {
  603. double d = luaL_checknumber(L, 1);
  604. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  605. return 1;
  606. }
  607. static int num2int (lua_State *L) {
  608. lua_pushinteger(L, lua_tointeger(L, 1));
  609. return 1;
  610. }
  611. static int newstate (lua_State *L) {
  612. void *ud;
  613. lua_Alloc f = lua_getallocf(L, &ud);
  614. lua_State *L1 = lua_newstate(f, ud);
  615. if (L1) {
  616. lua_atpanic(L1, tpanic);
  617. lua_pushlightuserdata(L, L1);
  618. }
  619. else
  620. lua_pushnil(L);
  621. return 1;
  622. }
  623. static lua_State *getstate (lua_State *L) {
  624. lua_State *L1 = cast(lua_State *, lua_touserdata(L, 1));
  625. luaL_argcheck(L, L1 != NULL, 1, "state expected");
  626. return L1;
  627. }
  628. static int loadlib (lua_State *L) {
  629. static const luaL_Reg libs[] = {
  630. {"baselibopen", luaopen_base},
  631. {"dblibopen", luaopen_debug},
  632. {"iolibopen", luaopen_io},
  633. {"mathlibopen", luaopen_math},
  634. {"strlibopen", luaopen_string},
  635. {"tablibopen", luaopen_table},
  636. {"packageopen", luaopen_package},
  637. {NULL, NULL}
  638. };
  639. lua_State *L1 = getstate(L);
  640. lua_pushglobaltable(L1);
  641. luaL_register(L1, NULL, libs);
  642. return 0;
  643. }
  644. static int closestate (lua_State *L) {
  645. lua_State *L1 = getstate(L);
  646. lua_close(L1);
  647. return 0;
  648. }
  649. static int doremote (lua_State *L) {
  650. lua_State *L1 = getstate(L);
  651. size_t lcode;
  652. const char *code = luaL_checklstring(L, 2, &lcode);
  653. int status;
  654. lua_settop(L1, 0);
  655. status = luaL_loadbuffer(L1, code, lcode, code);
  656. if (status == LUA_OK)
  657. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  658. if (status != LUA_OK) {
  659. lua_pushnil(L);
  660. lua_pushinteger(L, status);
  661. lua_pushstring(L, lua_tostring(L1, -1));
  662. return 3;
  663. }
  664. else {
  665. int i = 0;
  666. while (!lua_isnone(L1, ++i))
  667. lua_pushstring(L, lua_tostring(L1, i));
  668. lua_pop(L1, i-1);
  669. return i-1;
  670. }
  671. }
  672. static int int2fb_aux (lua_State *L) {
  673. int b = luaO_int2fb(luaL_checkint(L, 1));
  674. lua_pushinteger(L, b);
  675. lua_pushinteger(L, luaO_fb2int(b));
  676. return 2;
  677. }
  678. /*
  679. ** {======================================================
  680. ** function to test the API with C. It interprets a kind of assembler
  681. ** language with calls to the API, so the test can be driven by Lua code
  682. ** =======================================================
  683. */
  684. static void sethookaux (lua_State *L, int mask, int count, const char *code);
  685. static const char *const delimits = " \t\n,;";
  686. static void skip (const char **pc) {
  687. for (;;) {
  688. if (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  689. else if (**pc == '#') {
  690. while (**pc != '\n' && **pc != '\0') (*pc)++;
  691. }
  692. else break;
  693. }
  694. }
  695. static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
  696. int res = 0;
  697. int sig = 1;
  698. skip(pc);
  699. if (**pc == '.') {
  700. res = lua_tointeger(L1, -1);
  701. lua_pop(L1, 1);
  702. (*pc)++;
  703. return res;
  704. }
  705. else if (**pc == '-') {
  706. sig = -1;
  707. (*pc)++;
  708. }
  709. if (!lisdigit(cast(unsigned char, **pc)))
  710. luaL_error(L, "number expected (%s)", *pc);
  711. while (lisdigit(cast(unsigned char, **pc))) res = res*10 + (*(*pc)++) - '0';
  712. return sig*res;
  713. }
  714. static const char *getstring_aux (lua_State *L, char *buff, const char **pc) {
  715. int i = 0;
  716. skip(pc);
  717. if (**pc == '"' || **pc == '\'') { /* quoted string? */
  718. int quote = *(*pc)++;
  719. while (**pc != quote) {
  720. if (**pc == '\0') luaL_error(L, "unfinished string in C script");
  721. buff[i++] = *(*pc)++;
  722. }
  723. (*pc)++;
  724. }
  725. else {
  726. while (**pc != '\0' && !strchr(delimits, **pc))
  727. buff[i++] = *(*pc)++;
  728. }
  729. buff[i] = '\0';
  730. return buff;
  731. }
  732. static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
  733. skip(pc);
  734. switch (*(*pc)++) {
  735. case 'R': return LUA_REGISTRYINDEX;
  736. case 'G': return luaL_error(L, "deprecated index 'G'");
  737. case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc));
  738. default: (*pc)--; return getnum_aux(L, L1, pc);
  739. }
  740. }
  741. static void pushcode (lua_State *L, int code) {
  742. static const char *const codes[] = {"OK", "YIELD", "ERRRUN",
  743. "ERRSYNTAX", "ERRMEM", "ERRGCMM", "ERRERR"};
  744. lua_pushstring(L, codes[code]);
  745. }
  746. #define EQ(s1) (strcmp(s1, inst) == 0)
  747. #define getnum (getnum_aux(L, L1, &pc))
  748. #define getstring (getstring_aux(L, buff, &pc))
  749. #define getindex (getindex_aux(L, L1, &pc))
  750. static int testC (lua_State *L);
  751. static int Cfunck (lua_State *L);
  752. static int runC (lua_State *L, lua_State *L1, const char *pc) {
  753. char buff[300];
  754. int status = 0;
  755. if (pc == NULL) return luaL_error(L, "attempt to runC null script");
  756. for (;;) {
  757. const char *inst = getstring;
  758. if EQ("") return 0;
  759. else if EQ("isnumber") {
  760. lua_pushboolean(L1, lua_isnumber(L1, getindex));
  761. }
  762. else if EQ("isstring") {
  763. lua_pushboolean(L1, lua_isstring(L1, getindex));
  764. }
  765. else if EQ("istable") {
  766. lua_pushboolean(L1, lua_istable(L1, getindex));
  767. }
  768. else if EQ("iscfunction") {
  769. lua_pushboolean(L1, lua_iscfunction(L1, getindex));
  770. }
  771. else if EQ("isfunction") {
  772. lua_pushboolean(L1, lua_isfunction(L1, getindex));
  773. }
  774. else if EQ("isuserdata") {
  775. lua_pushboolean(L1, lua_isuserdata(L1, getindex));
  776. }
  777. else if EQ("isudataval") {
  778. lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
  779. }
  780. else if EQ("isnil") {
  781. lua_pushboolean(L1, lua_isnil(L1, getindex));
  782. }
  783. else if EQ("isnull") {
  784. lua_pushboolean(L1, lua_isnone(L1, getindex));
  785. }
  786. else if EQ("tonumber") {
  787. lua_pushnumber(L1, lua_tonumber(L1, getindex));
  788. }
  789. else if EQ("tostring") {
  790. const char *s = lua_tostring(L1, getindex);
  791. const char *s1 = lua_pushstring(L1, s);
  792. lua_assert((s == NULL && s1 == NULL) || (strcmp)(s, s1) == 0);
  793. }
  794. else if EQ("objsize") {
  795. lua_pushinteger(L1, lua_rawlen(L1, getindex));
  796. }
  797. else if EQ("len") {
  798. lua_len(L1, getindex);
  799. }
  800. else if EQ("Llen") {
  801. lua_pushinteger(L1, luaL_len(L1, getindex));
  802. }
  803. else if EQ("tocfunction") {
  804. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  805. }
  806. else if EQ("func2udata") {
  807. lua_CFunction func = lua_tocfunction(L1, getindex);
  808. lua_pushlightuserdata(L1, &func);
  809. }
  810. else if EQ("return") {
  811. int n = getnum;
  812. if (L1 != L) {
  813. int i;
  814. for (i = 0; i < n; i++)
  815. lua_pushstring(L, lua_tostring(L1, -(n - i)));
  816. }
  817. return n;
  818. }
  819. else if EQ("gettop") {
  820. lua_pushinteger(L1, lua_gettop(L1));
  821. }
  822. else if EQ("settop") {
  823. lua_settop(L1, getnum);
  824. }
  825. else if EQ("pop") {
  826. lua_pop(L1, getnum);
  827. }
  828. else if EQ("pushnum") {
  829. lua_pushinteger(L1, getnum);
  830. }
  831. else if EQ("pushstring") {
  832. lua_pushstring(L1, getstring);
  833. }
  834. else if EQ("pushnil") {
  835. lua_pushnil(L1);
  836. }
  837. else if EQ("pushbool") {
  838. lua_pushboolean(L1, getnum);
  839. }
  840. else if EQ("newtable") {
  841. lua_newtable(L1);
  842. }
  843. else if EQ("newuserdata") {
  844. lua_newuserdata(L1, getnum);
  845. }
  846. else if EQ("tobool") {
  847. lua_pushboolean(L1, lua_toboolean(L1, getindex));
  848. }
  849. else if EQ("pushvalue") {
  850. lua_pushvalue(L1, getindex);
  851. }
  852. else if EQ("pushcclosure") {
  853. lua_pushcclosure(L1, testC, getnum);
  854. }
  855. else if EQ("pushupvalueindex") {
  856. lua_pushinteger(L1, lua_upvalueindex(getnum));
  857. }
  858. else if EQ("remove") {
  859. lua_remove(L1, getnum);
  860. }
  861. else if EQ("insert") {
  862. lua_insert(L1, getnum);
  863. }
  864. else if EQ("replace") {
  865. lua_replace(L1, getindex);
  866. }
  867. else if EQ("copy") {
  868. int f = getindex;
  869. lua_copy(L1, f, getindex);
  870. }
  871. else if EQ("gettable") {
  872. lua_gettable(L1, getindex);
  873. }
  874. else if EQ("getglobal") {
  875. lua_getglobal(L1, getstring);
  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("setglobal") {
  893. lua_setglobal(L1, getstring);
  894. }
  895. else if EQ("next") {
  896. lua_next(L1, -2);
  897. }
  898. else if EQ("concat") {
  899. lua_concat(L1, getnum);
  900. }
  901. else if EQ("print") {
  902. int n = getnum;
  903. if (n != 0) {
  904. printf("%s\n", luaL_tolstring(L1, n, NULL));
  905. lua_pop(L1, 1);
  906. }
  907. else {
  908. int i;
  909. n = lua_gettop(L1);
  910. for (i = 1; i <= n; i++) {
  911. printf("%s ", luaL_tolstring(L1, i, NULL));
  912. lua_pop(L1, 1);
  913. }
  914. printf("\n");
  915. }
  916. }
  917. else if EQ("arith") {
  918. static char ops[] = "+-*/%^_";
  919. int op;
  920. skip(&pc);
  921. op = strchr(ops, *pc++) - ops;
  922. lua_arith(L1, op);
  923. }
  924. else if EQ("compare") {
  925. int a = getindex;
  926. int b = getindex;
  927. lua_pushboolean(L1, lua_compare(L1, a, b, getnum));
  928. }
  929. else if EQ("call") {
  930. int narg = getnum;
  931. int nres = getnum;
  932. lua_call(L1, narg, nres);
  933. }
  934. else if EQ("pcall") {
  935. int narg = getnum;
  936. int nres = getnum;
  937. status = lua_pcall(L1, narg, nres, 0);
  938. }
  939. else if EQ("pcallk") {
  940. int narg = getnum;
  941. int nres = getnum;
  942. int i = getindex;
  943. status = lua_pcallk(L1, narg, nres, 0, i, Cfunck);
  944. }
  945. else if EQ("callk") {
  946. int narg = getnum;
  947. int nres = getnum;
  948. int i = getindex;
  949. lua_callk(L1, narg, nres, i, Cfunck);
  950. }
  951. else if EQ("yield") {
  952. return lua_yield(L1, getnum);
  953. }
  954. else if EQ("yieldk") {
  955. int nres = getnum;
  956. int i = getindex;
  957. return lua_yieldk(L1, nres, i, Cfunck);
  958. }
  959. else if EQ("newthread") {
  960. lua_newthread(L1);
  961. }
  962. else if EQ("resume") {
  963. int i = getindex;
  964. status = lua_resume(lua_tothread(L1, i), getnum);
  965. }
  966. else if EQ("pushstatus") {
  967. pushcode(L1, status);
  968. }
  969. else if EQ("xmove") {
  970. int f = getindex;
  971. int t = getindex;
  972. lua_State *fs = (f == 0) ? L1 : lua_tothread(L1, f);
  973. lua_State *ts = (t == 0) ? L1 : lua_tothread(L1, t);
  974. int n = getnum;
  975. if (n == 0) n = lua_gettop(fs);
  976. lua_xmove(fs, ts, n);
  977. }
  978. else if EQ("loadstring") {
  979. size_t sl;
  980. const char *s = luaL_checklstring(L1, getnum, &sl);
  981. luaL_loadbuffer(L1, s, sl, s);
  982. }
  983. else if EQ("loadfile") {
  984. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  985. }
  986. else if EQ("setmetatable") {
  987. lua_setmetatable(L1, getindex);
  988. }
  989. else if EQ("getmetatable") {
  990. if (lua_getmetatable(L1, getindex) == 0)
  991. lua_pushnil(L1);
  992. }
  993. else if EQ("type") {
  994. lua_pushstring(L1, luaL_typename(L1, getnum));
  995. }
  996. else if EQ("append") {
  997. int t = getindex;
  998. int i = lua_rawlen(L1, t);
  999. lua_rawseti(L1, t, i + 1);
  1000. }
  1001. else if EQ("getctx") {
  1002. int i = 0;
  1003. int s = lua_getctx(L1, &i);
  1004. pushcode(L1, s);
  1005. lua_pushinteger(L1, i);
  1006. }
  1007. else if EQ("checkstack") {
  1008. if (!lua_checkstack(L1, getnum))
  1009. luaL_error(L, "C stack overflow");
  1010. }
  1011. else if EQ("newmetatable") {
  1012. lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
  1013. }
  1014. else if EQ("testudata") {
  1015. int i = getindex;
  1016. lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
  1017. }
  1018. else if EQ("gsub") {
  1019. int a = getnum; int b = getnum; int c = getnum;
  1020. luaL_gsub(L1, lua_tostring(L1, a),
  1021. lua_tostring(L1, b),
  1022. lua_tostring(L1, c));
  1023. }
  1024. else if EQ("sethook") {
  1025. int mask = getnum;
  1026. int count = getnum;
  1027. sethookaux(L1, mask, count, getstring);
  1028. }
  1029. else if EQ("throw") {
  1030. #if defined(__cplusplus)
  1031. static struct X { int x; } x;
  1032. throw x;
  1033. #else
  1034. luaL_error(L1, "C++");
  1035. #endif
  1036. break;
  1037. }
  1038. else luaL_error(L, "unknown instruction %s", buff);
  1039. }
  1040. return 0;
  1041. }
  1042. static int testC (lua_State *L) {
  1043. lua_State *L1;
  1044. const char *pc;
  1045. if (lua_isuserdata(L, 1)) {
  1046. L1 = getstate(L);
  1047. pc = luaL_checkstring(L, 2);
  1048. }
  1049. else if (lua_isthread(L, 1)) {
  1050. L1 = lua_tothread(L, 1);
  1051. pc = luaL_checkstring(L, 2);
  1052. }
  1053. else {
  1054. L1 = L;
  1055. pc = luaL_checkstring(L, 1);
  1056. }
  1057. return runC(L, L1, pc);
  1058. }
  1059. static int Cfunc (lua_State *L) {
  1060. return runC(L, L, lua_tostring(L, lua_upvalueindex(1)));
  1061. }
  1062. static int Cfunck (lua_State *L) {
  1063. int i = 0;
  1064. lua_getctx(L, &i);
  1065. return runC(L, L, lua_tostring(L, i));
  1066. }
  1067. static int makeCfunc (lua_State *L) {
  1068. luaL_checkstring(L, 1);
  1069. lua_pushcclosure(L, Cfunc, lua_gettop(L));
  1070. return 1;
  1071. }
  1072. /* }====================================================== */
  1073. /*
  1074. ** {======================================================
  1075. ** tests for C hooks
  1076. ** =======================================================
  1077. */
  1078. /*
  1079. ** C hook that runs the C script stored in registry.C_HOOK[L]
  1080. */
  1081. static void Chook (lua_State *L, lua_Debug *ar) {
  1082. const char *scpt;
  1083. const char *const events [] = {"call", "ret", "line", "count", "tailcall"};
  1084. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK");
  1085. lua_pushlightuserdata(L, L);
  1086. lua_gettable(L, -2); /* get C_HOOK[L] (script saved by sethookaux) */
  1087. scpt = lua_tostring(L, -1); /* not very religious (string will be popped) */
  1088. lua_pop(L, 2); /* remove C_HOOK and script */
  1089. lua_pushstring(L, events[ar->event]); /* may be used by script */
  1090. lua_pushinteger(L, ar->currentline); /* may be used by script */
  1091. runC(L, L, scpt); /* run script from C_HOOK[L] */
  1092. }
  1093. /*
  1094. ** sets registry.C_HOOK[L] = scpt and sets Chook as a hook
  1095. */
  1096. static void sethookaux (lua_State *L, int mask, int count, const char *scpt) {
  1097. if (*scpt == '\0') { /* no script? */
  1098. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1099. return;
  1100. }
  1101. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* get C_HOOK table */
  1102. if (!lua_istable(L, -1)) { /* no hook table? */
  1103. lua_pop(L, 1); /* remove previous value */
  1104. lua_newtable(L); /* create new C_HOOK table */
  1105. lua_pushvalue(L, -1);
  1106. lua_setfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* register it */
  1107. }
  1108. lua_pushlightuserdata(L, L);
  1109. lua_pushstring(L, scpt);
  1110. lua_settable(L, -3); /* C_HOOK[L] = script */
  1111. lua_sethook(L, Chook, mask, count);
  1112. }
  1113. static int sethook (lua_State *L) {
  1114. if (lua_isnoneornil(L, 1))
  1115. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1116. else {
  1117. const char *scpt = luaL_checkstring(L, 1);
  1118. const char *smask = luaL_checkstring(L, 2);
  1119. int count = luaL_optint(L, 3, 0);
  1120. int mask = 0;
  1121. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  1122. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  1123. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  1124. if (count > 0) mask |= LUA_MASKCOUNT;
  1125. sethookaux(L, mask, count, scpt);
  1126. }
  1127. return 0;
  1128. }
  1129. static int coresume (lua_State *L) {
  1130. int status;
  1131. lua_State *co = lua_tothread(L, 1);
  1132. luaL_argcheck(L, co, 1, "coroutine expected");
  1133. status = lua_resume(co, 0);
  1134. if (status != LUA_OK && status != LUA_YIELD) {
  1135. lua_pushboolean(L, 0);
  1136. lua_insert(L, -2);
  1137. return 2; /* return false + error message */
  1138. }
  1139. else {
  1140. lua_pushboolean(L, 1);
  1141. return 1;
  1142. }
  1143. }
  1144. /* }====================================================== */
  1145. static const struct luaL_Reg tests_funcs[] = {
  1146. {"checkmemory", lua_checkmemory},
  1147. {"closestate", closestate},
  1148. {"d2s", d2s},
  1149. {"doonnewstack", doonnewstack},
  1150. {"doremote", doremote},
  1151. {"gccolor", get_gccolor},
  1152. {"gcstate", gcstate},
  1153. {"getref", getref},
  1154. {"hash", hash_query},
  1155. {"int2fb", int2fb_aux},
  1156. {"limits", get_limits},
  1157. {"listcode", listcode},
  1158. {"listk", listk},
  1159. {"listlocals", listlocals},
  1160. {"loadlib", loadlib},
  1161. {"newstate", newstate},
  1162. {"newuserdata", newuserdata},
  1163. {"num2int", num2int},
  1164. {"pushuserdata", pushuserdata},
  1165. {"querystr", string_query},
  1166. {"querytab", table_query},
  1167. {"ref", tref},
  1168. {"resume", coresume},
  1169. {"s2d", s2d},
  1170. {"sethook", sethook},
  1171. {"stacklevel", stacklevel},
  1172. {"testC", testC},
  1173. {"makeCfunc", makeCfunc},
  1174. {"totalmem", mem_query},
  1175. {"trick", settrick},
  1176. {"udataval", udataval},
  1177. {"unref", unref},
  1178. {"upvalue", upvalue},
  1179. {NULL, NULL}
  1180. };
  1181. static void checkfinalmem (void) {
  1182. lua_assert(l_memcontrol.numblocks == 0);
  1183. lua_assert(l_memcontrol.total == 0);
  1184. }
  1185. int luaB_opentests (lua_State *L) {
  1186. void *ud;
  1187. lua_atpanic(L, &tpanic);
  1188. atexit(checkfinalmem);
  1189. lua_assert(lua_getallocf(L, &ud) == debug_realloc);
  1190. lua_assert(ud == cast(void *, &l_memcontrol));
  1191. lua_setallocf(L, lua_getallocf(L, NULL), ud);
  1192. luaL_register(L, "T", tests_funcs);
  1193. return 0;
  1194. }
  1195. #endif