lua-users home
lua-l archive

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


于 2012-5-27 10:40, John Belmonte 写道:
I noticed that the lua_yield / yieldk docs in 5.1 and 5.2 don't define
the return value of the function.  (If I understand correctly it's the
number of arguments placed on the stack by lua_resume.)

No.
in plain Lua (not LuaJIT, I mean), lua_yield actually doesn't return at the C API side.
lua_yield would do a `longjmp' to perform the yield, which would unwind all the C stack
till the nearest recover point, i.e. the nearest setjmp call.

this is the reason why you can't use pcall in a yieldable coroutine in Lua 5.1, in which case
the yield would be caught by the recover point set by pcall, not the one by lua_resume.
Lua 5.2 fixed this restriction by not setting a recover point in lua_pcall if one is already set by lua_resume.

the yield/resume semantic is all about the Lua side. lua_resume simply puts the values onto
the stack of the thread and calls into the Lua VM to execute the thread.
for the Lua side, this has the effect that the the last call to `coroutine.yield' has just returned.
but the C side lua_yield would never return at all.