ltests.c 47 KB

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