summaryrefslogtreecommitdiff
path: root/script/vm
diff options
context:
space:
mode:
authorSewbacca <sebastian.kalus@kolabnow.com>2023-07-17 14:34:49 +0200
committerSewbacca <sebastian.kalus@kolabnow.com>2023-07-17 14:34:49 +0200
commit393f3e844d5164116de8bd739fc06d76bd03439f (patch)
tree2e1b514bdc38885623781089fba3ef327c76f39a /script/vm
parent62068c0709d2fd131607940852b0d938b43794b4 (diff)
downloadlua-language-server-393f3e844d5164116de8bd739fc06d76bd03439f.zip
Extracted undefined global check
Diffstat (limited to 'script/vm')
-rw-r--r--script/vm/global.lua28
-rw-r--r--script/vm/node.lua1
2 files changed, 29 insertions, 0 deletions
diff --git a/script/vm/global.lua b/script/vm/global.lua
index c1b5f320..687d53c8 100644
--- a/script/vm/global.lua
+++ b/script/vm/global.lua
@@ -1,6 +1,7 @@
local util = require 'utility'
local scope = require 'workspace.scope'
local guide = require 'parser.guide'
+local config = require 'config'
---@class vm
local vm = require 'vm.vm'
@@ -518,6 +519,33 @@ function vm.hasGlobalSets(suri, cate, name)
return true
end
+---@param src parser.object
+local function checkIsUndefinedGlobal(src)
+ local key = src[1]
+
+ local uri = guide.getUri(src)
+ local dglobals = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals'))
+ local rspecial = config.get(uri, 'Lua.runtime.special')
+
+ local node = src.node
+ return src.type == 'getglobal' and key and not (
+ dglobals[key] or
+ rspecial[key] or
+ node.tag ~= '_ENV' or
+ vm.hasGlobalSets(uri, 'variable', key)
+ )
+end
+
+---@param src parser.object
+---@return boolean
+function vm.isUndefinedGlobal(src)
+ local node = vm.compileNode(src)
+ if node._undefined_global == nil then
+ node._undefined_global = checkIsUndefinedGlobal(src)
+ end
+ return node._undefined_global
+end
+
---@param source parser.object
function compileObject(source)
if source._globalNode ~= nil then
diff --git a/script/vm/node.lua b/script/vm/node.lua
index 0ffd8c70..90ae839d 100644
--- a/script/vm/node.lua
+++ b/script/vm/node.lua
@@ -15,6 +15,7 @@ vm.nodeCache = setmetatable({}, util.MODE_K)
---@field [integer] vm.node.object
---@field [vm.node.object] true
---@field fields? table<vm.node|string, vm.node>
+---@field _undefined_global boolean?
local mt = {}
mt.__index = mt
mt.id = 0