better error messages

This commit is contained in:
Roberto Ierusalimschy
2000-06-28 17:21:06 -03:00
parent b622282973
commit 014a09c509
10 changed files with 230 additions and 104 deletions

148
ldebug.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: ldebug.c,v 1.23 2000/06/12 13:52:05 roberto Exp roberto $
** $Id: ldebug.c,v 1.24 2000/06/26 19:28:31 roberto Exp roberto $
** Debug Interface
** See Copyright Notice in lua.h
*/
@@ -13,10 +13,12 @@
#include "lapi.h"
#include "lauxlib.h"
#include "lcode.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lstate.h"
#include "ltable.h"
#include "ltm.h"
@@ -97,6 +99,13 @@ static int lua_nups (StkId f) {
}
static int lua_currentpc (StkId f) {
CallInfo *ci = infovalue(f);
LUA_ASSERT(L, ttype(f) == TAG_LMARK, "function has no pc");
return (*ci->pc - 1) - ci->func->f.l->code;
}
static int lua_currentline (StkId f) {
if (ttype(f) != TAG_LMARK)
return -1; /* only active lua functions have current-line information */
@@ -104,15 +113,11 @@ static int lua_currentline (StkId f) {
CallInfo *ci = infovalue(f);
int *lines = ci->func->f.l->lines;
if (!lines) return -1; /* no static debug information */
else return lines[ci->pc];
else return lines[lua_currentpc(f)];
}
}
static int lua_currentpc (StkId f) {
return infovalue(f)->pc;
}
static Proto *getluaproto (StkId f) {
return (ttype(f) == TAG_LMARK) ? infovalue(f)->func->f.l : NULL;
@@ -225,17 +230,136 @@ int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
}
/*
** {======================================================
** Symbolic Execution
** =======================================================
*/
static void call_index_error (lua_State *L, TObject *o, const char *v) {
luaL_verror(L, "attempt to %.10s a %.10s value", v, lua_type(L, o));
static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
int stack[MAXSTACK]; /* stores last instruction that changes each value */
const Instruction *code = pt->code;
int top = pt->numparams;
int pc = 0;
if (pt->is_vararg) /* varargs? */
top++; /* `arg' */
while (pc < lastpc) {
const Instruction i = code[pc++];
switch (GET_OPCODE(i)) {
case OP_CALL: {
int nresults = GETARG_B(i);
if (nresults == MULT_RET) nresults = 1;
top = GETARG_A(i);
while (nresults--)
stack[top++] = pc-1;
break;
}
case OP_PUSHNIL: {
int n;
for (n=0; n<GETARG_U(i); n++)
stack[top++] = pc-1;
break;
}
case OP_POP: {
top -= GETARG_U(i);
break;
}
case OP_SETTABLE:
case OP_SETLIST: {
top -= GETARG_B(i);
break;
}
case OP_SETMAP: {
top -= 2*GETARG_U(i);
break;
}
case OP_CONCAT: {
top -= GETARG_U(i);
stack[top++] = pc-1;
break;
}
case OP_JMPONT:
case OP_JMPONF: {
int newpc = pc + GETARG_S(i);
if (newpc >= lastpc) {
stack[top-1] = pc-1; /* value generated by or-and */
pc = newpc; /* do the jump */
}
else
top--; /* original code did not jump; condition was false */
break;
}
case OP_PUSHNILJMP: {
break; /* do not `push', to compensate next instruction */
}
case OP_CLOSURE: {
top -= GETARG_B(i);
stack[top++] = pc-1;
break;
}
default: {
int n;
LUA_ASSERT(NULL, luaK_opproperties[GET_OPCODE(i)].push != VD,
"invalid opcode for default");
top -= luaK_opproperties[GET_OPCODE(i)].pop;
for (n=0; n<luaK_opproperties[GET_OPCODE(i)].push; n++)
stack[top++] = pc-1;
}
}
}
return code[stack[stackpos]];
}
void luaG_callerror (lua_State *L, TObject *func) {
call_index_error(L, func, "call");
static const char *getname (lua_State *L, StkId obj, const char **name) {
StkId func = aux_stackedfunction(L, 0, obj);
if (func == NULL || ttype(func) != TAG_LMARK)
return NULL; /* not a Lua function */
else {
Proto *p = infovalue(func)->func->f.l;
int pc = lua_currentpc(func);
int stackpos = obj - (func+1); /* func+1 == function base */
Instruction i = luaG_symbexec(p, pc, stackpos);
switch (GET_OPCODE(i)) {
case OP_GETGLOBAL: {
*name = p->kstr[GETARG_U(i)]->str;
return "global";
}
case OP_GETLOCAL: {
*name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
return (*name) ? "local" : NULL;
}
case OP_PUSHSELF:
case OP_GETDOTTED: {
*name = p->kstr[GETARG_U(i)]->str;
return "field";
}
default:
return NULL; /* no usefull name found */
}
}
}
void luaG_indexerror (lua_State *L, TObject *t) {
call_index_error(L, t, "index");
/* }====================================================== */
static void call_index_error (lua_State *L, StkId o, const char *op,
const char *tp) {
const char *name;
const char *kind = getname(L, o, &name);
if (kind)
luaL_verror(L, "%s `%s' is not a %s", kind, name, tp);
else
luaL_verror(L, "attempt to %.10s a %.10s value", op, lua_type(L, o));
}
void luaG_callerror (lua_State *L, StkId func) {
call_index_error(L, func, "call", "function");
}
void luaG_indexerror (lua_State *L, StkId t) {
call_index_error(L, t, "index", "table");
}