ltests.c 44 KB

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