ltests.c 34 KB

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