rpm  4.5
ltests.c
Go to the documentation of this file.
1 /*
2 ** $Id: ltests.c,v 1.1 2004/03/16 21:58:30 niemeyer Exp $
3 ** Internal Module for Debugging of the Lua Implementation
4 ** See Copyright Notice in lua.h
5 */
6 
7 
8 #include <ctype.h>
9 #include <limits.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #define ltests_c
15 
16 #include "lua.h"
17 
18 #include "lapi.h"
19 #include "lauxlib.h"
20 #include "lcode.h"
21 #include "ldebug.h"
22 #include "ldo.h"
23 #include "lfunc.h"
24 #include "lmem.h"
25 #include "lopcodes.h"
26 #include "lstate.h"
27 #include "lstring.h"
28 #include "ltable.h"
29 #include "lualib.h"
30 
31 
32 
33 /*
34 ** The whole module only makes sense with LUA_DEBUG on
35 */
36 #ifdef LUA_DEBUG
37 
38 
39 #define lua_pushintegral(L,i) lua_pushnumber(L, cast(lua_Number, (i)))
40 
41 
42 static lua_State *lua_state = NULL;
43 
44 int islocked = 0;
45 
46 
47 #define func_at(L,k) (L->ci->base+(k) - 1)
48 
49 
50 static void setnameval (lua_State *L, const char *name, int val)
51  /*@*/
52 {
53  lua_pushstring(L, name);
54  lua_pushintegral(L, val);
55  lua_settable(L, -3);
56 }
57 
58 
59 /*
60 ** {======================================================================
61 ** Controlled version for realloc.
62 ** =======================================================================
63 */
64 
65 #define MARK 0x55 /* 01010101 (a nice pattern) */
66 
67 #ifndef EXTERNMEMCHECK
68 /* full memory check */
69 #define HEADER (sizeof(L_Umaxalign)) /* ensures maximum alignment for HEADER */
70 #define MARKSIZE 16 /* size of marks after each block */
71 #define blockhead(b) (cast(char *, b) - HEADER)
72 #define setsize(newblock, size) (*cast(size_t *, newblock) = size)
73 #define checkblocksize(b, size) (size == (*cast(size_t *, blockhead(b))))
74 #define fillmem(mem,size) memset(mem, -MARK, size)
75 #else
76 /* external memory check: don't do it twice */
77 #define HEADER 0
78 #define MARKSIZE 0
79 #define blockhead(b) (b)
80 #define setsize(newblock, size) /* empty */
81 #define checkblocksize(b,size) (1)
82 #define fillmem(mem,size) /* empty */
83 #endif
84 
85 unsigned long memdebug_numblocks = 0;
86 unsigned long memdebug_total = 0;
87 unsigned long memdebug_maxmem = 0;
88 unsigned long memdebug_memlimit = ULONG_MAX;
89 
90 
91 static void *checkblock (void *block, size_t size)
92  /*@*/
93 {
94  void *b = blockhead(block);
95  int i;
96  for (i=0;i<MARKSIZE;i++)
97  lua_assert(*(cast(char *, b)+HEADER+size+i) == MARK+i); /* corrupted block? */
98  return b;
99 }
100 
101 
102 static void freeblock (void *block, size_t size)
103  /*@*/
104 {
105  if (block) {
106  lua_assert(checkblocksize(block, size));
107  block = checkblock(block, size);
108  fillmem(block, size+HEADER+MARKSIZE); /* erase block */
109  free(block); /* free original block */
110  memdebug_numblocks--;
111  memdebug_total -= size;
112  }
113 }
114 
115 
116 void *debug_realloc (void *block, size_t oldsize, size_t size) {
117  lua_assert(oldsize == 0 || checkblocksize(block, oldsize));
118  /* ISO does not specify what realloc(NULL, 0) does */
119  lua_assert(block != NULL || size > 0);
120  if (size == 0) {
121  freeblock(block, oldsize);
122  return NULL;
123  }
124  else if (size > oldsize && memdebug_total+size-oldsize > memdebug_memlimit)
125  return NULL; /* to test memory allocation errors */
126  else {
127  void *newblock;
128  int i;
129  size_t realsize = HEADER+size+MARKSIZE;
130  size_t commonsize = (oldsize < size) ? oldsize : size;
131  if (realsize < size) return NULL; /* overflow! */
132  newblock = malloc(realsize); /* alloc a new block */
133  if (newblock == NULL) return NULL;
134  if (block) {
135  memcpy(cast(char *, newblock)+HEADER, block, commonsize);
136  freeblock(block, oldsize); /* erase (and check) old copy */
137  }
138  /* initialize new part of the block with something `weird' */
139  fillmem(cast(char *, newblock)+HEADER+commonsize, size-commonsize);
140  memdebug_total += size;
141  if (memdebug_total > memdebug_maxmem)
142  memdebug_maxmem = memdebug_total;
143  memdebug_numblocks++;
144  setsize(newblock, size);
145  for (i=0;i<MARKSIZE;i++)
146  *(cast(char *, newblock)+HEADER+size+i) = cast(char, MARK+i);
147  return cast(char *, newblock)+HEADER;
148  }
149 }
150 
151 
152 /* }====================================================================== */
153 
154 
155 
156 /*
157 ** {======================================================
158 ** Disassembler
159 ** =======================================================
160 */
161 
162 
163 static char *buildop (Proto *p, int pc, char *buff)
164  /*@*/
165 {
166  Instruction i = p->code[pc];
167  OpCode o = GET_OPCODE(i);
168  const char *name = luaP_opnames[o];
169  int line = getline(p, pc);
170  sprintf(buff, "(%4d) %4d - ", line, pc);
171  switch (getOpMode(o)) {
172  case iABC:
173  sprintf(buff+strlen(buff), "%-12s%4d %4d %4d", name,
174  GETARG_A(i), GETARG_B(i), GETARG_C(i));
175  break;
176  case iABx:
177  sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_Bx(i));
178  break;
179  case iAsBx:
180  sprintf(buff+strlen(buff), "%-12s%4d %4d", name, GETARG_A(i), GETARG_sBx(i));
181  break;
182  }
183  return buff;
184 }
185 
186 
187 #if 0
188 void luaI_printcode (Proto *pt, int size) {
189  int pc;
190  for (pc=0; pc<size; pc++) {
191  char buff[100];
192  printf("%s\n", buildop(pt, pc, buff));
193  }
194  printf("-------\n");
195 }
196 #endif
197 
198 
199 static int listcode (lua_State *L)
200  /*@*/
201 {
202  int pc;
203  Proto *p;
204  luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
205  1, "Lua function expected");
206  p = clvalue(func_at(L, 1))->l.p;
207  lua_newtable(L);
208  setnameval(L, "maxstack", p->maxstacksize);
209  setnameval(L, "numparams", p->numparams);
210  for (pc=0; pc<p->sizecode; pc++) {
211  char buff[100];
212  lua_pushintegral(L, pc+1);
213  lua_pushstring(L, buildop(p, pc, buff));
214  lua_settable(L, -3);
215  }
216  return 1;
217 }
218 
219 
220 static int listk (lua_State *L)
221  /*@*/
222 {
223  Proto *p;
224  int i;
225  luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
226  1, "Lua function expected");
227  p = clvalue(func_at(L, 1))->l.p;
228  lua_newtable(L);
229  for (i=0; i<p->sizek; i++) {
230  lua_pushintegral(L, i+1);
231  luaA_pushobject(L, p->k+i);
232  lua_settable(L, -3);
233  }
234  return 1;
235 }
236 
237 
238 static int listlocals (lua_State *L)
239  /*@*/
240 {
241  Proto *p;
242  int pc = luaL_checkint(L, 2) - 1;
243  int i = 0;
244  const char *name;
245  luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
246  1, "Lua function expected");
247  p = clvalue(func_at(L, 1))->l.p;
248  while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
249  lua_pushstring(L, name);
250  return i-1;
251 }
252 
253 /* }====================================================== */
254 
255 
256 
257 
258 static int get_limits (lua_State *L)
259  /*@*/
260 {
261  lua_newtable(L);
262  setnameval(L, "BITS_INT", BITS_INT);
263  setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
264  setnameval(L, "MAXVARS", MAXVARS);
265  setnameval(L, "MAXPARAMS", MAXPARAMS);
266  setnameval(L, "MAXSTACK", MAXSTACK);
267  setnameval(L, "MAXUPVALUES", MAXUPVALUES);
268  return 1;
269 }
270 
271 
272 static int mem_query (lua_State *L)
273  /*@*/
274 {
275  if (lua_isnone(L, 1)) {
276  lua_pushintegral(L, memdebug_total);
277  lua_pushintegral(L, memdebug_numblocks);
278  lua_pushintegral(L, memdebug_maxmem);
279  return 3;
280  }
281  else {
282  memdebug_memlimit = luaL_checkint(L, 1);
283  return 0;
284  }
285 }
286 
287 
288 static int hash_query (lua_State *L)
289  /*@*/
290 {
291  if (lua_isnone(L, 2)) {
292  luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
293  lua_pushintegral(L, tsvalue(func_at(L, 1))->tsv.hash);
294  }
295  else {
296  TObject *o = func_at(L, 1);
297  Table *t;
298  luaL_checktype(L, 2, LUA_TTABLE);
299  t = hvalue(func_at(L, 2));
300  lua_pushintegral(L, luaH_mainposition(t, o) - t->node);
301  }
302  return 1;
303 }
304 
305 
306 static int stacklevel (lua_State *L)
307  /*@*/
308 {
309  unsigned long a = 0;
310  lua_pushintegral(L, (int)(L->top - L->stack));
311  lua_pushintegral(L, (int)(L->stack_last - L->stack));
312  lua_pushintegral(L, (int)(L->ci - L->base_ci));
313  lua_pushintegral(L, (int)(L->end_ci - L->base_ci));
314  lua_pushintegral(L, (unsigned long)&a);
315  return 5;
316 }
317 
318 
319 static int table_query (lua_State *L)
320  /*@*/
321 {
322  const Table *t;
323  int i = luaL_optint(L, 2, -1);
324  luaL_checktype(L, 1, LUA_TTABLE);
325  t = hvalue(func_at(L, 1));
326  if (i == -1) {
327  lua_pushintegral(L, t->sizearray);
328  lua_pushintegral(L, sizenode(t));
329  lua_pushintegral(L, t->firstfree - t->node);
330  }
331  else if (i < t->sizearray) {
332  lua_pushintegral(L, i);
333  luaA_pushobject(L, &t->array[i]);
334  lua_pushnil(L);
335  }
336  else if ((i -= t->sizearray) < sizenode(t)) {
337  if (!ttisnil(gval(gnode(t, i))) ||
338  ttisnil(gkey(gnode(t, i))) ||
339  ttisnumber(gkey(gnode(t, i)))) {
340  luaA_pushobject(L, gkey(gnode(t, i)));
341  }
342  else
343  lua_pushstring(L, "<undef>");
344  luaA_pushobject(L, gval(gnode(t, i)));
345  if (t->node[i].next)
346  lua_pushintegral(L, t->node[i].next - t->node);
347  else
348  lua_pushnil(L);
349  }
350  return 3;
351 }
352 
353 
354 static int string_query (lua_State *L)
355  /*@*/
356 {
357  stringtable *tb = &G(L)->strt;
358  int s = luaL_optint(L, 2, 0) - 1;
359  if (s==-1) {
360  lua_pushintegral(L ,tb->nuse);
361  lua_pushintegral(L ,tb->size);
362  return 2;
363  }
364  else if (s < tb->size) {
365  GCObject *ts;
366  int n = 0;
367  for (ts = tb->hash[s]; ts; ts = ts->gch.next) {
368  setsvalue2s(L->top, gcotots(ts));
369  incr_top(L);
370  n++;
371  }
372  return n;
373  }
374  return 0;
375 }
376 
377 
378 static int tref (lua_State *L)
379  /*@*/
380 {
381  int level = lua_gettop(L);
382  int lock = luaL_optint(L, 2, 1);
383  luaL_checkany(L, 1);
384  lua_pushvalue(L, 1);
385  lua_pushintegral(L, lua_ref(L, lock));
386  assert(lua_gettop(L) == level+1); /* +1 for result */
387  return 1;
388 }
389 
390 static int getref (lua_State *L)
391  /*@*/
392 {
393  int level = lua_gettop(L);
394  lua_getref(L, luaL_checkint(L, 1));
395  assert(lua_gettop(L) == level+1);
396  return 1;
397 }
398 
399 static int unref (lua_State *L)
400  /*@*/
401 {
402  int level = lua_gettop(L);
403  lua_unref(L, luaL_checkint(L, 1));
404  assert(lua_gettop(L) == level);
405  return 0;
406 }
407 
408 static int metatable (lua_State *L)
409  /*@*/
410 {
411  luaL_checkany(L, 1);
412  if (lua_isnone(L, 2)) {
413  if (lua_getmetatable(L, 1) == 0)
414  lua_pushnil(L);
415  }
416  else {
417  lua_settop(L, 2);
418  luaL_checktype(L, 2, LUA_TTABLE);
419  lua_setmetatable(L, 1);
420  }
421  return 1;
422 }
423 
424 
425 static int upvalue (lua_State *L)
426  /*@*/
427 {
428  int n = luaL_checkint(L, 2);
429  luaL_checktype(L, 1, LUA_TFUNCTION);
430  if (lua_isnone(L, 3)) {
431  const char *name = lua_getupvalue(L, 1, n);
432  if (name == NULL) return 0;
433  lua_pushstring(L, name);
434  return 2;
435  }
436  else {
437  const char *name = lua_setupvalue(L, 1, n);
438  lua_pushstring(L, name);
439  return 1;
440  }
441 }
442 
443 
444 static int newuserdata (lua_State *L)
445  /*@*/
446 {
447  size_t size = luaL_checkint(L, 1);
448  char *p = cast(char *, lua_newuserdata(L, size));
449  while (size--) *p++ = '\0';
450  return 1;
451 }
452 
453 
454 static int pushuserdata (lua_State *L)
455  /*@*/
456 {
457  lua_pushlightuserdata(L, cast(void *, luaL_checkint(L, 1)));
458  return 1;
459 }
460 
461 
462 static int udataval (lua_State *L)
463  /*@*/
464 {
465  lua_pushintegral(L, cast(int, lua_touserdata(L, 1)));
466  return 1;
467 }
468 
469 
470 static int doonnewstack (lua_State *L)
471  /*@*/
472 {
473  lua_State *L1 = lua_newthread(L);
474  size_t l;
475  const char *s = luaL_checklstring(L, 1, &l);
476  int status = luaL_loadbuffer(L1, s, l, s);
477  if (status == 0)
478  status = lua_pcall(L1, 0, 0, 0);
479  lua_pushintegral(L, status);
480  return 1;
481 }
482 
483 
484 static int s2d (lua_State *L)
485  /*@*/
486 {
487  lua_pushnumber(L, *cast(const double *, luaL_checkstring(L, 1)));
488  return 1;
489 }
490 
491 static int d2s (lua_State *L)
492  /*@*/
493 {
494  double d = luaL_checknumber(L, 1);
495  lua_pushlstring(L, cast(char *, &d), sizeof(d));
496  return 1;
497 }
498 
499 
500 static int newstate (lua_State *L)
501  /*@*/
502 {
503  lua_State *L1 = lua_open();
504  if (L1) {
505  lua_userstateopen(L1); /* init lock */
506  lua_pushintegral(L, (unsigned long)L1);
507  }
508  else
509  lua_pushnil(L);
510  return 1;
511 }
512 
513 
514 static int loadlib (lua_State *L)
515  /*@*/
516 {
517  static const luaL_reg libs[] = {
518  {"mathlibopen", luaopen_math},
519  {"strlibopen", luaopen_string},
520  {"iolibopen", luaopen_io},
521  {"tablibopen", luaopen_table},
522  {"dblibopen", luaopen_debug},
523  {"baselibopen", luaopen_base},
524  {NULL, NULL}
525  };
526  lua_State *L1 = cast(lua_State *,
527  cast(unsigned long, luaL_checknumber(L, 1)));
528  lua_pushvalue(L1, LUA_GLOBALSINDEX);
529  luaL_openlib(L1, NULL, libs, 0);
530  return 0;
531 }
532 
533 static int closestate (lua_State *L)
534  /*@*/
535 {
536  lua_State *L1 = cast(lua_State *, cast(unsigned long, luaL_checknumber(L, 1)));
537  lua_close(L1);
538  lua_unlock(L); /* close cannot unlock that */
539  return 0;
540 }
541 
542 static int doremote (lua_State *L)
543  /*@*/
544 {
545  lua_State *L1 = cast(lua_State *,cast(unsigned long,luaL_checknumber(L, 1)));
546  size_t lcode;
547  const char *code = luaL_checklstring(L, 2, &lcode);
548  int status;
549  lua_settop(L1, 0);
550  status = luaL_loadbuffer(L1, code, lcode, code);
551  if (status == 0)
552  status = lua_pcall(L1, 0, LUA_MULTRET, 0);
553  if (status != 0) {
554  lua_pushnil(L);
555  lua_pushintegral(L, status);
556  lua_pushstring(L, lua_tostring(L1, -1));
557  return 3;
558  }
559  else {
560  int i = 0;
561  while (!lua_isnone(L1, ++i))
562  lua_pushstring(L, lua_tostring(L1, i));
563  lua_pop(L1, i-1);
564  return i-1;
565  }
566 }
567 
568 
569 static int log2_aux (lua_State *L)
570  /*@*/
571 {
572  lua_pushintegral(L, luaO_log2(luaL_checkint(L, 1)));
573  return 1;
574 }
575 
576 static int int2fb_aux (lua_State *L)
577  /*@*/
578 {
579  int b = luaO_int2fb(luaL_checkint(L, 1));
580  lua_pushintegral(L, b);
581  lua_pushintegral(L, fb2int(b));
582  return 2;
583 }
584 
585 
586 static int test_do (lua_State *L)
587  /*@*/
588 {
589  const char *p = luaL_checkstring(L, 1);
590  if (*p == '@')
591  lua_dofile(L, p+1);
592  else
593  lua_dostring(L, p);
594  return lua_gettop(L);
595 }
596 
597 
598 
599 /*
600 ** {======================================================
601 ** function to test the API with C. It interprets a kind of assembler
602 ** language with calls to the API, so the test can be driven by Lua code
603 ** =======================================================
604 */
605 
606 static const char *const delimits = " \t\n,;";
607 
608 static void skip (const char **pc)
609  /*@*/
610 {
611  while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
612 }
613 
614 static int getnum_aux (lua_State *L, const char **pc)
615  /*@*/
616 {
617  int res = 0;
618  int sig = 1;
619  skip(pc);
620  if (**pc == '.') {
621  res = cast(int, lua_tonumber(L, -1));
622  lua_pop(L, 1);
623  (*pc)++;
624  return res;
625  }
626  else if (**pc == '-') {
627  sig = -1;
628  (*pc)++;
629  }
630  while (isdigit(cast(int, **pc))) res = res*10 + (*(*pc)++) - '0';
631  return sig*res;
632 }
633 
634 static const char *getname_aux (char *buff, const char **pc)
635  /*@*/
636 {
637  int i = 0;
638  skip(pc);
639  while (**pc != '\0' && !strchr(delimits, **pc))
640  buff[i++] = *(*pc)++;
641  buff[i] = '\0';
642  return buff;
643 }
644 
645 
646 #define EQ(s1) (strcmp(s1, inst) == 0)
647 
648 #define getnum (getnum_aux(L, &pc))
649 #define getname (getname_aux(buff, &pc))
650 
651 
652 static int testC (lua_State *L)
653  /*@*/
654 {
655  char buff[30];
656  const char *pc = luaL_checkstring(L, 1);
657  for (;;) {
658  const char *inst = getname;
659  if EQ("") return 0;
660  else if EQ("isnumber") {
661  lua_pushintegral(L, lua_isnumber(L, getnum));
662  }
663  else if EQ("isstring") {
664  lua_pushintegral(L, lua_isstring(L, getnum));
665  }
666  else if EQ("istable") {
667  lua_pushintegral(L, lua_istable(L, getnum));
668  }
669  else if EQ("iscfunction") {
670  lua_pushintegral(L, lua_iscfunction(L, getnum));
671  }
672  else if EQ("isfunction") {
673  lua_pushintegral(L, lua_isfunction(L, getnum));
674  }
675  else if EQ("isuserdata") {
676  lua_pushintegral(L, lua_isuserdata(L, getnum));
677  }
678  else if EQ("isudataval") {
679  lua_pushintegral(L, lua_islightuserdata(L, getnum));
680  }
681  else if EQ("isnil") {
682  lua_pushintegral(L, lua_isnil(L, getnum));
683  }
684  else if EQ("isnull") {
685  lua_pushintegral(L, lua_isnone(L, getnum));
686  }
687  else if EQ("tonumber") {
688  lua_pushnumber(L, lua_tonumber(L, getnum));
689  }
690  else if EQ("tostring") {
691  const char *s = lua_tostring(L, getnum);
692  lua_pushstring(L, s);
693  }
694  else if EQ("strlen") {
695  lua_pushintegral(L, lua_strlen(L, getnum));
696  }
697  else if EQ("tocfunction") {
698  lua_pushcfunction(L, lua_tocfunction(L, getnum));
699  }
700  else if EQ("return") {
701  return getnum;
702  }
703  else if EQ("gettop") {
704  lua_pushintegral(L, lua_gettop(L));
705  }
706  else if EQ("settop") {
707  lua_settop(L, getnum);
708  }
709  else if EQ("pop") {
710  lua_pop(L, getnum);
711  }
712  else if EQ("pushnum") {
713  lua_pushintegral(L, getnum);
714  }
715  else if EQ("pushnil") {
716  lua_pushnil(L);
717  }
718  else if EQ("pushbool") {
719  lua_pushboolean(L, getnum);
720  }
721  else if EQ("tobool") {
722  lua_pushintegral(L, lua_toboolean(L, getnum));
723  }
724  else if EQ("pushvalue") {
725  lua_pushvalue(L, getnum);
726  }
727  else if EQ("pushcclosure") {
728  lua_pushcclosure(L, testC, getnum);
729  }
730  else if EQ("pushupvalues") {
731  lua_pushupvalues(L);
732  }
733  else if EQ("remove") {
734  lua_remove(L, getnum);
735  }
736  else if EQ("insert") {
737  lua_insert(L, getnum);
738  }
739  else if EQ("replace") {
740  lua_replace(L, getnum);
741  }
742  else if EQ("gettable") {
743  lua_gettable(L, getnum);
744  }
745  else if EQ("settable") {
746  lua_settable(L, getnum);
747  }
748  else if EQ("next") {
749  lua_next(L, -2);
750  }
751  else if EQ("concat") {
752  lua_concat(L, getnum);
753  }
754  else if EQ("lessthan") {
755  int a = getnum;
756  lua_pushboolean(L, lua_lessthan(L, a, getnum));
757  }
758  else if EQ("equal") {
759  int a = getnum;
760  lua_pushboolean(L, lua_equal(L, a, getnum));
761  }
762  else if EQ("rawcall") {
763  int narg = getnum;
764  int nres = getnum;
765  lua_call(L, narg, nres);
766  }
767  else if EQ("call") {
768  int narg = getnum;
769  int nres = getnum;
770  lua_pcall(L, narg, nres, 0);
771  }
772  else if EQ("loadstring") {
773  size_t sl;
774  const char *s = luaL_checklstring(L, getnum, &sl);
775  luaL_loadbuffer(L, s, sl, s);
776  }
777  else if EQ("loadfile") {
778  luaL_loadfile(L, luaL_checkstring(L, getnum));
779  }
780  else if EQ("setmetatable") {
781  lua_setmetatable(L, getnum);
782  }
783  else if EQ("getmetatable") {
784  if (lua_getmetatable(L, getnum) == 0)
785  lua_pushnil(L);
786  }
787  else if EQ("type") {
788  lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
789  }
790  else if EQ("getn") {
791  int i = getnum;
792  lua_pushintegral(L, luaL_getn(L, i));
793  }
794  else if EQ("setn") {
795  int i = getnum;
796  int n = cast(int, lua_tonumber(L, -1));
797  luaL_setn(L, i, n);
798  lua_pop(L, 1);
799  }
800  else luaL_error(L, "unknown instruction %s", buff);
801  }
802  return 0;
803 }
804 
805 /* }====================================================== */
806 
807 
808 /*
809 ** {======================================================
810 ** tests for yield inside hooks
811 ** =======================================================
812 */
813 
814 static void yieldf (lua_State *L, lua_Debug *ar)
815  /*@*/
816 {
817  lua_yield(L, 0);
818 }
819 
820 static int setyhook (lua_State *L)
821  /*@*/
822 {
823  if (lua_isnoneornil(L, 1))
824  lua_sethook(L, NULL, 0, 0); /* turn off hooks */
825  else {
826  const char *smask = luaL_checkstring(L, 1);
827  int count = luaL_optint(L, 2, 0);
828  int mask = 0;
829  if (strchr(smask, 'l')) mask |= LUA_MASKLINE;
830  if (count > 0) mask |= LUA_MASKCOUNT;
831  lua_sethook(L, yieldf, mask, count);
832  }
833  return 0;
834 }
835 
836 
837 static int coresume (lua_State *L)
838  /*@*/
839 {
840  int status;
841  lua_State *co = lua_tothread(L, 1);
842  luaL_argcheck(L, co, 1, "coroutine expected");
843  status = lua_resume(co, 0);
844  if (status != 0) {
845  lua_pushboolean(L, 0);
846  lua_insert(L, -2);
847  return 2; /* return false + error message */
848  }
849  else {
850  lua_pushboolean(L, 1);
851  return 1;
852  }
853 }
854 
855 /* }====================================================== */
856 
857 
858 
859 static const struct luaL_reg tests_funcs[] = {
860  {"hash", hash_query},
861  {"limits", get_limits},
862  {"listcode", listcode},
863  {"listk", listk},
864  {"listlocals", listlocals},
865  {"loadlib", loadlib},
866  {"stacklevel", stacklevel},
867  {"querystr", string_query},
868  {"querytab", table_query},
869  {"doit", test_do},
870  {"testC", testC},
871  {"ref", tref},
872  {"getref", getref},
873  {"unref", unref},
874  {"d2s", d2s},
875  {"s2d", s2d},
876  {"metatable", metatable},
877  {"upvalue", upvalue},
878  {"newuserdata", newuserdata},
879  {"pushuserdata", pushuserdata},
880  {"udataval", udataval},
881  {"doonnewstack", doonnewstack},
882  {"newstate", newstate},
883  {"closestate", closestate},
884  {"doremote", doremote},
885  {"log2", log2_aux},
886  {"int2fb", int2fb_aux},
887  {"totalmem", mem_query},
888  {"resume", coresume},
889  {"setyhook", setyhook},
890  {NULL, NULL}
891 };
892 
893 
894 static void fim (void)
895  /*@*/
896 {
897  if (!islocked)
898  lua_close(lua_state);
899  lua_assert(memdebug_numblocks == 0);
900  lua_assert(memdebug_total == 0);
901 }
902 
903 
904 static int l_panic (lua_State *L)
905  /*@*/
906 {
907  UNUSED(L);
908  fprintf(stderr, "unable to recover; exiting\n");
909  return 0;
910 }
911 
912 
913 int luaB_opentests (lua_State *L) {
914  lua_atpanic(L, l_panic);
915  lua_userstateopen(L); /* init lock */
916  lua_state = L; /* keep first state to be opened */
917  luaL_openlib(L, "T", tests_funcs, 0);
918  atexit(fim);
919  return 0;
920 }
921 
922 
923 #undef main
924 int main (int argc, char *argv[]) {
925  char *limit = getenv("MEMLIMIT");
926  if (limit)
927  memdebug_memlimit = strtoul(limit, NULL, 10);
928  l_main(argc, argv);
929  return 0;
930 }
931 
932 #endif