ltests.c 34 KB

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