ltests.c 43 KB

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