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

local m = {}

function m:ref(source, callback)
    local node = source.node
    if     node.type == 'setfield'
    or     node.type == 'getfield'
    or     node.type == 'setmethod'
    or     node.type == 'getmethod'
    or     node.type == 'setindex'
    or     node.type == 'getindex' then
        local key = guide.getKeyName(node)
        self:eachField(node.node, key, function (src, mode)
            if mode == 'set' or mode == 'get' then
                callback(src, mode)
            end
        end)
    else
        self:eachRef(node, callback)
    end
end

function m:field(source, key, callback)
    local used = {}
    local found = false
    used[source] = true

    self:eachRef(source, function (src)
        used[src] = true
        local child, mode = self:childMode(src)
        if child then
            if key == guide.getKeyName(child) then
                callback(child, mode)
            end
            return
        end
        if src.type == 'getglobal' then
            local parent = src.parent
            child, mode = self:childMode(parent)
            if child then
                if key == guide.getKeyName(child) then
                    callback(child, mode)
                end
            end
        elseif src.type == 'setglobal' then
            local parent = src.parent
            child, mode = self:childMode(parent)
            if child then
                if key == guide.getKeyName(child) then
                    callback(child, mode)
                end
            end
        else
            self:eachField(src, key, callback)
        end
    end)

    checkSMT(self, key, used, found, callback)
end

function m:value(source, callback)
    if source.value then
        self:eachValue(source.value, callback)
    end
end

return m