ltests.c 36 KB

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