changed to test macros for single-state use

This commit is contained in:
Roberto Ierusalimschy
2000-06-02 14:06:42 -03:00
parent 190c3be739
commit 989ad7232a

270
ltests.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 1.20 2000/05/22 18:44:46 roberto Exp roberto $ ** $Id: ltests.c,v 1.21 2000/05/26 19:17:57 roberto Exp roberto $
** Internal Module for Debugging of the Lua Implementation ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -10,7 +10,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define LUA_REENTRANT
#include "lapi.h" #include "lapi.h"
#include "lauxlib.h" #include "lauxlib.h"
@@ -35,11 +34,11 @@ void luaB_opentests (lua_State *L);
static void setnameval (lua_State *L, lua_Object t, const char *name, int val) { static void setnameval (lua_Object t, const char *name, int val) {
lua_pushobject(L, t); lua_pushobject(t);
lua_pushstring(L, name); lua_pushstring(name);
lua_pushnumber(L, val); lua_pushnumber(val);
lua_settable(L); lua_settable();
} }
@@ -62,7 +61,7 @@ static const char *const instrname[OP_SETLINE+1] = {
}; };
static int pushop (lua_State *L, Instruction i) { static int pushop (Instruction i) {
char buff[100]; char buff[100];
OpCode o = GET_OPCODE(i); OpCode o = GET_OPCODE(i);
const char *name = instrname[o]; const char *name = instrname[o];
@@ -80,98 +79,99 @@ static int pushop (lua_State *L, Instruction i) {
sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i)); sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
break; break;
} }
lua_pushstring(L, buff); lua_pushstring(buff);
return (o != OP_END); return (o != OP_END);
} }
static void listcode (lua_State *L) { static void listcode (void) {
lua_Object o = luaL_nonnullarg(L, 1); lua_Object o = luaL_nonnullarg(1);
lua_Object t = lua_createtable(L); lua_Object t = lua_createtable();
Instruction *pc; Instruction *pc;
Proto *p; Proto *p;
int res; int res;
luaL_arg_check(L, ttype(o) == TAG_LCLOSURE, 1, "Lua function expected"); luaL_arg_check(ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
p = clvalue(o)->f.l; p = clvalue(o)->f.l;
setnameval(L, t, "maxstack", p->maxstacksize); setnameval(t, "maxstack", p->maxstacksize);
setnameval(L, t, "numparams", p->numparams); setnameval(t, "numparams", p->numparams);
pc = p->code; pc = p->code;
do { do {
lua_pushobject(L, t); lua_pushobject(t);
lua_pushnumber(L, pc - p->code + 1); lua_pushnumber(pc - p->code + 1);
res = pushop(L, *pc++); res = pushop(*pc++);
lua_settable(L); lua_settable();
} while (res); } while (res);
lua_pushobject(L, t); lua_pushobject(t);
} }
/* }====================================================== */ /* }====================================================== */
static void get_limits (lua_State *L) { static void get_limits (void) {
lua_Object t = lua_createtable(L); lua_Object t = lua_createtable();
setnameval(L, t, "SIZE_OP", SIZE_OP); setnameval(t, "SIZE_OP", SIZE_OP);
setnameval(L, t, "SIZE_U", SIZE_U); setnameval(t, "SIZE_U", SIZE_U);
setnameval(L, t, "SIZE_A", SIZE_A); setnameval(t, "SIZE_A", SIZE_A);
setnameval(L, t, "SIZE_B", SIZE_B); setnameval(t, "SIZE_B", SIZE_B);
setnameval(L, t, "MAXARG_U", MAXARG_U); setnameval(t, "MAXARG_U", MAXARG_U);
setnameval(L, t, "MAXARG_S", MAXARG_S); setnameval(t, "MAXARG_S", MAXARG_S);
setnameval(L, t, "MAXARG_A", MAXARG_A); setnameval(t, "MAXARG_A", MAXARG_A);
setnameval(L, t, "MAXARG_B", MAXARG_B); setnameval(t, "MAXARG_B", MAXARG_B);
setnameval(L, t, "MAXSTACK", MAXSTACK); setnameval(t, "MAXSTACK", MAXSTACK);
setnameval(L, t, "MAXLOCALS", MAXLOCALS); setnameval(t, "MAXLOCALS", MAXLOCALS);
setnameval(L, t, "MAXUPVALUES", MAXUPVALUES); setnameval(t, "MAXUPVALUES", MAXUPVALUES);
setnameval(L, t, "MAXVARSLH", MAXVARSLH); setnameval(t, "MAXVARSLH", MAXVARSLH);
setnameval(L, t, "MAXPARAMS", MAXPARAMS); setnameval(t, "MAXPARAMS", MAXPARAMS);
setnameval(L, t, "LFPF", LFIELDS_PER_FLUSH); setnameval(t, "LFPF", LFIELDS_PER_FLUSH);
setnameval(L, t, "RFPF", RFIELDS_PER_FLUSH); setnameval(t, "RFPF", RFIELDS_PER_FLUSH);
lua_pushobject(L, t); lua_pushobject(t);
} }
static void mem_query (lua_State *L) { static void mem_query (void) {
lua_pushnumber(L, memdebug_total); lua_pushnumber(memdebug_total);
lua_pushnumber(L, memdebug_numblocks); lua_pushnumber(memdebug_numblocks);
lua_pushnumber(L, memdebug_maxmem); lua_pushnumber(memdebug_maxmem);
} }
static void hash_query (lua_State *L) { static void hash_query (void) {
lua_Object o = luaL_nonnullarg(L, 1); lua_Object o = luaL_nonnullarg(1);
if (lua_getparam(L, 2) == LUA_NOOBJECT) { if (lua_getparam(2) == LUA_NOOBJECT) {
luaL_arg_check(L, ttype(o) == TAG_STRING, 1, "string expected"); luaL_arg_check(ttype(o) == TAG_STRING, 1, "string expected");
lua_pushnumber(L, tsvalue(o)->u.s.hash); lua_pushnumber(tsvalue(o)->u.s.hash);
} }
else { else {
const Hash *t = avalue(luaL_tablearg(L, 2)); const Hash *t = avalue(luaL_tablearg(2));
lua_pushnumber(L, luaH_mainposition(t, o) - t->node); lua_pushnumber(luaH_mainposition(t, o) - t->node);
} }
} }
static void table_query (lua_State *L) { static void table_query (void) {
const Hash *t = avalue(luaL_tablearg(L, 1)); const Hash *t = avalue(luaL_tablearg(1));
int i = luaL_opt_int(L, 2, -1); int i = luaL_opt_int(2, -1);
if (i == -1) { if (i == -1) {
lua_pushnumber(L, t->size); lua_pushnumber(t->size);
lua_pushnumber(L, t->firstfree - t->node); lua_pushnumber(t->firstfree - t->node);
} }
else if (i < t->size) { else if (i < t->size) {
luaA_pushobject(L, &t->node[i].key); luaA_pushobject(lua_state, &t->node[i].key);
luaA_pushobject(L, &t->node[i].val); luaA_pushobject(lua_state, &t->node[i].val);
if (t->node[i].next) if (t->node[i].next)
lua_pushnumber(L, t->node[i].next - t->node); lua_pushnumber(t->node[i].next - t->node);
} }
} }
static void string_query (lua_State *L) { static void string_query (void) {
stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &L->strt : &L->udt; lua_State *L = lua_state;
int s = luaL_opt_int(L, 2, 0) - 1; stringtable *tb = (*luaL_check_string(1) == 's') ? &L->strt : &L->udt;
int s = luaL_opt_int(2, 0) - 1;
if (s==-1) { if (s==-1) {
lua_pushnumber(L, tb->nuse); lua_pushnumber(tb->nuse);
lua_pushnumber(L, tb->size); lua_pushnumber(tb->size);
} }
else if (s < tb->size) { else if (s < tb->size) {
TString *ts; TString *ts;
@@ -203,9 +203,9 @@ static int getnum (const char **pc) {
return res; return res;
} }
static int getreg (lua_State *L, const char **pc) { static int getreg (const char **pc) {
skip(pc); skip(pc);
if (*(*pc)++ != 'r') lua_error(L, "`testC' expecting a register"); if (*(*pc)++ != 'r') lua_error("`testC' expecting a register");
return getnum(pc); return getnum(pc);
} }
@@ -223,149 +223,149 @@ static const char *getname (const char **pc) {
#define EQ(s1) (strcmp(s1, inst) == 0) #define EQ(s1) (strcmp(s1, inst) == 0)
static void testC (lua_State *L) { static void testC (void) {
lua_Object reg[10]; lua_Object reg[10];
const char *pc = luaL_check_string(L, 1); const char *pc = luaL_check_string(1);
for (;;) { for (;;) {
const char *inst = getname(&pc); const char *inst = getname(&pc);
if EQ("") return; if EQ("") return;
else if EQ("pushnum") { else if EQ("pushnum") {
lua_pushnumber(L, getnum(&pc)); lua_pushnumber(getnum(&pc));
} }
else if EQ("createtable") { else if EQ("createtable") {
reg[getreg(L, &pc)] = lua_createtable(L); reg[getreg(&pc)] = lua_createtable();
} }
else if EQ("closure") { else if EQ("closure") {
lua_CFunction f = lua_getcfunction(L, lua_getglobal(L, getname(&pc))); lua_CFunction f = lua_getcfunction(lua_getglobal(getname(&pc)));
lua_pushcclosure(L, f, getnum(&pc)); lua_pushcclosure(f, getnum(&pc));
} }
else if EQ("pop") { else if EQ("pop") {
reg[getreg(L, &pc)] = lua_pop(L); reg[getreg(&pc)] = lua_pop();
} }
else if EQ("getglobal") { else if EQ("getglobal") {
int n = getreg(L, &pc); int n = getreg(&pc);
reg[n] = lua_getglobal(L, getname(&pc)); reg[n] = lua_getglobal(getname(&pc));
} }
else if EQ("ref") { else if EQ("ref") {
lua_pushnumber(L, lua_ref(L, 0)); lua_pushnumber(lua_ref(0));
reg[getreg(L, &pc)] = lua_pop(L); reg[getreg(&pc)] = lua_pop();
} }
else if EQ("reflock") { else if EQ("reflock") {
lua_pushnumber(L, lua_ref(L, 1)); lua_pushnumber(lua_ref(1));
reg[getreg(L, &pc)] = lua_pop(L); reg[getreg(&pc)] = lua_pop();
} }
else if EQ("getref") { else if EQ("getref") {
int n = getreg(L, &pc); int n = getreg(&pc);
reg[n] = lua_getref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)])); reg[n] = lua_getref((int)lua_getnumber(reg[getreg(&pc)]));
} }
else if EQ("unref") { else if EQ("unref") {
lua_unref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)])); lua_unref((int)lua_getnumber(reg[getreg(&pc)]));
} }
else if EQ("getparam") { else if EQ("getparam") {
int n = getreg(L, &pc); int n = getreg(&pc);
reg[n] = lua_getparam(L, getnum(&pc)+1); /* skips the command itself */ reg[n] = lua_getparam(getnum(&pc)+1); /* skips the command itself */
} }
else if EQ("getresult") { else if EQ("getresult") {
int n = getreg(L, &pc); int n = getreg(&pc);
reg[n] = lua_getparam(L, getnum(&pc)); reg[n] = lua_getparam(getnum(&pc));
} }
else if EQ("setglobal") { else if EQ("setglobal") {
lua_setglobal(L, getname(&pc)); lua_setglobal(getname(&pc));
} }
else if EQ("pushglobals") { else if EQ("pushglobals") {
lua_pushglobaltable(L); lua_pushglobaltable();
} }
else if EQ("pushstring") { else if EQ("pushstring") {
lua_pushstring(L, getname(&pc)); lua_pushstring(getname(&pc));
} }
else if EQ("pushreg") { else if EQ("pushreg") {
lua_pushobject(L, reg[getreg(L, &pc)]); lua_pushobject(reg[getreg(&pc)]);
} }
else if EQ("call") { else if EQ("call") {
if (lua_call(L, getname(&pc))) lua_error(L, NULL); if (lua_call(getname(&pc))) lua_error(NULL);
} }
else if EQ("gettable") { else if EQ("gettable") {
reg[getreg(L, &pc)] = lua_gettable(L); reg[getreg(&pc)] = lua_gettable();
} }
else if EQ("rawget") { else if EQ("rawget") {
reg[getreg(L, &pc)] = lua_rawget(L); reg[getreg(&pc)] = lua_rawget();
} }
else if EQ("settable") { else if EQ("settable") {
lua_settable(L); lua_settable();
} }
else if EQ("rawset") { else if EQ("rawset") {
lua_rawset(L); lua_rawset();
} }
else if EQ("tag") { else if EQ("tag") {
lua_pushnumber(L, lua_tag(L, reg[getreg(L, &pc)])); lua_pushnumber(lua_tag(reg[getreg(&pc)]));
} }
else if EQ("type") { else if EQ("type") {
lua_pushstring(L, lua_type(L, reg[getreg(L, &pc)])); lua_pushstring(lua_type(reg[getreg(&pc)]));
} }
else if EQ("next") { else if EQ("next") {
int n = getreg(L, &pc); int n = getreg(&pc);
n = lua_next(L, reg[n], (int)lua_getnumber(L, reg[getreg(L, &pc)])); n = lua_next(reg[n], (int)lua_getnumber(reg[getreg(&pc)]));
lua_pushnumber(L, n); lua_pushnumber(n);
} }
else if EQ("equal") { else if EQ("equal") {
int n1 = getreg(L, &pc); int n1 = getreg(&pc);
int n2 = getreg(L, &pc); int n2 = getreg(&pc);
lua_pushnumber(L, lua_equal(L, reg[n1], reg[n2])); lua_pushnumber(lua_equal(reg[n1], reg[n2]));
} }
else if EQ("pushusertag") { else if EQ("pushusertag") {
int val = getreg(L, &pc); int val = getreg(&pc);
int tag = getreg(L, &pc); int tag = getreg(&pc);
lua_pushusertag(L, (void *)(int)lua_getnumber(L, reg[val]), lua_pushusertag((void *)(int)lua_getnumber(reg[val]),
(int)lua_getnumber(L, reg[tag])); (int)lua_getnumber(reg[tag]));
} }
else if EQ("udataval") { else if EQ("udataval") {
int n = getreg(L, &pc); int n = getreg(&pc);
lua_pushnumber(L, (int)lua_getuserdata(L, reg[getreg(L, &pc)])); lua_pushnumber((int)lua_getuserdata(reg[getreg(&pc)]));
reg[n] = lua_pop(L); reg[n] = lua_pop();
} }
else if EQ("settagmethod") { else if EQ("settagmethod") {
int n = getreg(L, &pc); int n = getreg(&pc);
lua_settagmethod(L, (int)lua_getnumber(L, reg[n]), getname(&pc)); lua_settagmethod((int)lua_getnumber(reg[n]), getname(&pc));
} }
else if EQ("beginblock") { else if EQ("beginblock") {
lua_beginblock(L); lua_beginblock();
} }
else if EQ("endblock") { else if EQ("endblock") {
lua_endblock(L); lua_endblock();
} }
else if EQ("newstate") { else if EQ("newstate") {
int stacksize = getnum(&pc); int stacksize = getnum(&pc);
lua_State *L1 = lua_newstate("stack", stacksize, lua_State *L1 = lua_newstate("stack", stacksize,
"builtin", getnum(&pc), NULL); "builtin", getnum(&pc), NULL);
lua_pushuserdata(L, L1); lua_pushuserdata(L1);
} }
else if EQ("closestate") { else if EQ("closestate") {
lua_close((lua_State *)lua_getuserdata(L, reg[getreg(L, &pc)])); (lua_close)((lua_State *)lua_getuserdata(reg[getreg(&pc)]));
} }
else if EQ("doremote") { else if EQ("doremote") {
lua_Object ol1 = reg[getreg(L, &pc)]; lua_Object ol1 = reg[getreg(&pc)];
lua_Object str = reg[getreg(L, &pc)]; lua_Object str = reg[getreg(&pc)];
lua_State *L1; lua_State *L1;
lua_Object temp; lua_Object temp;
int i; int i;
if (!lua_isuserdata(L, ol1) || !lua_isstring(L, str)) if (!lua_isuserdata(ol1) || !lua_isstring(str))
lua_error(L, "bad arguments for `doremote'"); lua_error("bad arguments for `doremote'");
L1 = (lua_State *)lua_getuserdata(L, ol1); L1 = (lua_State *)lua_getuserdata(ol1);
lua_dostring(L1, lua_getstring(L, str)); (lua_dostring)(L1, lua_getstring(str));
i = 1; i = 1;
while ((temp = lua_getresult(L1, i++)) != LUA_NOOBJECT) while ((temp = (lua_getresult)(L1, i++)) != LUA_NOOBJECT)
lua_pushstring(L, lua_getstring(L1, temp)); lua_pushstring((lua_getstring)(L1, temp));
} }
#if LUA_DEPRECATETFUNCS #if LUA_DEPRECATETFUNCS
else if EQ("rawsetglobal") { else if EQ("rawsetglobal") {
lua_rawsetglobal(L, getname(&pc)); lua_rawsetglobal(getname(&pc));
} }
else if EQ("rawgetglobal") { else if EQ("rawgetglobal") {
int n = getreg(L, &pc); int n = getreg(&pc);
reg[n] = lua_rawgetglobal(L, getname(&pc)); reg[n] = lua_rawgetglobal(getname(&pc));
} }
#endif #endif
else luaL_verror(L, "unknown command in `testC': %.20s", inst); else luaL_verror(lua_state, "unknown command in `testC': %.20s", inst);
} }
} }
@@ -374,18 +374,20 @@ static void testC (lua_State *L) {
static const struct luaL_reg tests_funcs[] = { static const struct luaL_reg tests_funcs[] = {
{"hash", hash_query}, {"hash", (lua_CFunction)hash_query},
{"limits", get_limits}, {"limits", (lua_CFunction)get_limits},
{"listcode", listcode}, {"listcode", (lua_CFunction)listcode},
{"querystr", string_query}, {"querystr", (lua_CFunction)string_query},
{"querytab", table_query}, {"querytab", (lua_CFunction)table_query},
{"testC", testC}, {"testC", (lua_CFunction)testC},
{"totalmem", mem_query} {"totalmem", (lua_CFunction)mem_query}
}; };
void luaB_opentests (lua_State *L) { void luaB_opentests (lua_State *L) {
luaL_openl(L, tests_funcs); if (lua_state != NULL) return; /* do not open tests for auxiliar states */
lua_state = L;
luaL_openl(tests_funcs);
} }
#endif #endif