summaryrefslogtreecommitdiff
path: root/server-beta/src/searcher/eachField.lua
blob: 3db2375f96cdbf8fe5be4e473d15bb0b9e211585 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
local guide = require 'parser.guide'

local function ofTabel(searcher, value, callback)
    for _, field in ipairs(value) do
        if field.type == 'tablefield'
        or field.type == 'tableindex' then
            callback {
                searcher = searcher,
                source   = field,
                key      = guide.getKeyName(field),
                value    = field.value,
                mode     = 'set',
            }
        end
    end
end

local function ofENV(searcher, source, callback)
    if source.type == 'getlocal' then
        local parent = source.parent
        if parent.type == 'getfield'
        or parent.type == 'getmethod'
        or parent.type == 'getindex' then
            callback {
                searcher = searcher,
                source   = parent,
                key      = guide.getKeyName(parent),
                mode     = 'get',
            }
        end
    elseif source.type == 'getglobal' then
        callback {
            searcher = searcher,
            source   = source,
            key      = guide.getKeyName(source),
            mode     = 'get',
        }
    elseif source.type == 'setglobal' then
        callback {
            searcher = searcher,
            source   = source,
            key      = guide.getKeyName(source),
            mode     = 'set',
            value    = source.value,
        }
    end
end

local function ofSpecialArg(searcher, source, callback)
    local args = source.parent
    local call = args.parent
    local func = call.node
    local name = searcher:getSpecialName(func)
    if    name == 'rawset' then
        if args[1] == source and args[2] then
            callback {
                searcher = searcher,
                source   = call,
                key      = guide.getKeyName(args[2]),
                value    = args[3],
                mode     = 'set',
            }
        end
    elseif name == 'rawget' then
        if args[1] == source and args[2] then
            callback {
                searcher = searcher,
                source   = call,
                key      = guide.getKeyName(args[2]),
                mode     = 'get',
            }
        end
    elseif name == 'setmetatable' then
        if args[1] == source and args[2] then
            searcher:eachField(args[2], function (info)
                if info.key == 's|__index' and info.value then
                    info.searcher:eachField(info.value, callback)
                end
            end)
        end
    end
end

local function ofVar(searcher, source, callback)
    local parent = source.parent
    if not parent then
        return
    end
    if parent.type == 'getfield'
    or parent.type == 'getmethod'
    or parent.type == 'getindex' then
        callback {
            searcher = searcher,
            source   = parent,
            key      = guide.getKeyName(parent),
            mode     = 'get',
        }
        return
    end
    if parent.type == 'setfield'
    or parent.type == 'setmethod'
    or parent.type == 'setindex' then
        callback {
            searcher = searcher,
            source   = parent,
            key      = guide.getKeyName(parent),
            value    = parent.value,
            mode     = 'set',
        }
        return
    end
    if parent.type == 'callargs' then
        ofSpecialArg(searcher, source, callback)
    end
end

return function (searcher, source, callback)
    searcher:eachRef(source, function (info)
        local src = info.source
        if src.tag == '_ENV' then
            if src.ref then
                for _, ref in ipairs(src.ref) do
                    ofENV(info.searcher, ref, callback)
                end
            end
        elseif src.type == 'getlocal'
        or     src.type == 'getglobal'
        or     src.type == 'getfield'
        or     src.type == 'getmethod'
        or     src.type == 'getindex' then
            ofVar(info.searcher, src, callback)
        elseif src.type == 'table' then
            ofTabel(info.searcher, src, callback)
        end
    end)
end