ltests.c 44 KB

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