-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex25.3.lua
More file actions
60 lines (52 loc) · 1.18 KB
/
ex25.3.lua
File metadata and controls
60 lines (52 loc) · 1.18 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
local function getvarvalue ()
local res = {}
local value
local found = false
local level = 2
-- try local variables
for i = 1, math.huge do
local n, v = debug.getlocal(level, i)
if not n then break end
res[n] = v
-- print("found local", n)
end
-- try non-local variables
local env
local func = debug.getinfo(level, "f").func
for i = 1, math.huge do
local n, v = debug.getupvalue(func, i)
if not n then break end
if n == "_ENV" then env = v else res[n] = v end
-- print("found upvalue", n)
end
if env then
res.__index = env
setmetatable(res, res)
end
return res
-- 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 x = 1
local y = 2
local function f ()
local z = 3
local m = 4
local t = getvarvalue()
return t
end
local t = getvarvalue()
for k, v in pairs(t) do
print(k, v)
end
assert(t.getvarvalue == getvarvalue)