ltests.c 39 KB

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