To avoid this kind of problem, Lua raises an error whenever it tries to yield across an API call, except for three functions: , lua_callk
, and . All those functions receive a continuation function (as a parameter called k
) to continue execution after a yield.
Suppose the running thread yields while executing the callee function. After the thread resumes, it eventually will finish running the callee function. However, the callee function cannot return to the original function, because its frame in the C stack was destroyed by the yield. Instead, Lua calls a continuation function, which was given as an argument to the callee function. As the name implies, the continuation function should continue the task of the original function.
The only difference in the Lua state between the original function and its continuation is the result of a call to .