ltests.c 37 KB

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