ltests.c 43 KB

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