ltests.c 47 KB

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