summaryrefslogtreecommitdiff
path: root/server/src/core/global.lua
blob: ba4fdc472c314bdd021502c48b5994b89f2601c1 (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
local mt = {}
mt.__index = mt

function mt:clearGlobal(uri)
    self.get[uri] = nil
    self.set[uri] = nil
end

function mt:markSet(uri, k, v)
    local sets = self.set[uri]
    if not sets then
        sets = {}
        self.set[uri] = sets
    end
    sets[k] = v
end

function mt:markGet(uri, k)
    local gets = self.get[uri]
    if not gets then
        gets = {}
        self.get[uri] = gets
    end
    gets[k] = true
end

function mt:compileVM(uri, vm)
    local seted = {}
    for k, v in next, vm.env.child do
        local get, set
        for _, info in ipairs(v) do
            if info.type == 'get' then
                get = true
            elseif info.type == 'set' then
                set = true
            end
        end
        if set then
            self:markSet(uri, k, v)
            seted[#seted+1] = k
        elseif get then
            self:markGet(uri, k)
        end
    end

    local needReCompile = {}
    for otherUri, gets in pairs(self.get) do
        for _, key in ipairs(seted) do
            if gets[key] ~= nil then
                needReCompile[#needReCompile+1] = otherUri
                goto CONTINUE
            end
        end
        ::CONTINUE::
    end

    return needReCompile
end

function mt:getGlobal(key)
    for _, sets in pairs(self.set) do
        local v = sets[key]
        if v ~= nil then
            return v
        end
    end
    return nil
end

return function ()
    return setmetatable({
        get = {},
        set = {},
    }, mt)
end