Skip to content

Commit 0849356

Browse files
committed
stash
1 parent 158b94b commit 0849356

File tree

3 files changed

+79
-29
lines changed

3 files changed

+79
-29
lines changed

script/vm/compiler.lua

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,22 +1252,29 @@ local compilerSwitch = util.switch()
12521252
end
12531253
end
12541254
else
1255-
---@cast key string
1256-
vm.compileByParentNode(source.node, key, function (src)
1257-
if src.value then
1258-
if bindDocs(src) then
1259-
vm.setNode(source, vm.compileNode(src))
1260-
elseif src.value.type ~= 'nil' then
1261-
vm.setNode(source, vm.compileNode(src.value))
1262-
local node = vm.getNode(src)
1263-
if node then
1264-
vm.setNode(source, node)
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
12651267
end
1268+
else
1269+
vm.setNode(source, vm.compileNode(src))
12661270
end
1267-
else
1268-
vm.setNode(source, vm.compileNode(src))
1271+
end)
1272+
else
1273+
local node = vm.traceNode(source)
1274+
if node then
1275+
vm.setNode(source, node)
12691276
end
1270-
end)
1277+
end
12711278
end
12721279
end)
12731280
: case 'setglobal'

script/vm/tracer.lua

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,9 @@ end
781781
---@field package _tracer vm.tracer
782782

783783
---@param source parser.object
784+
---@param name string
784785
---@return vm.tracer?
785-
local function createTracer(source)
786+
local function createTracer(source, name)
786787
local node = vm.compileNode(source)
787788
local tracer = node._tracer
788789
if tracer then
@@ -794,6 +795,7 @@ local function createTracer(source)
794795
end
795796
tracer = setmetatable({
796797
source = source,
798+
name = name,
797799
assigns = {},
798800
assignMap = {},
799801
getMap = {},
@@ -808,10 +810,8 @@ local function createTracer(source)
808810

809811
if source.type == 'local'
810812
or source.type == 'self' then
811-
tracer.name = source[1]
812813
tracer:collectLocal()
813814
else
814-
tracer.name = source.global:getName()
815815
tracer:collectGlobal()
816816
end
817817

@@ -821,15 +821,29 @@ end
821821
---@param source parser.object
822822
---@return vm.node?
823823
function vm.traceNode(source)
824-
local base
824+
local base, name
825825
if source.type == 'getlocal'
826826
or source.type == 'setlocal' then
827827
base = source.node
828-
else
828+
---@type string
829+
name = source[1]
830+
elseif vm.getGlobalNode(source) then
829831
base = vm.getGlobalBase(source)
832+
if not base then
833+
return nil
834+
end
835+
name = base.global:getCodeName()
836+
else
837+
base = vm.getVariableHead(source)
838+
if not base then
839+
return nil
840+
end
841+
name = vm.getVariableName(source)
842+
if not name then
843+
return nil
844+
end
830845
end
831-
assert(base)
832-
local tracer = createTracer(base)
846+
local tracer = createTracer(base, name)
833847
if not tracer then
834848
return nil
835849
end

script/vm/variable-id.lua

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ local vm = require 'vm.vm'
66
---@class vm.variable
77
---@field sets parser.object[]
88
---@field gets parser.object[]
9-
---@field node? vm.node
10-
---@field type 'global'|'local'
119

1210
---@class parser.object
1311
---@field package _variableID string|false
@@ -19,7 +17,7 @@ local compileSwitch = util.switch()
1917
: case 'local'
2018
: case 'self'
2119
: call(function (source)
22-
source._variableID = ('l|%d'):format(source.start)
20+
source._variableID = ('%d'):format(source.start)
2321
if not source.ref then
2422
return
2523
end
@@ -30,7 +28,7 @@ local compileSwitch = util.switch()
3028
: case 'setlocal'
3129
: case 'getlocal'
3230
: call(function (source)
33-
source._variableID = ('l|%d'):format(source.node.start)
31+
source._variableID = ('%d'):format(source.node.start)
3432
compileVariableID(source.next)
3533
end)
3634
: case 'getfield'
@@ -123,7 +121,6 @@ function vm.insertVariableID(id, source)
123121
if not root._variableIDs then
124122
root._variableIDs = util.multiTable(2, function (head)
125123
return {
126-
type = head:sub(1, 1) == 'l' and 'local' or 'global',
127124
sets = {},
128125
gets = {},
129126
}
@@ -154,18 +151,33 @@ function compileVariableID(source)
154151
end
155152

156153
---@param source parser.object
157-
---@return string|false
154+
---@return string?
158155
function vm.getVariableID(source)
159156
if source._variableID ~= nil then
160-
return source._variableID
157+
return source._variableID or nil
161158
end
162159
source._variableID = false
163160
local loc = getVariable(source)
164161
if not loc then
165-
return source._variableID
162+
return source._variableID or nil
166163
end
167164
compileVariableID(loc)
168-
return source._variableID
165+
return source._variableID or nil
166+
end
167+
168+
---@param source parser.object
169+
---@return string?
170+
function vm.getVariableName(source)
171+
local id = vm.getVariableID(source)
172+
if not id then
173+
return nil
174+
end
175+
local head = vm.getVariableHead(source)
176+
if not head then
177+
return nil
178+
end
179+
local name = id:gsub('%d+', head[1]):gsub(vm.ID_SPLITE, '.')
180+
return name
169181
end
170182

171183
---@param source parser.object
@@ -248,3 +260,20 @@ function vm.getVariableFields(source, includeGets)
248260
end
249261
return fields
250262
end
263+
264+
---@param source parser.object
265+
---@return parser.object?
266+
function vm.getVariableHead(source)
267+
local id = vm.getVariableID(source)
268+
if not id then
269+
return nil
270+
end
271+
for _ = 1, 1000 do
272+
if source.type == 'local'
273+
or source.type == 'self' then
274+
return source
275+
end
276+
source = source.node
277+
end
278+
return nil
279+
end

0 commit comments

Comments
 (0)