lua-users home
lua-l archive

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


> [...]
> 
> In the absence of misunderstandings in the above, I would tend to
> suggest that lua_callk, lua_pcallk, and lua_yieldk all return int,
> require being called at the end of a lua_CFunction, and always invoke
> the continuation function afterwards.  This would make them truly
> consistent CPS flow operations.
> 
> What do you folks think?

There are no misunderstandings in the above, but I think you missed
a subtle issue.

The problem is that C does not have proper tail calls, so we cannot
program in CPS style there. If all calls to lua_callk (or lua_pcallk)
are followed by a call to the continuation, any loop containing a
lua_callk could overflow the stack. (For a real example of such loop,
see function 'foreachcont' in 'ltablib.c'.)

When Lua calls the continuation, the original frame was previously
removed by a long jump, so this problem does not arise.

Another alternative would be for Lua itself to call the continuation
every time, so that these functions never return. But then they could
only be used in places where it is safe to yield, resulting in quite
complex restrictions.

-- Roberto