aboutsummaryrefslogtreecommitdiff
path: root/ldblib.c
diff options
context:
space:
mode:
Diffstat (limited to 'ldblib.c')
-rw-r--r--ldblib.c73
1 files changed, 38 insertions, 35 deletions
diff --git a/ldblib.c b/ldblib.c
index f016fcde..f3b732e9 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldblib.c,v 1.22 2000/10/02 20:10:55 roberto Exp roberto $ 2** $Id: ldblib.c,v 1.23 2000/10/20 16:39:03 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*/
@@ -109,58 +109,61 @@ static int setlocal (lua_State *L) {
109} 109}
110 110
111 111
112/*
113** because of these variables, this module is not reentrant, and should
114** not be used in multiple states
115*/
116
117static int linehook = LUA_NOREF; /* Lua reference to line hook function */
118static int callhook = LUA_NOREF; /* Lua reference to call hook function */
119 112
113#define KEY_CALLHOOK "dblib_callhook"
114#define KEY_LINEHOOK "dblib_linehook"
120 115
121 116
122static void linef (lua_State *L, lua_Debug *ar) { 117static void hookf (lua_State *L, const char *key) {
123 if (linehook != LUA_NOREF) { 118 lua_getregistry(L);
124 lua_getref(L, linehook); 119 lua_pushstring(L, key);
125 lua_pushnumber(L, ar->currentline); 120 lua_gettable(L, -2);
121 if (lua_isfunction(L, -1)) {
122 lua_pushvalue(L, 1);
126 lua_call(L, 1, 0); 123 lua_call(L, 1, 0);
127 } 124 }
125 else
126 lua_pop(L, 1); /* pop result from gettable */
127 lua_pop(L, 1); /* pop table */
128} 128}
129 129
130 130
131static void callf (lua_State *L, lua_Debug *ar) { 131static void callf (lua_State *L, lua_Debug *ar) {
132 if (callhook != LUA_NOREF) { 132 lua_pushstring(L, ar->event);
133 lua_getref(L, callhook); 133 hookf(L, KEY_CALLHOOK);
134 lua_pushstring(L, ar->event); 134}
135 lua_call(L, 1, 0); 135
136 } 136
137static void linef (lua_State *L, lua_Debug *ar) {
138 lua_pushnumber(L, ar->currentline);
139 hookf(L, KEY_LINEHOOK);
140}
141
142
143static void sethook (lua_State *L, const char *key, lua_Hook hook,
144 lua_Hook (*sethookf)(lua_State * L, lua_Hook h)) {
145 lua_settop(L, 1);
146 lua_getregistry(L);
147 lua_pushstring(L, key);
148 if (lua_isnil(L, 1))
149 (*sethookf)(L, NULL);
150 else if (lua_isfunction(L, 1))
151 (*sethookf)(L, hook);
152 else
153 luaL_argerror(L, 1, "function expected");
154 lua_pushvalue(L, 1);
155 lua_settable(L, -3);
137} 156}
138 157
139 158
140static int setcallhook (lua_State *L) { 159static int setcallhook (lua_State *L) {
141 lua_unref(L, callhook); 160 sethook(L, KEY_CALLHOOK, callf, lua_setcallhook);
142 if (lua_isnull(L, 1)) {
143 callhook = LUA_NOREF;
144 lua_setcallhook(L, NULL);
145 }
146 else {
147 callhook = lua_ref(L, 1);
148 lua_setcallhook(L, callf);
149 }
150 return 0; 161 return 0;
151} 162}
152 163
153 164
154static int setlinehook (lua_State *L) { 165static int setlinehook (lua_State *L) {
155 lua_unref(L, linehook); 166 sethook(L, KEY_LINEHOOK, linef, lua_setlinehook);
156 if (lua_isnull(L, 1)) {
157 linehook = LUA_NOREF;
158 lua_setlinehook(L, NULL);
159 }
160 else {
161 linehook = lua_ref(L, 1);
162 lua_setlinehook(L, linef);
163 }
164 return 0; 167 return 0;
165} 168}
166 169