summaryrefslogtreecommitdiff
path: root/server-beta/src/core/engineer.lua
blob: 675bd803f7b22b836f8099c3cd54a73de2757712 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
local guide       = require 'parser.guide'
local require     = require
local tableUnpack = table.unpack
local error       = error

local setmetatable = setmetatable

_ENV = nil
---@class engineer
local mt = {}
mt.__index = mt
mt.type = 'engineer'

mt['local']     = require 'core.local'
mt['getlocal']  = require 'core.getlocal'
mt['setlocal']  = mt['getlocal']
mt['getglobal'] = require 'core.getglobal'
mt['setglobal'] = mt['getglobal']
mt['field']     = require 'core.field'
mt['method']    = require 'core.method'
mt['index']     = require 'core.index'
mt['number']    = require 'core.number'
mt['boolean']   = require 'core.boolean'
mt['string']    = require 'core.string'
mt['table']     = require 'core.table'
mt['select']    = require 'core.select'

function mt:getSpecialName(source)
    if source.type == 'getglobal' then
        local node = source.node
        if node.tag ~= '_ENV' then
            return
        end
        local name = guide.getKeyName(source)
        if name:sub(1, 2) ~= 's|' then
            return nil
        end
        return name:sub(3)
    elseif source.type == 'local' then
        if source.tag == '_ENV' then
            return '_G'
        end
        return nil
    elseif source.type == 'getlocal' then
        local loc = source.loc
        if loc.tag == '_ENV' then
            return '_G'
        end
    end
end

function mt:eachSpecial(callback)
    local env = guide.getENV(self.ast)
    if not env then
        return
    end
    local refs = env.ref
    if not refs then
        return
    end
    for i = 1, #refs do
        local ref = refs[i]
        if ref.type == 'getglobal' then
            callback(ref[1], ref)
        end
    end
end

function mt:eachField(source, key, callback)
    local cache = self.cache.field[source]
    if cache and cache[key] then
        for i = 1, #cache[key] do
            callback(tableUnpack(cache[key][i]))
        end
        return
    end
    local tp = source.type
    local d = mt[tp]
    if not d then
        return
    end
    local f = d.field
    if not f then
        return
    end
    if self.step >= 100 then
        error('Stack overflow!')
        return
    end
    self.step = self.step + 1
    local mark = {}
    if not cache then
        cache = {}
        self.cache.field[source] = cache
    end
    cache[key] = {}
    f(self, source, key, function (src, ...)
        if mark[src] then
            return
        end
        mark[src] = true
        cache[key][#cache[key]+1] = { src, ... }
        callback(src, ...)
    end)
    self.step = self.step - 1
end

function mt:eachRef(source, callback)
    local cache = self.cache.ref[source]
    if cache then
        for i = 1, #cache do
            callback(tableUnpack(cache[i]))
        end
        return
    end
    local tp = source.type
    local d = mt[tp]
    if not d then
        return
    end
    local f = d.ref
    if not f then
        return
    end
    if self.step >= 100 then
        error('Stack overflow!')
        return
    end
    self.step = self.step + 1
    cache = {}
    self.cache.ref[source] = cache
    local mark = {}
    f(self, source, function (src, ...)
        if mark[src] then
            return
        end
        mark[src] = true
        cache[#cache+1] = {src, ...}
        callback(src, ...)
    end)
    self.step = self.step - 1
end

function mt:eachDef(source, callback)
    local cache = self.cache.def[source]
    if cache then
        for i = 1, #cache do
            callback(tableUnpack(cache[i]))
        end
        return
    end
    local tp = source.type
    local d = mt[tp]
    if not d then
        return
    end
    local f = d.def
    if not f then
        return
    end
    if self.step >= 100 then
        error('Stack overflow!')
        return
    end
    self.step = self.step + 1
    cache = {}
    self.cache.def[source] = cache
    local mark = {}
    f(self, source, function (src, ...)
        if mark[src] then
            return
        end
        mark[src] = true
        cache[#cache+1] = {src, ...}
        callback(src, ...)
    end)
    self.step = self.step - 1
end

function mt:eachValue(source, callback)
    local cache = self.cache.value[source]
    if cache then
        for i = 1, #cache do
            callback(tableUnpack(cache[i]))
        end
        return
    end
    local tp = source.type
    local d = mt[tp]
    if not d then
        return
    end
    local f = d.value
    if not f then
        return
    end
    if self.step >= 100 then
        error('Stack overflow!')
        return
    end
    self.step = self.step + 1
    cache = {}
    self.cache.value[source] = cache
    local mark = {}
    f(self, source, function (src, ...)
        if mark[src] then
            return
        end
        mark[src] = true
        cache[#cache+1] = {src, ...}
        callback(src, ...)
    end)
    self.step = self.step - 1
end

function mt:childDef(source, callback)
    local tp = source.type
    if     tp == 'setfield' then
        callback(source.field, 'set')
    elseif tp == 'setmethod' then
        callback(source.method, 'set')
    elseif tp == 'setindex' then
        callback(source.index, 'set')
    end
end

function mt:childRef(source, callback)
    local tp = source.type
    if     tp == 'setfield' then
        callback(source.field, 'set')
    elseif tp == 'setmethod' then
        callback(source.method, 'set')
    elseif tp == 'setindex' then
        callback(source.index, 'set')
    elseif tp == 'getfield' then
        callback(source.field, 'get')
    elseif tp == 'getmethod' then
        callback(source.method, 'get')
    elseif tp == 'getindex' then
        callback(source.index, 'get')
    end
end

function mt:callArgOf(source)
    if not source or source.type ~= 'call' then
        return
    end
    local args = source.args
    if not args then
        return
    end
    return tableUnpack(args)
end

function mt:callReturnOf(source)
    if not source or source.type ~= 'call' then
        return
    end
    local parent = source.parent
    local extParent = source.extParent
    if extParent then
        local returns = {parent.parent}
        for i = 1, #extParent do
            returns[i+1] = extParent[i].parent
        end
        return tableUnpack(returns)
    elseif parent then
        return parent.parent
    end
end

return function (ast)
    local self = setmetatable({
        step  = 0,
        ast   = ast.ast,
        cache = {
            def   = {},
            ref   = {},
            field = {},
            value = {},
        },
    }, mt)
    return self
end