lua-users home
lua-l archive

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


Nice example
Set continuation function when yield so that next resume will invoke it first, then continue to execute
Thanks for your sharing code

      if (ci->u.c.k != NULL) {  /* does it have a continuation function? */
        lua_unlock(L);
        n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
        lua_lock(L);
        api_checknelems(L, n);
        firstArg = L->top - n;  /* yield results come from continuation */
      }
      luaD_poscall(L, firstArg, n);  /* finish 'luaD_precall' */

2016-03-19 19:54 GMT+08:00 Nagaev Boris <bnagaev@gmail.com>:
On Wed, Mar 16, 2016 at 2:55 PM, Wangbo <wangbo.red@gmail.com> wrote:
> It is difficult for me to read chapter "yield in c". especially API
> lua_yieldk, lua_callk and  lua_pcallk
> Can someone give me some code/scenario that  need this function ? so i can
> debug it by gdb to study the implementation.

Hello,

I have created C module with a function which yields Fibonacci numbers
up to 1 billion.

https://github.com/starius/lua-yield-in-c-example/blob/master/fib.c

How to use this module:

https://github.com/starius/lua-yield-in-c-example/blob/master/test.lua

There are other cases of "yield in C". For example, lua_pcallk can be
used for the following. Consider you have C function which gets a Lua
callback in its arguments. The C function calls Lua callback. If you
want to support yielding Lua functions, you have to call them with
lua_pcallk instead of lua_pcall.

--


Best regards,
Boris Nagaev