ltests.c 44 KB

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