lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I've seen conflicting information on the Lua list about calling
lua_yield inside a hook. Can somebody state authoritatively if this is
supported or not?

A comment in luaV_execute code implies that it is legal (5.1 source):

      if (L->status == LUA_YIELD) {  /* did hook yield? */
        L->savedpc = pc - 1;
        return;
      }


Here is a very simple test:

static void hook(lua_State* l, lua_Debug* ar)
{
   lua_getinfo(l, "n", ar);
   printf("Line hook %d\n", ar->currentline);
   lua_yield(l, 0);
}

void debugTest()
{
   lua_State* l = luaL_newstate();
   luaL_openlibs(l);
   lua_sethook(l, hook, LUA_MASKLINE, 0);

   luaL_loadstring(l,
      "for i=1,5 do\n"
      "   print('iteration', i)\n"
      "end\n"
   );

   while (lua_resume(l, 0) == LUA_YIELD)
      printf("Yielding\n");
}

This doesn't work. I get an infinite loop with the following output:

Yielding
Line hook 1
Yielding
Line hook 1
Yielding
Line hook 1
...

Lua never steps beyond line 1.

Thanks,

-Erik