diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-07-08 15:21:33 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-07-08 15:21:33 -0300 |
commit | 39b2d58c39fd0cd554b27ed071926bc439338964 (patch) | |
tree | 6855ede1b0e908439476e54396f94ae9ad275269 /ldo.c | |
parent | d2d24f09713cfecf59a7688c45a3863a35f09254 (diff) | |
download | lua-39b2d58c39fd0cd554b27ed071926bc439338964.tar.gz lua-39b2d58c39fd0cd554b27ed071926bc439338964.tar.bz2 lua-39b2d58c39fd0cd554b27ed071926bc439338964.zip |
new interface for debug hooks
Diffstat (limited to 'ldo.c')
-rw-r--r-- | ldo.c | 67 |
1 files changed, 28 insertions, 39 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 1.184 2002/06/26 16:37:23 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.185 2002/07/04 12:29:32 roberto Exp roberto $ |
3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -135,40 +135,29 @@ static void luaD_openstack (lua_State *L, StkId pos) { | |||
135 | } | 135 | } |
136 | 136 | ||
137 | 137 | ||
138 | static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) { | 138 | void luaD_callhook (lua_State *L, lua_Hookevent event, int line) { |
139 | ptrdiff_t top = savestack(L, L->top); | 139 | lua_Hook hook = L->hook; |
140 | ptrdiff_t ci_top = savestack(L, L->ci->top); | 140 | if (hook && allowhook(L)) { |
141 | ar->i_ci = L->ci - L->base_ci; | 141 | ptrdiff_t top = savestack(L, L->top); |
142 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ | 142 | ptrdiff_t ci_top = savestack(L, L->ci->top); |
143 | L->ci->top = L->top + LUA_MINSTACK; | ||
144 | L->allowhooks = 0; /* cannot call hooks inside a hook */ | ||
145 | lua_unlock(L); | ||
146 | (*hook)(L, ar); | ||
147 | lua_lock(L); | ||
148 | lua_assert(L->allowhooks == 0); | ||
149 | L->allowhooks = 1; | ||
150 | L->ci->top = restorestack(L, ci_top); | ||
151 | L->top = restorestack(L, top); | ||
152 | } | ||
153 | |||
154 | |||
155 | void luaD_lineHook (lua_State *L, int line) { | ||
156 | if (L->allowhooks) { | ||
157 | lua_Debug ar; | ||
158 | ar.event = "line"; | ||
159 | ar.currentline = line; | ||
160 | dohook(L, &ar, L->linehook); | ||
161 | } | ||
162 | } | ||
163 | |||
164 | |||
165 | static void luaD_callHook (lua_State *L, lua_Hook callhook, const char *event) { | ||
166 | if (L->allowhooks) { | ||
167 | lua_Debug ar; | 143 | lua_Debug ar; |
168 | ar.event = event; | 144 | ar.event = event; |
169 | L->ci->pc = NULL; /* function is not active */ | 145 | ar.currentline = line; |
170 | L->ci->top = L->ci->base; /* `top' may not have a valid value yet */ | 146 | ar.i_ci = L->ci - L->base_ci; |
171 | dohook(L, &ar, callhook); | 147 | if (event <= LUA_HOOKRET) { /* `call' or `return' event? */ |
148 | L->ci->pc = NULL; /* function is not active */ | ||
149 | L->ci->top = L->ci->base; /* `top' may not have a valid value yet */ | ||
150 | } | ||
151 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ | ||
152 | L->ci->top = L->top + LUA_MINSTACK; | ||
153 | setallowhook(L, 0); /* cannot call hooks inside a hook */ | ||
154 | lua_unlock(L); | ||
155 | (*hook)(L, &ar); | ||
156 | lua_lock(L); | ||
157 | lua_assert(!allowhook(L)); | ||
158 | setallowhook(L, 1); | ||
159 | L->ci->top = restorestack(L, ci_top); | ||
160 | L->top = restorestack(L, top); | ||
172 | } | 161 | } |
173 | } | 162 | } |
174 | 163 | ||
@@ -219,8 +208,8 @@ StkId luaD_precall (lua_State *L, StkId func) { | |||
219 | if (ttype(func) != LUA_TFUNCTION) /* `func' is not a function? */ | 208 | if (ttype(func) != LUA_TFUNCTION) /* `func' is not a function? */ |
220 | func = tryfuncTM(L, func); /* check the `function' tag method */ | 209 | func = tryfuncTM(L, func); /* check the `function' tag method */ |
221 | cl = &clvalue(func)->l; | 210 | cl = &clvalue(func)->l; |
222 | if (L->callhook) { | 211 | if (L->hookmask & LUA_MASKCALL) { |
223 | luaD_callHook(L, L->callhook, "call"); | 212 | luaD_callhook(L, LUA_HOOKCALL, -1); |
224 | ci = L->ci; /* previous call may realocate `ci' */ | 213 | ci = L->ci; /* previous call may realocate `ci' */ |
225 | } | 214 | } |
226 | if (!cl->isC) { /* Lua function? prepare its call */ | 215 | if (!cl->isC) { /* Lua function? prepare its call */ |
@@ -252,9 +241,9 @@ StkId luaD_precall (lua_State *L, StkId func) { | |||
252 | 241 | ||
253 | void luaD_poscall (lua_State *L, int wanted, StkId firstResult) { | 242 | void luaD_poscall (lua_State *L, int wanted, StkId firstResult) { |
254 | StkId res; | 243 | StkId res; |
255 | if (L->callhook) { | 244 | if (L->hookmask & LUA_MASKRET) { |
256 | ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */ | 245 | ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */ |
257 | luaD_callHook(L, L->callhook, "return"); | 246 | luaD_callhook(L, LUA_HOOKRET, -1); |
258 | firstResult = restorestack(L, fr); | 247 | firstResult = restorestack(L, fr); |
259 | } | 248 | } |
260 | res = L->ci->base - 1; /* res == final position of 1st result */ | 249 | res = L->ci->base - 1; /* res == final position of 1st result */ |
@@ -483,7 +472,7 @@ int luaD_runprotected (lua_State *L, Pfunc f, TObject *ud) { | |||
483 | struct lua_longjmp lj; | 472 | struct lua_longjmp lj; |
484 | lj.ci = L->ci; | 473 | lj.ci = L->ci; |
485 | lj.top = L->top; | 474 | lj.top = L->top; |
486 | lj.allowhooks = L->allowhooks; | 475 | lj.allowhooks = allowhook(L); |
487 | lj.status = 0; | 476 | lj.status = 0; |
488 | lj.err = ud; | 477 | lj.err = ud; |
489 | lj.previous = L->errorJmp; /* chain new error handler */ | 478 | lj.previous = L->errorJmp; /* chain new error handler */ |
@@ -493,7 +482,7 @@ int luaD_runprotected (lua_State *L, Pfunc f, TObject *ud) { | |||
493 | else { /* an error occurred */ | 482 | else { /* an error occurred */ |
494 | L->ci = lj.ci; /* restore the state */ | 483 | L->ci = lj.ci; /* restore the state */ |
495 | L->top = lj.top; | 484 | L->top = lj.top; |
496 | L->allowhooks = lj.allowhooks; | 485 | setallowhook(L, lj.allowhooks); |
497 | restore_stack_limit(L); | 486 | restore_stack_limit(L); |
498 | } | 487 | } |
499 | L->errorJmp = lj.previous; /* restore old error handler */ | 488 | L->errorJmp = lj.previous; /* restore old error handler */ |