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

local function checkNext(source, callback)
    local nextSrc = source.next
    if not nextSrc then
        return
    end
    local ntype = nextSrc.type
    if ntype == 'setfield'
    or ntype == 'setmethod'
    or ntype == 'getfield'
    or ntype == 'getmethod' then
        callback(nextSrc)
    end
    if ntype == 'setindex'
    or ntype == 'getindex' then
        if nextSrc.node == source then
            callback(nextSrc)
        end
    end
    return
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'
        or ref.type == 'setglobal' then
            callback(ref)
            if guide.getName(ref) == '_G' then
                checkNext(ref, callback)
                vm.checkMetaValue(ref, callback)
                local call, index = vm.getArgInfo(ref)
                local special = vm.getSpecial(call)
                if (special == 'rawset' or special == 'rawget')
                and index == 1 then
                    callback(call.next)
                end
            end
        elseif ref.type == 'getlocal' then
            checkNext(ref, callback)
            vm.checkMetaValue(ref, callback)
        end
        vm.eachFieldInTable(ref.value, callback)
    end
end

local function ofLocal(source, callback)
    if source.tag == '_ENV' then
        ofENV(source, callback)
    else
        vm.eachRef(source, function (src)
            checkNext(src, callback)
            vm.checkMetaValue(src, callback)
            vm.eachFieldInTable(src.value, callback)
        end)
    end
end

local function ofGlobal(source, callback)
    vm.eachRef(source, function (src)
        checkNext(src, callback)
        vm.checkMetaValue(src, callback)
        vm.eachFieldInTable(src.value, callback)
    end)
end

local function ofGetField(source, callback)
    vm.eachRef(source, function (src)
        checkNext(src, callback)
        vm.checkMetaValue(src, callback)
        vm.eachFieldInTable(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
        vm.eachFieldInTable(source, callback)
    end
end

local function ofTableField(source, callback)
    vm.eachRef(source, function (src)
        checkNext(src, callback)
        vm.checkMetaValue(src, callback)
    end)
end

function vm.eachFieldInTable(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(field)
            vm.checkMetaValue(field, callback)
        end
    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)
    elseif stype == 'tablefield' then
        ofTableField(source, callback)
    elseif stype == 'getfield'
    or     stype == 'setfield'
    or     stype == 'getmethod'
    or     stype == 'setmethod'
    or     stype == 'getindex'
    or     stype == 'setindex' then
        ofGetField(source, callback)
    end
end