aboutsummaryrefslogtreecommitdiff
path: root/ldblib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-11-10 12:27:16 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-11-10 12:27:16 -0200
commitfee3aa518d37f55ae93c3039b21c55f5f27d19e5 (patch)
tree6fbb956e4d3541840bb0c3424983a0e673f43808 /ldblib.c
parentb58602d93dbbe92373bc451916b256cb2c9a4730 (diff)
downloadlua-fee3aa518d37f55ae93c3039b21c55f5f27d19e5.tar.gz
lua-fee3aa518d37f55ae93c3039b21c55f5f27d19e5.tar.bz2
lua-fee3aa518d37f55ae93c3039b21c55f5f27d19e5.zip
using address key (light userdata) for hook table, instead of
string
Diffstat (limited to 'ldblib.c')
-rw-r--r--ldblib.c24
1 files changed, 12 insertions, 12 deletions
diff --git a/ldblib.c b/ldblib.c
index eb334100..f6c945d9 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -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*/
27static const int HOOKKEY = 0;
24 28
25 29
26static int db_getregistry (lua_State *L) { 30static 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*/
286static void hookf (lua_State *L, lua_Debug *ar) { 283static 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 */