-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex25.1.lua
More file actions
43 lines (40 loc) · 1.16 KB
/
ex25.1.lua
File metadata and controls
43 lines (40 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
local function getvarvalue (co, name, level, isenv)
local value
local found = false
-- level = (level or 1) + 1
level = (level or 1) -- no need to plus 1 since getvarvalue is not in the same
-- stack with the coroutine we are probing try local
-- variables
for i = 1, math.huge do
local n, v = debug.getlocal(co, level, i)
if not n then break end
if n == name then
value = v
found = true
end
end
if found then return "local", value end
-- try non-local variables
local func = debug.getinfo(co, level, "f").func
for i = 1, math.huge do
local n, v = debug.getupvalue(co, func, i)
if not n then break end
if n == name then return "upvalue", v end
end
if isenv then return "noenv" end
-- avoid loop
-- not found; get value from the environment
local _, env = getvarvalue("_ENV", level, true)
if env then
return "global", env[name]
else
-- no _ENV available
return "noenv"
end
end
local co = coroutine.create(function ()
local x = 10
coroutine.yield()
end)
coroutine.resume(co)
print(getvarvalue(co, "x"))