Prechádzať zdrojové kódy

Bug: Yielding in a hook stops in the wrong instruction

Yielding in a hook must decrease the program counter, because it already
counted an instruction that, in the end, was not executed. However,
that decrement should be done only when about to restart the thread.
Otherwise, inspecting the thread with the debug library shows it one
instruction behind of where it really is.
Roberto Ierusalimschy 1 rok pred
rodič
commit
e288c5a918
3 zmenil súbory, kde vykonal 11 pridanie a 6 odobranie
  1. 2 3
      ldebug.c
  2. 4 0
      ldo.c
  3. 5 3
      testes/coroutine.lua

+ 2 - 3
ldebug.c

@@ -925,12 +925,12 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) {
   }
   }
   pc++;  /* reference is always next instruction */
   pc++;  /* reference is always next instruction */
   ci->u.l.savedpc = pc;  /* save 'pc' */
   ci->u.l.savedpc = pc;  /* save 'pc' */
-  counthook = (--L->hookcount == 0 && (mask & LUA_MASKCOUNT));
+  counthook = (mask & LUA_MASKCOUNT) && (--L->hookcount == 0);
   if (counthook)
   if (counthook)
     resethookcount(L);  /* reset count */
     resethookcount(L);  /* reset count */
   else if (!(mask & LUA_MASKLINE))
   else if (!(mask & LUA_MASKLINE))
     return 1;  /* no line hook and count != 0; nothing to be done now */
     return 1;  /* no line hook and count != 0; nothing to be done now */
-  if (ci->callstatus & CIST_HOOKYIELD) {  /* called hook last time? */
+  if (ci->callstatus & CIST_HOOKYIELD) {  /* hook yielded last time? */
     ci->callstatus &= ~CIST_HOOKYIELD;  /* erase mark */
     ci->callstatus &= ~CIST_HOOKYIELD;  /* erase mark */
     return 1;  /* do not call hook again (VM yielded, so it did not move) */
     return 1;  /* do not call hook again (VM yielded, so it did not move) */
   }
   }
@@ -952,7 +952,6 @@ int luaG_traceexec (lua_State *L, const Instruction *pc) {
   if (L->status == LUA_YIELD) {  /* did hook yield? */
   if (L->status == LUA_YIELD) {  /* did hook yield? */
     if (counthook)
     if (counthook)
       L->hookcount = 1;  /* undo decrement to zero */
       L->hookcount = 1;  /* undo decrement to zero */
-    ci->u.l.savedpc--;  /* undo increment (resume will increment it again) */
     ci->callstatus |= CIST_HOOKYIELD;  /* mark that it yielded */
     ci->callstatus |= CIST_HOOKYIELD;  /* mark that it yielded */
     luaD_throw(L, LUA_YIELD);
     luaD_throw(L, LUA_YIELD);
   }
   }

+ 4 - 0
ldo.c

@@ -792,6 +792,10 @@ static void resume (lua_State *L, void *ud) {
     lua_assert(L->status == LUA_YIELD);
     lua_assert(L->status == LUA_YIELD);
     L->status = LUA_OK;  /* mark that it is running (again) */
     L->status = LUA_OK;  /* mark that it is running (again) */
     if (isLua(ci)) {  /* yielded inside a hook? */
     if (isLua(ci)) {  /* yielded inside a hook? */
+      /* undo increment made by 'luaG_traceexec': instruction was not
+         executed yet */
+      lua_assert(ci->callstatus & CIST_HOOKYIELD);
+      ci->u.l.savedpc--;
       L->top.p = firstArg;  /* discard arguments */
       L->top.p = firstArg;  /* discard arguments */
       luaV_execute(L, ci);  /* just continue running Lua code */
       luaV_execute(L, ci);  /* just continue running Lua code */
     }
     }

+ 5 - 3
testes/coroutine.lua

@@ -610,18 +610,20 @@ else
     -- (bug in 5.2/5.3)
     -- (bug in 5.2/5.3)
     c = coroutine.create(function (a, ...)
     c = coroutine.create(function (a, ...)
       T.sethook("yield 0", "l")   -- will yield on next two lines
       T.sethook("yield 0", "l")   -- will yield on next two lines
-      assert(a == 10)
+      local b = a
       return ...
       return ...
     end)
     end)
 
 
     assert(coroutine.resume(c, 1, 2, 3))   -- start coroutine
     assert(coroutine.resume(c, 1, 2, 3))   -- start coroutine
     local n,v = debug.getlocal(c, 0, 1)    -- check its local
     local n,v = debug.getlocal(c, 0, 1)    -- check its local
-    assert(n == "a" and v == 1)
+    assert(n == "a" and v == 1 and debug.getlocal(c, 0, 2) ~= "b")
     assert(debug.setlocal(c, 0, 1, 10))     -- test 'setlocal'
     assert(debug.setlocal(c, 0, 1, 10))     -- test 'setlocal'
     local t = debug.getinfo(c, 0)        -- test 'getinfo'
     local t = debug.getinfo(c, 0)        -- test 'getinfo'
-    assert(t.currentline == t.linedefined + 1)
+    assert(t.currentline == t.linedefined + 2)
     assert(not debug.getinfo(c, 1))      -- no other level
     assert(not debug.getinfo(c, 1))      -- no other level
     assert(coroutine.resume(c))          -- run next line
     assert(coroutine.resume(c))          -- run next line
+    local n,v = debug.getlocal(c, 0, 2)    -- check next local
+    assert(n == "b" and v == 10)
     v = {coroutine.resume(c)}         -- finish coroutine
     v = {coroutine.resume(c)}         -- finish coroutine
     assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef)
     assert(v[1] == true and v[2] == 2 and v[3] == 3 and v[4] == undef)
     assert(not coroutine.resume(c))
     assert(not coroutine.resume(c))