ltests.c 44 KB

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