summaryrefslogtreecommitdiff
path: root/server-beta/src/core/local.lua
blob: 575a9360ef50df19496980049d9599a2d037b739 (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
local guide = require 'parser.guide'

local m = {}

function m:def(source, callback)
    if source.tag ~= 'self' then
        callback(source, 'local')
    end
    if source.ref then
        for _, ref in ipairs(source.ref) do
            if ref.type == 'setlocal' then
                callback(ref, 'set')
            end
        end
    end
    if source.tag == 'self' then
        local method = source.method
        local node = method.node
        self:eachDef(node, callback)
    end
end

function m:ref(source, callback)
    if source.tag ~= 'self' then
        callback(source, 'local')
    end
    if source.ref then
        for _, ref in ipairs(source.ref) do
            if ref.type == 'setlocal' then
                callback(ref, 'set')
            elseif ref.type == 'getlocal' then
                callback(ref, 'get')
            end
        end
    end
    if source.tag == 'self' then
        local method = source.method
        local node = method.node
        self:eachRef(node, callback)
    end
end

function m:field(source, key, callback)
    local used = {}
    local refs = source.ref
    if refs then
        for i = 1, #refs do
            local ref = refs[i]
            if ref.type == 'getlocal' then
                used[ref] = true
                local parent = ref.parent
                if key == guide.getKeyName(parent) then
                    self:childRef(parent, callback)
                end
            elseif ref.type == 'getglobal' then
                used[ref] = true
                if key == guide.getKeyName(ref) then
                    -- _ENV.XXX
                    callback(ref, 'get')
                end
            elseif ref.type == 'setglobal' then
                used[ref] = true
                -- _ENV.XXX = XXX
                if key == guide.getKeyName(ref) then
                    callback(ref, 'set')
                end
            end
        end
    end
    if source.tag == 'self' then
        local method = source.method
        local node = method.node
        self:eachField(node, key, callback)
    end
    self:eachValue(source, function (src)
        if source ~= src then
            self:eachField(src, key, callback)
        end
    end)
    self:eachSpecial(function (name, src)
        if name == 'rawset' then
            local t, k = self:callArgOf(src.parent)
            if used[t] and guide.getKeyName(k) == key then
                callback(src.parent, 'set')
            end
        elseif name == 'rawget' then
            local t, k, v = self:callArgOf(src.parent)
            if used[t] and guide.getKeyName(k) == key then
                callback(src.parent, 'get')
                self:eachField(v, key, callback)
            end
        elseif name == 'setmetatable' then
            local t, mt = self:callArgOf(src.parent)
            if used[t] then
                self:eachField(mt, 's|__index', function (src, mode)
                    if mode == 'set' then
                        self:eachValue(src, function (src)
                            self:eachField(src, key, callback)
                        end)
                    end
                end)
            end
        end
    end)
end

function m:value(source, callback)
    callback(source)
    local refs = source.ref
    if refs then
        for i = 1, #refs do
            local ref = refs[i]
            if ref.type == 'setlocal' then
                self:eachValue(ref.value, callback)
            end
        end
    end
    if source.value then
        self:eachValue(source.value, callback)
    end
end

return m