ltests.c 39 KB

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