ltests.c 37 KB

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