ltests.c 43 KB

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