ltests.c 44 KB

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