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