|
@ -1,5 +1,5 @@ |
|
|
/*
|
|
|
/*
|
|
|
** $Id: ldblib.c,v 1.60 2002/06/18 17:42:52 roberto Exp roberto $ |
|
|
** $Id: ldblib.c,v 1.61 2002/06/25 19:16:44 roberto Exp roberto $ |
|
|
** Interface from Lua to its debug API |
|
|
** Interface from Lua to its debug API |
|
|
** See Copyright Notice in lua.h |
|
|
** See Copyright Notice in lua.h |
|
|
*/ |
|
|
*/ |
|
@ -108,65 +108,70 @@ static int setlocal (lua_State *L) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const char KEY_CALLHOOK = 'c'; |
|
|
static const char KEY_HOOK = 'h'; |
|
|
static const char KEY_LINEHOOK = 'l'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void hookf (lua_State *L, void *key) { |
|
|
static void hookf (lua_State *L, lua_Debug *ar) { |
|
|
lua_pushudataval(L, key); |
|
|
static const char *const hooknames[] = {"call", "return", "line", "count"}; |
|
|
|
|
|
lua_pushudataval(L, (void *)&KEY_HOOK); |
|
|
lua_rawget(L, LUA_REGISTRYINDEX); |
|
|
lua_rawget(L, LUA_REGISTRYINDEX); |
|
|
if (lua_isfunction(L, -1)) { |
|
|
if (lua_isfunction(L, -1)) { |
|
|
lua_pushvalue(L, -2); /* original argument (below function) */ |
|
|
lua_pushstring(L, hooknames[(int)ar->event]); |
|
|
lua_call(L, 1, 0); |
|
|
if (ar->currentline >= 0) lua_pushnumber(L, ar->currentline); |
|
|
|
|
|
else lua_pushnil(L); |
|
|
|
|
|
lua_assert(lua_getinfo(L, "lS", ar)); |
|
|
|
|
|
lua_call(L, 2, 0); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
lua_pop(L, 1); /* pop result from gettable */ |
|
|
lua_pop(L, 1); /* pop result from gettable */ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void callf (lua_State *L, lua_Debug *ar) { |
|
|
static int makemask (const char *smask, int count) { |
|
|
lua_pushstring(L, ar->event); |
|
|
int mask = 0; |
|
|
lua_assert(lua_getinfo(L, "lS", ar) && ar->currentline == -1); |
|
|
if (strchr(smask, 'c')) mask |= LUA_MASKCALL; |
|
|
hookf(L, (void *)&KEY_CALLHOOK); |
|
|
if (strchr(smask, 'r')) mask |= LUA_MASKRET; |
|
|
|
|
|
if (strchr(smask, 'l')) mask |= LUA_MASKLINE; |
|
|
|
|
|
return mask | lua_maskcount(count); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void linef (lua_State *L, lua_Debug *ar) { |
|
|
static char *unmakemask (int mask, char *smask) { |
|
|
lua_pushnumber(L, ar->currentline); |
|
|
int i = 0; |
|
|
lua_assert((ar->currentline = ar->linedefined = -1, |
|
|
if (mask & LUA_MASKCALL) smask[i++] = 'c'; |
|
|
lua_getinfo(L, "lS", ar) && |
|
|
if (mask & LUA_MASKRET) smask[i++] = 'r'; |
|
|
ar->currentline == lua_tonumber(L, -1) && |
|
|
if (mask & LUA_MASKLINE) smask[i++] = 'l'; |
|
|
ar->linedefined >= 0)); |
|
|
smask[i] = '\0'; |
|
|
hookf(L, (void *)&KEY_LINEHOOK); |
|
|
return smask; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void sethook (lua_State *L, void *key, lua_Hook hook, |
|
|
static int sethook (lua_State *L) { |
|
|
lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) { |
|
|
if (lua_isnoneornil(L, 1)) { |
|
|
lua_settop(L, 1); |
|
|
lua_settop(L, 1); |
|
|
if (lua_isnoneornil(L, 1)) |
|
|
lua_sethook(L, NULL, 0); /* turn off hooks */ |
|
|
(*sethookf)(L, NULL); |
|
|
} |
|
|
else if (lua_isfunction(L, 1)) |
|
|
else { |
|
|
(*sethookf)(L, hook); |
|
|
const char *smask = luaL_check_string(L, 2); |
|
|
else |
|
|
int count = luaL_opt_int(L, 3, 0); |
|
|
luaL_argerror(L, 1, "function expected"); |
|
|
luaL_check_type(L, 1, LUA_TFUNCTION); |
|
|
lua_pushudataval(L, key); |
|
|
lua_sethook(L, hookf, makemask(smask, count)); |
|
|
lua_rawget(L, LUA_REGISTRYINDEX); /* get old value */ |
|
|
} |
|
|
lua_pushudataval(L, key); |
|
|
lua_pushudataval(L, (void *)&KEY_HOOK); |
|
|
lua_pushvalue(L, 1); |
|
|
lua_pushvalue(L, 1); |
|
|
lua_rawset(L, LUA_REGISTRYINDEX); /* set new value */ |
|
|
lua_rawset(L, LUA_REGISTRYINDEX); /* set new hook */ |
|
|
} |
|
|
return 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int setcallhook (lua_State *L) { |
|
|
|
|
|
sethook(L, (void *)&KEY_CALLHOOK, callf, lua_setcallhook); |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int setlinehook (lua_State *L) { |
|
|
static int gethook (lua_State *L) { |
|
|
sethook(L, (void *)&KEY_LINEHOOK, linef, lua_setlinehook); |
|
|
char buff[5]; |
|
|
return 1; |
|
|
int mask = lua_gethookmask(L); |
|
|
|
|
|
lua_pushudataval(L, (void *)&KEY_HOOK); |
|
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */ |
|
|
|
|
|
lua_pushstring(L, unmakemask(mask, buff)); |
|
|
|
|
|
lua_pushnumber(L, lua_getmaskcount(mask)); |
|
|
|
|
|
return 3; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -245,8 +250,8 @@ static int errorfb (lua_State *L) { |
|
|
static const luaL_reg dblib[] = { |
|
|
static const luaL_reg dblib[] = { |
|
|
{"getlocal", getlocal}, |
|
|
{"getlocal", getlocal}, |
|
|
{"getinfo", getinfo}, |
|
|
{"getinfo", getinfo}, |
|
|
{"setcallhook", setcallhook}, |
|
|
{"gethook", gethook}, |
|
|
{"setlinehook", setlinehook}, |
|
|
{"sethook", sethook}, |
|
|
{"setlocal", setlocal}, |
|
|
{"setlocal", setlocal}, |
|
|
{"debug", debug}, |
|
|
{"debug", debug}, |
|
|
{"traceback", errorfb}, |
|
|
{"traceback", errorfb}, |
|
|