ltests.c 44 KB

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