Skip to content

Commit 6c92b9a

Browse files
committed
stash
1 parent 5febd3a commit 6c92b9a

File tree

4 files changed

+66
-30
lines changed

4 files changed

+66
-30
lines changed

script/vm/compiler.lua

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,32 @@ local vm = require 'vm.vm'
99
---@class parser.object
1010
---@field _compiledNodes boolean
1111
---@field _node vm.node
12+
---@field package _hasBindedDocs? boolean
1213
---@field cindex integer
1314
---@field func parser.object
1415

1516
-- 该函数有副作用,会给source绑定node!
1617
---@param source parser.object
1718
---@return boolean
18-
local function bindDocs(source)
19+
function vm.bindDocs(source)
1920
local docs = source.bindDocs
2021
if not docs then
2122
return false
2223
end
24+
if source._hasBindedDocs ~= nil then
25+
return source._hasBindedDocs
26+
end
27+
source._hasBindedDocs = false
2328
for i = #docs, 1, -1 do
2429
local doc = docs[i]
2530
if doc.type == 'doc.type' then
2631
vm.setNode(source, vm.compileNode(doc))
32+
source._hasBindedDocs = true
2733
return true
2834
end
2935
if doc.type == 'doc.class' then
3036
vm.setNode(source, vm.compileNode(doc))
37+
source._hasBindedDocs = true
3138
return true
3239
end
3340
if doc.type == 'doc.param' then
@@ -36,23 +43,28 @@ local function bindDocs(source)
3643
node:addOptional()
3744
end
3845
vm.setNode(source, node)
46+
source._hasBindedDocs = true
3947
return true
4048
end
4149
if doc.type == 'doc.module' then
4250
local name = doc.module
4351
if not name then
52+
source._hasBindedDocs = true
4453
return true
4554
end
4655
local uri = rpath.findUrisByRequireName(guide.getUri(source), name)[1]
4756
if not uri then
57+
source._hasBindedDocs = true
4858
return true
4959
end
5060
local state = files.getState(uri)
5161
local ast = state and state.ast
5262
if not ast then
63+
source._hasBindedDocs = true
5364
return true
5465
end
5566
vm.setNode(source, vm.compileNode(ast))
67+
source._hasBindedDocs = true
5668
return true
5769
end
5870
end
@@ -90,7 +102,7 @@ local function searchFieldByLocalID(source, key, pushResult)
90102
local hasMarkDoc = {}
91103
for _, src in ipairs(fields) do
92104
if src.bindDocs then
93-
if bindDocs(src) then
105+
if vm.bindDocs(src) then
94106
local skey = guide.getKeyName(src)
95107
if skey then
96108
hasMarkDoc[skey] = true
@@ -170,7 +182,7 @@ local searchFieldSwitch = util.switch()
170182
hasFiled = true
171183
pushResult(field)
172184
end
173-
if key == nil then
185+
if key == vm.ANY then
174186
pushResult(field)
175187
end
176188
end
@@ -695,7 +707,7 @@ function vm.compileByParentNode(source, key, pushResult)
695707
pushResult(res)
696708
end
697709
end
698-
if #docedResults == 0 or key == nil then
710+
if #docedResults == 0 or key == vm.ANY then
699711
for _, res in ipairs(commonResults) do
700712
pushResult(res)
701713
end
@@ -970,7 +982,7 @@ local function compileLocal(source)
970982

971983
local hasMarkDoc
972984
if source.bindDocs then
973-
hasMarkDoc = bindDocs(source)
985+
hasMarkDoc = vm.bindDocs(source)
974986
end
975987
local hasMarkParam
976988
if not hasMarkDoc then
@@ -1035,7 +1047,7 @@ local function compileLocal(source)
10351047
-- for x = ... do
10361048
if source.parent.type == 'loop' then
10371049
if source.parent.loc == source then
1038-
if bindDocs(source) then
1050+
if vm.bindDocs(source) then
10391051
return
10401052
end
10411053
vm.setNode(source, vm.declareGlobal('type', 'integer'))
@@ -1199,7 +1211,7 @@ local compilerSwitch = util.switch()
11991211
end)
12001212
: case 'setlocal'
12011213
: call(function (source)
1202-
if bindDocs(source) then
1214+
if vm.bindDocs(source) then
12031215
return
12041216
end
12051217
local locNode = vm.compileNode(source.node)
@@ -1236,7 +1248,7 @@ local compilerSwitch = util.switch()
12361248
: case 'getmethod'
12371249
: case 'getindex'
12381250
: call(function (source)
1239-
if bindDocs(source) then
1251+
if vm.bindDocs(source) then
12401252
return
12411253
end
12421254
if guide.isGet(source) and bindAs(source) then
@@ -1278,7 +1290,7 @@ local compilerSwitch = util.switch()
12781290
end)
12791291
: case 'setglobal'
12801292
: call(function (source)
1281-
if bindDocs(source) then
1293+
if vm.bindDocs(source) then
12821294
return
12831295
end
12841296
if source.node[1] ~= '_ENV' then
@@ -1307,7 +1319,7 @@ local compilerSwitch = util.switch()
13071319
: call(function (source)
13081320
local hasMarkDoc
13091321
if source.bindDocs then
1310-
hasMarkDoc = bindDocs(source)
1322+
hasMarkDoc = vm.bindDocs(source)
13111323
end
13121324

13131325
if not hasMarkDoc then
@@ -1875,9 +1887,10 @@ function vm.compileNode(source)
18751887

18761888
---@cast source parser.object
18771889
vm.setNode(source, vm.createNode(), true)
1878-
vm.compileByGlobal(source)
1879-
vm.compileByVariable(source)
1880-
compileByNode(source)
1890+
if not vm.compileByGlobal(source) then
1891+
vm.compileByVariable(source)
1892+
compileByNode(source)
1893+
end
18811894
compileByParentNode(source)
18821895
matchCall(source)
18831896

script/vm/global.lua

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -540,31 +540,38 @@ function vm.getEnums(source)
540540
end
541541

542542
---@param source parser.object
543+
---@return boolean
543544
function vm.compileByGlobal(source)
544545
local global = vm.getGlobalNode(source)
545546
if not global then
546-
return
547+
return false
547548
end
548-
if global.cate == 'variable' then
549+
if global.cate == 'type' then
549550
vm.setNode(source, global)
550-
if guide.isAssign(source) then
551-
if source.value then
552-
vm.setNode(source, vm.compileNode(source.value))
553-
end
554-
return
551+
return false
552+
end
553+
vm.setNode(source, global)
554+
if guide.isAssign(source) then
555+
if vm.bindDocs(source) then
556+
return true
555557
end
556-
local node = vm.traceNode(source)
557-
if node then
558-
vm.setNode(source, node, true)
558+
if source.value then
559+
vm.setNode(source, vm.compileNode(source.value))
559560
end
560-
return
561+
return true
561562
end
562-
local globalBase = vm.getGlobalBase(source)
563-
if not globalBase then
564-
return
563+
local node = vm.traceNode(source)
564+
if node then
565+
vm.setNode(source, node, true)
566+
else
567+
local globalBase = vm.getGlobalBase(source)
568+
if not globalBase then
569+
return false
570+
end
571+
local globalNode = vm.compileNode(globalBase)
572+
vm.setNode(source, globalNode, true)
565573
end
566-
local globalNode = vm.compileNode(globalBase)
567-
vm.setNode(source, globalNode, true)
574+
return true
568575
end
569576

570577
---@param source parser.object

script/vm/variable.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,14 @@ function vm.getVariableFields(source, includeGets)
344344
end
345345

346346
---@param source parser.object
347+
---@return boolean
347348
function vm.compileByVariable(source)
348349
local variable = vm.getVariableNode(source)
349350
if not variable then
350-
return
351+
return false
351352
end
352353
vm.setNode(source, variable)
354+
return true
353355
end
354356

355357
---@param source parser.object

test/type_inference/init.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4175,3 +4175,17 @@ if xxx == <?t?> then
41754175
print(t)
41764176
end
41774177
]]
4178+
4179+
TEST 'V' [[
4180+
---@class V
4181+
X = 1
4182+
4183+
print(<?X?>)
4184+
]]
4185+
4186+
TEST 'V' [[
4187+
---@class V
4188+
X.Y = 1
4189+
4190+
print(X.<?Y?>)
4191+
]]

0 commit comments

Comments
 (0)