ltests.c 51 KB

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