lua-users home
lua-l archive

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


> I finally had some time to make a minimal example how to provoke this problem.
> This is the entire c file that uses the Lua 5.2.0 
> 
> #include "lua.h"
> #include "lauxlib.h"
> #include "lualib.h"
> 
> int calldepth;
> 
> static void lyieldhook (lua_State *L, lua_Debug *ar) {
>   if(ar->event == LUA_HOOKCALL) {
>       calldepth++;
>       printf("Hookcall: %i\n", calldepth);
>   }
>   else {
>       calldepth--;
>       printf("Hookret: %i\n", calldepth);
>       if(calldepth == 0) {
>         lua_sethook(L, NULL,0,0);
>         lua_yield(L, 0);
>       }
>    }
> }
> 
> 
> int main (int argc, char **argv) {
>   int status;
>   lua_State *GL = luaL_newstate();  /* create state */
>   lua_State *L = lua_newthread(GL);
>   luaL_openlibs(L);
> 
>   status = luaL_loadstring(L,
>           "print \"1\"\n"
>           "print \"2\"\n"
>           "print \"3\"\n"
>           "print \"4\"\n"
>           );
> 
>   printf("loadstatus=%i\n", status);
>   lua_sethook(L, lyieldhook, LUA_MASKRET | LUA_MASKCALL, 0);
>   calldepth = -1;
>   status = lua_resume(L, 0, 0);
>   while(status == LUA_YIELD) {
>       printf("Yielded\n");
>       calldepth = -1;
>       lua_sethook(L, lyieldhook, LUA_MASKRET | LUA_MASKCALL, 0);
> I still believe the best solution is to just throw an error in this
> case, from looking at the code I think there is actually no easy
> solution to implement a proper yield inside a return-hook, on the
> other hand there is a simple workaround.

You are right. Lua does not support yielding from any hook. At least
the documentation should say that.

-- Roberto