ltests.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. /*
  2. ** $Id: ltests.c,v 2.215 2017/04/24 16:59:26 roberto Exp roberto $
  3. ** Internal Module for Debugging of the Lua Implementation
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define ltests_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <limits.h>
  10. #include <setjmp.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "lua.h"
  15. #include "lapi.h"
  16. #include "lauxlib.h"
  17. #include "lcode.h"
  18. #include "lctype.h"
  19. #include "ldebug.h"
  20. #include "ldo.h"
  21. #include "lfunc.h"
  22. #include "lmem.h"
  23. #include "lopcodes.h"
  24. #include "lstate.h"
  25. #include "lstring.h"
  26. #include "ltable.h"
  27. #include "lualib.h"
  28. /*
  29. ** The whole module only makes sense with LUA_DEBUG on
  30. */
  31. #if defined(LUA_DEBUG)
  32. void *l_Trick = 0;
  33. int islocked = 0;
  34. #define obj_at(L,k) (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_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->allweak);
  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 = 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", name,
  451. GETARG_A(i), GETARG_B(i), GETARG_C(i));
  452. break;
  453. case iABx:
  454. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
  455. break;
  456. case iAsBx:
  457. sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
  458. break;
  459. case iAx:
  460. sprintf(buff+strlen(buff), "%-12s%4d", name, GETARG_Ax(i));
  461. break;
  462. }
  463. return buff;
  464. }
  465. #if 0
  466. void luaI_printcode (Proto *pt, int size) {
  467. int pc;
  468. for (pc=0; pc<size; pc++) {
  469. char buff[100];
  470. printf("%s\n", buildop(pt, pc, buff));
  471. }
  472. printf("-------\n");
  473. }
  474. void luaI_printinst (Proto *pt, int pc) {
  475. char buff[100];
  476. printf("%s\n", buildop(pt, pc, buff));
  477. }
  478. #endif
  479. static int listcode (lua_State *L) {
  480. int pc;
  481. Proto *p;
  482. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  483. 1, "Lua function expected");
  484. p = getproto(obj_at(L, 1));
  485. lua_newtable(L);
  486. setnameval(L, "maxstack", p->maxstacksize);
  487. setnameval(L, "numparams", p->numparams);
  488. for (pc=0; pc<p->sizecode; pc++) {
  489. char buff[100];
  490. lua_pushinteger(L, pc+1);
  491. lua_pushstring(L, buildop(p, pc, buff));
  492. lua_settable(L, -3);
  493. }
  494. return 1;
  495. }
  496. static int listk (lua_State *L) {
  497. Proto *p;
  498. int i;
  499. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  500. 1, "Lua function expected");
  501. p = getproto(obj_at(L, 1));
  502. lua_createtable(L, p->sizek, 0);
  503. for (i=0; i<p->sizek; i++) {
  504. pushobject(L, p->k+i);
  505. lua_rawseti(L, -2, i+1);
  506. }
  507. return 1;
  508. }
  509. static int listlocals (lua_State *L) {
  510. Proto *p;
  511. int pc = cast_int(luaL_checkinteger(L, 2)) - 1;
  512. int i = 0;
  513. const char *name;
  514. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  515. 1, "Lua function expected");
  516. p = getproto(obj_at(L, 1));
  517. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  518. lua_pushstring(L, name);
  519. return i-1;
  520. }
  521. /* }====================================================== */
  522. static void printstack (lua_State *L) {
  523. int i;
  524. int n = lua_gettop(L);
  525. for (i = 1; i <= n; i++) {
  526. printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL));
  527. lua_pop(L, 1);
  528. }
  529. printf("\n");
  530. }
  531. static int get_limits (lua_State *L) {
  532. lua_createtable(L, 0, 5);
  533. setnameval(L, "BITS_INT", LUAI_BITSINT);
  534. setnameval(L, "MAXARG_Ax", MAXARG_Ax);
  535. setnameval(L, "MAXARG_Bx", MAXARG_Bx);
  536. setnameval(L, "MAXARG_sBx", MAXARG_sBx);
  537. setnameval(L, "BITS_INT", LUAI_BITSINT);
  538. setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
  539. setnameval(L, "NUM_OPCODES", NUM_OPCODES);
  540. return 1;
  541. }
  542. static int mem_query (lua_State *L) {
  543. if (lua_isnone(L, 1)) {
  544. lua_pushinteger(L, l_memcontrol.total);
  545. lua_pushinteger(L, l_memcontrol.numblocks);
  546. lua_pushinteger(L, l_memcontrol.maxmem);
  547. return 3;
  548. }
  549. else if (lua_isnumber(L, 1)) {
  550. unsigned long limit = cast(unsigned long, luaL_checkinteger(L, 1));
  551. if (limit == 0) limit = ULONG_MAX;
  552. l_memcontrol.memlimit = limit;
  553. return 0;
  554. }
  555. else {
  556. const char *t = luaL_checkstring(L, 1);
  557. int i;
  558. for (i = LUA_NUMTAGS - 1; i >= 0; i--) {
  559. if (strcmp(t, ttypename(i)) == 0) {
  560. lua_pushinteger(L, l_memcontrol.objcount[i]);
  561. return 1;
  562. }
  563. }
  564. return luaL_error(L, "unkown type '%s'", t);
  565. }
  566. }
  567. static int settrick (lua_State *L) {
  568. if (ttisnil(obj_at(L, 1)))
  569. l_Trick = NULL;
  570. else
  571. l_Trick = gcvalue(obj_at(L, 1));
  572. return 0;
  573. }
  574. static int gc_color (lua_State *L) {
  575. TValue *o;
  576. luaL_checkany(L, 1);
  577. o = obj_at(L, 1);
  578. if (!iscollectable(o))
  579. lua_pushstring(L, "no collectable");
  580. else {
  581. GCObject *obj = gcvalue(o);
  582. lua_pushstring(L, isdead(G(L), obj) ? "dead" :
  583. iswhite(obj) ? "white" :
  584. isblack(obj) ? "black" : "grey");
  585. }
  586. return 1;
  587. }
  588. static int gc_age (lua_State *L) {
  589. TValue *o;
  590. luaL_checkany(L, 1);
  591. o = obj_at(L, 1);
  592. if (!iscollectable(o))
  593. lua_pushstring(L, "no collectable");
  594. else {
  595. static const char *gennames[] = {"new", "survival", "old0", "old1",
  596. "old", "touched1", "touched2"};
  597. GCObject *obj = gcvalue(o);
  598. lua_pushstring(L, gennames[getage(obj)]);
  599. }
  600. return 1;
  601. }
  602. static int gc_printobj (lua_State *L) {
  603. TValue *o;
  604. luaL_checkany(L, 1);
  605. o = obj_at(L, 1);
  606. if (!iscollectable(o))
  607. printf("no collectable\n");
  608. else {
  609. GCObject *obj = gcvalue(o);
  610. printobj(G(L), obj);
  611. printf("\n");
  612. }
  613. return 0;
  614. }
  615. static int gc_state (lua_State *L) {
  616. static const char *statenames[] = {"propagate", "atomic", "sweepallgc",
  617. "sweepfinobj", "sweeptobefnz", "sweepend", "pause", ""};
  618. static const int states[] = {GCSpropagate, GCSenteratomic, GCSswpallgc,
  619. GCSswpfinobj, GCSswptobefnz, GCSswpend, GCSpause, -1};
  620. int option = states[luaL_checkoption(L, 1, "", statenames)];
  621. if (option == -1) {
  622. lua_pushstring(L, statenames[G(L)->gcstate]);
  623. return 1;
  624. }
  625. else {
  626. global_State *g = G(L);
  627. if (G(L)->gckind == KGC_GEN)
  628. luaL_error(L, "cannot change states in generational mode");
  629. lua_lock(L);
  630. if (option < g->gcstate) { /* must cross 'pause'? */
  631. luaC_runtilstate(L, bitmask(GCSpause)); /* run until pause */
  632. }
  633. luaC_runtilstate(L, bitmask(option));
  634. lua_assert(G(L)->gcstate == option);
  635. lua_unlock(L);
  636. return 0;
  637. }
  638. }
  639. static int hash_query (lua_State *L) {
  640. if (lua_isnone(L, 2)) {
  641. luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  642. lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
  643. }
  644. else {
  645. TValue *o = obj_at(L, 1);
  646. Table *t;
  647. luaL_checktype(L, 2, LUA_TTABLE);
  648. t = hvalue(obj_at(L, 2));
  649. lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
  650. }
  651. return 1;
  652. }
  653. static int stacklevel (lua_State *L) {
  654. unsigned long a = 0;
  655. lua_pushinteger(L, (L->top - L->stack));
  656. lua_pushinteger(L, (L->stack_last - L->stack));
  657. lua_pushinteger(L, (unsigned long)&a);
  658. return 3;
  659. }
  660. static int table_query (lua_State *L) {
  661. const Table *t;
  662. int i = cast_int(luaL_optinteger(L, 2, -1));
  663. luaL_checktype(L, 1, LUA_TTABLE);
  664. t = hvalue(obj_at(L, 1));
  665. if (i == -1) {
  666. lua_pushinteger(L, t->sizearray);
  667. lua_pushinteger(L, allocsizenode(t));
  668. lua_pushinteger(L, isdummy(t) ? 0 : t->lastfree - t->node);
  669. }
  670. else if ((unsigned int)i < t->sizearray) {
  671. lua_pushinteger(L, i);
  672. pushobject(L, &t->array[i]);
  673. lua_pushnil(L);
  674. }
  675. else if ((i -= t->sizearray) < sizenode(t)) {
  676. if (!ttisnil(gval(gnode(t, i))) ||
  677. ttisnil(gkey(gnode(t, i))) ||
  678. ttisnumber(gkey(gnode(t, i)))) {
  679. pushobject(L, gkey(gnode(t, i)));
  680. }
  681. else
  682. lua_pushliteral(L, "<undef>");
  683. pushobject(L, gval(gnode(t, i)));
  684. if (gnext(&t->node[i]) != 0)
  685. lua_pushinteger(L, gnext(&t->node[i]));
  686. else
  687. lua_pushnil(L);
  688. }
  689. return 3;
  690. }
  691. static int string_query (lua_State *L) {
  692. stringtable *tb = &G(L)->strt;
  693. int s = cast_int(luaL_optinteger(L, 1, 0)) - 1;
  694. if (s == -1) {
  695. lua_pushinteger(L ,tb->size);
  696. lua_pushinteger(L ,tb->nuse);
  697. return 2;
  698. }
  699. else if (s < tb->size) {
  700. TString *ts;
  701. int n = 0;
  702. for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) {
  703. setsvalue2s(L, L->top, ts);
  704. api_incr_top(L);
  705. n++;
  706. }
  707. return n;
  708. }
  709. else return 0;
  710. }
  711. static int tref (lua_State *L) {
  712. int level = lua_gettop(L);
  713. luaL_checkany(L, 1);
  714. lua_pushvalue(L, 1);
  715. lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX));
  716. lua_assert(lua_gettop(L) == level+1); /* +1 for result */
  717. return 1;
  718. }
  719. static int getref (lua_State *L) {
  720. int level = lua_gettop(L);
  721. lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1));
  722. lua_assert(lua_gettop(L) == level+1);
  723. return 1;
  724. }
  725. static int unref (lua_State *L) {
  726. int level = lua_gettop(L);
  727. luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1)));
  728. lua_assert(lua_gettop(L) == level);
  729. return 0;
  730. }
  731. static int upvalue (lua_State *L) {
  732. int n = cast_int(luaL_checkinteger(L, 2));
  733. luaL_checktype(L, 1, LUA_TFUNCTION);
  734. if (lua_isnone(L, 3)) {
  735. const char *name = lua_getupvalue(L, 1, n);
  736. if (name == NULL) return 0;
  737. lua_pushstring(L, name);
  738. return 2;
  739. }
  740. else {
  741. const char *name = lua_setupvalue(L, 1, n);
  742. lua_pushstring(L, name);
  743. return 1;
  744. }
  745. }
  746. static int newuserdata (lua_State *L) {
  747. size_t size = cast(size_t, luaL_checkinteger(L, 1));
  748. char *p = cast(char *, lua_newuserdata(L, size));
  749. while (size--) *p++ = '\0';
  750. return 1;
  751. }
  752. static int pushuserdata (lua_State *L) {
  753. lua_Integer u = luaL_checkinteger(L, 1);
  754. lua_pushlightuserdata(L, cast(void *, cast(size_t, u)));
  755. return 1;
  756. }
  757. static int udataval (lua_State *L) {
  758. lua_pushinteger(L, cast(long, lua_touserdata(L, 1)));
  759. return 1;
  760. }
  761. static int doonnewstack (lua_State *L) {
  762. lua_State *L1 = lua_newthread(L);
  763. size_t l;
  764. const char *s = luaL_checklstring(L, 1, &l);
  765. int status = luaL_loadbuffer(L1, s, l, s);
  766. if (status == LUA_OK)
  767. status = lua_pcall(L1, 0, 0, 0);
  768. lua_pushinteger(L, status);
  769. return 1;
  770. }
  771. static int s2d (lua_State *L) {
  772. lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
  773. return 1;
  774. }
  775. static int d2s (lua_State *L) {
  776. double d = luaL_checknumber(L, 1);
  777. lua_pushlstring(L, cast(char *, &d), sizeof(d));
  778. return 1;
  779. }
  780. static int num2int (lua_State *L) {
  781. lua_pushinteger(L, lua_tointeger(L, 1));
  782. return 1;
  783. }
  784. static int newstate (lua_State *L) {
  785. void *ud;
  786. lua_Alloc f = lua_getallocf(L, &ud);
  787. lua_State *L1 = lua_newstate(f, ud);
  788. if (L1) {
  789. lua_atpanic(L1, tpanic);
  790. lua_pushlightuserdata(L, L1);
  791. }
  792. else
  793. lua_pushnil(L);
  794. return 1;
  795. }
  796. static lua_State *getstate (lua_State *L) {
  797. lua_State *L1 = cast(lua_State *, lua_touserdata(L, 1));
  798. luaL_argcheck(L, L1 != NULL, 1, "state expected");
  799. return L1;
  800. }
  801. static int loadlib (lua_State *L) {
  802. static const luaL_Reg libs[] = {
  803. {"_G", luaopen_base},
  804. {"coroutine", luaopen_coroutine},
  805. {"debug", luaopen_debug},
  806. {"io", luaopen_io},
  807. {"os", luaopen_os},
  808. {"math", luaopen_math},
  809. {"string", luaopen_string},
  810. {"table", luaopen_table},
  811. {NULL, NULL}
  812. };
  813. lua_State *L1 = getstate(L);
  814. int i;
  815. luaL_requiref(L1, "package", luaopen_package, 0);
  816. lua_assert(lua_type(L1, -1) == LUA_TTABLE);
  817. /* 'requiref' should not reload module already loaded... */
  818. luaL_requiref(L1, "package", NULL, 1); /* seg. fault if it reloads */
  819. /* ...but should return the same module */
  820. lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ));
  821. luaL_getsubtable(L1, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
  822. for (i = 0; libs[i].name; i++) {
  823. lua_pushcfunction(L1, libs[i].func);
  824. lua_setfield(L1, -2, libs[i].name);
  825. }
  826. return 0;
  827. }
  828. static int closestate (lua_State *L) {
  829. lua_State *L1 = getstate(L);
  830. lua_close(L1);
  831. return 0;
  832. }
  833. static int doremote (lua_State *L) {
  834. lua_State *L1 = getstate(L);
  835. size_t lcode;
  836. const char *code = luaL_checklstring(L, 2, &lcode);
  837. int status;
  838. lua_settop(L1, 0);
  839. status = luaL_loadbuffer(L1, code, lcode, code);
  840. if (status == LUA_OK)
  841. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  842. if (status != LUA_OK) {
  843. lua_pushnil(L);
  844. lua_pushstring(L, lua_tostring(L1, -1));
  845. lua_pushinteger(L, status);
  846. return 3;
  847. }
  848. else {
  849. int i = 0;
  850. while (!lua_isnone(L1, ++i))
  851. lua_pushstring(L, lua_tostring(L1, i));
  852. lua_pop(L1, i-1);
  853. return i-1;
  854. }
  855. }
  856. static int int2fb_aux (lua_State *L) {
  857. int b = luaO_int2fb((unsigned int)luaL_checkinteger(L, 1));
  858. lua_pushinteger(L, b);
  859. lua_pushinteger(L, (unsigned int)luaO_fb2int(b));
  860. return 2;
  861. }
  862. static int log2_aux (lua_State *L) {
  863. unsigned int x = (unsigned int)luaL_checkinteger(L, 1);
  864. lua_pushinteger(L, luaO_ceillog2(x));
  865. return 1;
  866. }
  867. struct Aux { jmp_buf jb; const char *paniccode; lua_State *L; };
  868. /*
  869. ** does a long-jump back to "main program".
  870. */
  871. static int panicback (lua_State *L) {
  872. struct Aux *b;
  873. lua_checkstack(L, 1); /* open space for 'Aux' struct */
  874. lua_getfield(L, LUA_REGISTRYINDEX, "_jmpbuf"); /* get 'Aux' struct */
  875. b = (struct Aux *)lua_touserdata(L, -1);
  876. lua_pop(L, 1); /* remove 'Aux' struct */
  877. runC(b->L, L, b->paniccode); /* run optional panic code */
  878. longjmp(b->jb, 1);
  879. return 1; /* to avoid warnings */
  880. }
  881. static int checkpanic (lua_State *L) {
  882. struct Aux b;
  883. void *ud;
  884. lua_State *L1;
  885. const char *code = luaL_checkstring(L, 1);
  886. lua_Alloc f = lua_getallocf(L, &ud);
  887. b.paniccode = luaL_optstring(L, 2, "");
  888. b.L = L;
  889. L1 = lua_newstate(f, ud); /* create new state */
  890. if (L1 == NULL) { /* error? */
  891. lua_pushnil(L);
  892. return 1;
  893. }
  894. lua_atpanic(L1, panicback); /* set its panic function */
  895. lua_pushlightuserdata(L1, &b);
  896. lua_setfield(L1, LUA_REGISTRYINDEX, "_jmpbuf"); /* store 'Aux' struct */
  897. if (setjmp(b.jb) == 0) { /* set jump buffer */
  898. runC(L, L1, code); /* run code unprotected */
  899. lua_pushliteral(L, "no errors");
  900. }
  901. else { /* error handling */
  902. /* move error message to original state */
  903. lua_pushstring(L, lua_tostring(L1, -1));
  904. }
  905. lua_close(L1);
  906. return 1;
  907. }
  908. /*
  909. ** {====================================================================
  910. ** function to test the API with C. It interprets a kind of assembler
  911. ** language with calls to the API, so the test can be driven by Lua code
  912. ** =====================================================================
  913. */
  914. static void sethookaux (lua_State *L, int mask, int count, const char *code);
  915. static const char *const delimits = " \t\n,;";
  916. static void skip (const char **pc) {
  917. for (;;) {
  918. if (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  919. else if (**pc == '#') {
  920. while (**pc != '\n' && **pc != '\0') (*pc)++;
  921. }
  922. else break;
  923. }
  924. }
  925. static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
  926. int res = 0;
  927. int sig = 1;
  928. skip(pc);
  929. if (**pc == '.') {
  930. res = cast_int(lua_tointeger(L1, -1));
  931. lua_pop(L1, 1);
  932. (*pc)++;
  933. return res;
  934. }
  935. else if (**pc == '*') {
  936. res = lua_gettop(L1);
  937. (*pc)++;
  938. return res;
  939. }
  940. else if (**pc == '-') {
  941. sig = -1;
  942. (*pc)++;
  943. }
  944. if (!lisdigit(cast_uchar(**pc)))
  945. luaL_error(L, "number expected (%s)", *pc);
  946. while (lisdigit(cast_uchar(**pc))) res = res*10 + (*(*pc)++) - '0';
  947. return sig*res;
  948. }
  949. static const char *getstring_aux (lua_State *L, char *buff, const char **pc) {
  950. int i = 0;
  951. skip(pc);
  952. if (**pc == '"' || **pc == '\'') { /* quoted string? */
  953. int quote = *(*pc)++;
  954. while (**pc != quote) {
  955. if (**pc == '\0') luaL_error(L, "unfinished string in C script");
  956. buff[i++] = *(*pc)++;
  957. }
  958. (*pc)++;
  959. }
  960. else {
  961. while (**pc != '\0' && !strchr(delimits, **pc))
  962. buff[i++] = *(*pc)++;
  963. }
  964. buff[i] = '\0';
  965. return buff;
  966. }
  967. static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
  968. skip(pc);
  969. switch (*(*pc)++) {
  970. case 'R': return LUA_REGISTRYINDEX;
  971. case 'G': return luaL_error(L, "deprecated index 'G'");
  972. case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc));
  973. default: (*pc)--; return getnum_aux(L, L1, pc);
  974. }
  975. }
  976. static void pushcode (lua_State *L, int code) {
  977. static const char *const codes[] = {"OK", "YIELD", "ERRRUN",
  978. "ERRSYNTAX", "ERRMEM", "ERRGCMM", "ERRERR"};
  979. lua_pushstring(L, codes[code]);
  980. }
  981. #define EQ(s1) (strcmp(s1, inst) == 0)
  982. #define getnum (getnum_aux(L, L1, &pc))
  983. #define getstring (getstring_aux(L, buff, &pc))
  984. #define getindex (getindex_aux(L, L1, &pc))
  985. static int testC (lua_State *L);
  986. static int Cfunck (lua_State *L, int status, lua_KContext ctx);
  987. /*
  988. ** arithmetic operation encoding for 'arith' instruction
  989. ** LUA_OPIDIV -> \
  990. ** LUA_OPSHL -> <
  991. ** LUA_OPSHR -> >
  992. ** LUA_OPUNM -> _
  993. ** LUA_OPBNOT -> !
  994. */
  995. static const char ops[] = "+-*%^/\\&|~<>_!";
  996. static int runC (lua_State *L, lua_State *L1, const char *pc) {
  997. char buff[300];
  998. int status = 0;
  999. if (pc == NULL) return luaL_error(L, "attempt to runC null script");
  1000. for (;;) {
  1001. const char *inst = getstring;
  1002. if EQ("") return 0;
  1003. else if EQ("absindex") {
  1004. lua_pushnumber(L1, lua_absindex(L1, getindex));
  1005. }
  1006. else if EQ("append") {
  1007. int t = getindex;
  1008. int i = lua_rawlen(L1, t);
  1009. lua_rawseti(L1, t, i + 1);
  1010. }
  1011. else if EQ("arith") {
  1012. int op;
  1013. skip(&pc);
  1014. op = strchr(ops, *pc++) - ops;
  1015. lua_arith(L1, op);
  1016. }
  1017. else if EQ("call") {
  1018. int narg = getnum;
  1019. int nres = getnum;
  1020. lua_call(L1, narg, nres);
  1021. }
  1022. else if EQ("callk") {
  1023. int narg = getnum;
  1024. int nres = getnum;
  1025. int i = getindex;
  1026. lua_callk(L1, narg, nres, i, Cfunck);
  1027. }
  1028. else if EQ("checkstack") {
  1029. int sz = getnum;
  1030. const char *msg = getstring;
  1031. if (*msg == '\0')
  1032. msg = NULL; /* to test 'luaL_checkstack' with no message */
  1033. luaL_checkstack(L1, sz, msg);
  1034. }
  1035. else if EQ("compare") {
  1036. const char *opt = getstring; /* EQ, LT, or LE */
  1037. int op = (opt[0] == 'E') ? LUA_OPEQ
  1038. : (opt[1] == 'T') ? LUA_OPLT : LUA_OPLE;
  1039. int a = getindex;
  1040. int b = getindex;
  1041. lua_pushboolean(L1, lua_compare(L1, a, b, op));
  1042. }
  1043. else if EQ("concat") {
  1044. lua_concat(L1, getnum);
  1045. }
  1046. else if EQ("copy") {
  1047. int f = getindex;
  1048. lua_copy(L1, f, getindex);
  1049. }
  1050. else if EQ("func2num") {
  1051. lua_CFunction func = lua_tocfunction(L1, getindex);
  1052. lua_pushnumber(L1, cast(size_t, func));
  1053. }
  1054. else if EQ("getfield") {
  1055. int t = getindex;
  1056. lua_getfield(L1, t, getstring);
  1057. }
  1058. else if EQ("getglobal") {
  1059. lua_getglobal(L1, getstring);
  1060. }
  1061. else if EQ("getmetatable") {
  1062. if (lua_getmetatable(L1, getindex) == 0)
  1063. lua_pushnil(L1);
  1064. }
  1065. else if EQ("gettable") {
  1066. lua_gettable(L1, getindex);
  1067. }
  1068. else if EQ("gettop") {
  1069. lua_pushinteger(L1, lua_gettop(L1));
  1070. }
  1071. else if EQ("gsub") {
  1072. int a = getnum; int b = getnum; int c = getnum;
  1073. luaL_gsub(L1, lua_tostring(L1, a),
  1074. lua_tostring(L1, b),
  1075. lua_tostring(L1, c));
  1076. }
  1077. else if EQ("insert") {
  1078. lua_insert(L1, getnum);
  1079. }
  1080. else if EQ("iscfunction") {
  1081. lua_pushboolean(L1, lua_iscfunction(L1, getindex));
  1082. }
  1083. else if EQ("isfunction") {
  1084. lua_pushboolean(L1, lua_isfunction(L1, getindex));
  1085. }
  1086. else if EQ("isnil") {
  1087. lua_pushboolean(L1, lua_isnil(L1, getindex));
  1088. }
  1089. else if EQ("isnull") {
  1090. lua_pushboolean(L1, lua_isnone(L1, getindex));
  1091. }
  1092. else if EQ("isnumber") {
  1093. lua_pushboolean(L1, lua_isnumber(L1, getindex));
  1094. }
  1095. else if EQ("isstring") {
  1096. lua_pushboolean(L1, lua_isstring(L1, getindex));
  1097. }
  1098. else if EQ("istable") {
  1099. lua_pushboolean(L1, lua_istable(L1, getindex));
  1100. }
  1101. else if EQ("isudataval") {
  1102. lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
  1103. }
  1104. else if EQ("isuserdata") {
  1105. lua_pushboolean(L1, lua_isuserdata(L1, getindex));
  1106. }
  1107. else if EQ("len") {
  1108. lua_len(L1, getindex);
  1109. }
  1110. else if EQ("Llen") {
  1111. lua_pushinteger(L1, luaL_len(L1, getindex));
  1112. }
  1113. else if EQ("loadfile") {
  1114. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  1115. }
  1116. else if EQ("loadstring") {
  1117. const char *s = luaL_checkstring(L1, getnum);
  1118. luaL_loadstring(L1, s);
  1119. }
  1120. else if EQ("newmetatable") {
  1121. lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
  1122. }
  1123. else if EQ("newtable") {
  1124. lua_newtable(L1);
  1125. }
  1126. else if EQ("newthread") {
  1127. lua_newthread(L1);
  1128. }
  1129. else if EQ("newuserdata") {
  1130. lua_newuserdata(L1, getnum);
  1131. }
  1132. else if EQ("next") {
  1133. lua_next(L1, -2);
  1134. }
  1135. else if EQ("objsize") {
  1136. lua_pushinteger(L1, lua_rawlen(L1, getindex));
  1137. }
  1138. else if EQ("pcall") {
  1139. int narg = getnum;
  1140. int nres = getnum;
  1141. status = lua_pcall(L1, narg, nres, getnum);
  1142. }
  1143. else if EQ("pcallk") {
  1144. int narg = getnum;
  1145. int nres = getnum;
  1146. int i = getindex;
  1147. status = lua_pcallk(L1, narg, nres, 0, i, Cfunck);
  1148. }
  1149. else if EQ("pop") {
  1150. lua_pop(L1, getnum);
  1151. }
  1152. else if EQ("print") {
  1153. int n = getnum;
  1154. if (n != 0) {
  1155. printf("%s\n", luaL_tolstring(L1, n, NULL));
  1156. lua_pop(L1, 1);
  1157. }
  1158. else printstack(L1);
  1159. }
  1160. else if EQ("pushbool") {
  1161. lua_pushboolean(L1, getnum);
  1162. }
  1163. else if EQ("pushcclosure") {
  1164. lua_pushcclosure(L1, testC, getnum);
  1165. }
  1166. else if EQ("pushint") {
  1167. lua_pushinteger(L1, getnum);
  1168. }
  1169. else if EQ("pushnil") {
  1170. lua_pushnil(L1);
  1171. }
  1172. else if EQ("pushnum") {
  1173. lua_pushnumber(L1, (lua_Number)getnum);
  1174. }
  1175. else if EQ("pushstatus") {
  1176. pushcode(L1, status);
  1177. }
  1178. else if EQ("pushstring") {
  1179. lua_pushstring(L1, getstring);
  1180. }
  1181. else if EQ("pushupvalueindex") {
  1182. lua_pushinteger(L1, lua_upvalueindex(getnum));
  1183. }
  1184. else if EQ("pushvalue") {
  1185. lua_pushvalue(L1, getindex);
  1186. }
  1187. else if EQ("rawgeti") {
  1188. int t = getindex;
  1189. lua_rawgeti(L1, t, getnum);
  1190. }
  1191. else if EQ("rawgetp") {
  1192. int t = getindex;
  1193. lua_rawgetp(L1, t, cast(void *, cast(size_t, getnum)));
  1194. }
  1195. else if EQ("rawsetp") {
  1196. int t = getindex;
  1197. lua_rawsetp(L1, t, cast(void *, cast(size_t, getnum)));
  1198. }
  1199. else if EQ("remove") {
  1200. lua_remove(L1, getnum);
  1201. }
  1202. else if EQ("replace") {
  1203. lua_replace(L1, getindex);
  1204. }
  1205. else if EQ("resume") {
  1206. int i = getindex;
  1207. status = lua_resume(lua_tothread(L1, i), L, getnum);
  1208. }
  1209. else if EQ("return") {
  1210. int n = getnum;
  1211. if (L1 != L) {
  1212. int i;
  1213. for (i = 0; i < n; i++)
  1214. lua_pushstring(L, lua_tostring(L1, -(n - i)));
  1215. }
  1216. return n;
  1217. }
  1218. else if EQ("rotate") {
  1219. int i = getindex;
  1220. lua_rotate(L1, i, getnum);
  1221. }
  1222. else if EQ("setfield") {
  1223. int t = getindex;
  1224. lua_setfield(L1, t, getstring);
  1225. }
  1226. else if EQ("setglobal") {
  1227. lua_setglobal(L1, getstring);
  1228. }
  1229. else if EQ("sethook") {
  1230. int mask = getnum;
  1231. int count = getnum;
  1232. sethookaux(L1, mask, count, getstring);
  1233. }
  1234. else if EQ("setmetatable") {
  1235. lua_setmetatable(L1, getindex);
  1236. }
  1237. else if EQ("settable") {
  1238. lua_settable(L1, getindex);
  1239. }
  1240. else if EQ("settop") {
  1241. lua_settop(L1, getnum);
  1242. }
  1243. else if EQ("testudata") {
  1244. int i = getindex;
  1245. lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
  1246. }
  1247. else if EQ("error") {
  1248. lua_error(L1);
  1249. }
  1250. else if EQ("throw") {
  1251. #if defined(__cplusplus)
  1252. static struct X { int x; } x;
  1253. throw x;
  1254. #else
  1255. luaL_error(L1, "C++");
  1256. #endif
  1257. break;
  1258. }
  1259. else if EQ("tobool") {
  1260. lua_pushboolean(L1, lua_toboolean(L1, getindex));
  1261. }
  1262. else if EQ("tocfunction") {
  1263. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  1264. }
  1265. else if EQ("tointeger") {
  1266. lua_pushinteger(L1, lua_tointeger(L1, getindex));
  1267. }
  1268. else if EQ("tonumber") {
  1269. lua_pushnumber(L1, lua_tonumber(L1, getindex));
  1270. }
  1271. else if EQ("topointer") {
  1272. lua_pushnumber(L1, cast(size_t, lua_topointer(L1, getindex)));
  1273. }
  1274. else if EQ("tostring") {
  1275. const char *s = lua_tostring(L1, getindex);
  1276. const char *s1 = lua_pushstring(L1, s);
  1277. lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0);
  1278. }
  1279. else if EQ("type") {
  1280. lua_pushstring(L1, luaL_typename(L1, getnum));
  1281. }
  1282. else if EQ("xmove") {
  1283. int f = getindex;
  1284. int t = getindex;
  1285. lua_State *fs = (f == 0) ? L1 : lua_tothread(L1, f);
  1286. lua_State *ts = (t == 0) ? L1 : lua_tothread(L1, t);
  1287. int n = getnum;
  1288. if (n == 0) n = lua_gettop(fs);
  1289. lua_xmove(fs, ts, n);
  1290. }
  1291. else if EQ("yield") {
  1292. return lua_yield(L1, getnum);
  1293. }
  1294. else if EQ("yieldk") {
  1295. int nres = getnum;
  1296. int i = getindex;
  1297. return lua_yieldk(L1, nres, i, Cfunck);
  1298. }
  1299. else luaL_error(L, "unknown instruction %s", buff);
  1300. }
  1301. return 0;
  1302. }
  1303. static int testC (lua_State *L) {
  1304. lua_State *L1;
  1305. const char *pc;
  1306. if (lua_isuserdata(L, 1)) {
  1307. L1 = getstate(L);
  1308. pc = luaL_checkstring(L, 2);
  1309. }
  1310. else if (lua_isthread(L, 1)) {
  1311. L1 = lua_tothread(L, 1);
  1312. pc = luaL_checkstring(L, 2);
  1313. }
  1314. else {
  1315. L1 = L;
  1316. pc = luaL_checkstring(L, 1);
  1317. }
  1318. return runC(L, L1, pc);
  1319. }
  1320. static int Cfunc (lua_State *L) {
  1321. return runC(L, L, lua_tostring(L, lua_upvalueindex(1)));
  1322. }
  1323. static int Cfunck (lua_State *L, int status, lua_KContext ctx) {
  1324. pushcode(L, status);
  1325. lua_setglobal(L, "status");
  1326. lua_pushinteger(L, ctx);
  1327. lua_setglobal(L, "ctx");
  1328. return runC(L, L, lua_tostring(L, ctx));
  1329. }
  1330. static int makeCfunc (lua_State *L) {
  1331. luaL_checkstring(L, 1);
  1332. lua_pushcclosure(L, Cfunc, lua_gettop(L));
  1333. return 1;
  1334. }
  1335. /* }====================================================== */
  1336. /*
  1337. ** {======================================================
  1338. ** tests for C hooks
  1339. ** =======================================================
  1340. */
  1341. /*
  1342. ** C hook that runs the C script stored in registry.C_HOOK[L]
  1343. */
  1344. static void Chook (lua_State *L, lua_Debug *ar) {
  1345. const char *scpt;
  1346. const char *const events [] = {"call", "ret", "line", "count", "tailcall"};
  1347. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK");
  1348. lua_pushlightuserdata(L, L);
  1349. lua_gettable(L, -2); /* get C_HOOK[L] (script saved by sethookaux) */
  1350. scpt = lua_tostring(L, -1); /* not very religious (string will be popped) */
  1351. lua_pop(L, 2); /* remove C_HOOK and script */
  1352. lua_pushstring(L, events[ar->event]); /* may be used by script */
  1353. lua_pushinteger(L, ar->currentline); /* may be used by script */
  1354. runC(L, L, scpt); /* run script from C_HOOK[L] */
  1355. }
  1356. /*
  1357. ** sets 'registry.C_HOOK[L] = scpt' and sets 'Chook' as a hook
  1358. */
  1359. static void sethookaux (lua_State *L, int mask, int count, const char *scpt) {
  1360. if (*scpt == '\0') { /* no script? */
  1361. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1362. return;
  1363. }
  1364. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* get C_HOOK table */
  1365. if (!lua_istable(L, -1)) { /* no hook table? */
  1366. lua_pop(L, 1); /* remove previous value */
  1367. lua_newtable(L); /* create new C_HOOK table */
  1368. lua_pushvalue(L, -1);
  1369. lua_setfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* register it */
  1370. }
  1371. lua_pushlightuserdata(L, L);
  1372. lua_pushstring(L, scpt);
  1373. lua_settable(L, -3); /* C_HOOK[L] = script */
  1374. lua_sethook(L, Chook, mask, count);
  1375. }
  1376. static int sethook (lua_State *L) {
  1377. if (lua_isnoneornil(L, 1))
  1378. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1379. else {
  1380. const char *scpt = luaL_checkstring(L, 1);
  1381. const char *smask = luaL_checkstring(L, 2);
  1382. int count = cast_int(luaL_optinteger(L, 3, 0));
  1383. int mask = 0;
  1384. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  1385. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  1386. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  1387. if (count > 0) mask |= LUA_MASKCOUNT;
  1388. sethookaux(L, mask, count, scpt);
  1389. }
  1390. return 0;
  1391. }
  1392. static int coresume (lua_State *L) {
  1393. int status;
  1394. lua_State *co = lua_tothread(L, 1);
  1395. luaL_argcheck(L, co, 1, "coroutine expected");
  1396. status = lua_resume(co, L, 0);
  1397. if (status != LUA_OK && status != LUA_YIELD) {
  1398. lua_pushboolean(L, 0);
  1399. lua_insert(L, -2);
  1400. return 2; /* return false + error message */
  1401. }
  1402. else {
  1403. lua_pushboolean(L, 1);
  1404. return 1;
  1405. }
  1406. }
  1407. /* }====================================================== */
  1408. static const struct luaL_Reg tests_funcs[] = {
  1409. {"checkmemory", lua_checkmemory},
  1410. {"closestate", closestate},
  1411. {"d2s", d2s},
  1412. {"doonnewstack", doonnewstack},
  1413. {"doremote", doremote},
  1414. {"gccolor", gc_color},
  1415. {"gcage", gc_age},
  1416. {"gcstate", gc_state},
  1417. {"pobj", gc_printobj},
  1418. {"getref", getref},
  1419. {"hash", hash_query},
  1420. {"int2fb", int2fb_aux},
  1421. {"log2", log2_aux},
  1422. {"limits", get_limits},
  1423. {"listcode", listcode},
  1424. {"listk", listk},
  1425. {"listlocals", listlocals},
  1426. {"loadlib", loadlib},
  1427. {"checkpanic", checkpanic},
  1428. {"newstate", newstate},
  1429. {"newuserdata", newuserdata},
  1430. {"num2int", num2int},
  1431. {"pushuserdata", pushuserdata},
  1432. {"querystr", string_query},
  1433. {"querytab", table_query},
  1434. {"ref", tref},
  1435. {"resume", coresume},
  1436. {"s2d", s2d},
  1437. {"sethook", sethook},
  1438. {"stacklevel", stacklevel},
  1439. {"testC", testC},
  1440. {"makeCfunc", makeCfunc},
  1441. {"totalmem", mem_query},
  1442. {"trick", settrick},
  1443. {"udataval", udataval},
  1444. {"unref", unref},
  1445. {"upvalue", upvalue},
  1446. {NULL, NULL}
  1447. };
  1448. static void checkfinalmem (void) {
  1449. lua_assert(l_memcontrol.numblocks == 0);
  1450. lua_assert(l_memcontrol.total == 0);
  1451. }
  1452. int luaB_opentests (lua_State *L) {
  1453. void *ud;
  1454. lua_atpanic(L, &tpanic);
  1455. atexit(checkfinalmem);
  1456. lua_assert(lua_getallocf(L, &ud) == debug_realloc);
  1457. lua_assert(ud == cast(void *, &l_memcontrol));
  1458. lua_setallocf(L, lua_getallocf(L, NULL), ud);
  1459. luaL_newlib(L, tests_funcs);
  1460. return 1;
  1461. }
  1462. #endif