Path: blob/devel/contrib/lua-5.1.5/test/trace-globals.lua
3203 views
-- trace assigments to global variables12do3-- a tostring that quotes strings. note the use of the original tostring.4local _tostring=tostring5local tostring=function(a)6if type(a)=="string" then7return string.format("%q",a)8else9return _tostring(a)10end11end1213local log=function (name,old,new)14local t=debug.getinfo(3,"Sl")15local line=t.currentline16io.write(t.short_src)17if line>=0 then io.write(":",line) end18io.write(": ",name," is now ",tostring(new)," (was ",tostring(old),")","\n")19end2021local g={}22local set=function (t,name,value)23log(name,g[name],value)24g[name]=value25end26setmetatable(getfenv(),{__index=g,__newindex=set})27end2829-- an example3031a=132b=233a=1034b=2035b=nil36b=20037print(a,b,c)383940