summaryrefslogtreecommitdiff
path: root/script/vm/global.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-03-02 20:51:58 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-03-02 20:51:58 +0800
commit98ec375b0d758a4bd206149aef5f9d2d84878962 (patch)
treee9dddc46773b5b536b16c5c7df0fe0662c55c4c7 /script/vm/global.lua
parentbbf4d92a166bb8676f8c7e9a5f735a81c5ed2b03 (diff)
downloadlua-language-server-98ec375b0d758a4bd206149aef5f9d2d84878962.zip
cleanup
Diffstat (limited to 'script/vm/global.lua')
-rw-r--r--script/vm/global.lua84
1 files changed, 84 insertions, 0 deletions
diff --git a/script/vm/global.lua b/script/vm/global.lua
new file mode 100644
index 00000000..d5974e63
--- /dev/null
+++ b/script/vm/global.lua
@@ -0,0 +1,84 @@
+local util = require 'utility'
+
+---@class vm.node.global.link
+---@field gets parser.object[]
+---@field sets parser.object[]
+
+---@class vm.node.global
+---@field links table<uri, vm.node.global.link>
+---@field setsCache parser.object[]
+---@field getsCache parser.object[]
+local mt = {}
+mt.__index = mt
+mt.type = 'global'
+mt.name = ''
+
+---@param uri uri
+---@param source parser.object
+function mt:addSet(uri, source)
+ local link = self.links[uri]
+ link.sets[#link.sets+1] = source
+end
+
+---@param uri uri
+---@param source parser.object
+function mt:addGet(uri, source)
+ local link = self.links[uri]
+ link.gets[#link.gets+1] = source
+end
+
+---@return parser.object[]
+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
+
+---@return parser.object[]
+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
+
+---@param uri uri
+function mt:dropUri(uri)
+ self.links[uri] = nil
+ self.setsCache = nil
+ self.getsCache = nil
+end
+
+---@return string
+function mt:getName()
+ return self.name
+end
+
+---@return boolean
+function mt:isAlive()
+ return next(self.links) ~= nil
+end
+
+---@return vm.node.global
+return function (name)
+ return setmetatable({
+ name = name,
+ links = util.defaultTable(function ()
+ return {
+ sets = {},
+ gets = {},
+ }
+ end),
+ }, mt)
+end