ltests.c 39 KB

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