ltests.c 43 KB

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