ltests.c 42 KB

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