Imported from ../lua-3.1.tar.gz.
[lua.git] / test / save.lua
blobce2e5b345c9385f6c9bc9499a2d41accaea98d32
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 write("\n-- global environment\n")
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 -- ow some examples
43 a = 3
44 x = {a = 4, b = "name", l={4,5,67}}
46 b = {t=5}
47 x.next = b
49 save()