diff options
Diffstat (limited to '')
-rw-r--r-- | LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/trace-globals.lua | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/trace-globals.lua b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/trace-globals.lua new file mode 100644 index 0000000..295e670 --- /dev/null +++ b/LuaSL/testLua/yueliang-0.4.1/test_lua/5.0/trace-globals.lua | |||
@@ -0,0 +1,38 @@ | |||
1 | -- trace assigments to global variables | ||
2 | |||
3 | do | ||
4 | -- a tostring that quotes strings. note the use of the original tostring. | ||
5 | local _tostring=tostring | ||
6 | local tostring=function(a) | ||
7 | if type(a)=="string" then | ||
8 | return string.format("%q",a) | ||
9 | else | ||
10 | return _tostring(a) | ||
11 | end | ||
12 | end | ||
13 | |||
14 | local log=function (name,old,new) | ||
15 | local t=debug.getinfo(3,"Sl") | ||
16 | local line=t.currentline | ||
17 | io.write(t.short_src) | ||
18 | if line>=0 then io.write(":",line) end | ||
19 | io.write(": ",name," is now ",tostring(new)," (was ",tostring(old),")","\n") | ||
20 | end | ||
21 | |||
22 | local g={} | ||
23 | local set=function (t,name,value) | ||
24 | log(name,g[name],value) | ||
25 | g[name]=value | ||
26 | end | ||
27 | setmetatable(getfenv(),{__index=g,__newindex=set}) | ||
28 | end | ||
29 | |||
30 | -- an example | ||
31 | |||
32 | a=1 | ||
33 | b=2 | ||
34 | a=10 | ||
35 | b=20 | ||
36 | b=nil | ||
37 | b=200 | ||
38 | print(a,b,c) | ||