summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-03-19 13:54:05 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-03-19 13:54:05 +0800
commite1cd2b6866daa172aa19a87019ef38e3daaa702b (patch)
tree50edd6edeef6f2b0a01e001ab772295a1afd0131 /server/src
parentb23a9a5f34874916ea402e6141f426f00294a334 (diff)
downloadlua-language-server-e1cd2b6866daa172aa19a87019ef38e3daaa702b.zip
修改全局变量诊断的实现
Diffstat (limited to 'server/src')
-rw-r--r--server/src/core/diagnostics.lua36
-rw-r--r--server/src/service.lua88
-rw-r--r--server/src/vm/global.lua2
-rw-r--r--server/src/vm/library.lua2
-rw-r--r--server/src/vm/source.lua3
-rw-r--r--server/src/vm/value.lua4
-rw-r--r--server/src/vm/vm.lua27
7 files changed, 86 insertions, 76 deletions
diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua
index 24f7132d..def54a35 100644
--- a/server/src/core/diagnostics.lua
+++ b/server/src/core/diagnostics.lua
@@ -39,38 +39,40 @@ function mt:searchUnusedLocals(callback)
end
function mt:searchUndefinedGlobal(callback)
+ local definedGlobal = {}
+ local definedValue = {}
+ for name in pairs(config.config.diagnostics.globals) do
+ definedGlobal[name] = true
+ end
+ local envValue = self.vm.env:getValue()
+ envValue:eachInfo(function (info)
+ if info.type == 'set child' then
+ local value = info[2]
+ definedValue[value] = true
+ end
+ end)
self.vm:eachSource(function (source)
if not source:get 'global' then
return
end
- local value = source:bindValue()
- if not value then
+ local name = source:getName()
+ if name == '' then
return
end
- if source:action() ~= 'get' then
+ if definedGlobal[name] then
return
end
- if value:getLib() then
+ local value = source:bindValue()
+ if not value then
return
end
- local name = source:getName()
- if name == '' then
+ if definedValue[value] then
return
end
if type(name) ~= 'string' then
return
end
- if config.config.diagnostics.globals[name] then
- return
- end
- local defined = value:eachInfo(function (info)
- if info.type == 'set' then
- return true
- end
- end)
- if not defined then
- callback(source.start, source.finish, name)
- end
+ callback(source.start, source.finish, name)
end)
end
diff --git a/server/src/service.lua b/server/src/service.lua
index 2026bfa9..42f0e1a0 100644
--- a/server/src/service.lua
+++ b/server/src/service.lua
@@ -8,6 +8,7 @@ local core = require 'core'
local lang = require 'language'
local updateTimer= require 'timer'
local buildVM = require 'vm'
+local source = require 'vm.source'
local ErrorCodes = {
-- Defined by JSON RPC
@@ -533,49 +534,56 @@ function mt:_loadProto()
end
end
-function mt:onTick()
- self:_loadProto()
- self:_doCompileTask()
-
- if os.clock() - self._clock >= 60 then
- self._clock = os.clock()
- local count = 0
- for _ in pairs(self._file) do
- count = count + 1
+function mt:_testMemory()
+ if os.clock() - self._clock < 60 then
+ return
+ end
+ self._clock = os.clock()
+ local cachedVM = 0
+ for _ in pairs(self._file) do
+ cachedVM = cachedVM + 1
+ end
+ local aliveVM = 0
+ local deadVM = 0
+ for vm in pairs(CachedVM) do
+ if vm:isRemoved() then
+ deadVM = deadVM + 1
+ else
+ aliveVM = aliveVM + 1
end
- local alive = 0
- local dead = 0
- for vm in pairs(CachedVM) do
- if vm:isRemoved() then
- dead = dead + 1
- else
- alive = alive + 1
- end
+ end
+
+ local alivedSource = 0
+ local deadSource = 0
+ for _, id in pairs(source.watch) do
+ if source.list[id] then
+ alivedSource = alivedSource + 1
+ else
+ deadSource = deadSource + 1
end
- local mem = collectgarbage 'count'
- log.debug(('\n\z
- State\n\z
- Mem: [%.3f]kb\n\z
- CachedVM: [%d]\n\z
- AlivedVM: [%d]\n\z
- DeadVM: [%d]'):format(
- mem,
- count,
- alive,
- dead
- ))
-
- --TODO source测试
- --local total = 0
- --local alive = 0
- --for source in pairs(CachedSource) do
- -- if not source:isDead() then
- -- alive = alive + 1
- -- end
- -- total = total + 1
- --end
- --log.debug(('CachedSource: %d/%d'):format(alive, total))
end
+ local mem = collectgarbage 'count'
+ log.debug(('\n\z
+ State\n\z
+ Mem: [%.3f]kb\n\z
+ CachedVM: [%d]\n\z
+ AlivedVM: [%d]\n\z
+ DeadVM: [%d]\n\z
+ AlivedSrc:[%d]\n\z
+ DeadSrc: [%d]'):format(
+ mem,
+ cachedVM,
+ aliveVM,
+ deadVM,
+ alivedSource,
+ deadSource
+ ))
+end
+
+function mt:onTick()
+ self:_loadProto()
+ self:_doCompileTask()
+ self:_testMemory()
end
function mt:listen()
diff --git a/server/src/vm/global.lua b/server/src/vm/global.lua
index b0f44901..8af38206 100644
--- a/server/src/vm/global.lua
+++ b/server/src/vm/global.lua
@@ -14,7 +14,7 @@ return function (lsp)
global = t._G
for k, v in pairs(t) do
global:setChild(k, v)
- global:addInfo('set child', sourceMgr.dummy(), k)
+ global:addInfo('set child', sourceMgr.dummy(), k, v)
end
end
if lsp then
diff --git a/server/src/vm/library.lua b/server/src/vm/library.lua
index 167ac726..24a17389 100644
--- a/server/src/vm/library.lua
+++ b/server/src/vm/library.lua
@@ -54,7 +54,7 @@ function buildLibValue(lib)
for fName, fLib in pairs(lib.child) do
local fValue = buildLibValue(fLib)
value:rawSet(fName, fValue)
- value:addInfo('set child', sourceMgr.dummy(), fName)
+ value:addInfo('set child', sourceMgr.dummy(), fName, fValue)
end
end
diff --git a/server/src/vm/source.lua b/server/src/vm/source.lua
index 0f167063..810db769 100644
--- a/server/src/vm/source.lua
+++ b/server/src/vm/source.lua
@@ -4,6 +4,7 @@ mt.uri = ''
local Id = 0
local List = {}
+local Watch = setmetatable({}, {__mode = 'k'})
function mt:bindLocal(loc, action)
if loc then
@@ -99,6 +100,7 @@ local function instant(source)
Id = Id + 1
source.id = Id
List[Id] = source
+ Watch[source] = Id
setmetatable(source, mt)
return true
end
@@ -115,5 +117,6 @@ end
return {
instant = instant,
list = List,
+ watch = Watch,
dummy = dummy,
}
diff --git a/server/src/vm/value.lua b/server/src/vm/value.lua
index 9b33afe1..a1ee897b 100644
--- a/server/src/vm/value.lua
+++ b/server/src/vm/value.lua
@@ -39,10 +39,6 @@ local function create (tp, source, literal)
end
local function isDeadChild(value, index)
- local child = value._child[index]
- if not child:getSource() then
- return true
- end
for srcId, info in pairs(value._info) do
local src = sourceMgr.list[srcId]
if src
diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua
index 2a81372b..48736fe4 100644
--- a/server/src/vm/vm.lua
+++ b/server/src/vm/vm.lua
@@ -46,13 +46,13 @@ function mt:buildTable(source)
if key.index then
local index = self:getIndex(obj)
key:set('parent', tbl)
- tbl:addInfo('set child', key, index)
+ tbl:addInfo('set child', key, index, value)
tbl:setChild(index, value)
else
if key.type == 'name' then
key:set('parent', tbl)
key:set('table index', true)
- tbl:addInfo('set child', key, key[1])
+ tbl:addInfo('set child', key, key[1], value)
tbl:setChild(key[1], value)
end
end
@@ -62,17 +62,18 @@ function mt:buildTable(source)
if index == #source then
value:eachValue(function (_, v)
n = n + 1
- tbl:addInfo('set child', obj, n)
+ tbl:addInfo('set child', obj, n, v)
tbl:setChild(n, v)
end)
else
n = n + 1
- tbl:addInfo('set child', obj, n)
- tbl:setChild(n, self:getFirstInMulti(value))
+ local v = self:getFirstInMulti(value)
+ tbl:addInfo('set child', obj, n, v)
+ tbl:setChild(n, v)
end
else
n = n + 1
- tbl:addInfo('set child', obj, n)
+ tbl:addInfo('set child', obj, n, value)
tbl:setChild(n, value)
end
-- 处理写了一半的 key = value,把name类的数组元素视为哈希键
@@ -387,7 +388,7 @@ function mt:setName(name, source, value)
local ENV = self:loadLocal('_ENV')
local ENVValue = ENV:getValue()
source:bindValue(value, 'set')
- ENVValue:addInfo('set child', source, name)
+ ENVValue:addInfo('set child', source, name, value)
ENVValue:setChild(name, value)
source:set('global', true)
source:set('parentValue', ENVValue)
@@ -786,12 +787,12 @@ function mt:setOne(var, value)
if key.type == 'index' then
local index = self:getIndex(key)
key[1]:set('parent', parent)
- parent:addInfo('set child', key[1], index)
+ parent:addInfo('set child', key[1], index, value)
parent:setChild(index, value)
elseif key.type == 'name' then
local index = key[1]
key:set('parent', parent)
- parent:addInfo('set child', key, index)
+ parent:addInfo('set child', key, index, value)
parent:setChild(index, value)
end
key:bindValue(value, 'set')
@@ -921,11 +922,11 @@ function mt:doFunction(action)
source:set('object', parent)
if source.type == 'index' then
local index = self:getIndex(source)
- parent:addInfo('set child', source[1], index)
+ parent:addInfo('set child', source[1], index, value)
parent:setChild(index, value)
elseif source.type == 'name' then
local index = source[1]
- parent:addInfo('set child', source, index)
+ parent:addInfo('set child', source, index, value)
parent:setChild(index, value)
end
source:bindValue(value, 'set')
@@ -948,11 +949,11 @@ function mt:doFunction(action)
self:instantSource(source)
if source.type == 'index' then
local index = self:getIndex(source)
- parent:addInfo('set child', source[1], index)
+ parent:addInfo('set child', source[1], index, value)
parent:setChild(index, value)
elseif source.type == 'name' then
local index = source[1]
- parent:addInfo('set child', source, index)
+ parent:addInfo('set child', source, index, value)
parent:setChild(index, value)
end
source:bindValue(value, 'set')