summaryrefslogtreecommitdiff
path: root/script-beta/vm/eachField.lua
blob: 5148e153bf9238f2c7fcdc3785cc25d92173dadb (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
local guide   = require 'parser.guide'
local files   = require 'files'
local vm      = require 'vm.vm'

local function checkNext(source)
    local nextSrc = source.next
    if not nextSrc then
        return nil
    end
    local ntype = nextSrc.type
    if     ntype == 'setfield'
    or     ntype == 'setmethod'
    or     ntype == 'setindex' then
        return nextSrc, 'set'
    elseif ntype == 'getfield'
    or     ntype == 'getmethod'
    or     ntype == 'getindex' then
        return nextSrc, 'get'
    end
    return nil
end

local function findFieldInTable(value, callback)
    if not value then
        return
    end
    if value.type ~= 'table' then
        return
    end
    for i = 1, #value do
        local field = value[i]
        if field.type == 'tablefield'
        or field.type == 'tableindex' then
            callback {
                source   = field,
                key      = guide.getKeyName(field),
                value    = field.value,
                mode     = 'set',
            }
        end
    end
end

local function ofENV(source, callback)
    local refs = source.ref
    if not refs then
        return
    end
    for i = 1, #refs do
        local ref = refs[i]
        if ref.type == 'getglobal' then
            callback {
                source = ref,
                key    = guide.getKeyName(ref),
                mode   = 'get',
            }
        elseif ref.type == 'setglobal' then
            callback {
                source = ref,
                key    = guide.getKeyName(ref),
                mode   = 'set',
            }
        end
        findFieldInTable(ref.value, callback)
    end
end

local function ofLocal(source, callback)
    if source.tag == '_ENV' then
        ofENV(source, callback)
    else
        vm.eachRef(source, function (info)
            local src = info.source
            findFieldInTable(src.value, callback)
            local nextSrc, mode = checkNext(src)
            if not nextSrc then
                return
            end
            callback {
                source = nextSrc,
                key    = guide.getKeyName(nextSrc),
                mode   = mode,
            }
        end)
    end
end

local function ofGlobal(source, callback)
    vm.eachRef(source, function (info)
        local src = info.source
        local nextSrc, mode = checkNext(src)
        if not nextSrc then
            return
        end
        callback {
            source = nextSrc,
            key    = guide.getKeyName(nextSrc),
            mode   = mode,
        }
        findFieldInTable(src.value, callback)
    end)
end

local function ofTable(source, callback)
    local parent = source.parent
    if parent and parent.value == source then
        return vm.eachField(parent, callback)
    else
        findFieldInTable(source, callback)
    end
end

function vm.eachField(source, callback)
    local stype = source.type
    if     stype == 'local' then
        ofLocal(source, callback)
    elseif stype == 'getlocal'
    or     stype == 'setlocal' then
        ofLocal(source.node, callback)
    elseif stype == 'getglobal'
    or     stype == 'setglobal' then
        ofGlobal(source, callback)
    elseif stype == 'table' then
        ofTable(source, callback)
    end
end