ltests.c 51 KB

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