ltests.c 37 KB

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