ltests.c 44 KB

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