ltests.c 37 KB

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