ltests.c 44 KB

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