ltests.c 51 KB

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