diff options
Diffstat (limited to 'ldo.c')
-rw-r--r-- | ldo.c | 55 |
1 files changed, 31 insertions, 24 deletions
@@ -295,8 +295,8 @@ void luaD_hook (lua_State *L, int event, int line, | |||
295 | if (hook && L->allowhook) { /* make sure there is a hook */ | 295 | if (hook && L->allowhook) { /* make sure there is a hook */ |
296 | int mask = CIST_HOOKED; | 296 | int mask = CIST_HOOKED; |
297 | CallInfo *ci = L->ci; | 297 | CallInfo *ci = L->ci; |
298 | ptrdiff_t top = savestack(L, L->top); | 298 | ptrdiff_t top = savestack(L, L->top); /* preserve original 'top' */ |
299 | ptrdiff_t ci_top = savestack(L, ci->top); | 299 | ptrdiff_t ci_top = savestack(L, ci->top); /* idem for 'ci->top' */ |
300 | lua_Debug ar; | 300 | lua_Debug ar; |
301 | ar.event = event; | 301 | ar.event = event; |
302 | ar.currentline = line; | 302 | ar.currentline = line; |
@@ -307,7 +307,7 @@ void luaD_hook (lua_State *L, int event, int line, | |||
307 | ci->u2.transferinfo.ntransfer = ntransfer; | 307 | ci->u2.transferinfo.ntransfer = ntransfer; |
308 | } | 308 | } |
309 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ | 309 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ |
310 | if (L->top + LUA_MINSTACK > ci->top) | 310 | if (ci->top < L->top + LUA_MINSTACK) |
311 | ci->top = L->top + LUA_MINSTACK; | 311 | ci->top = L->top + LUA_MINSTACK; |
312 | L->allowhook = 0; /* cannot call hooks inside a hook */ | 312 | L->allowhook = 0; /* cannot call hooks inside a hook */ |
313 | ci->callstatus |= mask; | 313 | ci->callstatus |= mask; |
@@ -329,39 +329,44 @@ void luaD_hook (lua_State *L, int event, int line, | |||
329 | ** active. | 329 | ** active. |
330 | */ | 330 | */ |
331 | void luaD_hookcall (lua_State *L, CallInfo *ci) { | 331 | void luaD_hookcall (lua_State *L, CallInfo *ci) { |
332 | int hook = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL : LUA_HOOKCALL; | 332 | if (L->hookmask & LUA_MASKCALL) { /* is call hook on? */ |
333 | Proto *p; | 333 | int event = (ci->callstatus & CIST_TAIL) ? LUA_HOOKTAILCALL |
334 | if (!(L->hookmask & LUA_MASKCALL)) /* some other hook? */ | 334 | : LUA_HOOKCALL; |
335 | return; /* don't call hook */ | 335 | Proto *p = ci_func(ci)->p; |
336 | p = clLvalue(s2v(ci->func))->p; | 336 | L->top = ci->top; /* prepare top */ |
337 | L->top = ci->top; /* prepare top */ | 337 | ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ |
338 | ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */ | 338 | luaD_hook(L, event, -1, 1, p->numparams); |
339 | luaD_hook(L, hook, -1, 1, p->numparams); | 339 | ci->u.l.savedpc--; /* correct 'pc' */ |
340 | ci->u.l.savedpc--; /* correct 'pc' */ | 340 | } |
341 | } | 341 | } |
342 | 342 | ||
343 | 343 | ||
344 | /* | ||
345 | ** Executes a call hook for Lua and C functions. This function is called | ||
346 | ** whenever 'hookmask' is not zero, so it checks whether return hooks are | ||
347 | ** active. | ||
348 | */ | ||
344 | static void rethook (lua_State *L, CallInfo *ci, int nres) { | 349 | static void rethook (lua_State *L, CallInfo *ci, int nres) { |
345 | StkId firstres = L->top - nres; /* index of first result */ | ||
346 | ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ | ||
347 | int delta = 0; | ||
348 | if (isLuacode(ci)) { | ||
349 | Proto *p = ci_func(ci)->p; | ||
350 | if (p->is_vararg) | ||
351 | delta = ci->u.l.nextraargs + p->numparams + 1; | ||
352 | if (L->top < ci->top) | ||
353 | L->top = ci->top; /* correct top to run hook */ | ||
354 | } | ||
355 | if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ | 350 | if (L->hookmask & LUA_MASKRET) { /* is return hook on? */ |
351 | StkId firstres = L->top - nres; /* index of first result */ | ||
352 | ptrdiff_t oldtop = savestack(L, L->top); /* hook may change top */ | ||
353 | int delta = 0; /* correction for vararg functions */ | ||
356 | int ftransfer; | 354 | int ftransfer; |
355 | if (isLuacode(ci)) { | ||
356 | Proto *p = ci_func(ci)->p; | ||
357 | if (p->is_vararg) | ||
358 | delta = ci->u.l.nextraargs + p->numparams + 1; | ||
359 | if (L->top < ci->top) | ||
360 | L->top = ci->top; /* correct top to run hook */ | ||
361 | } | ||
357 | ci->func += delta; /* if vararg, back to virtual 'func' */ | 362 | ci->func += delta; /* if vararg, back to virtual 'func' */ |
358 | ftransfer = cast(unsigned short, firstres - ci->func); | 363 | ftransfer = cast(unsigned short, firstres - ci->func); |
359 | luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ | 364 | luaD_hook(L, LUA_HOOKRET, -1, ftransfer, nres); /* call it */ |
360 | ci->func -= delta; | 365 | ci->func -= delta; |
366 | L->top = restorestack(L, oldtop); | ||
361 | } | 367 | } |
362 | if (isLua(ci = ci->previous)) | 368 | if (isLua(ci = ci->previous)) |
363 | L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ | 369 | L->oldpc = pcRel(ci->u.l.savedpc, ci_func(ci)->p); /* update 'oldpc' */ |
364 | L->top = restorestack(L, oldtop); | ||
365 | } | 370 | } |
366 | 371 | ||
367 | 372 | ||
@@ -420,7 +425,9 @@ static void moveresults (lua_State *L, StkId res, int nres, int wanted) { | |||
420 | } | 425 | } |
421 | firstresult = L->top - nres; /* index of first result */ | 426 | firstresult = L->top - nres; /* index of first result */ |
422 | /* move all results to correct place */ | 427 | /* move all results to correct place */ |
423 | for (i = 0; i < nres && i < wanted; i++) | 428 | if (nres > wanted) |
429 | nres = wanted; /* don't need more than that */ | ||
430 | for (i = 0; i < nres; i++) | ||
424 | setobjs2s(L, res + i, firstresult + i); | 431 | setobjs2s(L, res + i, firstresult + i); |
425 | for (; i < wanted; i++) /* complete wanted number of results */ | 432 | for (; i < wanted; i++) /* complete wanted number of results */ |
426 | setnilvalue(s2v(res + i)); | 433 | setnilvalue(s2v(res + i)); |