ltests.c 37 KB

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