diff --git a/src/lib_debug.c b/src/lib_debug.c index 740708cf..f6818bbf 100644 --- a/src/lib_debug.c +++ b/src/lib_debug.c @@ -145,9 +145,15 @@ LJLIB_CF(debug_getlocal) lua_State *L1 = getthread(L, &arg); lua_Debug ar; const char *name; + int slot = lj_lib_checkint(L, arg+2); + if (tvisfunc(L->base+arg)) { + L->top = L->base+arg+1; + lua_pushstring(L, lua_getlocal(L, NULL, slot)); + return 1; + } if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar)) lj_err_arg(L, arg+1, LJ_ERR_LVLRNG); - name = lua_getlocal(L1, &ar, lj_lib_checkint(L, arg+2)); + name = lua_getlocal(L1, &ar, slot); if (name) { lua_xmove(L1, L, 1); lua_pushstring(L, name); diff --git a/src/lj_debug.c b/src/lj_debug.c index 85dbac50..d5693779 100644 --- a/src/lj_debug.c +++ b/src/lj_debug.c @@ -401,10 +401,14 @@ void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc) LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n) { const char *name = NULL; - TValue *o = debug_localname(L, ar, &name, (BCReg)n); - if (name) { - copyTV(L, L->top, o); - incr_top(L); + if (ar) { + TValue *o = debug_localname(L, ar, &name, (BCReg)n); + if (name) { + copyTV(L, L->top, o); + incr_top(L); + } + } else if (tvisfunc(L->top-1) && isluafunc(funcV(L->top-1))) { + name = debug_varname(funcproto(funcV(L->top-1)), 0, (BCReg)n-1); } return name; }