ltests.c 43 KB

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