ltests.c 37 KB

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