ltests.c 36 KB

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