ltests.c 43 KB

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