ltests.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. /*
  2. ** $Id: ltests.c $
  3. ** Internal Module for Debugging of the Lua Implementation
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define ltests_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <limits.h>
  10. #include <setjmp.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "lua.h"
  15. #include "lapi.h"
  16. #include "lauxlib.h"
  17. #include "lcode.h"
  18. #include "lctype.h"
  19. #include "ldebug.h"
  20. #include "ldo.h"
  21. #include "lfunc.h"
  22. #include "lmem.h"
  23. #include "lopcodes.h"
  24. #include "lopnames.h"
  25. #include "lstate.h"
  26. #include "lstring.h"
  27. #include "ltable.h"
  28. #include "lualib.h"
  29. /*
  30. ** The whole module only makes sense with LUA_DEBUG on
  31. */
  32. #if defined(LUA_DEBUG)
  33. void *l_Trick = 0;
  34. #define obj_at(L,k) s2v(L->ci->func.p + (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_pushinteger(L, val);
  38. lua_setfield(L, -2, name);
  39. }
  40. static void pushobject (lua_State *L, const TValue *o) {
  41. setobj2s(L, L->top.p, o);
  42. api_incr_top(L);
  43. }
  44. static void badexit (const char *fmt, const char *s1, const char *s2) {
  45. fprintf(stderr, fmt, s1);
  46. if (s2)
  47. fprintf(stderr, "extra info: %s\n", s2);
  48. /* avoid assertion failures when exiting */
  49. l_memcontrol.numblocks = l_memcontrol.total = 0;
  50. exit(EXIT_FAILURE);
  51. }
  52. static int tpanic (lua_State *L) {
  53. const char *msg = (lua_type(L, -1) == LUA_TSTRING)
  54. ? lua_tostring(L, -1)
  55. : "error object is not a string";
  56. return (badexit("PANIC: unprotected error in call to Lua API (%s)\n",
  57. msg, NULL),
  58. 0); /* do not return to Lua */
  59. }
  60. /*
  61. ** Warning function for tests. First, it concatenates all parts of
  62. ** a warning in buffer 'buff'. Then, it has three modes:
  63. ** - 0.normal: messages starting with '#' are shown on standard output;
  64. ** - other messages abort the tests (they represent real warning
  65. ** conditions; the standard tests should not generate these conditions
  66. ** unexpectedly);
  67. ** - 1.allow: all messages are shown;
  68. ** - 2.store: all warnings go to the global '_WARN';
  69. */
  70. static void warnf (void *ud, const char *msg, int tocont) {
  71. lua_State *L = cast(lua_State *, ud);
  72. static char buff[200] = ""; /* should be enough for tests... */
  73. static int onoff = 0;
  74. static int mode = 0; /* start in normal mode */
  75. static int lasttocont = 0;
  76. if (!lasttocont && !tocont && *msg == '@') { /* control message? */
  77. if (buff[0] != '\0')
  78. badexit("Control warning during warning: %s\naborting...\n", msg, buff);
  79. if (strcmp(msg, "@off") == 0)
  80. onoff = 0;
  81. else if (strcmp(msg, "@on") == 0)
  82. onoff = 1;
  83. else if (strcmp(msg, "@normal") == 0)
  84. mode = 0;
  85. else if (strcmp(msg, "@allow") == 0)
  86. mode = 1;
  87. else if (strcmp(msg, "@store") == 0)
  88. mode = 2;
  89. else
  90. badexit("Invalid control warning in test mode: %s\naborting...\n",
  91. msg, NULL);
  92. return;
  93. }
  94. lasttocont = tocont;
  95. if (strlen(msg) >= sizeof(buff) - strlen(buff))
  96. badexit("warnf-buffer overflow (%s)\n", msg, buff);
  97. strcat(buff, msg); /* add new message to current warning */
  98. if (!tocont) { /* message finished? */
  99. lua_unlock(L);
  100. luaL_checkstack(L, 1, "warn stack space");
  101. lua_getglobal(L, "_WARN");
  102. if (!lua_toboolean(L, -1))
  103. lua_pop(L, 1); /* ok, no previous unexpected warning */
  104. else {
  105. badexit("Unhandled warning in store mode: %s\naborting...\n",
  106. lua_tostring(L, -1), buff);
  107. }
  108. lua_lock(L);
  109. switch (mode) {
  110. case 0: { /* normal */
  111. if (buff[0] != '#' && onoff) /* unexpected warning? */
  112. badexit("Unexpected warning in test mode: %s\naborting...\n",
  113. buff, NULL);
  114. } /* FALLTHROUGH */
  115. case 1: { /* allow */
  116. if (onoff)
  117. fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */
  118. break;
  119. }
  120. case 2: { /* store */
  121. lua_unlock(L);
  122. luaL_checkstack(L, 1, "warn stack space");
  123. lua_pushstring(L, buff);
  124. lua_setglobal(L, "_WARN"); /* assign message to global '_WARN' */
  125. lua_lock(L);
  126. break;
  127. }
  128. }
  129. buff[0] = '\0'; /* prepare buffer for next warning */
  130. }
  131. }
  132. /*
  133. ** {======================================================================
  134. ** Controlled version for realloc.
  135. ** =======================================================================
  136. */
  137. #define MARK 0x55 /* 01010101 (a nice pattern) */
  138. typedef union Header {
  139. LUAI_MAXALIGN;
  140. struct {
  141. size_t size;
  142. int type;
  143. } d;
  144. } Header;
  145. #if !defined(EXTERNMEMCHECK)
  146. /* full memory check */
  147. #define MARKSIZE 16 /* size of marks after each block */
  148. #define fillmem(mem,size) memset(mem, -MARK, size)
  149. #else
  150. /* external memory check: don't do it twice */
  151. #define MARKSIZE 0
  152. #define fillmem(mem,size) /* empty */
  153. #endif
  154. Memcontrol l_memcontrol =
  155. {0, 0UL, 0UL, 0UL, 0UL, (~0UL),
  156. {0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL, 0UL}};
  157. static void freeblock (Memcontrol *mc, Header *block) {
  158. if (block) {
  159. size_t size = block->d.size;
  160. int i;
  161. for (i = 0; i < MARKSIZE; i++) /* check marks after block */
  162. lua_assert(*(cast_charp(block + 1) + size + i) == MARK);
  163. mc->objcount[block->d.type]--;
  164. fillmem(block, sizeof(Header) + size + MARKSIZE); /* erase block */
  165. free(block); /* actually free block */
  166. mc->numblocks--; /* update counts */
  167. mc->total -= size;
  168. }
  169. }
  170. void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
  171. Memcontrol *mc = cast(Memcontrol *, ud);
  172. Header *block = cast(Header *, b);
  173. int type;
  174. if (mc->memlimit == 0) { /* first time? */
  175. char *limit = getenv("MEMLIMIT"); /* initialize memory limit */
  176. mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX;
  177. }
  178. if (block == NULL) {
  179. type = (oldsize < LUA_NUMTYPES) ? cast_int(oldsize) : 0;
  180. oldsize = 0;
  181. }
  182. else {
  183. block--; /* go to real header */
  184. type = block->d.type;
  185. lua_assert(oldsize == block->d.size);
  186. }
  187. if (size == 0) {
  188. freeblock(mc, block);
  189. return NULL;
  190. }
  191. if (mc->failnext) {
  192. mc->failnext = 0;
  193. return NULL; /* fake a single memory allocation error */
  194. }
  195. if (mc->countlimit != ~0UL && size != oldsize) { /* count limit in use? */
  196. if (mc->countlimit == 0)
  197. return NULL; /* fake a memory allocation error */
  198. mc->countlimit--;
  199. }
  200. if (size > oldsize && mc->total+size-oldsize > mc->memlimit)
  201. return NULL; /* fake a memory allocation error */
  202. else {
  203. Header *newblock;
  204. int i;
  205. size_t commonsize = (oldsize < size) ? oldsize : size;
  206. size_t realsize = sizeof(Header) + size + MARKSIZE;
  207. if (realsize < size) return NULL; /* arithmetic overflow! */
  208. newblock = cast(Header *, malloc(realsize)); /* alloc a new block */
  209. if (newblock == NULL)
  210. return NULL; /* really out of memory? */
  211. if (block) {
  212. memcpy(newblock + 1, block + 1, commonsize); /* copy old contents */
  213. freeblock(mc, block); /* erase (and check) old copy */
  214. }
  215. /* initialize new part of the block with something weird */
  216. fillmem(cast_charp(newblock + 1) + commonsize, size - commonsize);
  217. /* initialize marks after block */
  218. for (i = 0; i < MARKSIZE; i++)
  219. *(cast_charp(newblock + 1) + size + i) = MARK;
  220. newblock->d.size = size;
  221. newblock->d.type = type;
  222. mc->total += size;
  223. if (mc->total > mc->maxmem)
  224. mc->maxmem = mc->total;
  225. mc->numblocks++;
  226. mc->objcount[type]++;
  227. return newblock + 1;
  228. }
  229. }
  230. /* }====================================================================== */
  231. /*
  232. ** {=====================================================================
  233. ** Functions to check memory consistency.
  234. ** Most of these checks are done through asserts, so this code does
  235. ** not make sense with asserts off. For this reason, it uses 'assert'
  236. ** directly, instead of 'lua_assert'.
  237. ** ======================================================================
  238. */
  239. #include <assert.h>
  240. /*
  241. ** Check GC invariants. For incremental mode, a black object cannot
  242. ** point to a white one. For generational mode, really old objects
  243. ** cannot point to young objects. Both old1 and touched2 objects
  244. ** cannot point to new objects (but can point to survivals).
  245. ** (Threads and open upvalues, despite being marked "really old",
  246. ** continue to be visited in all collections, and therefore can point to
  247. ** new objects. They, and only they, are old but gray.)
  248. */
  249. static int testobjref1 (global_State *g, GCObject *f, GCObject *t) {
  250. if (isdead(g,t)) return 0;
  251. if (issweepphase(g))
  252. return 1; /* no invariants */
  253. else if (g->gckind != KGC_GENMINOR)
  254. return !(isblack(f) && iswhite(t)); /* basic incremental invariant */
  255. else { /* generational mode */
  256. if ((getage(f) == G_OLD && isblack(f)) && !isold(t))
  257. return 0;
  258. if ((getage(f) == G_OLD1 || getage(f) == G_TOUCHED2) &&
  259. getage(t) == G_NEW)
  260. return 0;
  261. return 1;
  262. }
  263. }
  264. static void printobj (global_State *g, GCObject *o) {
  265. printf("||%s(%p)-%c%c(%02X)||",
  266. ttypename(novariant(o->tt)), (void *)o,
  267. isdead(g,o) ? 'd' : isblack(o) ? 'b' : iswhite(o) ? 'w' : 'g',
  268. "ns01oTt"[getage(o)], o->marked);
  269. if (o->tt == LUA_VSHRSTR || o->tt == LUA_VLNGSTR)
  270. printf(" '%s'", getstr(gco2ts(o)));
  271. }
  272. void lua_printobj (lua_State *L, struct GCObject *o) {
  273. printobj(G(L), o);
  274. }
  275. static int testobjref (global_State *g, GCObject *f, GCObject *t) {
  276. int r1 = testobjref1(g, f, t);
  277. if (!r1) {
  278. printf("%d(%02X) - ", g->gcstate, g->currentwhite);
  279. printobj(g, f);
  280. printf(" -> ");
  281. printobj(g, t);
  282. printf("\n");
  283. }
  284. return r1;
  285. }
  286. static void checkobjref (global_State *g, GCObject *f, GCObject *t) {
  287. assert(testobjref(g, f, t));
  288. }
  289. /*
  290. ** Version where 't' can be NULL. In that case, it should not apply the
  291. ** macro 'obj2gco' over the object. ('t' may have several types, so this
  292. ** definition must be a macro.) Most checks need this version, because
  293. ** the check may run while an object is still being created.
  294. */
  295. #define checkobjrefN(g,f,t) { if (t) checkobjref(g,f,obj2gco(t)); }
  296. static void checkvalref (global_State *g, GCObject *f, const TValue *t) {
  297. assert(!iscollectable(t) || (righttt(t) && testobjref(g, f, gcvalue(t))));
  298. }
  299. static void checktable (global_State *g, Table *h) {
  300. unsigned int i;
  301. unsigned int asize = luaH_realasize(h);
  302. Node *n, *limit = gnode(h, sizenode(h));
  303. GCObject *hgc = obj2gco(h);
  304. checkobjrefN(g, hgc, h->metatable);
  305. for (i = 0; i < asize; i++) {
  306. TValue aux;
  307. arr2obj(h, i, &aux);
  308. checkvalref(g, hgc, &aux);
  309. }
  310. for (n = gnode(h, 0); n < limit; n++) {
  311. if (!isempty(gval(n))) {
  312. TValue k;
  313. getnodekey(g->mainthread, &k, n);
  314. assert(!keyisnil(n));
  315. checkvalref(g, hgc, &k);
  316. checkvalref(g, hgc, gval(n));
  317. }
  318. }
  319. }
  320. static void checkudata (global_State *g, Udata *u) {
  321. int i;
  322. GCObject *hgc = obj2gco(u);
  323. checkobjrefN(g, hgc, u->metatable);
  324. for (i = 0; i < u->nuvalue; i++)
  325. checkvalref(g, hgc, &u->uv[i].uv);
  326. }
  327. static void checkproto (global_State *g, Proto *f) {
  328. int i;
  329. GCObject *fgc = obj2gco(f);
  330. checkobjrefN(g, fgc, f->source);
  331. for (i=0; i<f->sizek; i++) {
  332. if (iscollectable(f->k + i))
  333. checkobjref(g, fgc, gcvalue(f->k + i));
  334. }
  335. for (i=0; i<f->sizeupvalues; i++)
  336. checkobjrefN(g, fgc, f->upvalues[i].name);
  337. for (i=0; i<f->sizep; i++)
  338. checkobjrefN(g, fgc, f->p[i]);
  339. for (i=0; i<f->sizelocvars; i++)
  340. checkobjrefN(g, fgc, f->locvars[i].varname);
  341. }
  342. static void checkCclosure (global_State *g, CClosure *cl) {
  343. GCObject *clgc = obj2gco(cl);
  344. int i;
  345. for (i = 0; i < cl->nupvalues; i++)
  346. checkvalref(g, clgc, &cl->upvalue[i]);
  347. }
  348. static void checkLclosure (global_State *g, LClosure *cl) {
  349. GCObject *clgc = obj2gco(cl);
  350. int i;
  351. checkobjrefN(g, clgc, cl->p);
  352. for (i=0; i<cl->nupvalues; i++) {
  353. UpVal *uv = cl->upvals[i];
  354. if (uv) {
  355. checkobjrefN(g, clgc, uv);
  356. if (!upisopen(uv))
  357. checkvalref(g, obj2gco(uv), uv->v.p);
  358. }
  359. }
  360. }
  361. static int lua_checkpc (CallInfo *ci) {
  362. if (!isLua(ci)) return 1;
  363. else {
  364. StkId f = ci->func.p;
  365. Proto *p = clLvalue(s2v(f))->p;
  366. return p->code <= ci->u.l.savedpc &&
  367. ci->u.l.savedpc <= p->code + p->sizecode;
  368. }
  369. }
  370. static void checkstack (global_State *g, lua_State *L1) {
  371. StkId o;
  372. CallInfo *ci;
  373. UpVal *uv;
  374. assert(!isdead(g, L1));
  375. if (L1->stack.p == NULL) { /* incomplete thread? */
  376. assert(L1->openupval == NULL && L1->ci == NULL);
  377. return;
  378. }
  379. for (uv = L1->openupval; uv != NULL; uv = uv->u.open.next)
  380. assert(upisopen(uv)); /* must be open */
  381. assert(L1->top.p <= L1->stack_last.p);
  382. assert(L1->tbclist.p <= L1->top.p);
  383. for (ci = L1->ci; ci != NULL; ci = ci->previous) {
  384. assert(ci->top.p <= L1->stack_last.p);
  385. assert(lua_checkpc(ci));
  386. }
  387. for (o = L1->stack.p; o < L1->stack_last.p; o++)
  388. checkliveness(L1, s2v(o)); /* entire stack must have valid values */
  389. }
  390. static void checkrefs (global_State *g, GCObject *o) {
  391. switch (o->tt) {
  392. case LUA_VUSERDATA: {
  393. checkudata(g, gco2u(o));
  394. break;
  395. }
  396. case LUA_VUPVAL: {
  397. checkvalref(g, o, gco2upv(o)->v.p);
  398. break;
  399. }
  400. case LUA_VTABLE: {
  401. checktable(g, gco2t(o));
  402. break;
  403. }
  404. case LUA_VTHREAD: {
  405. checkstack(g, gco2th(o));
  406. break;
  407. }
  408. case LUA_VLCL: {
  409. checkLclosure(g, gco2lcl(o));
  410. break;
  411. }
  412. case LUA_VCCL: {
  413. checkCclosure(g, gco2ccl(o));
  414. break;
  415. }
  416. case LUA_VPROTO: {
  417. checkproto(g, gco2p(o));
  418. break;
  419. }
  420. case LUA_VSHRSTR:
  421. case LUA_VLNGSTR: {
  422. assert(!isgray(o)); /* strings are never gray */
  423. break;
  424. }
  425. default: assert(0);
  426. }
  427. }
  428. /*
  429. ** Check consistency of an object:
  430. ** - Dead objects can only happen in the 'allgc' list during a sweep
  431. ** phase (controlled by the caller through 'maybedead').
  432. ** - During pause, all objects must be white.
  433. ** - In generational mode:
  434. ** * objects must be old enough for their lists ('listage').
  435. ** * old objects cannot be white.
  436. ** * old objects must be black, except for 'touched1', 'old0',
  437. ** threads, and open upvalues.
  438. ** * 'touched1' objects must be gray.
  439. */
  440. static void checkobject (global_State *g, GCObject *o, int maybedead,
  441. int listage) {
  442. if (isdead(g, o))
  443. assert(maybedead);
  444. else {
  445. assert(g->gcstate != GCSpause || iswhite(o));
  446. if (g->gckind == KGC_GENMINOR) { /* generational mode? */
  447. assert(getage(o) >= listage);
  448. if (isold(o)) {
  449. assert(!iswhite(o));
  450. assert(isblack(o) ||
  451. getage(o) == G_TOUCHED1 ||
  452. getage(o) == G_OLD0 ||
  453. o->tt == LUA_VTHREAD ||
  454. (o->tt == LUA_VUPVAL && upisopen(gco2upv(o))));
  455. }
  456. assert(getage(o) != G_TOUCHED1 || isgray(o));
  457. }
  458. checkrefs(g, o);
  459. }
  460. }
  461. static l_mem checkgraylist (global_State *g, GCObject *o) {
  462. int total = 0; /* count number of elements in the list */
  463. cast_void(g); /* better to keep it if we need to print an object */
  464. while (o) {
  465. assert(!!isgray(o) ^ (getage(o) == G_TOUCHED2));
  466. assert(!testbit(o->marked, TESTBIT));
  467. if (keepinvariant(g))
  468. l_setbit(o->marked, TESTBIT); /* mark that object is in a gray list */
  469. total++;
  470. switch (o->tt) {
  471. case LUA_VTABLE: o = gco2t(o)->gclist; break;
  472. case LUA_VLCL: o = gco2lcl(o)->gclist; break;
  473. case LUA_VCCL: o = gco2ccl(o)->gclist; break;
  474. case LUA_VTHREAD: o = gco2th(o)->gclist; break;
  475. case LUA_VPROTO: o = gco2p(o)->gclist; break;
  476. case LUA_VUSERDATA:
  477. assert(gco2u(o)->nuvalue > 0);
  478. o = gco2u(o)->gclist;
  479. break;
  480. default: assert(0); /* other objects cannot be in a gray list */
  481. }
  482. }
  483. return total;
  484. }
  485. /*
  486. ** Check objects in gray lists.
  487. */
  488. static l_mem checkgrays (global_State *g) {
  489. l_mem total = 0; /* count number of elements in all lists */
  490. if (!keepinvariant(g)) return total;
  491. total += checkgraylist(g, g->gray);
  492. total += checkgraylist(g, g->grayagain);
  493. total += checkgraylist(g, g->weak);
  494. total += checkgraylist(g, g->allweak);
  495. total += checkgraylist(g, g->ephemeron);
  496. return total;
  497. }
  498. /*
  499. ** Check whether 'o' should be in a gray list. If so, increment
  500. ** 'count' and check its TESTBIT. (It must have been previously set by
  501. ** 'checkgraylist'.)
  502. */
  503. static void incifingray (global_State *g, GCObject *o, l_mem *count) {
  504. if (!keepinvariant(g))
  505. return; /* gray lists not being kept in these phases */
  506. if (o->tt == LUA_VUPVAL) {
  507. /* only open upvalues can be gray */
  508. assert(!isgray(o) || upisopen(gco2upv(o)));
  509. return; /* upvalues are never in gray lists */
  510. }
  511. /* these are the ones that must be in gray lists */
  512. if (isgray(o) || getage(o) == G_TOUCHED2) {
  513. (*count)++;
  514. assert(testbit(o->marked, TESTBIT));
  515. resetbit(o->marked, TESTBIT); /* prepare for next cycle */
  516. }
  517. }
  518. static l_mem checklist (global_State *g, int maybedead, int tof,
  519. GCObject *newl, GCObject *survival, GCObject *old, GCObject *reallyold) {
  520. GCObject *o;
  521. l_mem total = 0; /* number of object that should be in gray lists */
  522. for (o = newl; o != survival; o = o->next) {
  523. checkobject(g, o, maybedead, G_NEW);
  524. incifingray(g, o, &total);
  525. assert(!tof == !tofinalize(o));
  526. }
  527. for (o = survival; o != old; o = o->next) {
  528. checkobject(g, o, 0, G_SURVIVAL);
  529. incifingray(g, o, &total);
  530. assert(!tof == !tofinalize(o));
  531. }
  532. for (o = old; o != reallyold; o = o->next) {
  533. checkobject(g, o, 0, G_OLD1);
  534. incifingray(g, o, &total);
  535. assert(!tof == !tofinalize(o));
  536. }
  537. for (o = reallyold; o != NULL; o = o->next) {
  538. checkobject(g, o, 0, G_OLD);
  539. incifingray(g, o, &total);
  540. assert(!tof == !tofinalize(o));
  541. }
  542. return total;
  543. }
  544. int lua_checkmemory (lua_State *L) {
  545. global_State *g = G(L);
  546. GCObject *o;
  547. int maybedead;
  548. l_mem totalin; /* total of objects that are in gray lists */
  549. l_mem totalshould; /* total of objects that should be in gray lists */
  550. if (keepinvariant(g)) {
  551. assert(!iswhite(g->mainthread));
  552. assert(!iswhite(gcvalue(&g->l_registry)));
  553. }
  554. assert(!isdead(g, gcvalue(&g->l_registry)));
  555. assert(g->sweepgc == NULL || issweepphase(g));
  556. totalin = checkgrays(g);
  557. /* check 'fixedgc' list */
  558. for (o = g->fixedgc; o != NULL; o = o->next) {
  559. assert(o->tt == LUA_VSHRSTR && isgray(o) && getage(o) == G_OLD);
  560. }
  561. /* check 'allgc' list */
  562. maybedead = (GCSatomic < g->gcstate && g->gcstate <= GCSswpallgc);
  563. totalshould = checklist(g, maybedead, 0, g->allgc,
  564. g->survival, g->old1, g->reallyold);
  565. /* check 'finobj' list */
  566. totalshould += checklist(g, 0, 1, g->finobj,
  567. g->finobjsur, g->finobjold1, g->finobjrold);
  568. /* check 'tobefnz' list */
  569. for (o = g->tobefnz; o != NULL; o = o->next) {
  570. checkobject(g, o, 0, G_NEW);
  571. incifingray(g, o, &totalshould);
  572. assert(tofinalize(o));
  573. assert(o->tt == LUA_VUSERDATA || o->tt == LUA_VTABLE);
  574. }
  575. if (keepinvariant(g))
  576. assert(totalin == totalshould);
  577. return 0;
  578. }
  579. /* }====================================================== */
  580. /*
  581. ** {======================================================
  582. ** Disassembler
  583. ** =======================================================
  584. */
  585. static char *buildop (Proto *p, int pc, char *buff) {
  586. char *obuff = buff;
  587. Instruction i = p->code[pc];
  588. OpCode o = GET_OPCODE(i);
  589. const char *name = opnames[o];
  590. int line = luaG_getfuncline(p, pc);
  591. int lineinfo = (p->lineinfo != NULL) ? p->lineinfo[pc] : 0;
  592. if (lineinfo == ABSLINEINFO)
  593. buff += sprintf(buff, "(__");
  594. else
  595. buff += sprintf(buff, "(%2d", lineinfo);
  596. buff += sprintf(buff, " - %4d) %4d - ", line, pc);
  597. switch (getOpMode(o)) {
  598. case iABC:
  599. sprintf(buff, "%-12s%4d %4d %4d%s", name,
  600. GETARG_A(i), GETARG_B(i), GETARG_C(i),
  601. GETARG_k(i) ? " (k)" : "");
  602. break;
  603. case ivABC:
  604. sprintf(buff, "%-12s%4d %4d %4d%s", name,
  605. GETARG_A(i), GETARG_vB(i), GETARG_vC(i),
  606. GETARG_k(i) ? " (k)" : "");
  607. break;
  608. case iABx:
  609. sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
  610. break;
  611. case iAsBx:
  612. sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
  613. break;
  614. case iAx:
  615. sprintf(buff, "%-12s%4d", name, GETARG_Ax(i));
  616. break;
  617. case isJ:
  618. sprintf(buff, "%-12s%4d", name, GETARG_sJ(i));
  619. break;
  620. }
  621. return obuff;
  622. }
  623. #if 0
  624. void luaI_printcode (Proto *pt, int size) {
  625. int pc;
  626. for (pc=0; pc<size; pc++) {
  627. char buff[100];
  628. printf("%s\n", buildop(pt, pc, buff));
  629. }
  630. printf("-------\n");
  631. }
  632. void luaI_printinst (Proto *pt, int pc) {
  633. char buff[100];
  634. printf("%s\n", buildop(pt, pc, buff));
  635. }
  636. #endif
  637. static int listcode (lua_State *L) {
  638. int pc;
  639. Proto *p;
  640. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  641. 1, "Lua function expected");
  642. p = getproto(obj_at(L, 1));
  643. lua_newtable(L);
  644. setnameval(L, "maxstack", p->maxstacksize);
  645. setnameval(L, "numparams", p->numparams);
  646. for (pc=0; pc<p->sizecode; pc++) {
  647. char buff[100];
  648. lua_pushinteger(L, pc+1);
  649. lua_pushstring(L, buildop(p, pc, buff));
  650. lua_settable(L, -3);
  651. }
  652. return 1;
  653. }
  654. static int printcode (lua_State *L) {
  655. int pc;
  656. Proto *p;
  657. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  658. 1, "Lua function expected");
  659. p = getproto(obj_at(L, 1));
  660. printf("maxstack: %d\n", p->maxstacksize);
  661. printf("numparams: %d\n", p->numparams);
  662. for (pc=0; pc<p->sizecode; pc++) {
  663. char buff[100];
  664. printf("%s\n", buildop(p, pc, buff));
  665. }
  666. return 0;
  667. }
  668. static int listk (lua_State *L) {
  669. Proto *p;
  670. int i;
  671. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  672. 1, "Lua function expected");
  673. p = getproto(obj_at(L, 1));
  674. lua_createtable(L, cast_uint(p->sizek), 0);
  675. for (i=0; i<p->sizek; i++) {
  676. pushobject(L, p->k+i);
  677. lua_rawseti(L, -2, i+1);
  678. }
  679. return 1;
  680. }
  681. static int listabslineinfo (lua_State *L) {
  682. Proto *p;
  683. int i;
  684. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  685. 1, "Lua function expected");
  686. p = getproto(obj_at(L, 1));
  687. luaL_argcheck(L, p->abslineinfo != NULL, 1, "function has no debug info");
  688. lua_createtable(L, 2u * cast_uint(p->sizeabslineinfo), 0);
  689. for (i=0; i < p->sizeabslineinfo; i++) {
  690. lua_pushinteger(L, p->abslineinfo[i].pc);
  691. lua_rawseti(L, -2, 2 * i + 1);
  692. lua_pushinteger(L, p->abslineinfo[i].line);
  693. lua_rawseti(L, -2, 2 * i + 2);
  694. }
  695. return 1;
  696. }
  697. static int listlocals (lua_State *L) {
  698. Proto *p;
  699. int pc = cast_int(luaL_checkinteger(L, 2)) - 1;
  700. int i = 0;
  701. const char *name;
  702. luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
  703. 1, "Lua function expected");
  704. p = getproto(obj_at(L, 1));
  705. while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
  706. lua_pushstring(L, name);
  707. return i-1;
  708. }
  709. /* }====================================================== */
  710. void lua_printstack (lua_State *L) {
  711. int i;
  712. int n = lua_gettop(L);
  713. printf("stack: >>\n");
  714. for (i = 1; i <= n; i++) {
  715. printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL));
  716. lua_pop(L, 1);
  717. }
  718. printf("<<\n");
  719. }
  720. static int get_limits (lua_State *L) {
  721. lua_createtable(L, 0, 6);
  722. setnameval(L, "IS32INT", LUAI_IS32INT);
  723. setnameval(L, "MAXARG_Ax", MAXARG_Ax);
  724. setnameval(L, "MAXARG_Bx", MAXARG_Bx);
  725. setnameval(L, "OFFSET_sBx", OFFSET_sBx);
  726. setnameval(L, "NUM_OPCODES", NUM_OPCODES);
  727. return 1;
  728. }
  729. static int mem_query (lua_State *L) {
  730. if (lua_isnone(L, 1)) {
  731. lua_pushinteger(L, cast(lua_Integer, l_memcontrol.total));
  732. lua_pushinteger(L, cast(lua_Integer, l_memcontrol.numblocks));
  733. lua_pushinteger(L, cast(lua_Integer, l_memcontrol.maxmem));
  734. return 3;
  735. }
  736. else if (lua_isnumber(L, 1)) {
  737. unsigned long limit = cast(unsigned long, luaL_checkinteger(L, 1));
  738. if (limit == 0) limit = ULONG_MAX;
  739. l_memcontrol.memlimit = limit;
  740. return 0;
  741. }
  742. else {
  743. const char *t = luaL_checkstring(L, 1);
  744. int i;
  745. for (i = LUA_NUMTYPES - 1; i >= 0; i--) {
  746. if (strcmp(t, ttypename(i)) == 0) {
  747. lua_pushinteger(L, cast(lua_Integer, l_memcontrol.objcount[i]));
  748. return 1;
  749. }
  750. }
  751. return luaL_error(L, "unknown type '%s'", t);
  752. }
  753. }
  754. static int alloc_count (lua_State *L) {
  755. if (lua_isnone(L, 1))
  756. l_memcontrol.countlimit = cast(unsigned long, ~0L);
  757. else
  758. l_memcontrol.countlimit = cast(unsigned long, luaL_checkinteger(L, 1));
  759. return 0;
  760. }
  761. static int alloc_failnext (lua_State *L) {
  762. UNUSED(L);
  763. l_memcontrol.failnext = 1;
  764. return 0;
  765. }
  766. static int settrick (lua_State *L) {
  767. if (ttisnil(obj_at(L, 1)))
  768. l_Trick = NULL;
  769. else
  770. l_Trick = gcvalue(obj_at(L, 1));
  771. return 0;
  772. }
  773. static int gc_color (lua_State *L) {
  774. TValue *o;
  775. luaL_checkany(L, 1);
  776. o = obj_at(L, 1);
  777. if (!iscollectable(o))
  778. lua_pushstring(L, "no collectable");
  779. else {
  780. GCObject *obj = gcvalue(o);
  781. lua_pushstring(L, isdead(G(L), obj) ? "dead" :
  782. iswhite(obj) ? "white" :
  783. isblack(obj) ? "black" : "gray");
  784. }
  785. return 1;
  786. }
  787. static int gc_age (lua_State *L) {
  788. TValue *o;
  789. luaL_checkany(L, 1);
  790. o = obj_at(L, 1);
  791. if (!iscollectable(o))
  792. lua_pushstring(L, "no collectable");
  793. else {
  794. static const char *gennames[] = {"new", "survival", "old0", "old1",
  795. "old", "touched1", "touched2"};
  796. GCObject *obj = gcvalue(o);
  797. lua_pushstring(L, gennames[getage(obj)]);
  798. }
  799. return 1;
  800. }
  801. static int gc_printobj (lua_State *L) {
  802. TValue *o;
  803. luaL_checkany(L, 1);
  804. o = obj_at(L, 1);
  805. if (!iscollectable(o))
  806. printf("no collectable\n");
  807. else {
  808. GCObject *obj = gcvalue(o);
  809. printobj(G(L), obj);
  810. printf("\n");
  811. }
  812. return 0;
  813. }
  814. static const char *statenames[] = {
  815. "propagate", "enteratomic", "atomic", "sweepallgc", "sweepfinobj",
  816. "sweeptobefnz", "sweepend", "callfin", "pause", ""};
  817. static int gc_state (lua_State *L) {
  818. static const int states[] = {
  819. GCSpropagate, GCSenteratomic, GCSatomic, GCSswpallgc, GCSswpfinobj,
  820. GCSswptobefnz, GCSswpend, GCScallfin, GCSpause, -1};
  821. int option = states[luaL_checkoption(L, 1, "", statenames)];
  822. global_State *g = G(L);
  823. if (option == -1) {
  824. lua_pushstring(L, statenames[g->gcstate]);
  825. return 1;
  826. }
  827. else {
  828. if (g->gckind != KGC_INC)
  829. luaL_error(L, "cannot change states in generational mode");
  830. lua_lock(L);
  831. if (option < g->gcstate) { /* must cross 'pause'? */
  832. luaC_runtilstate(L, GCSpause, 1); /* run until pause */
  833. }
  834. luaC_runtilstate(L, option, 0); /* do not skip propagation state */
  835. lua_assert(g->gcstate == option);
  836. lua_unlock(L);
  837. return 0;
  838. }
  839. }
  840. static int tracinggc = 0;
  841. void luai_tracegctest (lua_State *L, int first) {
  842. if (!tracinggc) return;
  843. else {
  844. global_State *g = G(L);
  845. lua_unlock(L);
  846. g->gcstp = GCSTPGC;
  847. lua_checkstack(L, 10);
  848. lua_getfield(L, LUA_REGISTRYINDEX, "tracegc");
  849. lua_pushboolean(L, first);
  850. lua_call(L, 1, 0);
  851. g->gcstp = 0;
  852. lua_lock(L);
  853. }
  854. }
  855. static int tracegc (lua_State *L) {
  856. if (lua_isnil(L, 1))
  857. tracinggc = 0;
  858. else {
  859. tracinggc = 1;
  860. lua_setfield(L, LUA_REGISTRYINDEX, "tracegc");
  861. }
  862. return 0;
  863. }
  864. static int hash_query (lua_State *L) {
  865. if (lua_isnone(L, 2)) {
  866. luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
  867. lua_pushinteger(L, cast_int(tsvalue(obj_at(L, 1))->hash));
  868. }
  869. else {
  870. TValue *o = obj_at(L, 1);
  871. Table *t;
  872. luaL_checktype(L, 2, LUA_TTABLE);
  873. t = hvalue(obj_at(L, 2));
  874. lua_pushinteger(L, cast(lua_Integer, luaH_mainposition(t, o) - t->node));
  875. }
  876. return 1;
  877. }
  878. static int stacklevel (lua_State *L) {
  879. int a = 0;
  880. lua_pushinteger(L, cast(lua_Integer, L->top.p - L->stack.p));
  881. lua_pushinteger(L, stacksize(L));
  882. lua_pushinteger(L, cast(lua_Integer, L->nCcalls));
  883. lua_pushinteger(L, L->nci);
  884. lua_pushinteger(L, (lua_Integer)(size_t)&a);
  885. return 5;
  886. }
  887. static int table_query (lua_State *L) {
  888. const Table *t;
  889. int i = cast_int(luaL_optinteger(L, 2, -1));
  890. unsigned int asize;
  891. luaL_checktype(L, 1, LUA_TTABLE);
  892. t = hvalue(obj_at(L, 1));
  893. asize = luaH_realasize(t);
  894. if (i == -1) {
  895. lua_pushinteger(L, cast(lua_Integer, asize));
  896. lua_pushinteger(L, cast(lua_Integer, allocsizenode(t)));
  897. lua_pushinteger(L, cast(lua_Integer, t->alimit));
  898. return 3;
  899. }
  900. else if (cast_uint(i) < asize) {
  901. lua_pushinteger(L, i);
  902. arr2obj(t, cast_uint(i), s2v(L->top.p));
  903. api_incr_top(L);
  904. lua_pushnil(L);
  905. }
  906. else if (cast_uint(i -= cast_int(asize)) < sizenode(t)) {
  907. TValue k;
  908. getnodekey(L, &k, gnode(t, i));
  909. if (!isempty(gval(gnode(t, i))) ||
  910. ttisnil(&k) ||
  911. ttisnumber(&k)) {
  912. pushobject(L, &k);
  913. }
  914. else
  915. lua_pushliteral(L, "<undef>");
  916. pushobject(L, gval(gnode(t, i)));
  917. if (gnext(&t->node[i]) != 0)
  918. lua_pushinteger(L, gnext(&t->node[i]));
  919. else
  920. lua_pushnil(L);
  921. }
  922. return 3;
  923. }
  924. static int gc_query (lua_State *L) {
  925. global_State *g = G(L);
  926. lua_pushstring(L, g->gckind == KGC_INC ? "inc"
  927. : g->gckind == KGC_GENMAJOR ? "genmajor"
  928. : "genminor");
  929. lua_pushstring(L, statenames[g->gcstate]);
  930. lua_pushinteger(L, cast_st2S(gettotalbytes(g)));
  931. lua_pushinteger(L, cast_st2S(g->GCdebt));
  932. lua_pushinteger(L, cast_st2S(g->GCmarked));
  933. lua_pushinteger(L, cast_st2S(g->GCmajorminor));
  934. return 6;
  935. }
  936. static int test_codeparam (lua_State *L) {
  937. lua_Integer p = luaL_checkinteger(L, 1);
  938. lua_pushinteger(L, luaO_codeparam(cast_uint(p)));
  939. return 1;
  940. }
  941. static int test_applyparam (lua_State *L) {
  942. lua_Integer p = luaL_checkinteger(L, 1);
  943. lua_Integer x = luaL_checkinteger(L, 2);
  944. lua_pushinteger(L, cast(lua_Integer, luaO_applyparam(cast_byte(p), x)));
  945. return 1;
  946. }
  947. static int string_query (lua_State *L) {
  948. stringtable *tb = &G(L)->strt;
  949. int s = cast_int(luaL_optinteger(L, 1, 0)) - 1;
  950. if (s == -1) {
  951. lua_pushinteger(L ,tb->size);
  952. lua_pushinteger(L ,tb->nuse);
  953. return 2;
  954. }
  955. else if (s < tb->size) {
  956. TString *ts;
  957. int n = 0;
  958. for (ts = tb->hash[s]; ts != NULL; ts = ts->u.hnext) {
  959. setsvalue2s(L, L->top.p, ts);
  960. api_incr_top(L);
  961. n++;
  962. }
  963. return n;
  964. }
  965. else return 0;
  966. }
  967. static int getreftable (lua_State *L) {
  968. if (lua_istable(L, 2)) /* is there a table as second argument? */
  969. return 2; /* use it as the table */
  970. else
  971. return LUA_REGISTRYINDEX; /* default is to use the register */
  972. }
  973. static int tref (lua_State *L) {
  974. int t = getreftable(L);
  975. int level = lua_gettop(L);
  976. luaL_checkany(L, 1);
  977. lua_pushvalue(L, 1);
  978. lua_pushinteger(L, luaL_ref(L, t));
  979. cast_void(level); /* to avoid warnings */
  980. lua_assert(lua_gettop(L) == level+1); /* +1 for result */
  981. return 1;
  982. }
  983. static int getref (lua_State *L) {
  984. int t = getreftable(L);
  985. int level = lua_gettop(L);
  986. lua_rawgeti(L, t, luaL_checkinteger(L, 1));
  987. cast_void(level); /* to avoid warnings */
  988. lua_assert(lua_gettop(L) == level+1);
  989. return 1;
  990. }
  991. static int unref (lua_State *L) {
  992. int t = getreftable(L);
  993. int level = lua_gettop(L);
  994. luaL_unref(L, t, cast_int(luaL_checkinteger(L, 1)));
  995. cast_void(level); /* to avoid warnings */
  996. lua_assert(lua_gettop(L) == level);
  997. return 0;
  998. }
  999. static int upvalue (lua_State *L) {
  1000. int n = cast_int(luaL_checkinteger(L, 2));
  1001. luaL_checktype(L, 1, LUA_TFUNCTION);
  1002. if (lua_isnone(L, 3)) {
  1003. const char *name = lua_getupvalue(L, 1, n);
  1004. if (name == NULL) return 0;
  1005. lua_pushstring(L, name);
  1006. return 2;
  1007. }
  1008. else {
  1009. const char *name = lua_setupvalue(L, 1, n);
  1010. lua_pushstring(L, name);
  1011. return 1;
  1012. }
  1013. }
  1014. static int newuserdata (lua_State *L) {
  1015. size_t size = cast_sizet(luaL_optinteger(L, 1, 0));
  1016. int nuv = cast_int(luaL_optinteger(L, 2, 0));
  1017. char *p = cast_charp(lua_newuserdatauv(L, size, nuv));
  1018. while (size--) *p++ = '\0';
  1019. return 1;
  1020. }
  1021. static int pushuserdata (lua_State *L) {
  1022. lua_Integer u = luaL_checkinteger(L, 1);
  1023. lua_pushlightuserdata(L, cast_voidp(cast_sizet(u)));
  1024. return 1;
  1025. }
  1026. static int udataval (lua_State *L) {
  1027. lua_pushinteger(L, cast(lua_Integer, cast(size_t, lua_touserdata(L, 1))));
  1028. return 1;
  1029. }
  1030. static int doonnewstack (lua_State *L) {
  1031. lua_State *L1 = lua_newthread(L);
  1032. size_t l;
  1033. const char *s = luaL_checklstring(L, 1, &l);
  1034. int status = luaL_loadbuffer(L1, s, l, s);
  1035. if (status == LUA_OK)
  1036. status = lua_pcall(L1, 0, 0, 0);
  1037. lua_pushinteger(L, status);
  1038. return 1;
  1039. }
  1040. static int s2d (lua_State *L) {
  1041. lua_pushnumber(L, cast_num(*cast(const double *, luaL_checkstring(L, 1))));
  1042. return 1;
  1043. }
  1044. static int d2s (lua_State *L) {
  1045. double d = cast(double, luaL_checknumber(L, 1));
  1046. lua_pushlstring(L, cast_charp(&d), sizeof(d));
  1047. return 1;
  1048. }
  1049. static int num2int (lua_State *L) {
  1050. lua_pushinteger(L, lua_tointeger(L, 1));
  1051. return 1;
  1052. }
  1053. static int makeseed (lua_State *L) {
  1054. lua_pushinteger(L, cast(lua_Integer, luaL_makeseed(L)));
  1055. return 1;
  1056. }
  1057. static int newstate (lua_State *L) {
  1058. void *ud;
  1059. lua_Alloc f = lua_getallocf(L, &ud);
  1060. lua_State *L1 = lua_newstate(f, ud, 0);
  1061. if (L1) {
  1062. lua_atpanic(L1, tpanic);
  1063. lua_pushlightuserdata(L, L1);
  1064. }
  1065. else
  1066. lua_pushnil(L);
  1067. return 1;
  1068. }
  1069. static lua_State *getstate (lua_State *L) {
  1070. lua_State *L1 = cast(lua_State *, lua_touserdata(L, 1));
  1071. luaL_argcheck(L, L1 != NULL, 1, "state expected");
  1072. return L1;
  1073. }
  1074. static int loadlib (lua_State *L) {
  1075. lua_State *L1 = getstate(L);
  1076. int load = cast_int(luaL_checkinteger(L, 2));
  1077. int preload = cast_int(luaL_checkinteger(L, 3));
  1078. luaL_openselectedlibs(L1, load, preload);
  1079. luaL_requiref(L1, "T", luaB_opentests, 0);
  1080. lua_assert(lua_type(L1, -1) == LUA_TTABLE);
  1081. /* 'requiref' should not reload module already loaded... */
  1082. luaL_requiref(L1, "T", NULL, 1); /* seg. fault if it reloads */
  1083. /* ...but should return the same module */
  1084. lua_assert(lua_compare(L1, -1, -2, LUA_OPEQ));
  1085. return 0;
  1086. }
  1087. static int closestate (lua_State *L) {
  1088. lua_State *L1 = getstate(L);
  1089. lua_close(L1);
  1090. return 0;
  1091. }
  1092. static int doremote (lua_State *L) {
  1093. lua_State *L1 = getstate(L);
  1094. size_t lcode;
  1095. const char *code = luaL_checklstring(L, 2, &lcode);
  1096. int status;
  1097. lua_settop(L1, 0);
  1098. status = luaL_loadbuffer(L1, code, lcode, code);
  1099. if (status == LUA_OK)
  1100. status = lua_pcall(L1, 0, LUA_MULTRET, 0);
  1101. if (status != LUA_OK) {
  1102. lua_pushnil(L);
  1103. lua_pushstring(L, lua_tostring(L1, -1));
  1104. lua_pushinteger(L, status);
  1105. return 3;
  1106. }
  1107. else {
  1108. int i = 0;
  1109. while (!lua_isnone(L1, ++i))
  1110. lua_pushstring(L, lua_tostring(L1, i));
  1111. lua_pop(L1, i-1);
  1112. return i-1;
  1113. }
  1114. }
  1115. static int log2_aux (lua_State *L) {
  1116. unsigned int x = (unsigned int)luaL_checkinteger(L, 1);
  1117. lua_pushinteger(L, luaO_ceillog2(x));
  1118. return 1;
  1119. }
  1120. struct Aux { jmp_buf jb; const char *paniccode; lua_State *L; };
  1121. /*
  1122. ** does a long-jump back to "main program".
  1123. */
  1124. static int panicback (lua_State *L) {
  1125. struct Aux *b;
  1126. lua_checkstack(L, 1); /* open space for 'Aux' struct */
  1127. lua_getfield(L, LUA_REGISTRYINDEX, "_jmpbuf"); /* get 'Aux' struct */
  1128. b = (struct Aux *)lua_touserdata(L, -1);
  1129. lua_pop(L, 1); /* remove 'Aux' struct */
  1130. runC(b->L, L, b->paniccode); /* run optional panic code */
  1131. longjmp(b->jb, 1);
  1132. return 1; /* to avoid warnings */
  1133. }
  1134. static int checkpanic (lua_State *L) {
  1135. struct Aux b;
  1136. void *ud;
  1137. lua_State *L1;
  1138. const char *code = luaL_checkstring(L, 1);
  1139. lua_Alloc f = lua_getallocf(L, &ud);
  1140. b.paniccode = luaL_optstring(L, 2, "");
  1141. b.L = L;
  1142. L1 = lua_newstate(f, ud, 0); /* create new state */
  1143. if (L1 == NULL) { /* error? */
  1144. lua_pushnil(L);
  1145. return 1;
  1146. }
  1147. lua_atpanic(L1, panicback); /* set its panic function */
  1148. lua_pushlightuserdata(L1, &b);
  1149. lua_setfield(L1, LUA_REGISTRYINDEX, "_jmpbuf"); /* store 'Aux' struct */
  1150. if (setjmp(b.jb) == 0) { /* set jump buffer */
  1151. runC(L, L1, code); /* run code unprotected */
  1152. lua_pushliteral(L, "no errors");
  1153. }
  1154. else { /* error handling */
  1155. /* move error message to original state */
  1156. lua_pushstring(L, lua_tostring(L1, -1));
  1157. }
  1158. lua_close(L1);
  1159. return 1;
  1160. }
  1161. static int externKstr (lua_State *L) {
  1162. size_t len;
  1163. const char *s = luaL_checklstring(L, 1, &len);
  1164. lua_pushextlstring(L, s, len, NULL, NULL);
  1165. return 1;
  1166. }
  1167. /*
  1168. ** Create a buffer with the content of a given string and then
  1169. ** create an external string using that buffer. Use the allocation
  1170. ** function from Lua to create and free the buffer.
  1171. */
  1172. static int externstr (lua_State *L) {
  1173. size_t len;
  1174. const char *s = luaL_checklstring(L, 1, &len);
  1175. void *ud;
  1176. lua_Alloc allocf = lua_getallocf(L, &ud); /* get allocation function */
  1177. /* create the buffer */
  1178. char *buff = cast_charp((*allocf)(ud, NULL, 0, len + 1));
  1179. if (buff == NULL) { /* memory error? */
  1180. lua_pushliteral(L, "not enough memory");
  1181. lua_error(L); /* raise a memory error */
  1182. }
  1183. /* copy string content to buffer, including ending 0 */
  1184. memcpy(buff, s, (len + 1) * sizeof(char));
  1185. /* create external string */
  1186. lua_pushextlstring(L, buff, len, allocf, ud);
  1187. return 1;
  1188. }
  1189. /*
  1190. ** {====================================================================
  1191. ** function to test the API with C. It interprets a kind of assembler
  1192. ** language with calls to the API, so the test can be driven by Lua code
  1193. ** =====================================================================
  1194. */
  1195. static void sethookaux (lua_State *L, int mask, int count, const char *code);
  1196. static const char *const delimits = " \t\n,;";
  1197. static void skip (const char **pc) {
  1198. for (;;) {
  1199. if (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
  1200. else if (**pc == '#') { /* comment? */
  1201. while (**pc != '\n' && **pc != '\0') (*pc)++; /* until end-of-line */
  1202. }
  1203. else break;
  1204. }
  1205. }
  1206. static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) {
  1207. int res = 0;
  1208. int sig = 1;
  1209. skip(pc);
  1210. if (**pc == '.') {
  1211. res = cast_int(lua_tointeger(L1, -1));
  1212. lua_pop(L1, 1);
  1213. (*pc)++;
  1214. return res;
  1215. }
  1216. else if (**pc == '*') {
  1217. res = lua_gettop(L1);
  1218. (*pc)++;
  1219. return res;
  1220. }
  1221. else if (**pc == '!') {
  1222. (*pc)++;
  1223. if (**pc == 'G')
  1224. res = LUA_RIDX_GLOBALS;
  1225. else if (**pc == 'M')
  1226. res = LUA_RIDX_MAINTHREAD;
  1227. else lua_assert(0);
  1228. (*pc)++;
  1229. return res;
  1230. }
  1231. else if (**pc == '-') {
  1232. sig = -1;
  1233. (*pc)++;
  1234. }
  1235. if (!lisdigit(cast_uchar(**pc)))
  1236. luaL_error(L, "number expected (%s)", *pc);
  1237. while (lisdigit(cast_uchar(**pc))) res = res*10 + (*(*pc)++) - '0';
  1238. return sig*res;
  1239. }
  1240. static const char *getstring_aux (lua_State *L, char *buff, const char **pc) {
  1241. int i = 0;
  1242. skip(pc);
  1243. if (**pc == '"' || **pc == '\'') { /* quoted string? */
  1244. int quote = *(*pc)++;
  1245. while (**pc != quote) {
  1246. if (**pc == '\0') luaL_error(L, "unfinished string in C script");
  1247. buff[i++] = *(*pc)++;
  1248. }
  1249. (*pc)++;
  1250. }
  1251. else {
  1252. while (**pc != '\0' && !strchr(delimits, **pc))
  1253. buff[i++] = *(*pc)++;
  1254. }
  1255. buff[i] = '\0';
  1256. return buff;
  1257. }
  1258. static int getindex_aux (lua_State *L, lua_State *L1, const char **pc) {
  1259. skip(pc);
  1260. switch (*(*pc)++) {
  1261. case 'R': return LUA_REGISTRYINDEX;
  1262. case 'G': return luaL_error(L, "deprecated index 'G'");
  1263. case 'U': return lua_upvalueindex(getnum_aux(L, L1, pc));
  1264. default: (*pc)--; return getnum_aux(L, L1, pc);
  1265. }
  1266. }
  1267. static const char *const statcodes[] = {"OK", "YIELD", "ERRRUN",
  1268. "ERRSYNTAX", MEMERRMSG, "ERRGCMM", "ERRERR"};
  1269. /*
  1270. ** Avoid these stat codes from being collected, to avoid possible
  1271. ** memory error when pushing them.
  1272. */
  1273. static void regcodes (lua_State *L) {
  1274. unsigned int i;
  1275. for (i = 0; i < sizeof(statcodes) / sizeof(statcodes[0]); i++) {
  1276. lua_pushboolean(L, 1);
  1277. lua_setfield(L, LUA_REGISTRYINDEX, statcodes[i]);
  1278. }
  1279. }
  1280. #define EQ(s1) (strcmp(s1, inst) == 0)
  1281. #define getnum (getnum_aux(L, L1, &pc))
  1282. #define getstring (getstring_aux(L, buff, &pc))
  1283. #define getindex (getindex_aux(L, L1, &pc))
  1284. static int testC (lua_State *L);
  1285. static int Cfunck (lua_State *L, int status, lua_KContext ctx);
  1286. /*
  1287. ** arithmetic operation encoding for 'arith' instruction
  1288. ** LUA_OPIDIV -> \
  1289. ** LUA_OPSHL -> <
  1290. ** LUA_OPSHR -> >
  1291. ** LUA_OPUNM -> _
  1292. ** LUA_OPBNOT -> !
  1293. */
  1294. static const char ops[] = "+-*%^/\\&|~<>_!";
  1295. static int runC (lua_State *L, lua_State *L1, const char *pc) {
  1296. char buff[300];
  1297. int status = 0;
  1298. if (pc == NULL) return luaL_error(L, "attempt to runC null script");
  1299. for (;;) {
  1300. const char *inst = getstring;
  1301. if EQ("") return 0;
  1302. else if EQ("absindex") {
  1303. lua_pushinteger(L1, lua_absindex(L1, getindex));
  1304. }
  1305. else if EQ("append") {
  1306. int t = getindex;
  1307. int i = cast_int(lua_rawlen(L1, t));
  1308. lua_rawseti(L1, t, i + 1);
  1309. }
  1310. else if EQ("arith") {
  1311. int op;
  1312. skip(&pc);
  1313. op = cast_int(strchr(ops, *pc++) - ops);
  1314. lua_arith(L1, op);
  1315. }
  1316. else if EQ("call") {
  1317. int narg = getnum;
  1318. int nres = getnum;
  1319. lua_call(L1, narg, nres);
  1320. }
  1321. else if EQ("callk") {
  1322. int narg = getnum;
  1323. int nres = getnum;
  1324. int i = getindex;
  1325. lua_callk(L1, narg, nres, i, Cfunck);
  1326. }
  1327. else if EQ("checkstack") {
  1328. int sz = getnum;
  1329. const char *msg = getstring;
  1330. if (*msg == '\0')
  1331. msg = NULL; /* to test 'luaL_checkstack' with no message */
  1332. luaL_checkstack(L1, sz, msg);
  1333. }
  1334. else if EQ("rawcheckstack") {
  1335. int sz = getnum;
  1336. lua_pushboolean(L1, lua_checkstack(L1, sz));
  1337. }
  1338. else if EQ("compare") {
  1339. const char *opt = getstring; /* EQ, LT, or LE */
  1340. int op = (opt[0] == 'E') ? LUA_OPEQ
  1341. : (opt[1] == 'T') ? LUA_OPLT : LUA_OPLE;
  1342. int a = getindex;
  1343. int b = getindex;
  1344. lua_pushboolean(L1, lua_compare(L1, a, b, op));
  1345. }
  1346. else if EQ("concat") {
  1347. lua_concat(L1, getnum);
  1348. }
  1349. else if EQ("copy") {
  1350. int f = getindex;
  1351. lua_copy(L1, f, getindex);
  1352. }
  1353. else if EQ("func2num") {
  1354. lua_CFunction func = lua_tocfunction(L1, getindex);
  1355. lua_pushinteger(L1, cast(lua_Integer, cast(size_t, func)));
  1356. }
  1357. else if EQ("getfield") {
  1358. int t = getindex;
  1359. int tp = lua_getfield(L1, t, getstring);
  1360. lua_assert(tp == lua_type(L1, -1));
  1361. }
  1362. else if EQ("getglobal") {
  1363. lua_getglobal(L1, getstring);
  1364. }
  1365. else if EQ("getmetatable") {
  1366. if (lua_getmetatable(L1, getindex) == 0)
  1367. lua_pushnil(L1);
  1368. }
  1369. else if EQ("gettable") {
  1370. int tp = lua_gettable(L1, getindex);
  1371. lua_assert(tp == lua_type(L1, -1));
  1372. }
  1373. else if EQ("gettop") {
  1374. lua_pushinteger(L1, lua_gettop(L1));
  1375. }
  1376. else if EQ("gsub") {
  1377. int a = getnum; int b = getnum; int c = getnum;
  1378. luaL_gsub(L1, lua_tostring(L1, a),
  1379. lua_tostring(L1, b),
  1380. lua_tostring(L1, c));
  1381. }
  1382. else if EQ("insert") {
  1383. lua_insert(L1, getnum);
  1384. }
  1385. else if EQ("iscfunction") {
  1386. lua_pushboolean(L1, lua_iscfunction(L1, getindex));
  1387. }
  1388. else if EQ("isfunction") {
  1389. lua_pushboolean(L1, lua_isfunction(L1, getindex));
  1390. }
  1391. else if EQ("isnil") {
  1392. lua_pushboolean(L1, lua_isnil(L1, getindex));
  1393. }
  1394. else if EQ("isnull") {
  1395. lua_pushboolean(L1, lua_isnone(L1, getindex));
  1396. }
  1397. else if EQ("isnumber") {
  1398. lua_pushboolean(L1, lua_isnumber(L1, getindex));
  1399. }
  1400. else if EQ("isstring") {
  1401. lua_pushboolean(L1, lua_isstring(L1, getindex));
  1402. }
  1403. else if EQ("istable") {
  1404. lua_pushboolean(L1, lua_istable(L1, getindex));
  1405. }
  1406. else if EQ("isudataval") {
  1407. lua_pushboolean(L1, lua_islightuserdata(L1, getindex));
  1408. }
  1409. else if EQ("isuserdata") {
  1410. lua_pushboolean(L1, lua_isuserdata(L1, getindex));
  1411. }
  1412. else if EQ("len") {
  1413. lua_len(L1, getindex);
  1414. }
  1415. else if EQ("Llen") {
  1416. lua_pushinteger(L1, luaL_len(L1, getindex));
  1417. }
  1418. else if EQ("loadfile") {
  1419. luaL_loadfile(L1, luaL_checkstring(L1, getnum));
  1420. }
  1421. else if EQ("loadstring") {
  1422. size_t slen;
  1423. const char *s = luaL_checklstring(L1, getnum, &slen);
  1424. const char *name = getstring;
  1425. const char *mode = getstring;
  1426. luaL_loadbufferx(L1, s, slen, name, mode);
  1427. }
  1428. else if EQ("newmetatable") {
  1429. lua_pushboolean(L1, luaL_newmetatable(L1, getstring));
  1430. }
  1431. else if EQ("newtable") {
  1432. lua_newtable(L1);
  1433. }
  1434. else if EQ("newthread") {
  1435. lua_newthread(L1);
  1436. }
  1437. else if EQ("resetthread") {
  1438. lua_pushinteger(L1, lua_resetthread(L1)); /* deprecated */
  1439. }
  1440. else if EQ("newuserdata") {
  1441. lua_newuserdata(L1, cast_sizet(getnum));
  1442. }
  1443. else if EQ("next") {
  1444. lua_next(L1, -2);
  1445. }
  1446. else if EQ("objsize") {
  1447. lua_pushinteger(L1, l_castU2S(lua_rawlen(L1, getindex)));
  1448. }
  1449. else if EQ("pcall") {
  1450. int narg = getnum;
  1451. int nres = getnum;
  1452. status = lua_pcall(L1, narg, nres, getnum);
  1453. }
  1454. else if EQ("pcallk") {
  1455. int narg = getnum;
  1456. int nres = getnum;
  1457. int i = getindex;
  1458. status = lua_pcallk(L1, narg, nres, 0, i, Cfunck);
  1459. }
  1460. else if EQ("pop") {
  1461. lua_pop(L1, getnum);
  1462. }
  1463. else if EQ("printstack") {
  1464. int n = getnum;
  1465. if (n != 0) {
  1466. printf("%s\n", luaL_tolstring(L1, n, NULL));
  1467. lua_pop(L1, 1);
  1468. }
  1469. else lua_printstack(L1);
  1470. }
  1471. else if EQ("print") {
  1472. const char *msg = getstring;
  1473. printf("%s\n", msg);
  1474. }
  1475. else if EQ("warningC") {
  1476. const char *msg = getstring;
  1477. lua_warning(L1, msg, 1);
  1478. }
  1479. else if EQ("warning") {
  1480. const char *msg = getstring;
  1481. lua_warning(L1, msg, 0);
  1482. }
  1483. else if EQ("pushbool") {
  1484. lua_pushboolean(L1, getnum);
  1485. }
  1486. else if EQ("pushcclosure") {
  1487. lua_pushcclosure(L1, testC, getnum);
  1488. }
  1489. else if EQ("pushint") {
  1490. lua_pushinteger(L1, getnum);
  1491. }
  1492. else if EQ("pushnil") {
  1493. lua_pushnil(L1);
  1494. }
  1495. else if EQ("pushnum") {
  1496. lua_pushnumber(L1, (lua_Number)getnum);
  1497. }
  1498. else if EQ("pushstatus") {
  1499. lua_pushstring(L1, statcodes[status]);
  1500. }
  1501. else if EQ("pushstring") {
  1502. lua_pushstring(L1, getstring);
  1503. }
  1504. else if EQ("pushupvalueindex") {
  1505. lua_pushinteger(L1, lua_upvalueindex(getnum));
  1506. }
  1507. else if EQ("pushvalue") {
  1508. lua_pushvalue(L1, getindex);
  1509. }
  1510. else if EQ("pushfstringI") {
  1511. lua_pushfstring(L1, lua_tostring(L, -2), (int)lua_tointeger(L, -1));
  1512. }
  1513. else if EQ("pushfstringS") {
  1514. lua_pushfstring(L1, lua_tostring(L, -2), lua_tostring(L, -1));
  1515. }
  1516. else if EQ("pushfstringP") {
  1517. lua_pushfstring(L1, lua_tostring(L, -2), lua_topointer(L, -1));
  1518. }
  1519. else if EQ("rawget") {
  1520. int t = getindex;
  1521. lua_rawget(L1, t);
  1522. }
  1523. else if EQ("rawgeti") {
  1524. int t = getindex;
  1525. lua_rawgeti(L1, t, getnum);
  1526. }
  1527. else if EQ("rawgetp") {
  1528. int t = getindex;
  1529. lua_rawgetp(L1, t, cast_voidp(cast_sizet(getnum)));
  1530. }
  1531. else if EQ("rawset") {
  1532. int t = getindex;
  1533. lua_rawset(L1, t);
  1534. }
  1535. else if EQ("rawseti") {
  1536. int t = getindex;
  1537. lua_rawseti(L1, t, getnum);
  1538. }
  1539. else if EQ("rawsetp") {
  1540. int t = getindex;
  1541. lua_rawsetp(L1, t, cast_voidp(cast_sizet(getnum)));
  1542. }
  1543. else if EQ("remove") {
  1544. lua_remove(L1, getnum);
  1545. }
  1546. else if EQ("replace") {
  1547. lua_replace(L1, getindex);
  1548. }
  1549. else if EQ("resume") {
  1550. int i = getindex;
  1551. int nres;
  1552. status = lua_resume(lua_tothread(L1, i), L, getnum, &nres);
  1553. }
  1554. else if EQ("traceback") {
  1555. const char *msg = getstring;
  1556. int level = getnum;
  1557. luaL_traceback(L1, L1, msg, level);
  1558. }
  1559. else if EQ("return") {
  1560. int n = getnum;
  1561. if (L1 != L) {
  1562. int i;
  1563. for (i = 0; i < n; i++) {
  1564. int idx = -(n - i);
  1565. switch (lua_type(L1, idx)) {
  1566. case LUA_TBOOLEAN:
  1567. lua_pushboolean(L, lua_toboolean(L1, idx));
  1568. break;
  1569. default:
  1570. lua_pushstring(L, lua_tostring(L1, idx));
  1571. break;
  1572. }
  1573. }
  1574. }
  1575. return n;
  1576. }
  1577. else if EQ("rotate") {
  1578. int i = getindex;
  1579. lua_rotate(L1, i, getnum);
  1580. }
  1581. else if EQ("setfield") {
  1582. int t = getindex;
  1583. const char *s = getstring;
  1584. lua_setfield(L1, t, s);
  1585. }
  1586. else if EQ("seti") {
  1587. int t = getindex;
  1588. lua_seti(L1, t, getnum);
  1589. }
  1590. else if EQ("setglobal") {
  1591. const char *s = getstring;
  1592. lua_setglobal(L1, s);
  1593. }
  1594. else if EQ("sethook") {
  1595. int mask = getnum;
  1596. int count = getnum;
  1597. const char *s = getstring;
  1598. sethookaux(L1, mask, count, s);
  1599. }
  1600. else if EQ("setmetatable") {
  1601. int idx = getindex;
  1602. lua_setmetatable(L1, idx);
  1603. }
  1604. else if EQ("settable") {
  1605. lua_settable(L1, getindex);
  1606. }
  1607. else if EQ("settop") {
  1608. lua_settop(L1, getnum);
  1609. }
  1610. else if EQ("testudata") {
  1611. int i = getindex;
  1612. lua_pushboolean(L1, luaL_testudata(L1, i, getstring) != NULL);
  1613. }
  1614. else if EQ("error") {
  1615. lua_error(L1);
  1616. }
  1617. else if EQ("abort") {
  1618. abort();
  1619. }
  1620. else if EQ("throw") {
  1621. #if defined(__cplusplus)
  1622. static struct X { int x; } x;
  1623. throw x;
  1624. #else
  1625. luaL_error(L1, "C++");
  1626. #endif
  1627. break;
  1628. }
  1629. else if EQ("tobool") {
  1630. lua_pushboolean(L1, lua_toboolean(L1, getindex));
  1631. }
  1632. else if EQ("tocfunction") {
  1633. lua_pushcfunction(L1, lua_tocfunction(L1, getindex));
  1634. }
  1635. else if EQ("tointeger") {
  1636. lua_pushinteger(L1, lua_tointeger(L1, getindex));
  1637. }
  1638. else if EQ("tonumber") {
  1639. lua_pushnumber(L1, lua_tonumber(L1, getindex));
  1640. }
  1641. else if EQ("topointer") {
  1642. lua_pushlightuserdata(L1, cast_voidp(lua_topointer(L1, getindex)));
  1643. }
  1644. else if EQ("touserdata") {
  1645. lua_pushlightuserdata(L1, lua_touserdata(L1, getindex));
  1646. }
  1647. else if EQ("tostring") {
  1648. const char *s = lua_tostring(L1, getindex);
  1649. const char *s1 = lua_pushstring(L1, s);
  1650. cast_void(s1); /* to avoid warnings */
  1651. lua_longassert((s == NULL && s1 == NULL) || strcmp(s, s1) == 0);
  1652. }
  1653. else if EQ("Ltolstring") {
  1654. luaL_tolstring(L1, getindex, NULL);
  1655. }
  1656. else if EQ("type") {
  1657. lua_pushstring(L1, luaL_typename(L1, getnum));
  1658. }
  1659. else if EQ("xmove") {
  1660. int f = getindex;
  1661. int t = getindex;
  1662. lua_State *fs = (f == 0) ? L1 : lua_tothread(L1, f);
  1663. lua_State *ts = (t == 0) ? L1 : lua_tothread(L1, t);
  1664. int n = getnum;
  1665. if (n == 0) n = lua_gettop(fs);
  1666. lua_xmove(fs, ts, n);
  1667. }
  1668. else if EQ("isyieldable") {
  1669. lua_pushboolean(L1, lua_isyieldable(lua_tothread(L1, getindex)));
  1670. }
  1671. else if EQ("yield") {
  1672. return lua_yield(L1, getnum);
  1673. }
  1674. else if EQ("yieldk") {
  1675. int nres = getnum;
  1676. int i = getindex;
  1677. return lua_yieldk(L1, nres, i, Cfunck);
  1678. }
  1679. else if EQ("toclose") {
  1680. lua_toclose(L1, getnum);
  1681. }
  1682. else if EQ("closeslot") {
  1683. lua_closeslot(L1, getnum);
  1684. }
  1685. else luaL_error(L, "unknown instruction %s", buff);
  1686. }
  1687. return 0;
  1688. }
  1689. static int testC (lua_State *L) {
  1690. lua_State *L1;
  1691. const char *pc;
  1692. if (lua_isuserdata(L, 1)) {
  1693. L1 = getstate(L);
  1694. pc = luaL_checkstring(L, 2);
  1695. }
  1696. else if (lua_isthread(L, 1)) {
  1697. L1 = lua_tothread(L, 1);
  1698. pc = luaL_checkstring(L, 2);
  1699. }
  1700. else {
  1701. L1 = L;
  1702. pc = luaL_checkstring(L, 1);
  1703. }
  1704. return runC(L, L1, pc);
  1705. }
  1706. static int Cfunc (lua_State *L) {
  1707. return runC(L, L, lua_tostring(L, lua_upvalueindex(1)));
  1708. }
  1709. static int Cfunck (lua_State *L, int status, lua_KContext ctx) {
  1710. lua_pushstring(L, statcodes[status]);
  1711. lua_setglobal(L, "status");
  1712. lua_pushinteger(L, cast(lua_Integer, ctx));
  1713. lua_setglobal(L, "ctx");
  1714. return runC(L, L, lua_tostring(L, cast_int(ctx)));
  1715. }
  1716. static int makeCfunc (lua_State *L) {
  1717. luaL_checkstring(L, 1);
  1718. lua_pushcclosure(L, Cfunc, lua_gettop(L));
  1719. return 1;
  1720. }
  1721. /* }====================================================== */
  1722. /*
  1723. ** {======================================================
  1724. ** tests for C hooks
  1725. ** =======================================================
  1726. */
  1727. /*
  1728. ** C hook that runs the C script stored in registry.C_HOOK[L]
  1729. */
  1730. static void Chook (lua_State *L, lua_Debug *ar) {
  1731. const char *scpt;
  1732. const char *const events [] = {"call", "ret", "line", "count", "tailcall"};
  1733. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK");
  1734. lua_pushlightuserdata(L, L);
  1735. lua_gettable(L, -2); /* get C_HOOK[L] (script saved by sethookaux) */
  1736. scpt = lua_tostring(L, -1); /* not very religious (string will be popped) */
  1737. lua_pop(L, 2); /* remove C_HOOK and script */
  1738. lua_pushstring(L, events[ar->event]); /* may be used by script */
  1739. lua_pushinteger(L, ar->currentline); /* may be used by script */
  1740. runC(L, L, scpt); /* run script from C_HOOK[L] */
  1741. }
  1742. /*
  1743. ** sets 'registry.C_HOOK[L] = scpt' and sets 'Chook' as a hook
  1744. */
  1745. static void sethookaux (lua_State *L, int mask, int count, const char *scpt) {
  1746. if (*scpt == '\0') { /* no script? */
  1747. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1748. return;
  1749. }
  1750. lua_getfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* get C_HOOK table */
  1751. if (!lua_istable(L, -1)) { /* no hook table? */
  1752. lua_pop(L, 1); /* remove previous value */
  1753. lua_newtable(L); /* create new C_HOOK table */
  1754. lua_pushvalue(L, -1);
  1755. lua_setfield(L, LUA_REGISTRYINDEX, "C_HOOK"); /* register it */
  1756. }
  1757. lua_pushlightuserdata(L, L);
  1758. lua_pushstring(L, scpt);
  1759. lua_settable(L, -3); /* C_HOOK[L] = script */
  1760. lua_sethook(L, Chook, mask, count);
  1761. }
  1762. static int sethook (lua_State *L) {
  1763. if (lua_isnoneornil(L, 1))
  1764. lua_sethook(L, NULL, 0, 0); /* turn off hooks */
  1765. else {
  1766. const char *scpt = luaL_checkstring(L, 1);
  1767. const char *smask = luaL_checkstring(L, 2);
  1768. int count = cast_int(luaL_optinteger(L, 3, 0));
  1769. int mask = 0;
  1770. if (strchr(smask, 'c')) mask |= LUA_MASKCALL;
  1771. if (strchr(smask, 'r')) mask |= LUA_MASKRET;
  1772. if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
  1773. if (count > 0) mask |= LUA_MASKCOUNT;
  1774. sethookaux(L, mask, count, scpt);
  1775. }
  1776. return 0;
  1777. }
  1778. static int coresume (lua_State *L) {
  1779. int status, nres;
  1780. lua_State *co = lua_tothread(L, 1);
  1781. luaL_argcheck(L, co, 1, "coroutine expected");
  1782. status = lua_resume(co, L, 0, &nres);
  1783. if (status != LUA_OK && status != LUA_YIELD) {
  1784. lua_pushboolean(L, 0);
  1785. lua_insert(L, -2);
  1786. return 2; /* return false + error message */
  1787. }
  1788. else {
  1789. lua_pushboolean(L, 1);
  1790. return 1;
  1791. }
  1792. }
  1793. /* }====================================================== */
  1794. static const struct luaL_Reg tests_funcs[] = {
  1795. {"checkmemory", lua_checkmemory},
  1796. {"closestate", closestate},
  1797. {"d2s", d2s},
  1798. {"doonnewstack", doonnewstack},
  1799. {"doremote", doremote},
  1800. {"gccolor", gc_color},
  1801. {"gcage", gc_age},
  1802. {"gcstate", gc_state},
  1803. {"tracegc", tracegc},
  1804. {"pobj", gc_printobj},
  1805. {"getref", getref},
  1806. {"hash", hash_query},
  1807. {"log2", log2_aux},
  1808. {"limits", get_limits},
  1809. {"listcode", listcode},
  1810. {"printcode", printcode},
  1811. {"listk", listk},
  1812. {"listabslineinfo", listabslineinfo},
  1813. {"listlocals", listlocals},
  1814. {"loadlib", loadlib},
  1815. {"checkpanic", checkpanic},
  1816. {"newstate", newstate},
  1817. {"newuserdata", newuserdata},
  1818. {"num2int", num2int},
  1819. {"makeseed", makeseed},
  1820. {"pushuserdata", pushuserdata},
  1821. {"gcquery", gc_query},
  1822. {"querystr", string_query},
  1823. {"querytab", table_query},
  1824. {"codeparam", test_codeparam},
  1825. {"applyparam", test_applyparam},
  1826. {"ref", tref},
  1827. {"resume", coresume},
  1828. {"s2d", s2d},
  1829. {"sethook", sethook},
  1830. {"stacklevel", stacklevel},
  1831. {"testC", testC},
  1832. {"makeCfunc", makeCfunc},
  1833. {"totalmem", mem_query},
  1834. {"alloccount", alloc_count},
  1835. {"allocfailnext", alloc_failnext},
  1836. {"trick", settrick},
  1837. {"udataval", udataval},
  1838. {"unref", unref},
  1839. {"upvalue", upvalue},
  1840. {"externKstr", externKstr},
  1841. {"externstr", externstr},
  1842. {NULL, NULL}
  1843. };
  1844. static void checkfinalmem (void) {
  1845. lua_assert(l_memcontrol.numblocks == 0);
  1846. lua_assert(l_memcontrol.total == 0);
  1847. }
  1848. int luaB_opentests (lua_State *L) {
  1849. void *ud;
  1850. lua_Alloc f = lua_getallocf(L, &ud);
  1851. lua_atpanic(L, &tpanic);
  1852. lua_setwarnf(L, &warnf, L);
  1853. lua_pushboolean(L, 0);
  1854. lua_setglobal(L, "_WARN"); /* _WARN = false */
  1855. regcodes(L);
  1856. atexit(checkfinalmem);
  1857. lua_assert(f == debug_realloc && ud == cast_voidp(&l_memcontrol));
  1858. lua_setallocf(L, f, ud); /* exercise this function */
  1859. luaL_newlib(L, tests_funcs);
  1860. return 1;
  1861. }
  1862. #endif