ltests.c 36 KB

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