summaryrefslogtreecommitdiff
path: root/script/vm/node/global.lua
blob: 0e293ca3467a9371f512811db8b8a130cef74e78 (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
local util = require 'utility'

---@class vm.node.global
---@field links table<uri, { gets: table, sets: table }>
---@field setsCache parser.guide.object[]
---@field getsCache parser.guide.object[]
local mt = {}
mt.__index = mt
mt.type = 'global'
mt.name = ''

function mt:addSet(uri, source)
    local link = self.links[uri]
    link.sets[#link.sets+1] = source
end

function mt:addGet(uri, source)
    local link = self.links[uri]
    link.gets[#link.gets+1] = source
end

function mt:getSets()
    if not self.setsCache then
        self.setsCache = {}
        for _, link in pairs(self.links) do
            for _, source in ipairs(link.sets) do
                self.setsCache[#self.setsCache+1] = source
            end
        end
    end
    return self.setsCache
end

function mt:getGets()
    if not self.getsCache then
        self.getsCache = {}
        for _, link in pairs(self.links) do
            for _, source in ipairs(link.gets) do
                self.getsCache[#self.getsCache+1] = source
            end
        end
    end
    return self.getsCache
end

function mt:dropUri(uri)
    self.links[uri] = nil
    self.setsCache = nil
    self.getsCache = nil
end

---@return vm.node.global
return function (name)
    local global = setmetatable({
        name  = name,
        links = util.defaultTable(function ()
            return {
                sets = {},
                gets = {},
            }
        end),
    }, mt)
    return global
end