From daf09c476fe375d8a9a47bc1294a1cd430290226 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 10 Aug 2000 16:50:47 -0300 Subject: still better error messages --- lcode.c | 5 ++-- ldebug.c | 84 +++++++++++++++++++++++++++++++--------------------------------- ldebug.h | 6 ++--- ldo.c | 4 ++-- lvm.c | 34 +++++++++++++++----------- 5 files changed, 67 insertions(+), 66 deletions(-) diff --git a/lcode.c b/lcode.c index 08068f01..33236931 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 1.45 2000/08/09 14:49:13 roberto Exp roberto $ +** $Id: lcode.c,v 1.46 2000/08/09 19:16:57 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -333,7 +333,6 @@ void luaK_tostack (LexState *ls, expdesc *v, int onlyone) { luaK_deltastack(fs, -1); /* next PUSHes may be skipped */ } p_nil = code_label(fs, OP_PUSHNILJMP, 0); - luaK_deltastack(fs, -1); /* next PUSH is skipped */ p_1 = code_label(fs, OP_PUSHINT, 1); luaK_patchlist(fs, j, luaK_getlabel(fs)); } @@ -690,7 +689,7 @@ const struct OpProperties luaK_opproperties[NUM_OPCODES] = { {iS, 0, 1}, /* OP_JMPONT */ {iS, 0, 1}, /* OP_JMPONF */ {iS, 0, 0}, /* OP_JMP */ - {iO, 1, 0}, /* OP_PUSHNILJMP */ + {iO, 0, 0}, /* OP_PUSHNILJMP */ {iS, 0, 0}, /* OP_FORPREP */ {iS, 0, 3}, /* OP_FORLOOP */ {iS, 3, 0}, /* OP_LFORPREP */ diff --git a/ldebug.c b/ldebug.c index 57d01b49..e81661e2 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 1.30 2000/08/08 20:42:07 roberto Exp roberto $ +** $Id: ldebug.c,v 1.31 2000/08/09 19:16:57 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -57,7 +57,7 @@ lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) { static StkId aux_stackedfunction (lua_State *L, int level, StkId top) { int i; - for (i = (top-1)-L->stack; i>=0; i--) { + for (i = (top-1) - L->stack; i>=0; i--) { if (is_T_MARK(L->stack[i].ttype)) { if (level == 0) return L->stack+i; @@ -214,7 +214,7 @@ static const char *travglobals (lua_State *L, const TObject *o) { } -static void lua_getobjname (lua_State *L, StkId f, lua_Debug *ar) { +static void lua_getname (lua_State *L, StkId f, lua_Debug *ar) { TObject o; setnormalized(&o, f); /* try to find a name for given function */ @@ -247,7 +247,7 @@ int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { ar->nups = lua_nups(func); break; case 'n': - lua_getobjname(L, func, ar); + lua_getname(L, func, ar); break; case 'f': setnormalized(L->top, func); @@ -267,8 +267,16 @@ int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { ** ======================================================= */ + +static int pushpc (int *stack, int pc, int top, int n) { + while (n--) + stack[top++] = pc-1; + return top; +} + + static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) { - int stack[MAXSTACK]; /* stores last instruction that changes each value */ + int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */ const Instruction *code = pt->code; int top = pt->numparams; int pc = 0; @@ -276,7 +284,7 @@ static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) { top++; /* `arg' */ while (pc < lastpc) { const Instruction i = code[pc++]; - LUA_ASSERT(top <= pt->maxstacksize, "wrong stack"); + LUA_ASSERT(0 <= top && top <= pt->maxstacksize, "wrong stack"); switch (GET_OPCODE(i)) { case OP_RETURN: { LUA_ASSERT(top >= GETARG_U(i), "wrong stack"); @@ -287,9 +295,7 @@ static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) { int nresults = GETARG_B(i); if (nresults == MULT_RET) nresults = 1; LUA_ASSERT(top >= GETARG_A(i), "wrong stack"); - top = GETARG_A(i); - while (nresults--) - stack[top++] = pc-1; + top = pushpc(stack, pc, GETARG_A(i), nresults); break; } case OP_TAILCALL: { @@ -298,9 +304,7 @@ static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) { break; } case OP_PUSHNIL: { - int n; - for (n=0; n= 0, "wrong stack"); - for (n=0; ntop = top; - luaG_indexerror(L, table); + luaG_typeerror(L, table, "index"); } } else { /* object is a table... */ @@ -156,7 +156,7 @@ void luaV_settable (lua_State *L, StkId t, StkId top) { L->top = top; im = luaT_getimbyObj(L, t, IM_SETTABLE); if (ttype(im) == TAG_NIL) - luaG_indexerror(L, t); + luaG_typeerror(L, t, "index"); } else { /* object is a table... */ im = luaT_getim(L, hvalue(t)->htag, IM_SETTABLE); @@ -223,7 +223,7 @@ void luaV_setglobal (lua_State *L, TString *s, StkId top) { } -static void call_binTM (lua_State *L, StkId top, IMS event, const char *msg) { +static int call_binTM (lua_State *L, StkId top, IMS event) { /* try first operand */ const TObject *im = luaT_getimbyObj(L, top-2, event); L->top = top; @@ -232,16 +232,18 @@ static void call_binTM (lua_State *L, StkId top, IMS event, const char *msg) { if (ttype(im) == TAG_NIL) { im = luaT_getim(L, 0, event); /* try a `global' method */ if (ttype(im) == TAG_NIL) - lua_error(L, msg); + return 0; /* error */ } } lua_pushstring(L, luaT_eventname[event]); luaD_callTM(L, im, 3, 1); + return 1; } static void call_arith (lua_State *L, StkId top, IMS event) { - call_binTM(L, top, event, "unexpected type in arithmetic operation"); + if (!call_binTM(L, top, event)) + luaG_binerror(L, top-2, TAG_NUMBER, "perform arithmetic on"); } @@ -276,7 +278,8 @@ int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) luaD_checkstack(L, 2); *top++ = *l; *top++ = *r; - call_binTM(L, top, IM_LT, "unexpected type in comparison"); + if (!call_binTM(L, top, IM_LT)) + lua_error(L, "unexpected type in comparison"); L->top--; return (ttype(L->top) != TAG_NIL); } @@ -286,8 +289,10 @@ int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) static void strconc (lua_State *L, int total, StkId top) { do { int n = 2; /* number of elements handled in this pass (at least 2) */ - if (tostring(L, top-2) || tostring(L, top-1)) - call_binTM(L, top, IM_CONCAT, "unexpected type for concatenation"); + if (tostring(L, top-2) || tostring(L, top-1)) { + if (!call_binTM(L, top, IM_CONCAT)) + luaG_binerror(L, top-2, TAG_STRING, "concat"); + } else if (tsvalue(top-1)->u.s.len > 0) { /* if len=0, do nothing */ /* at least two string values; get as many as possible */ lint32 tl = (lint32)tsvalue(top-1)->u.s.len + @@ -508,7 +513,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { } case OP_ADD: - if (tonumber(top-1) || tonumber(top-2)) + if (tonumber(top-2) || tonumber(top-1)) call_arith(L, top, IM_ADD); else nvalue(top-2) += nvalue(top-1); @@ -526,7 +531,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; case OP_SUB: - if (tonumber(top-1) || tonumber(top-2)) + if (tonumber(top-2) || tonumber(top-1)) call_arith(L, top, IM_SUB); else nvalue(top-2) -= nvalue(top-1); @@ -534,7 +539,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; case OP_MULT: - if (tonumber(top-1) || tonumber(top-2)) + if (tonumber(top-2) || tonumber(top-1)) call_arith(L, top, IM_MUL); else nvalue(top-2) *= nvalue(top-1); @@ -542,7 +547,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; case OP_DIV: - if (tonumber(top-1) || tonumber(top-2)) + if (tonumber(top-2) || tonumber(top-1)) call_arith(L, top, IM_DIV); else nvalue(top-2) /= nvalue(top-1); @@ -550,7 +555,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; case OP_POW: - call_binTM(L, top, IM_POW, "undefined operation"); + if (!call_binTM(L, top, IM_POW)) + lua_error(L, "undefined operation"); top--; break; -- cgit v1.2.3-55-g6feb