ltests.c 36 KB

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