[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: No debug api to get the function before coroutine running
- From: Daurnimator <quae@...>
- Date: Sat, 3 Dec 2016 21:20:16 +1100
On 3 December 2016 at 18:44, 云风 Cloud Wu <cloudwu@gmail.com> wrote:
> local co = corotuine.create(function() end)
>
> It seems that there is no way to get the function in 'co' by lua debug.*
> api.
Correct in the general case.
With tail calls, lua won't even be holding a reference to the original
function. consider:
local co do
local foo, bar
local i = 0
foo = function() i = i + 1; coroutine.yield(i); return bar() end
bar = function() i = i + 1; coroutine.yield(i); return foo() end
co = coroutine.create(foo)
end
However, you can get the 'current' function via debug.getinfo(); but
only *after* the first resume.
coroutine.resume(co)
print(debug.getinfo(co, 1, "f").func)
This is because coroutine.create just pushes the function onto the new
thread's stack: the first resume does the actual work.
So for this case, I guess what you're asking for is a way to inspect a
lua thread's stack from lua itself.
This wouldn't be a terrible addition to the debug.* api.
But I can't help but bring up a slippery slope argument: once we allow
inspecting the stack, why shouldn't we be allowed to manipulate it?