ltests.c 36 KB

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