ltests.c 34 KB

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