ltests.c 36 KB

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