diff options
Diffstat (limited to 'ldblib.c')
-rw-r--r-- | ldblib.c | 24 |
1 files changed, 12 insertions, 12 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldblib.c,v 1.144 2014/10/29 16:12:30 roberto Exp roberto $ | 2 | ** $Id: ldblib.c,v 1.145 2014/11/02 19:19:04 roberto Exp roberto $ |
3 | ** Interface from Lua to its debug API | 3 | ** Interface from Lua to its debug API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -20,7 +20,11 @@ | |||
20 | #include "lualib.h" | 20 | #include "lualib.h" |
21 | 21 | ||
22 | 22 | ||
23 | #define HOOKKEY "_HKEY" | 23 | /* |
24 | ** The hook table at registry[&HOOKKEY] maps threads to their current | ||
25 | ** hook function. (We only need the unique address of 'HOOKKEY'.) | ||
26 | */ | ||
27 | static const int HOOKKEY = 0; | ||
24 | 28 | ||
25 | 29 | ||
26 | static int db_getregistry (lua_State *L) { | 30 | static int db_getregistry (lua_State *L) { |
@@ -273,20 +277,13 @@ static int db_upvaluejoin (lua_State *L) { | |||
273 | 277 | ||
274 | 278 | ||
275 | /* | 279 | /* |
276 | ** The hook table (at registry[HOOKKEY]) maps threads to their current | ||
277 | ** hook function | ||
278 | */ | ||
279 | #define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY) | ||
280 | |||
281 | |||
282 | /* | ||
283 | ** Call hook function registered at hook table for the current | 280 | ** Call hook function registered at hook table for the current |
284 | ** thread (if there is one) | 281 | ** thread (if there is one) |
285 | */ | 282 | */ |
286 | static void hookf (lua_State *L, lua_Debug *ar) { | 283 | static void hookf (lua_State *L, lua_Debug *ar) { |
287 | static const char *const hooknames[] = | 284 | static const char *const hooknames[] = |
288 | {"call", "return", "line", "count", "tail call"}; | 285 | {"call", "return", "line", "count", "tail call"}; |
289 | gethooktable(L); | 286 | lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); |
290 | lua_pushthread(L); | 287 | lua_pushthread(L); |
291 | if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ | 288 | if (lua_rawget(L, -2) == LUA_TFUNCTION) { /* is there a hook function? */ |
292 | lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ | 289 | lua_pushstring(L, hooknames[(int)ar->event]); /* push event name */ |
@@ -339,7 +336,10 @@ static int db_sethook (lua_State *L) { | |||
339 | count = (int)luaL_optinteger(L, arg + 3, 0); | 336 | count = (int)luaL_optinteger(L, arg + 3, 0); |
340 | func = hookf; mask = makemask(smask, count); | 337 | func = hookf; mask = makemask(smask, count); |
341 | } | 338 | } |
342 | if (gethooktable(L) == 0) { /* creating hook table? */ | 339 | if (lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY) == LUA_TNIL) { |
340 | lua_createtable(L, 0, 2); /* create a hook table */ | ||
341 | lua_pushvalue(L, -1); | ||
342 | lua_rawsetp(L, LUA_REGISTRYINDEX, &HOOKKEY); /* set it in position */ | ||
343 | lua_pushstring(L, "k"); | 343 | lua_pushstring(L, "k"); |
344 | lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ | 344 | lua_setfield(L, -2, "__mode"); /** hooktable.__mode = "k" */ |
345 | lua_pushvalue(L, -1); | 345 | lua_pushvalue(L, -1); |
@@ -362,7 +362,7 @@ static int db_gethook (lua_State *L) { | |||
362 | if (hook != NULL && hook != hookf) /* external hook? */ | 362 | if (hook != NULL && hook != hookf) /* external hook? */ |
363 | lua_pushliteral(L, "external hook"); | 363 | lua_pushliteral(L, "external hook"); |
364 | else { | 364 | else { |
365 | gethooktable(L); | 365 | lua_rawgetp(L, LUA_REGISTRYINDEX, &HOOKKEY); |
366 | lua_pushthread(L1); lua_xmove(L1, L, 1); | 366 | lua_pushthread(L1); lua_xmove(L1, L, 1); |
367 | lua_rawget(L, -2); /* 1st result = hooktable[L1] */ | 367 | lua_rawget(L, -2); /* 1st result = hooktable[L1] */ |
368 | lua_remove(L, -2); /* remove hook table */ | 368 | lua_remove(L, -2); /* remove hook table */ |