ltests.c 37 KB

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