ltests.c 37 KB

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