summaryrefslogtreecommitdiff
path: root/test/trace-globals.lua
blob: 566e1106b75d82f7e0d89a0b900f48f7c3c1aea1 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
-- shows how to trace assigments to global variables

-- a tostring that quotes strings. note the use of the original tostring.
local tostring=function(a)
 if tag(a)==tag("") then
  return format("%q",a)
 else
  return %tostring(a)
 end
end

local T=newtag()

local Tlog=function (name,old,new)
 local t=getinfo(3,"Sl")
 local line=t.currentline
 write(t.source)
 if line>=0 then write(":",line) end
 write(" -- ",name," is now ",%tostring(new)," (was ",%tostring(old),")","\n")
end

local Tgetnew=function (name)
 local t=settag({},%T)
 rawset(globals(),name,t)
 return nil
end

local Tsetnew=function (name,old,new)
 %Tlog(name,old,new)
 local t=settag({value=new},%T)
 rawset(globals(),name,t)
 return t
end

local Tsetglobal=function (name,old,new)
 %Tlog(name,old.value,new)
 old.value=new 
end

local Tgetglobal=function (x,value)
 return value.value
end

settagmethod(T,"getglobal",Tgetglobal)
settagmethod(T,"setglobal",Tsetglobal)

-- to trace only selected variables, use the following function
-- and comment the next two calls to settagmethod

function trace(name)
 local t=settag({value=rawget(globals(),name)},%T)
 rawset(globals(),name,t)
end
 
settagmethod(tag(nil),"getglobal",Tgetnew)
settagmethod(tag(nil),"setglobal",Tsetnew)

-- an example

trace"a"
print(a)
a=1
b=2
c=3
a=10
b=20
c=30
trace"b"
b="lua"
c={}
a=print
c=nil
c=100

print(a,b,c,d)