ltests.c 36 KB

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