ltests.c 36 KB

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