Imported from ../lua-3.0.tar.gz.
[lua.git] / test / dump.lua
blobbcf514100a2729b4472ecfceab712476e6069cc1
1 -- dump global environment
3 function savevar (n,v)
4 if v == nil then return end
5 if type(v)=="userdata" or type(v)=="function" then return end
6 -- if type(v)=="userdata" or type(v)=="function" then write("\t-- ") end
7 write(n,"=")
8 if type(v) == "string" then write(format("%q",v))
9 elseif type(v) == "table" then
10 if v.__visited__ ~= nil then
11 write(v.__visited__)
12 else
13 write("{}\n")
14 v.__visited__ = n
15 local r,f
16 r,f = next(v,nil)
17 while r ~= nil do
18 if r ~= "__visited__" then
19 if type(r) == 'string' then
20 savevar(n.."."..r,f)
21 else
22 savevar(n.."["..r.."]",f)
23 end
24 end
25 r,f = next(v,r)
26 end
27 end
28 else write(tostring(v)) end
29 write("\n")
30 end
32 function save ()
33 print("\n-- global environment")
34 local n,v = nextvar(nil)
35 while n ~= nil do
36 savevar(n,v)
37 n,v = nextvar(n)
38 end
39 end
41 save()