1999-12-14 21:31:20 +03:00
|
|
|
/*
|
2000-05-26 23:17:57 +04:00
|
|
|
** $Id: ltests.c,v 1.20 2000/05/22 18:44:46 roberto Exp roberto $
|
1999-12-14 21:31:20 +03:00
|
|
|
** Internal Module for Debugging of the Lua Implementation
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2000-04-06 21:35:23 +04:00
|
|
|
#include <stdio.h>
|
1999-12-14 21:31:20 +03:00
|
|
|
#include <stdlib.h>
|
2000-02-21 21:30:06 +03:00
|
|
|
#include <string.h>
|
1999-12-14 21:31:20 +03:00
|
|
|
|
|
|
|
#define LUA_REENTRANT
|
|
|
|
|
|
|
|
#include "lapi.h"
|
|
|
|
#include "lauxlib.h"
|
2000-05-22 22:44:46 +04:00
|
|
|
#include "lcode.h"
|
2000-05-10 20:33:20 +04:00
|
|
|
#include "ldo.h"
|
1999-12-14 21:31:20 +03:00
|
|
|
#include "lmem.h"
|
2000-04-06 21:35:23 +04:00
|
|
|
#include "lopcodes.h"
|
1999-12-14 21:31:20 +03:00
|
|
|
#include "lstate.h"
|
|
|
|
#include "lstring.h"
|
|
|
|
#include "ltable.h"
|
|
|
|
#include "lua.h"
|
1999-12-23 21:19:57 +03:00
|
|
|
#include "luadebug.h"
|
1999-12-14 21:31:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
void luaB_opentests (lua_State *L);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The whole module only makes sense with DEBUG on
|
|
|
|
*/
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
|
|
|
|
static void setnameval (lua_State *L, lua_Object t, const char *name, int val) {
|
|
|
|
lua_pushobject(L, t);
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
lua_pushnumber(L, val);
|
|
|
|
lua_settable(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** Disassembler
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-05-22 22:44:46 +04:00
|
|
|
static const char *const instrname[OP_SETLINE+1] = {
|
|
|
|
"END", "RETURN", "CALL", "TAILCALL", "PUSHNIL", "POP", "PUSHINT",
|
|
|
|
"PUSHSTRING", "PUSHNUM", "PUSHNEGNUM", "PUSHUPVALUE", "GETLOCAL",
|
|
|
|
"GETGLOBAL", "GETTABLE", "GETDOTTED", "GETINDEXED", "PUSHSELF",
|
|
|
|
"CREATETABLE", "SETLOCAL", "SETGLOBAL", "SETTABLE", "SETLIST", "SETMAP",
|
|
|
|
"ADD", "ADDI", "SUB", "MULT", "DIV", "POW", "CONCAT", "MINUS", "NOT",
|
|
|
|
"JMPNE", "JMPEQ", "JMPLT", "JMPLE", "JMPGT", "JMPGE", "JMPT", "JMPF",
|
|
|
|
"JMPONT", "JMPONF", "JMP", "PUSHNILJMP", "FORPREP", "FORLOOP", "LFORPREP",
|
|
|
|
"LFORLOOP", "CLOSURE", "SETLINE"
|
|
|
|
};
|
2000-04-06 21:35:23 +04:00
|
|
|
|
|
|
|
|
2000-04-14 21:46:15 +04:00
|
|
|
static int pushop (lua_State *L, Instruction i) {
|
2000-04-06 21:35:23 +04:00
|
|
|
char buff[100];
|
2000-05-22 22:44:46 +04:00
|
|
|
OpCode o = GET_OPCODE(i);
|
|
|
|
const char *name = instrname[o];
|
|
|
|
switch ((enum Mode)luaK_opproperties[o].mode) {
|
|
|
|
case iO:
|
|
|
|
sprintf(buff, "%s", name);
|
|
|
|
break;
|
|
|
|
case iU:
|
|
|
|
sprintf(buff, "%-12s%4u", name, GETARG_U(i));
|
|
|
|
break;
|
|
|
|
case iS:
|
|
|
|
sprintf(buff, "%-12s%4d", name, GETARG_S(i));
|
|
|
|
break;
|
|
|
|
case iAB:
|
|
|
|
sprintf(buff, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
|
|
|
|
break;
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
|
|
|
lua_pushstring(L, buff);
|
2000-05-22 22:44:46 +04:00
|
|
|
return (o != OP_END);
|
2000-04-06 21:35:23 +04:00
|
|
|
}
|
|
|
|
|
2000-05-22 22:44:46 +04:00
|
|
|
|
2000-04-14 21:46:15 +04:00
|
|
|
static void listcode (lua_State *L) {
|
2000-04-06 21:35:23 +04:00
|
|
|
lua_Object o = luaL_nonnullarg(L, 1);
|
|
|
|
lua_Object t = lua_createtable(L);
|
|
|
|
Instruction *pc;
|
|
|
|
Proto *p;
|
|
|
|
int res;
|
|
|
|
luaL_arg_check(L, ttype(o) == TAG_LCLOSURE, 1, "Lua function expected");
|
|
|
|
p = clvalue(o)->f.l;
|
|
|
|
setnameval(L, t, "maxstack", p->maxstacksize);
|
|
|
|
setnameval(L, t, "numparams", p->numparams);
|
|
|
|
pc = p->code;
|
|
|
|
do {
|
|
|
|
lua_pushobject(L, t);
|
|
|
|
lua_pushnumber(L, pc - p->code + 1);
|
2000-04-14 21:46:15 +04:00
|
|
|
res = pushop(L, *pc++);
|
2000-04-06 21:35:23 +04:00
|
|
|
lua_settable(L);
|
|
|
|
} while (res);
|
|
|
|
lua_pushobject(L, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void get_limits (lua_State *L) {
|
|
|
|
lua_Object t = lua_createtable(L);
|
|
|
|
setnameval(L, t, "SIZE_OP", SIZE_OP);
|
|
|
|
setnameval(L, t, "SIZE_U", SIZE_U);
|
|
|
|
setnameval(L, t, "SIZE_A", SIZE_A);
|
|
|
|
setnameval(L, t, "SIZE_B", SIZE_B);
|
|
|
|
setnameval(L, t, "MAXARG_U", MAXARG_U);
|
|
|
|
setnameval(L, t, "MAXARG_S", MAXARG_S);
|
|
|
|
setnameval(L, t, "MAXARG_A", MAXARG_A);
|
|
|
|
setnameval(L, t, "MAXARG_B", MAXARG_B);
|
|
|
|
setnameval(L, t, "MAXSTACK", MAXSTACK);
|
|
|
|
setnameval(L, t, "MAXLOCALS", MAXLOCALS);
|
|
|
|
setnameval(L, t, "MAXUPVALUES", MAXUPVALUES);
|
|
|
|
setnameval(L, t, "MAXVARSLH", MAXVARSLH);
|
|
|
|
setnameval(L, t, "MAXPARAMS", MAXPARAMS);
|
|
|
|
setnameval(L, t, "LFPF", LFIELDS_PER_FLUSH);
|
|
|
|
setnameval(L, t, "RFPF", RFIELDS_PER_FLUSH);
|
|
|
|
lua_pushobject(L, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-14 21:31:20 +03:00
|
|
|
static void mem_query (lua_State *L) {
|
2000-01-13 19:30:47 +03:00
|
|
|
lua_pushnumber(L, memdebug_total);
|
|
|
|
lua_pushnumber(L, memdebug_numblocks);
|
2000-03-16 23:35:07 +03:00
|
|
|
lua_pushnumber(L, memdebug_maxmem);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void hash_query (lua_State *L) {
|
|
|
|
lua_Object o = luaL_nonnullarg(L, 1);
|
|
|
|
if (lua_getparam(L, 2) == LUA_NOOBJECT) {
|
2000-03-10 21:37:44 +03:00
|
|
|
luaL_arg_check(L, ttype(o) == TAG_STRING, 1, "string expected");
|
2000-05-08 23:32:53 +04:00
|
|
|
lua_pushnumber(L, tsvalue(o)->u.s.hash);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const Hash *t = avalue(luaL_tablearg(L, 2));
|
|
|
|
lua_pushnumber(L, luaH_mainposition(t, o) - t->node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void table_query (lua_State *L) {
|
|
|
|
const Hash *t = avalue(luaL_tablearg(L, 1));
|
|
|
|
int i = luaL_opt_int(L, 2, -1);
|
|
|
|
if (i == -1) {
|
|
|
|
lua_pushnumber(L, t->size);
|
|
|
|
lua_pushnumber(L, t->firstfree - t->node);
|
|
|
|
}
|
|
|
|
else if (i < t->size) {
|
|
|
|
luaA_pushobject(L, &t->node[i].key);
|
|
|
|
luaA_pushobject(L, &t->node[i].val);
|
|
|
|
if (t->node[i].next)
|
|
|
|
lua_pushnumber(L, t->node[i].next - t->node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
static void string_query (lua_State *L) {
|
2000-05-10 20:33:20 +04:00
|
|
|
stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &L->strt : &L->udt;
|
1999-12-14 21:31:20 +03:00
|
|
|
int s = luaL_opt_int(L, 2, 0) - 1;
|
|
|
|
if (s==-1) {
|
2000-05-10 20:33:20 +04:00
|
|
|
lua_pushnumber(L, tb->nuse);
|
|
|
|
lua_pushnumber(L, tb->size);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
2000-05-10 20:33:20 +04:00
|
|
|
else if (s < tb->size) {
|
|
|
|
TString *ts;
|
|
|
|
for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
|
|
|
|
ttype(L->top) = TAG_STRING;
|
|
|
|
tsvalue(L->top) = ts;
|
|
|
|
incr_top;
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** function to test the API with C. It interprets a kind of "assembler"
|
|
|
|
** language with calls to the API, so the test can be driven by Lua code
|
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char *const delimits = " \t\n,;";
|
1999-12-14 21:31:20 +03:00
|
|
|
|
|
|
|
static void skip (const char **pc) {
|
|
|
|
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getnum (const char **pc) {
|
|
|
|
int res = 0;
|
|
|
|
skip(pc);
|
|
|
|
while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getreg (lua_State *L, const char **pc) {
|
|
|
|
skip(pc);
|
|
|
|
if (*(*pc)++ != 'r') lua_error(L, "`testC' expecting a register");
|
|
|
|
return getnum(pc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *getname (const char **pc) {
|
|
|
|
static char buff[30];
|
|
|
|
int i = 0;
|
|
|
|
skip(pc);
|
|
|
|
while (**pc != '\0' && !strchr(delimits, **pc))
|
|
|
|
buff[i++] = *(*pc)++;
|
|
|
|
buff[i] = '\0';
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define EQ(s1) (strcmp(s1, inst) == 0)
|
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
|
1999-12-14 21:31:20 +03:00
|
|
|
static void testC (lua_State *L) {
|
|
|
|
lua_Object reg[10];
|
|
|
|
const char *pc = luaL_check_string(L, 1);
|
|
|
|
for (;;) {
|
|
|
|
const char *inst = getname(&pc);
|
|
|
|
if EQ("") return;
|
|
|
|
else if EQ("pushnum") {
|
|
|
|
lua_pushnumber(L, getnum(&pc));
|
|
|
|
}
|
|
|
|
else if EQ("createtable") {
|
|
|
|
reg[getreg(L, &pc)] = lua_createtable(L);
|
|
|
|
}
|
|
|
|
else if EQ("closure") {
|
|
|
|
lua_CFunction f = lua_getcfunction(L, lua_getglobal(L, getname(&pc)));
|
|
|
|
lua_pushcclosure(L, f, getnum(&pc));
|
|
|
|
}
|
|
|
|
else if EQ("pop") {
|
|
|
|
reg[getreg(L, &pc)] = lua_pop(L);
|
|
|
|
}
|
|
|
|
else if EQ("getglobal") {
|
|
|
|
int n = getreg(L, &pc);
|
|
|
|
reg[n] = lua_getglobal(L, getname(&pc));
|
|
|
|
}
|
|
|
|
else if EQ("ref") {
|
|
|
|
lua_pushnumber(L, lua_ref(L, 0));
|
|
|
|
reg[getreg(L, &pc)] = lua_pop(L);
|
|
|
|
}
|
|
|
|
else if EQ("reflock") {
|
|
|
|
lua_pushnumber(L, lua_ref(L, 1));
|
|
|
|
reg[getreg(L, &pc)] = lua_pop(L);
|
|
|
|
}
|
|
|
|
else if EQ("getref") {
|
|
|
|
int n = getreg(L, &pc);
|
|
|
|
reg[n] = lua_getref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
|
|
|
|
}
|
|
|
|
else if EQ("unref") {
|
|
|
|
lua_unref(L, (int)lua_getnumber(L, reg[getreg(L, &pc)]));
|
|
|
|
}
|
|
|
|
else if EQ("getparam") {
|
|
|
|
int n = getreg(L, &pc);
|
1999-12-27 20:33:22 +03:00
|
|
|
reg[n] = lua_getparam(L, getnum(&pc)+1); /* skips the command itself */
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
else if EQ("getresult") {
|
|
|
|
int n = getreg(L, &pc);
|
|
|
|
reg[n] = lua_getparam(L, getnum(&pc));
|
|
|
|
}
|
|
|
|
else if EQ("setglobal") {
|
|
|
|
lua_setglobal(L, getname(&pc));
|
|
|
|
}
|
2000-05-26 23:17:57 +04:00
|
|
|
else if EQ("pushglobals") {
|
|
|
|
lua_pushglobaltable(L);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
else if EQ("pushstring") {
|
|
|
|
lua_pushstring(L, getname(&pc));
|
|
|
|
}
|
|
|
|
else if EQ("pushreg") {
|
|
|
|
lua_pushobject(L, reg[getreg(L, &pc)]);
|
|
|
|
}
|
|
|
|
else if EQ("call") {
|
2000-01-24 23:11:26 +03:00
|
|
|
if (lua_call(L, getname(&pc))) lua_error(L, NULL);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
else if EQ("gettable") {
|
|
|
|
reg[getreg(L, &pc)] = lua_gettable(L);
|
|
|
|
}
|
2000-05-26 23:17:57 +04:00
|
|
|
else if EQ("rawget") {
|
|
|
|
reg[getreg(L, &pc)] = lua_rawget(L);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
else if EQ("settable") {
|
|
|
|
lua_settable(L);
|
|
|
|
}
|
2000-05-26 23:17:57 +04:00
|
|
|
else if EQ("rawset") {
|
|
|
|
lua_rawset(L);
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
1999-12-23 21:19:57 +03:00
|
|
|
else if EQ("tag") {
|
|
|
|
lua_pushnumber(L, lua_tag(L, reg[getreg(L, &pc)]));
|
|
|
|
}
|
|
|
|
else if EQ("type") {
|
|
|
|
lua_pushstring(L, lua_type(L, reg[getreg(L, &pc)]));
|
|
|
|
}
|
1999-12-14 21:31:20 +03:00
|
|
|
else if EQ("next") {
|
|
|
|
int n = getreg(L, &pc);
|
|
|
|
n = lua_next(L, reg[n], (int)lua_getnumber(L, reg[getreg(L, &pc)]));
|
|
|
|
lua_pushnumber(L, n);
|
|
|
|
}
|
|
|
|
else if EQ("equal") {
|
|
|
|
int n1 = getreg(L, &pc);
|
|
|
|
int n2 = getreg(L, &pc);
|
|
|
|
lua_pushnumber(L, lua_equal(L, reg[n1], reg[n2]));
|
|
|
|
}
|
|
|
|
else if EQ("pushusertag") {
|
|
|
|
int val = getreg(L, &pc);
|
|
|
|
int tag = getreg(L, &pc);
|
|
|
|
lua_pushusertag(L, (void *)(int)lua_getnumber(L, reg[val]),
|
|
|
|
(int)lua_getnumber(L, reg[tag]));
|
|
|
|
}
|
|
|
|
else if EQ("udataval") {
|
|
|
|
int n = getreg(L, &pc);
|
|
|
|
lua_pushnumber(L, (int)lua_getuserdata(L, reg[getreg(L, &pc)]));
|
|
|
|
reg[n] = lua_pop(L);
|
|
|
|
}
|
|
|
|
else if EQ("settagmethod") {
|
|
|
|
int n = getreg(L, &pc);
|
|
|
|
lua_settagmethod(L, (int)lua_getnumber(L, reg[n]), getname(&pc));
|
|
|
|
}
|
|
|
|
else if EQ("beginblock") {
|
|
|
|
lua_beginblock(L);
|
|
|
|
}
|
|
|
|
else if EQ("endblock") {
|
|
|
|
lua_endblock(L);
|
|
|
|
}
|
|
|
|
else if EQ("newstate") {
|
|
|
|
int stacksize = getnum(&pc);
|
|
|
|
lua_State *L1 = lua_newstate("stack", stacksize,
|
|
|
|
"builtin", getnum(&pc), NULL);
|
|
|
|
lua_pushuserdata(L, L1);
|
|
|
|
}
|
|
|
|
else if EQ("closestate") {
|
2000-02-08 19:39:42 +03:00
|
|
|
lua_close((lua_State *)lua_getuserdata(L, reg[getreg(L, &pc)]));
|
1999-12-14 21:31:20 +03:00
|
|
|
}
|
|
|
|
else if EQ("doremote") {
|
|
|
|
lua_Object ol1 = reg[getreg(L, &pc)];
|
|
|
|
lua_Object str = reg[getreg(L, &pc)];
|
|
|
|
lua_State *L1;
|
|
|
|
lua_Object temp;
|
|
|
|
int i;
|
|
|
|
if (!lua_isuserdata(L, ol1) || !lua_isstring(L, str))
|
|
|
|
lua_error(L, "bad arguments for `doremote'");
|
2000-02-08 19:39:42 +03:00
|
|
|
L1 = (lua_State *)lua_getuserdata(L, ol1);
|
1999-12-14 21:31:20 +03:00
|
|
|
lua_dostring(L1, lua_getstring(L, str));
|
|
|
|
i = 1;
|
|
|
|
while ((temp = lua_getresult(L1, i++)) != LUA_NOOBJECT)
|
|
|
|
lua_pushstring(L, lua_getstring(L1, temp));
|
|
|
|
}
|
2000-05-26 23:17:57 +04:00
|
|
|
#if LUA_DEPRECATETFUNCS
|
|
|
|
else if EQ("rawsetglobal") {
|
|
|
|
lua_rawsetglobal(L, getname(&pc));
|
|
|
|
}
|
|
|
|
else if EQ("rawgetglobal") {
|
|
|
|
int n = getreg(L, &pc);
|
|
|
|
reg[n] = lua_rawgetglobal(L, getname(&pc));
|
|
|
|
}
|
|
|
|
#endif
|
1999-12-14 21:31:20 +03:00
|
|
|
else luaL_verror(L, "unknown command in `testC': %.20s", inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-06 21:35:23 +04:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
1999-12-14 21:31:20 +03:00
|
|
|
|
|
|
|
static const struct luaL_reg tests_funcs[] = {
|
|
|
|
{"hash", hash_query},
|
2000-04-06 21:35:23 +04:00
|
|
|
{"limits", get_limits},
|
2000-04-14 21:46:15 +04:00
|
|
|
{"listcode", listcode},
|
2000-04-06 21:35:23 +04:00
|
|
|
{"querystr", string_query},
|
1999-12-14 21:31:20 +03:00
|
|
|
{"querytab", table_query},
|
|
|
|
{"testC", testC},
|
|
|
|
{"totalmem", mem_query}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void luaB_opentests (lua_State *L) {
|
|
|
|
luaL_openl(L, tests_funcs);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|