summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/src/core/diagnostics.lua23
-rw-r--r--server/src/vm/global.lua1
-rw-r--r--server/src/vm/value.lua36
-rw-r--r--server/test/diagnostics/init.lua2
4 files changed, 45 insertions, 17 deletions
diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua
index def54a35..7ebe297a 100644
--- a/server/src/core/diagnostics.lua
+++ b/server/src/core/diagnostics.lua
@@ -40,15 +40,14 @@ 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
+ local name = info[1]
+ definedGlobal[name] = true
end
end)
self.vm:eachSource(function (source)
@@ -59,14 +58,24 @@ function mt:searchUndefinedGlobal(callback)
if name == '' then
return
end
- if definedGlobal[name] then
- return
- end
local value = source:bindValue()
if not value then
return
end
- if definedValue[value] then
+ if not value:isGlobal() then
+ -- 上文重载了 _ENV 的情况
+ local ok = value:eachInfo(function (info)
+ if info.type == 'set' then
+ return true
+ end
+ end)
+ if ok then
+ return
+ end
+ callback(source.start, source.finish, name)
+ return
+ end
+ if definedGlobal[name] then
return
end
if type(name) ~= 'string' then
diff --git a/server/src/vm/global.lua b/server/src/vm/global.lua
index a775acd8..806c8931 100644
--- a/server/src/vm/global.lua
+++ b/server/src/vm/global.lua
@@ -12,6 +12,7 @@ return function (lsp)
end
global = t._G
+ global:markGlobal()
for k, v in pairs(t) do
global:setChild(k, v, sourceMgr.dummy())
end
diff --git a/server/src/vm/value.lua b/server/src/vm/value.lua
index 838bca15..7ac70181 100644
--- a/server/src/vm/value.lua
+++ b/server/src/vm/value.lua
@@ -10,6 +10,7 @@ mt.type = 'value'
mt.uri = ''
mt._infoCount = 0
mt._infoLimit = 10
+mt._global = false
local function create (tp, source, literal)
if tp == '...' then
@@ -97,6 +98,9 @@ function mt:rawSet(index, value, source)
self._child[index] = value
end
self:addInfo('set child', source, index, value)
+ if self._global then
+ value:markGlobal()
+ end
end
function mt:rawGet(index)
@@ -248,15 +252,15 @@ function mt:mergeValue(value)
end
end
value._type = self._type
- if value._child then
- if not self._child then
- self._child = {}
- end
- for k, v in pairs(value._child) do
- self._child[k] = v
- end
- end
- value._child = self._child
+ --if value._child then
+ -- if not self._child then
+ -- self._child = {}
+ -- end
+ -- for k, v in pairs(value._child) do
+ -- self._child[k] = v
+ -- end
+ --end
+ --value._child = self._child
for srcId, info in pairs(value._info) do
local src = sourceMgr.list[srcId]
@@ -383,4 +387,18 @@ function mt:getSource()
return sourceMgr.list[self.source]
end
+function mt:markGlobal()
+ if self._global then
+ return
+ end
+ self._global = true
+ self:rawEach(function (index, value)
+ value:markGlobal()
+ end)
+end
+
+function mt:isGlobal()
+ return self._global
+end
+
return create
diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua
index 12626eb7..4af8f975 100644
--- a/server/test/diagnostics/init.lua
+++ b/server/test/diagnostics/init.lua
@@ -72,7 +72,7 @@ print(<!X!>)
print(<!Log!>)
print(_VERSION)
print(<!y!>)
-print(<!z!>)
+print(z)
z = 1
]]