diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-03-13 17:18:32 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-03-13 17:18:32 +0800 |
commit | 628a272b7babbeab995cf582ff4337b897ac09e6 (patch) | |
tree | de859adf802e7ea941cf990007e40d98a1fee77b /server/src/vm | |
parent | 42ec4d53d770a053c0909f14b5aa7c8cd42bbcdb (diff) | |
download | lua-language-server-628a272b7babbeab995cf582ff4337b897ac09e6.zip |
解除value->info->source的循环引用
Diffstat (limited to 'server/src/vm')
-rw-r--r-- | server/src/vm/source.lua | 17 | ||||
-rw-r--r-- | server/src/vm/value.lua | 51 | ||||
-rw-r--r-- | server/src/vm/vm.lua | 4 |
3 files changed, 46 insertions, 26 deletions
diff --git a/server/src/vm/source.lua b/server/src/vm/source.lua index d53019c7..8f65da8b 100644 --- a/server/src/vm/source.lua +++ b/server/src/vm/source.lua @@ -1,6 +1,8 @@ local mt = {} mt.__index = mt -mt._hasInstant = true + +local Id = 0 +local List = {} function mt:bindLocal(loc, action) if loc then @@ -87,16 +89,25 @@ function mt:kill() self._bindCall = nil self._bindFunction = nil self._bindCallArgs = nil + List[self.id] = nil end function mt:isDead() return self._dead end -return function (vm, source) - if source._hasInstant then +local function instant(vm, source) + if source.id then return false end + Id = Id + 1 + source.id = Id + List[Id] = source setmetatable(source, mt) return true end + +return { + instant = instant, + list = List, +} diff --git a/server/src/vm/value.lua b/server/src/vm/value.lua index 2463e646..f8d3c4b2 100644 --- a/server/src/vm/value.lua +++ b/server/src/vm/value.lua @@ -1,5 +1,6 @@ local libraryBuilder = require 'vm.library' local library = require 'core.library' +local sourceMgr = require 'vm.source' local function getDefaultSource() return { @@ -229,13 +230,14 @@ function mt:mergeValue(value) end end - for source, info in pairs(value._info) do - if source:isDead() then - value._info[source] = nil - value._infoCount = value._infoCount - 1 - else + for srcId, info in pairs(value._info) do + local src = sourceMgr.list[srcId] + if src then self._infoCount = self._infoCount + 1 - self._info[source] = info + self._info[srcId] = info + else + value._info[srcId] = nil + value._infoCount = value._infoCount - 1 end end if value._meta then @@ -253,29 +255,34 @@ function mt:mergeValue(value) end function mt:addInfo(tp, source, ...) - if source and not source.start then + if not source then + return + end + if not source.start then error('Miss start: ' .. table.dump(source)) end - if self._info[source] then - return + local id = source.id + if not id then + error('Not instanted source') end - if not source or not source._hasInstant then + if self._info[id] then return end Sort = Sort + 1 local info = { type = tp, - source = source, + source = id, _sort = Sort, ... } - self._info[source] = info + self._info[id] = info self._infoCount = self._infoCount + 1 if self._infoCount > self._infoLimit then - for src in pairs(self._info) do - if src:isDead() then - self._info[src] = nil + for srcId in pairs(self._info) do + local src = sourceMgr.list[srcId] + if not src then + self._info[srcId] = nil self._infoCount = self._infoCount - 1 end end @@ -288,19 +295,21 @@ end function mt:eachInfo(callback) local list = {} - for source, info in pairs(self._info) do - if source:isDead() then - self._info[source] = nil - self._infoCount = self._infoCount - 1 - else + for srcId, info in pairs(self._info) do + local src = sourceMgr.list[srcId] + if src then list[#list+1] = info + else + self._info[srcId] = nil + self._infoCount = self._infoCount - 1 end end table.sort(list, function (a, b) return a._sort < b._sort end) for i = 1, #list do - local res = callback(list[i]) + local info = list[i] + local res = callback(info, sourceMgr.list[info.source]) if res ~= nil then return res end diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua index d9d29886..d89a9a56 100644 --- a/server/src/vm/vm.lua +++ b/server/src/vm/vm.lua @@ -3,7 +3,7 @@ local createValue = require 'vm.value' local createLocal = require 'vm.local' local createLabel = require 'vm.label' local createFunction = require 'vm.function' -local instantSource = require 'vm.source' +local sourceMgr = require 'vm.source' local buildGlobal = require 'vm.global' local createMulti = require 'vm.multi' local libraryBuilder = require 'vm.library' @@ -1074,7 +1074,7 @@ function mt:getUri() end function mt:instantSource(source) - if instantSource(self, source) then + if sourceMgr.instant(self, source) then source:setUri(self:getUri()) self.sources[#self.sources+1] = source CachedSource[source] = true |