39 #define lua_pushintegral(L,i) lua_pushnumber(L, cast(lua_Number, (i)))
47 #define func_at(L,k) (L->ci->base+(k) - 1)
50 static void setnameval (
lua_State *L,
const char *
name,
int val)
54 lua_pushintegral(L, val);
67 #ifndef EXTERNMEMCHECK
69 #define HEADER (sizeof(L_Umaxalign))
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)
79 #define blockhead(b) (b)
80 #define setsize(newblock, size)
81 #define checkblocksize(b,size) (1)
82 #define fillmem(mem,size)
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;
91 static void *checkblock (
void *
block,
size_t size)
94 void *b = blockhead(block);
96 for (i=0;i<MARKSIZE;i++)
102 static void freeblock (
void *block,
size_t size)
107 block = checkblock(block, size);
108 fillmem(block, size+HEADER+MARKSIZE);
110 memdebug_numblocks--;
111 memdebug_total -= size;
116 void *debug_realloc (
void *block,
size_t oldsize,
size_t size) {
117 lua_assert(oldsize == 0 || checkblocksize(block, oldsize));
121 freeblock(block, oldsize);
124 else if (size > oldsize && memdebug_total+size-oldsize > memdebug_memlimit)
129 size_t realsize = HEADER+size+MARKSIZE;
130 size_t commonsize = (oldsize < size) ? oldsize : size;
131 if (realsize < size)
return NULL;
132 newblock = malloc(realsize);
133 if (newblock == NULL)
return NULL;
135 memcpy(
cast(
char *, newblock)+HEADER, block, commonsize);
136 freeblock(block, oldsize);
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;
163 static char *buildop (
Proto *p,
int pc,
char *buff)
168 const char *
name = luaP_opnames[o];
170 sprintf(buff,
"(%4d) %4d - ", line, pc);
173 sprintf(buff+strlen(buff),
"%-12s%4d %4d %4d", name,
188 void luaI_printcode (
Proto *pt,
int size) {
190 for (pc=0; pc<size; pc++) {
192 printf(
"%s\n", buildop(pt, pc, buff));
205 1,
"Lua function expected");
206 p =
clvalue(func_at(L, 1))->l.p;
209 setnameval(L,
"numparams", p->
numparams);
212 lua_pushintegral(L, pc+1);
226 1,
"Lua function expected");
227 p =
clvalue(func_at(L, 1))->l.p;
229 for (i=0; i<p->
sizek; i++) {
230 lua_pushintegral(L, i+1);
242 int pc = luaL_checkint(L, 2) - 1;
246 1,
"Lua function expected");
247 p =
clvalue(func_at(L, 1))->l.p;
262 setnameval(L,
"BITS_INT",
BITS_INT);
264 setnameval(L,
"MAXVARS",
MAXVARS);
266 setnameval(L,
"MAXSTACK",
MAXSTACK);
275 if (lua_isnone(L, 1)) {
276 lua_pushintegral(L, memdebug_total);
277 lua_pushintegral(L, memdebug_numblocks);
278 lua_pushintegral(L, memdebug_maxmem);
282 memdebug_memlimit = luaL_checkint(L, 1);
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);
298 luaL_checktype(L, 2, LUA_TTABLE);
299 t =
hvalue(func_at(L, 2));
310 lua_pushintegral(L, (
int)(L->
top - L->
stack));
312 lua_pushintegral(L, (
int)(L->
ci - L->
base_ci));
314 lua_pushintegral(L, (
unsigned long)&a);
323 int i = luaL_optint(L, 2, -1);
324 luaL_checktype(L, 1, LUA_TTABLE);
325 t =
hvalue(func_at(L, 1));
331 else if (i < t->sizearray) {
332 lua_pushintegral(L, i);
358 int s = luaL_optint(L, 2, 0) - 1;
360 lua_pushintegral(L ,tb->
nuse);
361 lua_pushintegral(L ,tb->
size);
364 else if (s < tb->size) {
382 int lock = luaL_optint(L, 2, 1);
385 lua_pushintegral(L, lua_ref(L, lock));
394 lua_getref(L, luaL_checkint(L, 1));
403 lua_unref(L, luaL_checkint(L, 1));
412 if (lua_isnone(L, 2)) {
418 luaL_checktype(L, 2, LUA_TTABLE);
428 int n = luaL_checkint(L, 2);
429 luaL_checktype(L, 1, LUA_TFUNCTION);
430 if (lua_isnone(L, 3)) {
432 if (name == NULL)
return 0;
447 size_t size = luaL_checkint(L, 1);
449 while (size--) *p++ =
'\0';
475 const char *s = luaL_checklstring(L, 1, &l);
476 int status = luaL_loadbuffer(L1, s, l, s);
479 lua_pushintegral(L, status);
494 double d = luaL_checknumber(L, 1);
506 lua_pushintegral(L, (
unsigned long)L1);
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},
527 cast(
unsigned long, luaL_checknumber(L, 1)));
529 luaL_openlib(L1, NULL, libs, 0);
547 const char *code = luaL_checklstring(L, 2, &lcode);
550 status = luaL_loadbuffer(L1, code, lcode, code);
552 status =
lua_pcall(L1, 0, LUA_MULTRET, 0);
555 lua_pushintegral(L, status);
561 while (!lua_isnone(L1, ++i))
572 lua_pushintegral(L,
luaO_log2(luaL_checkint(L, 1)));
580 lua_pushintegral(L, b);
581 lua_pushintegral(L,
fb2int(b));
589 const char *p = luaL_checkstring(L, 1);
606 static const char *
const delimits =
" \t\n,;";
608 static void skip (
const char **pc)
611 while (**pc !=
'\0' && strchr(delimits, **pc)) (*pc)++;
614 static int getnum_aux (
lua_State *L,
const char **pc)
626 else if (**pc ==
'-') {
630 while (isdigit(
cast(
int, **pc))) res = res*10 + (*(*pc)++) -
'0';
634 static const char *getname_aux (
char *buff,
const char **pc)
639 while (**pc !=
'\0' && !strchr(delimits, **pc))
640 buff[i++] = *(*pc)++;
646 #define EQ(s1) (strcmp(s1, inst) == 0)
648 #define getnum (getnum_aux(L, &pc))
649 #define getname (getname_aux(buff, &pc))
656 const char *pc = luaL_checkstring(L, 1);
658 const char *inst = getname;
660 else if EQ("isnumber") {
663 else if EQ(
"isstring") {
666 else if EQ(
"istable") {
667 lua_pushintegral(L, lua_istable(L, getnum));
669 else if EQ(
"iscfunction") {
672 else if EQ(
"isfunction") {
673 lua_pushintegral(L, lua_isfunction(L, getnum));
675 else if EQ(
"isuserdata") {
678 else if EQ(
"isudataval") {
679 lua_pushintegral(L, lua_islightuserdata(L, getnum));
681 else if EQ(
"isnil") {
682 lua_pushintegral(L, lua_isnil(L, getnum));
684 else if EQ(
"isnull") {
685 lua_pushintegral(L, lua_isnone(L, getnum));
687 else if EQ(
"tonumber") {
690 else if EQ(
"tostring") {
694 else if EQ(
"strlen") {
697 else if EQ(
"tocfunction") {
700 else if EQ(
"return") {
703 else if EQ(
"gettop") {
706 else if EQ(
"settop") {
712 else if EQ(
"pushnum") {
713 lua_pushintegral(L, getnum);
715 else if EQ(
"pushnil") {
718 else if EQ(
"pushbool") {
721 else if EQ(
"tobool") {
724 else if EQ(
"pushvalue") {
727 else if EQ(
"pushcclosure") {
730 else if EQ(
"pushupvalues") {
733 else if EQ(
"remove") {
736 else if EQ(
"insert") {
739 else if EQ(
"replace") {
742 else if EQ(
"gettable") {
745 else if EQ(
"settable") {
751 else if EQ(
"concat") {
754 else if EQ(
"lessthan") {
758 else if EQ(
"equal") {
762 else if EQ(
"rawcall") {
772 else if EQ(
"loadstring") {
774 const char *s = luaL_checklstring(L, getnum, &sl);
775 luaL_loadbuffer(L, s, sl, s);
777 else if EQ(
"loadfile") {
778 luaL_loadfile(L, luaL_checkstring(L, getnum));
780 else if EQ(
"setmetatable") {
783 else if EQ(
"getmetatable") {
792 lua_pushintegral(L, luaL_getn(L, i));
800 else luaL_error(L,
"unknown instruction %s", buff);
814 static void yieldf (
lua_State *L, lua_Debug *ar)
823 if (lua_isnoneornil(L, 1))
826 const char *smask = luaL_checkstring(L, 1);
827 int count = luaL_optint(L, 2, 0);
829 if (strchr(smask,
'l')) mask |= LUA_MASKLINE;
830 if (count > 0) mask |= LUA_MASKCOUNT;
842 luaL_argcheck(L, co, 1,
"coroutine expected");
859 static const struct luaL_reg tests_funcs[] = {
860 {
"hash", hash_query},
861 {
"limits", get_limits},
862 {
"listcode", listcode},
864 {
"listlocals", listlocals},
865 {
"loadlib", loadlib},
866 {
"stacklevel", stacklevel},
867 {
"querystr", string_query},
868 {
"querytab", table_query},
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},
886 {
"int2fb", int2fb_aux},
887 {
"totalmem", mem_query},
888 {
"resume", coresume},
889 {
"setyhook", setyhook},
894 static void fim (
void)
908 fprintf(stderr,
"unable to recover; exiting\n");
917 luaL_openlib(L,
"T", tests_funcs, 0);
924 int main (
int argc,
char *argv[]) {
925 char *limit =
getenv(
"MEMLIMIT");
927 memdebug_memlimit = strtoul(limit, NULL, 10);