lua-users home
lua-l archive

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


Philipp

I imagined that the lua VM maintained a separate "virtual" call stack
for every lua chunk, not only coroutines. What you mean is that every
lua call frame(except for those in coroutines) is also a C call frame?
If so, I understand the problem.

Thanks for the explanation

Thiago.

On Mon, Oct 6, 2014 at 1:53 PM, Philipp Janda <siffiejoe@gmx.net> wrote:
> Am 06.10.2014 um 17:45 schröbte Thiago Padilha:
>>
>> Hi
>
>
> Hi!
>
>>
>> - Why Lua has this limitation? I can't understand why a coroutine can't be
>>    created/yieled by call coming from C, and resumed after the C call
>> returns.
>
>
> Every Lua coroutine has its own Lua stack where local Lua variables are
> stored, so it is easy to yield (just stop using this stack) and resume later
> (start using that stack again). C uses a single global stack, so you can
> yield without problems (e.g. using longjmp), skipping stack frames, but you
> can't restore the stack frames once they are gone -- especially if that
> stack space has been reused. Coco solves this problem by giving each Lua
> coroutine a separate C stack as well, but this is not possible in portable
> ISO C.
>
>> - Is there a way I can reorganize my code work around this issue without
>> relying
>>    on a patched lua VM or on luajit?(I want to publish the code to
>> luarocks)
>
>
> In Lua 5.2+ you have `lua_pcallk` and `lua_callk`, which will circumvent the
> problem by calling a separate function on resume. The C stack is still gone,
> so you have to save any data you need after the yield/resume on the Lua
> stack instead.
>
> As a code example I have attached the `protect` factory from
> FinalizedExceptions[1], which supports yielding on Lua 5.2 and 5.3-alpha
> (and Lua 5.1 with Coco, probably).
>
>>
>> Best regards
>>
>> Thiago
>>
>
> Philipp
>
>   [1]: http://lua-users.org/wiki/FinalizedExceptions
>
>