ltests.c 48 KB

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