Skip to content

Commit 709ec79

Browse files
committed
stash
1 parent 0849356 commit 709ec79

File tree

4 files changed

+99
-54
lines changed

4 files changed

+99
-54
lines changed

script/vm/compiler.lua

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,29 +1252,29 @@ local compilerSwitch = util.switch()
12521252
end
12531253
end
12541254
else
1255-
if guide.isAssign(source) then
1256-
---@cast key string
1257-
vm.compileByParentNode(source.node, key, function (src)
1258-
if src.value then
1259-
if bindDocs(src) then
1260-
vm.setNode(source, vm.compileNode(src))
1261-
elseif src.value.type ~= 'nil' then
1262-
vm.setNode(source, vm.compileNode(src.value))
1263-
local node = vm.getNode(src)
1264-
if node then
1265-
vm.setNode(source, node)
1266-
end
1267-
end
1268-
else
1269-
vm.setNode(source, vm.compileNode(src))
1270-
end
1271-
end)
1272-
else
1255+
if guide.isGet(source) then
12731256
local node = vm.traceNode(source)
12741257
if node then
12751258
vm.setNode(source, node)
1259+
return
12761260
end
12771261
end
1262+
---@cast key string
1263+
vm.compileByParentNode(source.node, key, function (src)
1264+
if src.value then
1265+
if bindDocs(src) then
1266+
vm.setNode(source, vm.compileNode(src))
1267+
elseif src.value.type ~= 'nil' then
1268+
vm.setNode(source, vm.compileNode(src.value))
1269+
local node = vm.getNode(src)
1270+
if node then
1271+
vm.setNode(source, node)
1272+
end
1273+
end
1274+
else
1275+
vm.setNode(source, vm.compileNode(src))
1276+
end
1277+
end)
12781278
end
12791279
end)
12801280
: case 'setglobal'

script/vm/doc.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ local guide = require 'parser.guide'
44
local vm = require 'vm.vm'
55
local config = require 'config'
66

7+
---@class parser.object
8+
---@field package _castTargetHead parser.object | vm.global | false
9+
710
---获取class与alias
811
---@param suri uri
912
---@param name? string
@@ -414,3 +417,27 @@ function vm.isDiagDisabledAt(uri, position, name, err)
414417
end
415418
return count > 0
416419
end
420+
421+
---@param doc parser.object
422+
---@return (parser.object | vm.global)?
423+
function vm.getCastTargetHead(doc)
424+
if doc._castTargetHead ~= nil then
425+
return doc._castTargetHead or nil
426+
end
427+
local name = doc.name[1]:match '^[^%.]+'
428+
if not name then
429+
doc._castTargetHead = false
430+
return nil
431+
end
432+
local loc = guide.getLocal(doc, name, doc.start)
433+
if loc then
434+
doc._castTargetHead = loc
435+
return loc
436+
end
437+
local global = vm.getGlobal('variable', name)
438+
if global then
439+
doc._castTargetHead = global
440+
return global
441+
end
442+
return nil
443+
end

script/vm/tracer.lua

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ local util = require 'utility'
77
---@field package _tracer? vm.tracer
88
---@field package _casts? parser.object[]
99

10+
---@alias tracer.mode 'local' | 'global'
11+
1012
---@class vm.tracer
13+
---@field mode tracer.mode
1114
---@field name string
1215
---@field source parser.object
1316
---@field assigns parser.object[]
@@ -94,21 +97,24 @@ function mt:collectLocal()
9497
self.assigns[#self.assigns+1] = self.source
9598
self.assignMap[self.source] = true
9699

97-
for _, obj in ipairs(self.source.ref) do
98-
if obj.type == 'setlocal' then
99-
self.assigns[#self.assigns+1] = obj
100-
self.assignMap[obj] = true
101-
self:collectCare(obj)
102-
if obj.finish > finishPos then
103-
finishPos = obj.finish
104-
end
100+
local varInfo = vm.getVariableInfoByName(self.source, self.name)
101+
102+
assert(varInfo)
103+
104+
for _, set in ipairs(varInfo.sets) do
105+
self.assigns[#self.assigns+1] = set
106+
self.assignMap[set] = true
107+
self:collectCare(set)
108+
if set.finish > finishPos then
109+
finishPos = set.finish
105110
end
106-
if obj.type == 'getlocal' then
107-
self:collectCare(obj)
108-
self.getMap[obj] = true
109-
if obj.finish > finishPos then
110-
finishPos = obj.finish
111-
end
111+
end
112+
113+
for _, get in ipairs(varInfo.gets) do
114+
self:collectCare(get)
115+
self.getMap[get] = true
116+
if get.finish > finishPos then
117+
finishPos = get.finish
112118
end
113119
end
114120

@@ -117,7 +123,7 @@ function mt:collectLocal()
117123
if cast.name[1] == self.name
118124
and cast.start > startPos
119125
and cast.finish < finishPos
120-
and guide.getLocal(self.source, self.name, cast.start) == self.source then
126+
and vm.getCastTargetHead(cast) == self.source then
121127
self.casts[#self.casts+1] = cast
122128
end
123129
end
@@ -128,9 +134,6 @@ function mt:collectLocal()
128134
end
129135

130136
function mt:collectGlobal()
131-
local startPos = 0
132-
local finishPos = 0
133-
134137
self.assigns[#self.assigns+1] = self.source
135138
self.assignMap[self.source] = true
136139

@@ -142,23 +145,20 @@ function mt:collectGlobal()
142145
self.assigns[#self.assigns+1] = set
143146
self.assignMap[set] = true
144147
self:collectCare(set)
145-
if set.finish > finishPos then
146-
finishPos = set.finish
147-
end
148148
end
149149

150150
for _, get in ipairs(link.gets) do
151151
self:collectCare(get)
152152
self.getMap[get] = true
153-
if get.finish > finishPos then
154-
finishPos = get.finish
155-
end
156153
end
157154

158155
local casts = self:getCasts()
159156
for _, cast in ipairs(casts) do
160157
if cast.name[1] == self.name then
161-
self.casts[#self.casts+1] = cast
158+
local castTarget = vm.getCastTargetHead(cast)
159+
if castTarget and castTarget.type == 'global' then
160+
self.casts[#self.casts+1] = cast
161+
end
162162
end
163163
end
164164

@@ -780,10 +780,11 @@ end
780780
---@class vm.node
781781
---@field package _tracer vm.tracer
782782

783+
---@param mode tracer.mode
783784
---@param source parser.object
784785
---@param name string
785786
---@return vm.tracer?
786-
local function createTracer(source, name)
787+
local function createTracer(mode, source, name)
787788
local node = vm.compileNode(source)
788789
local tracer = node._tracer
789790
if tracer then
@@ -795,6 +796,7 @@ local function createTracer(source, name)
795796
end
796797
tracer = setmetatable({
797798
source = source,
799+
mode = mode,
798800
name = name,
799801
assigns = {},
800802
assignMap = {},
@@ -808,8 +810,7 @@ local function createTracer(source, name)
808810
}, mt)
809811
node._tracer = tracer
810812

811-
if source.type == 'local'
812-
or source.type == 'self' then
813+
if tracer.mode == 'local' then
813814
tracer:collectLocal()
814815
else
815816
tracer:collectGlobal()
@@ -821,17 +822,13 @@ end
821822
---@param source parser.object
822823
---@return vm.node?
823824
function vm.traceNode(source)
824-
local base, name
825-
if source.type == 'getlocal'
826-
or source.type == 'setlocal' then
827-
base = source.node
828-
---@type string
829-
name = source[1]
830-
elseif vm.getGlobalNode(source) then
825+
local mode, base, name
826+
if vm.getGlobalNode(source) then
831827
base = vm.getGlobalBase(source)
832828
if not base then
833829
return nil
834830
end
831+
mode = 'global'
835832
name = base.global:getCodeName()
836833
else
837834
base = vm.getVariableHead(source)
@@ -842,8 +839,9 @@ function vm.traceNode(source)
842839
if not name then
843840
return nil
844841
end
842+
mode = 'local'
845843
end
846-
local tracer = createTracer(base, name)
844+
local tracer = createTracer(mode, base, name)
847845
if not tracer then
848846
return nil
849847
end

script/vm/variable-id.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,26 @@ function vm.getVariableInfo(source, key)
201201
return root._variableIDs[id]
202202
end
203203

204+
---@param source parser.object
205+
---@param name string
206+
---@return vm.variable?
207+
function vm.getVariableInfoByName(source, name)
208+
local id = vm.getVariableID(source)
209+
if not id then
210+
return nil
211+
end
212+
local root = guide.getRoot(source)
213+
if not root._variableIDs then
214+
return nil
215+
end
216+
local headPos = name:find('.', 1, true)
217+
if not headPos then
218+
return root._variableIDs[id]
219+
end
220+
local vid = id .. name:sub(headPos):gsub('%.', vm.ID_SPLITE)
221+
return root._variableIDs[vid]
222+
end
223+
204224
---@param source parser.object
205225
---@param key? string
206226
---@return parser.object[]?

0 commit comments

Comments
 (0)