From 6532be8730474d4cbfd8fbc706e74120512b760b Mon Sep 17 00:00:00 2001 From: farteryhr Date: Mon, 8 Oct 2018 10:57:32 +0800 Subject: [PATCH] modded debug.getlocal and upvalue related Signed-off-by: farteryhr --- lapi.c | 20 ++++++++++---------- ldebug.c | 8 ++++---- lfunc.c | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lapi.c b/lapi.c index c9455a5..b07d3c4 100644 --- a/lapi.c +++ b/lapi.c @@ -1198,8 +1198,8 @@ static const char *aux_upvalue (StkId fi, int n, TValue **val, switch (ttype(fi)) { case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); - if (!(1 <= n && n <= f->nupvalues)) return NULL; - *val = &f->upvalue[n-1]; + if (!(0 <= n && n < f->nupvalues)) return NULL; + *val = &f->upvalue[n]; if (owner) *owner = f; return ""; } @@ -1207,10 +1207,10 @@ static const char *aux_upvalue (StkId fi, int n, TValue **val, LClosure *f = clLvalue(fi); TString *name; Proto *p = f->p; - if (!(1 <= n && n <= p->sizeupvalues)) return NULL; - *val = f->upvals[n-1]->v; - if (uv) *uv = f->upvals[n - 1]; - name = p->upvalues[n-1].name; + if (!(0 <= n && n < p->sizeupvalues)) return NULL; + *val = f->upvals[n]->v; + if (uv) *uv = f->upvals[n]; + name = p->upvalues[n].name; return (name == NULL) ? "(*no name)" : getstr(name); } default: return NULL; /* not a closure */ @@ -1258,9 +1258,9 @@ static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { StkId fi = index2addr(L, fidx); api_check(L, ttisLclosure(fi), "Lua function expected"); f = clLvalue(fi); - api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); + api_check(L, (0 <= n && n < f->p->sizeupvalues), "invalid upvalue index"); if (pf) *pf = f; - return &f->upvals[n - 1]; /* get its upvalue pointer */ + return &f->upvals[n]; /* get its upvalue pointer */ } @@ -1272,8 +1272,8 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { } case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); - api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); - return &f->upvalue[n - 1]; + api_check(L, 0 <= n && n < f->nupvalues, "invalid upvalue index"); + return &f->upvalue[n]; } default: { api_check(L, 0, "closure expected"); diff --git a/ldebug.c b/ldebug.c index 239affb..e2e6f52 100644 --- a/ldebug.c +++ b/ldebug.c @@ -158,12 +158,12 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, base = ci->func + 1; if (name == NULL) { /* no 'standard' name? */ StkId limit = (ci == L->ci) ? L->top : ci->next->func; - if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */ + if (limit - base > n && n >= 0) /* is 'n' inside 'ci' stack? */ name = "(*temporary)"; /* generic name for any valid slot */ else return NULL; /* no name */ } - *pos = base + (n - 1); + *pos = base + n; return name; } @@ -431,7 +431,7 @@ static int findsetreg (Proto *p, int lastpc, int reg) { static const char *getobjname (Proto *p, int lastpc, int reg, const char **name) { int pc; - *name = luaF_getlocalname(p, reg + 1, lastpc); + *name = luaF_getlocalname(p, reg, lastpc); if (*name) /* is a local? */ return "local"; /* else try symbolic execution */ @@ -451,7 +451,7 @@ static const char *getobjname (Proto *p, int lastpc, int reg, int k = GETARG_C(i); /* key index */ int t = GETARG_B(i); /* table index */ const char *vn = (op == OP_GETTABLE) /* name of indexed variable */ - ? luaF_getlocalname(p, t + 1, pc) + ? luaF_getlocalname(p, t, pc) : upvalname(p, t); kname(p, pc, k, name); return (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field"; diff --git a/lfunc.c b/lfunc.c index 67967da..a9e71ec 100644 --- a/lfunc.c +++ b/lfunc.c @@ -141,9 +141,9 @@ const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { int i; for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { if (pc < f->locvars[i].endpc) { /* is variable active? */ - local_number--; if (local_number == 0) return getstr(f->locvars[i].varname); + local_number--; } } return NULL; /* not found */