Browse Source

new macro 'isLuacode' (to distinguish regular Lua code from
hooks, where C code can run inside a Lua function).

Roberto Ierusalimschy 7 năm trước cách đây
mục cha
commit
4dc0be950a
4 tập tin đã thay đổi với 16 bổ sung11 xóa
  1. 4 3
      ldo.c
  2. 2 2
      lstate.c
  3. 7 3
      lstate.h
  4. 3 3
      ltm.c

+ 4 - 3
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 */
   }

+ 2 - 2
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++;

+ 7 - 3
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))

+ 3 - 3
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);