ltests.c 36 KB

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