From 4dc0be950ad67e4385400aacd25e10325a2a6e59 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 19 Dec 2017 14:40:17 -0200 Subject: new macro 'isLuacode' (to distinguish regular Lua code from hooks, where C code can run inside a Lua function). --- ldo.c | 7 ++++--- lstate.c | 4 ++-- lstate.h | 10 +++++++--- ltm.c | 6 +++--- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ldo.c b/ldo.c index e58efa52..fa8a382b 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.180 2017/12/12 11:57:30 roberto Exp roberto $ +** $Id: ldo.c,v 2.181 2017/12/15 13:07:10 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -454,7 +454,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { ci->func = func; ci->top = L->top + LUA_MINSTACK; lua_assert(ci->top <= L->stack_last); - ci->callstatus = 0; + ci->callstatus = CIST_C; if (L->hookmask & LUA_MASKCALL) luaD_hook(L, LUA_HOOKCALL, -1); lua_unlock(L); @@ -479,7 +479,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) { L->top = ci->top = func + 1 + fsize; lua_assert(ci->top <= L->stack_last); ci->u.l.savedpc = p->code; /* starting point */ - ci->callstatus = CIST_LUA; + ci->callstatus = 0; if (L->hookmask) callhook(L, ci, 0); luaV_execute(L, ci); /* run the function */ @@ -698,6 +698,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, } L->status = LUA_YIELD; if (isLua(ci)) { /* inside a hook? */ + lua_assert(!isLuacode(ci)); api_check(L, k == NULL, "hooks cannot continue after yielding"); ci->u2.nyield = 0; /* no results */ } diff --git a/lstate.c b/lstate.c index 22702a00..81e10851 100644 --- a/lstate.c +++ b/lstate.c @@ -1,5 +1,5 @@ /* -** $Id: lstate.c,v 2.147 2017/11/13 15:36:52 roberto Exp roberto $ +** $Id: lstate.c,v 2.148 2017/11/23 16:35:54 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -177,7 +177,7 @@ static void stack_init (lua_State *L1, lua_State *L) { /* initialize first ci */ ci = &L1->base_ci; ci->next = ci->previous = NULL; - ci->callstatus = 0; + ci->callstatus = CIST_C; ci->func = L1->top; setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */ L1->top++; diff --git a/lstate.h b/lstate.h index fad54634..bc4df4e5 100644 --- a/lstate.h +++ b/lstate.h @@ -1,5 +1,5 @@ /* -** $Id: lstate.h,v 2.151 2017/11/13 15:36:52 roberto Exp roberto $ +** $Id: lstate.h,v 2.152 2017/11/23 16:35:54 roberto Exp roberto $ ** Global State ** See Copyright Notice in lua.h */ @@ -112,7 +112,7 @@ typedef struct CallInfo { ** Bits in CallInfo status */ #define CIST_OAH (1<<0) /* original value of 'allowhook' */ -#define CIST_LUA (1<<1) /* call is running a Lua function */ +#define CIST_C (1<<1) /* call is running a C function */ #define CIST_HOOKED (1<<2) /* call is running a debug hook */ #define CIST_YPCALL (1<<3) /* call is a yieldable protected call */ #define CIST_TAIL (1<<4) /* call was tail called */ @@ -120,7 +120,11 @@ typedef struct CallInfo { #define CIST_LEQ (1<<6) /* using __lt for __le */ #define CIST_FIN (1<<7) /* call is running a finalizer */ -#define isLua(ci) ((ci)->callstatus & CIST_LUA) +/* active function is a Lua function */ +#define isLua(ci) (!((ci)->callstatus & CIST_C)) + +/* call is running Lua code (not a hook) */ +#define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED))) /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */ #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v)) diff --git a/ltm.c b/ltm.c index 049c1712..5906a2fd 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.52 2017/12/13 18:32:09 roberto Exp roberto $ +** $Id: ltm.c,v 2.53 2017/12/15 13:07:10 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -108,7 +108,7 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, setobj2s(L, func + 3, p3); /* 3rd argument */ L->top += 4; /* metamethod may yield only when called from Lua code */ - if (isLua(L->ci)) + if (isLuacode(L->ci)) luaD_call(L, func, 0); else luaD_callnoyield(L, func, 0); @@ -124,7 +124,7 @@ void luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1, setobj2s(L, func + 2, p2); /* 2nd argument */ L->top += 3; /* metamethod may yield only when called from Lua code */ - if (isLua(L->ci)) + if (isLuacode(L->ci)) luaD_call(L, func, 1); else luaD_callnoyield(L, func, 1); -- cgit v1.2.3-55-g6feb