diff options
-rw-r--r-- | script/cli/doc.lua | 2 | ||||
-rw-r--r-- | script/core/workspace-symbol.lua | 2 | ||||
-rw-r--r-- | script/vm/compiler.lua | 67 | ||||
-rw-r--r-- | script/vm/field.lua | 2 | ||||
-rw-r--r-- | script/vm/type.lua | 4 | ||||
-rw-r--r-- | script/vm/variable.lua | 3 |
6 files changed, 43 insertions, 37 deletions
diff --git a/script/cli/doc.lua b/script/cli/doc.lua index e4448927..3790d43b 100644 --- a/script/cli/doc.lua +++ b/script/cli/doc.lua @@ -149,7 +149,7 @@ local function collect(global) results[#results+1] = result ---@async ---@diagnostic disable-next-line: not-yieldable - vm.getClassFields(rootUri, global, nil, function (source) + vm.getClassFields(rootUri, global, vm.ANY, function (source) if source.type == 'doc.field' then ---@cast source parser.object if files.isLibrary(guide.getUri(source)) then diff --git a/script/core/workspace-symbol.lua b/script/core/workspace-symbol.lua index 1773b34d..a41cfc1d 100644 --- a/script/core/workspace-symbol.lua +++ b/script/core/workspace-symbol.lua @@ -116,7 +116,7 @@ local function searchClassField(key, suri, results) return end suri = suri or guide.getUri(set) - vm.getClassFields(suri, global, nil, function (field) + vm.getClassFields(suri, global, vm.ANY, function (field) if field.type == 'generic' then return end diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index dc81e836..b4378a98 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -60,16 +60,16 @@ local function bindDocs(source) end ---@param source parser.object | vm.variable ----@param key any +---@param key string|vm.global|vm.ANY ---@param pushResult fun(res: parser.object, markDoc?: boolean) local function searchFieldByLocalID(source, key, pushResult) local fields - if key then + if key ~= vm.ANY then + if type(key) ~= 'string' then + return + end if source.type == 'variable' then ---@cast source vm.variable - if type(key) ~= 'string' then - return - end fields = source:getSets(key) else ---@cast source parser.object @@ -109,7 +109,7 @@ end ---@param suri uri ---@param source parser.object ----@param key any +---@param key string|vm.global|vm.ANY ---@param pushResult fun(res: parser.object, markDoc?: boolean) local function searchFieldByGlobalID(suri, source, key, pushResult) local node = vm.getGlobalNode(source) @@ -117,7 +117,7 @@ local function searchFieldByGlobalID(suri, source, key, pushResult) return end if node.cate == 'variable' then - if key then + if key ~= vm.ANY then if type(key) ~= 'string' then return end @@ -149,14 +149,14 @@ local searchFieldSwitch = util.switch() if field.type == 'tablefield' or field.type == 'tableindex' then local fieldKey = guide.getKeyName(field) - if key == nil + if key == vm.ANY or key == fieldKey then hasFiled = true pushResult(field) end end if field.type == 'tableexp' then - if key == nil + if key == vm.ANY or key == field.tindex then hasFiled = true pushResult(field) @@ -211,7 +211,7 @@ local searchFieldSwitch = util.switch() local fieldNode = vm.compileNode(fieldKey) for fn in fieldNode:eachObject() do if fn.type == 'global' and fn.cate == 'type' then - if key == nil + if key == vm.ANY or fn.name == 'any' or (fn.name == 'boolean' and type(key) == 'boolean') or (fn.name == 'number' and type(key) == 'number') @@ -222,7 +222,7 @@ local searchFieldSwitch = util.switch() elseif fn.type == 'doc.type.string' or fn.type == 'doc.type.integer' or fn.type == 'doc.type.boolean' then - if key == nil + if key == vm.ANY or fn[1] == key then pushResult(field) end @@ -230,7 +230,7 @@ local searchFieldSwitch = util.switch() end end if fieldKey.type == 'doc.field.name' then - if key == nil or fieldKey[1] == key then + if key == vm.ANY or fieldKey[1] == key then pushResult(field) end end @@ -269,7 +269,7 @@ local searchFieldSwitch = util.switch() ---@param suri uri ---@param object vm.global ----@param key? string|number|integer|boolean|vm.global +---@param key string|number|integer|boolean|vm.global|vm.ANY ---@param pushResult fun(field: vm.object, isMark?: boolean) function vm.getClassFields(suri, object, key, pushResult) local mark = {} @@ -298,7 +298,7 @@ function vm.getClassFields(suri, object, key, pushResult) local fieldKey = guide.getKeyName(field) if fieldKey then -- ---@field x boolean -> class.x - if key == nil + if key == vm.ANY or fieldKey == key then if not searchedFields[fieldKey] then pushResult(field, true) @@ -307,7 +307,7 @@ function vm.getClassFields(suri, object, key, pushResult) end goto CONTINUE end - if key == nil then + if key == vm.ANY then pushResult(field, true) goto CONTINUE end @@ -444,7 +444,7 @@ function vm.getClassFields(suri, object, key, pushResult) local function searchGlobal(class) if class.cate == 'type' and class.name == '_G' then - if key == nil then + if key == vm.ANY then local sets = vm.getGlobalSets(suri, 'variable') for _, set in ipairs(sets) do pushResult(set) @@ -635,7 +635,7 @@ local function bindAs(source) end ---@param source parser.object | vm.variable ----@param key? string|vm.global +---@param key string|vm.global|vm.ANY ---@param pushResult fun(source: parser.object) function vm.compileByParentNode(source, key, pushResult) local parentNode = vm.compileNode(source) @@ -1277,14 +1277,13 @@ local compilerSwitch = util.switch() vm.setNode(source, vm.compileNode(src)) end) else - local hasDefinedField ---@cast key string - vm.compileByParentNode(source.node, key, function (src) - hasDefinedField = true - vm.setNode(source, vm.compileNode(src)) - end) - if not hasDefinedField and source.value then + if source.value then vm.setNode(source, vm.compileNode(source.value)) + else + vm.compileByParentNode(source.node, key, function (src) + vm.setNode(source, vm.compileNode(src)) + end) end end end @@ -1308,6 +1307,9 @@ local compilerSwitch = util.switch() return end local key = guide.getKeyName(source) + if not key then + return + end vm.compileByParentNode(source.node, key, function (src) vm.setNode(source, vm.compileNode(src)) end) @@ -1321,14 +1323,17 @@ local compilerSwitch = util.switch() end if not hasMarkDoc then - vm.compileByParentNode(source.node, guide.getKeyName(source), function (src) - if src.type == 'doc.field' - or src.type == 'doc.type.field' - or src.type == 'doc.type.name' then - hasMarkDoc = true - vm.setNode(source, vm.compileNode(src)) - end - end) + local key = guide.getKeyName(source) + if key then + vm.compileByParentNode(source.node, key, function (src) + if src.type == 'doc.field' + or src.type == 'doc.type.field' + or src.type == 'doc.type.name' then + hasMarkDoc = true + vm.setNode(source, vm.compileNode(src)) + end + end) + end end if not hasMarkDoc and source.value then diff --git a/script/vm/field.lua b/script/vm/field.lua index 9e7e9091..d41e886a 100644 --- a/script/vm/field.lua +++ b/script/vm/field.lua @@ -31,7 +31,7 @@ local function searchByNode(source, pushResult, mark) end mark[source] = true local uri = guide.getUri(source) - vm.compileByParentNode(source, nil, function (field) + vm.compileByParentNode(source, vm.ANY, function (field) searchByNodeSwitch(field.type, uri, field, pushResult) end) vm.compileByNodeChain(source, function (src) diff --git a/script/vm/type.lua b/script/vm/type.lua index 01f6c231..14a7fe6e 100644 --- a/script/vm/type.lua +++ b/script/vm/type.lua @@ -5,6 +5,10 @@ local config = require 'config.config' local util = require 'utility' local lang = require 'language' +---@class vm.ANY +---@diagnostic disable-next-line: assign-type-mismatch +vm.ANY = debug.upvalueid(require, 1) + ---@alias typecheck.err vm.node.object|string|vm.node ---@param object vm.node.object diff --git a/script/vm/variable.lua b/script/vm/variable.lua index 55d876da..539d8507 100644 --- a/script/vm/variable.lua +++ b/script/vm/variable.lua @@ -265,9 +265,6 @@ function vm.getVariable(source, key) if not key then return variable end - if type(key) ~= 'string' then - return nil - end local root = guide.getRoot(source) if not root._variableNodes then return nil |