diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-04 09:16:08 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-10-04 09:16:08 -0300 |
commit | cd2ddaded97f7f2b2af02cecfd165cf70e6f83f4 (patch) | |
tree | b3b2ca6acbafed7c5dd34d81ad58e26af588a894 | |
parent | d68209e822c21d3678cc53f1e02ba1c9dd26e23e (diff) | |
download | lua-cd2ddaded97f7f2b2af02cecfd165cf70e6f83f4.tar.gz lua-cd2ddaded97f7f2b2af02cecfd165cf70e6f83f4.tar.bz2 lua-cd2ddaded97f7f2b2af02cecfd165cf70e6f83f4.zip |
call hooks can only be called when `pc' is active (that is, inside
`execute'...)
-rw-r--r-- | ldo.c | 21 | ||||
-rw-r--r-- | ldo.h | 4 | ||||
-rw-r--r-- | lvm.c | 24 |
3 files changed, 30 insertions, 19 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 1.99 2000/10/02 14:47:43 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.100 2000/10/02 20:10:55 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 | */ |
@@ -112,7 +112,7 @@ void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) { | |||
112 | } | 112 | } |
113 | 113 | ||
114 | 114 | ||
115 | static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, | 115 | void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, |
116 | const char *event) { | 116 | const char *event) { |
117 | if (L->allowhooks) { | 117 | if (L->allowhooks) { |
118 | lua_Debug ar; | 118 | lua_Debug ar; |
@@ -124,6 +124,7 @@ static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, | |||
124 | 124 | ||
125 | 125 | ||
126 | static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { | 126 | static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { |
127 | lua_Hook callhook = L->callhook; | ||
127 | int nup = cl->nupvalues; /* number of upvalues */ | 128 | int nup = cl->nupvalues; /* number of upvalues */ |
128 | StkId old_Cbase = L->Cbase; | 129 | StkId old_Cbase = L->Cbase; |
129 | int n; | 130 | int n; |
@@ -131,7 +132,11 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { | |||
131 | luaD_checkstack(L, nup+LUA_MINSTACK); /* assures minimum stack size */ | 132 | luaD_checkstack(L, nup+LUA_MINSTACK); /* assures minimum stack size */ |
132 | for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ | 133 | for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ |
133 | *(L->top++) = cl->upvalue[n]; | 134 | *(L->top++) = cl->upvalue[n]; |
135 | if (callhook) | ||
136 | luaD_callHook(L, base-1, callhook, "call"); | ||
134 | n = (*cl->f.c)(L); /* do the actual call */ | 137 | n = (*cl->f.c)(L); /* do the actual call */ |
138 | if (callhook) /* same hook that was active at entry */ | ||
139 | luaD_callHook(L, base-1, callhook, "return"); | ||
135 | L->Cbase = old_Cbase; /* restore old C base */ | 140 | L->Cbase = old_Cbase; /* restore old C base */ |
136 | return L->top - n; /* return index of first result */ | 141 | return L->top - n; /* return index of first result */ |
137 | } | 142 | } |
@@ -154,25 +159,21 @@ void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) { | |||
154 | */ | 159 | */ |
155 | void luaD_call (lua_State *L, StkId func, int nResults) { | 160 | void luaD_call (lua_State *L, StkId func, int nResults) { |
156 | StkId firstResult; | 161 | StkId firstResult; |
157 | lua_Hook callhook = L->callhook; | ||
158 | retry: /* for `function' tag method */ | 162 | retry: /* for `function' tag method */ |
159 | switch (ttype(func)) { | 163 | switch (ttype(func)) { |
160 | case TAG_LCLOSURE: { | 164 | case TAG_LCLOSURE: { |
161 | CallInfo ci; | 165 | CallInfo ci; |
162 | ci.func = clvalue(func); | 166 | ci.func = clvalue(func); |
163 | ci.line = 0; | ||
164 | ttype(func) = TAG_LMARK; | ||
165 | infovalue(func) = &ci; | 167 | infovalue(func) = &ci; |
166 | if (callhook) | 168 | ttype(func) = TAG_LMARK; |
167 | luaD_callHook(L, func, callhook, "call"); | ||
168 | firstResult = luaV_execute(L, ci.func, func+1); | 169 | firstResult = luaV_execute(L, ci.func, func+1); |
170 | LUA_ASSERT(ttype(func) == TAG_LMARK, "invalid tag"); | ||
169 | break; | 171 | break; |
170 | } | 172 | } |
171 | case TAG_CCLOSURE: { | 173 | case TAG_CCLOSURE: { |
172 | ttype(func) = TAG_CMARK; | 174 | ttype(func) = TAG_CMARK; |
173 | if (callhook) | ||
174 | luaD_callHook(L, func, callhook, "call"); | ||
175 | firstResult = callCclosure(L, clvalue(func), func+1); | 175 | firstResult = callCclosure(L, clvalue(func), func+1); |
176 | LUA_ASSERT(ttype(func) == TAG_CMARK, "invalid tag"); | ||
176 | break; | 177 | break; |
177 | } | 178 | } |
178 | default: { /* `func' is not a function; check the `function' tag method */ | 179 | default: { /* `func' is not a function; check the `function' tag method */ |
@@ -184,8 +185,6 @@ void luaD_call (lua_State *L, StkId func, int nResults) { | |||
184 | goto retry; /* retry the call */ | 185 | goto retry; /* retry the call */ |
185 | } | 186 | } |
186 | } | 187 | } |
187 | if (callhook) /* same hook that was active at entry */ | ||
188 | luaD_callHook(L, func, callhook, "return"); | ||
189 | /* adjust the number of results */ | 188 | /* adjust the number of results */ |
190 | if (nResults == LUA_MULTRET) | 189 | if (nResults == LUA_MULTRET) |
191 | nResults = L->top - firstResult; | 190 | nResults = L->top - firstResult; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.h,v 1.24 2000/08/29 14:48:16 roberto Exp roberto $ | 2 | ** $Id: ldo.h,v 1.25 2000/09/25 16:22:42 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 | */ |
@@ -22,6 +22,8 @@ | |||
22 | void luaD_init (lua_State *L, int stacksize); | 22 | void luaD_init (lua_State *L, int stacksize); |
23 | void luaD_adjusttop (lua_State *L, StkId base, int extra); | 23 | void luaD_adjusttop (lua_State *L, StkId base, int extra); |
24 | void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook); | 24 | void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook); |
25 | void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, | ||
26 | const char *event); | ||
25 | void luaD_call (lua_State *L, StkId func, int nResults); | 27 | void luaD_call (lua_State *L, StkId func, int nResults); |
26 | void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults); | 28 | void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults); |
27 | void luaD_checkstack (lua_State *L, int n); | 29 | void luaD_checkstack (lua_State *L, int n); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.140 2000/10/03 14:03:21 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.141 2000/10/03 14:27:44 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -69,7 +69,7 @@ static void traceexec (lua_State *L, StkId base, StkId top, lua_Hook linehook) { | |||
69 | int *lineinfo = ci->func->f.l->lineinfo; | 69 | int *lineinfo = ci->func->f.l->lineinfo; |
70 | int pc = (*ci->pc - 1) - ci->func->f.l->code; | 70 | int pc = (*ci->pc - 1) - ci->func->f.l->code; |
71 | int newline; | 71 | int newline; |
72 | if (ci->line == 0) { /* first time? */ | 72 | if (pc == 0) { /* may be first time? */ |
73 | ci->line = 1; | 73 | ci->line = 1; |
74 | ci->refi = 0; | 74 | ci->refi = 0; |
75 | ci->lastpc = pc+1; /* make sure it will call linehook */ | 75 | ci->lastpc = pc+1; /* make sure it will call linehook */ |
@@ -348,14 +348,18 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
348 | StkId top; /* keep top local, for performance */ | 348 | StkId top; /* keep top local, for performance */ |
349 | const Instruction *pc = tf->code; | 349 | const Instruction *pc = tf->code; |
350 | TString **kstr = tf->kstr; | 350 | TString **kstr = tf->kstr; |
351 | lua_Hook linehook = L->linehook; | 351 | lua_Hook callhook = L->callhook; |
352 | lua_Hook linehook; /* set it only after calling eventual call hook */ | ||
352 | infovalue(base-1)->pc = &pc; | 353 | infovalue(base-1)->pc = &pc; |
353 | luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK); | 354 | luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK); |
354 | if (tf->is_vararg) /* varargs? */ | 355 | if (tf->is_vararg) /* varargs? */ |
355 | adjust_varargs(L, base, tf->numparams); | 356 | adjust_varargs(L, base, tf->numparams); |
356 | else | 357 | else |
357 | luaD_adjusttop(L, base, tf->numparams); | 358 | luaD_adjusttop(L, base, tf->numparams); |
359 | if (callhook) | ||
360 | luaD_callHook(L, base-1, callhook, "call"); | ||
358 | top = L->top; | 361 | top = L->top; |
362 | linehook = L->linehook; | ||
359 | /* main loop of interpreter */ | 363 | /* main loop of interpreter */ |
360 | for (;;) { | 364 | for (;;) { |
361 | const Instruction i = *pc++; | 365 | const Instruction i = *pc++; |
@@ -363,11 +367,13 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
363 | traceexec(L, base, top, linehook); | 367 | traceexec(L, base, top, linehook); |
364 | switch (GET_OPCODE(i)) { | 368 | switch (GET_OPCODE(i)) { |
365 | case OP_END: { | 369 | case OP_END: { |
366 | return L->top; /* no results */ | 370 | L->top = top; |
371 | goto endloop; | ||
367 | } | 372 | } |
368 | case OP_RETURN: { | 373 | case OP_RETURN: { |
369 | L->top = top; | 374 | L->top = top; |
370 | return base+GETARG_U(i); | 375 | top = base+GETARG_U(i); |
376 | goto endloop; | ||
371 | } | 377 | } |
372 | case OP_CALL: { | 378 | case OP_CALL: { |
373 | int nres = GETARG_B(i); | 379 | int nres = GETARG_B(i); |
@@ -380,7 +386,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
380 | case OP_TAILCALL: { | 386 | case OP_TAILCALL: { |
381 | L->top = top; | 387 | L->top = top; |
382 | luaD_call(L, base+GETARG_A(i), LUA_MULTRET); | 388 | luaD_call(L, base+GETARG_A(i), LUA_MULTRET); |
383 | return base+GETARG_B(i); | 389 | top = base+GETARG_B(i); |
390 | goto endloop; | ||
384 | } | 391 | } |
385 | case OP_PUSHNIL: { | 392 | case OP_PUSHNIL: { |
386 | int n = GETARG_U(i); | 393 | int n = GETARG_U(i); |
@@ -700,5 +707,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
700 | break; | 707 | break; |
701 | } | 708 | } |
702 | } | 709 | } |
703 | } | 710 | } endloop: |
711 | if (callhook) /* same hook that was active at entry */ | ||
712 | luaD_callHook(L, base-1, callhook, "return"); | ||
713 | return top; | ||
704 | } | 714 | } |