Skip to content

Commit 72277da

Browse files
committed
unify upvalue and table tracking
1 parent 52efc3a commit 72277da

10 files changed

Lines changed: 307 additions & 287 deletions

File tree

nattlua/analyzer/analyzer.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,13 @@ function META.New(config)
340340
max_iterations = false,
341341
break_out_scope = false,
342342
_continue_ = false,
343-
tracked_tables = false,
344343
inverted_index_tracking = false,
345344
deferred_calls = false,
346345
function_scope = false,
347346
call_stack = false,
348347
self_arg_stack = false,
349-
tracked_upvalues_done = false,
350-
tracked_upvalues = false,
351-
tracked_tables_done = false,
348+
tracked_objects_done = false,
349+
tracked_objects = false,
352350
scope = false,
353351
current_statement = false,
354352
left_assigned = false,

nattlua/analyzer/base/lexical_scope.lua

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -161,48 +161,43 @@ function META:Copy()
161161
return copy
162162
end
163163

164-
META:GetSet("TrackedUpvalues", false)
165-
META:GetSet("TrackedTables", false)
164+
META:GetSet("TrackedNarrowings", false)
166165

167166
function META:FindTrackedUpvalue(upvalue)
168-
local upvalues = self:GetTrackedUpvalues()
167+
local objects = self:GetTrackedNarrowings()
169168

170-
if not upvalues then return false end
169+
if not objects then return false end
171170

172-
for _, data in ipairs(upvalues) do
173-
if data.upvalue == upvalue then return data end
171+
for _, data in ipairs(objects) do
172+
if data.kind == "upvalue" and data.upvalue == upvalue then return data end
174173
end
175174
end
176175

177176
function META:TracksSameAs(scope, obj)
178-
local upvalues_a, tables_a = self:GetTrackedUpvalues(), self:GetTrackedTables()
179-
local upvalues_b, tables_b = scope:GetTrackedUpvalues(), scope:GetTrackedTables()
180-
181-
if not upvalues_a or not upvalues_b then return false end
182-
183-
if not tables_a or not tables_b then return false end
184-
185-
for i, data_a in ipairs(upvalues_a) do
186-
for i, data_b in ipairs(upvalues_b) do
187-
if data_a.upvalue == data_b.upvalue then
188-
if data_a.stack and data_b.stack then
189-
local a = data_a.stack[#data_a.stack].truthy
190-
local b = data_b.stack[#data_b.stack].truthy
191-
192-
if a:Equal(b) then return true end
193-
else
194-
return true
177+
local objects_a = self:GetTrackedNarrowings()
178+
local objects_b = scope:GetTrackedNarrowings()
179+
180+
if not objects_a or not objects_b then return false end
181+
182+
for i, data_a in ipairs(objects_a) do
183+
for i, data_b in ipairs(objects_b) do
184+
if data_a.kind == "upvalue" and data_b.kind == "upvalue" then
185+
if data_a.upvalue == data_b.upvalue then
186+
if data_a.stack and data_b.stack then
187+
local a = data_a.stack[#data_a.stack].truthy
188+
local b = data_b.stack[#data_b.stack].truthy
189+
190+
if a:Equal(b) then return true end
191+
else
192+
return true
193+
end
195194
end
195+
elseif data_a.kind == "table" and data_b.kind == "table" then
196+
if data_a.obj == data_b.obj and data_a.obj == obj then return true end
196197
end
197198
end
198199
end
199200

200-
for i, data_a in ipairs(tables_a) do
201-
for i, data_b in ipairs(tables_b) do
202-
if data_a.obj == data_b.obj and data_a.obj == obj then return true end
203-
end
204-
end
205-
206201
return false
207202
end
208203

nattlua/analyzer/control_flow.lua

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ return function(META--[[#: any]])
5858
}
5959
push_break_state(self, break_state)
6060
self:PushScope(loop_scope)
61-
self:ApplyMutationsAfterStatement(scope, true, scope:GetTrackedUpvalues(), scope:GetTrackedTables())
61+
self:ApplyMutationsAfterStatement(scope, true, scope:GetTrackedNarrowings())
6262
self:PopScope()
6363
end
6464

@@ -256,34 +256,27 @@ return function(META--[[#: any]])
256256

257257
if assert_expression and assert_expression:IsTruthy() then
258258
-- track the assertion expression
259-
local upvalues
260-
local tracked = self:GetTrackedUpvalues(nil, frame.scope)
261-
262-
if tracked[1] then
263-
upvalues = {}
264-
265-
for _, a in ipairs(tracked) do
266-
for _, b in ipairs(self:GetTrackedUpvalues()) do
267-
if a.upvalue == b.upvalue then table_insert(upvalues, a) end
268-
end
269-
end
270-
end
271-
272-
local tables
273-
local tracked = self:GetTrackedTables(nil, frame.scope)
274-
275-
if tracked[1] then
276-
tables = {}
277-
278-
for _, a in ipairs(tracked) do
279-
for _, b in ipairs(self:GetTrackedTables()) do
280-
if a.obj == b.obj then table_insert(tables, a) end
259+
local tracked_objects
260+
local tracked_from_scope = self:GetTrackedObjects(nil, frame.scope)
261+
262+
if tracked_from_scope[1] then
263+
local current_tracked = self:GetTrackedObjects()
264+
tracked_objects = {}
265+
266+
for _, a in ipairs(tracked_from_scope) do
267+
for _, b in ipairs(current_tracked) do
268+
if
269+
(a.kind == "upvalue" and b.kind == "upvalue" and a.upvalue == b.upvalue) or
270+
(a.kind == "table" and b.kind == "table" and a.obj == b.obj)
271+
then
272+
table_insert(tracked_objects, a)
273+
end
281274
end
282275
end
283276
end
284277

285278
self:PushScope(function_scope)
286-
self:ApplyMutationsAfterStatement(frame.scope, false, upvalues, tables)
279+
self:ApplyMutationsAfterStatement(frame.scope, false, tracked_objects)
287280
self:PopScope()
288281
return
289282
end
@@ -292,8 +285,7 @@ return function(META--[[#: any]])
292285
self:ApplyMutationsAfterStatement(
293286
frame.scope,
294287
true,
295-
frame.scope:GetTrackedUpvalues(),
296-
frame.scope:GetTrackedTables()
288+
frame.scope:GetTrackedNarrowings()
297289
)
298290
self:PopScope()
299291
end
@@ -323,9 +315,9 @@ return function(META--[[#: any]])
323315
end
324316

325317
local scope = self:GetScope()
326-
local u, t = self:GetTrackedUpvalues(old), self:GetTrackedTables()
318+
local tracked = self:GetTrackedObjects(old)
327319
self:PushScope(self:GetScope():GetNearestFunctionScope())
328-
self:ApplyMutationsAfterStatement(scope, false, u, t)
320+
self:ApplyMutationsAfterStatement(scope, false, tracked)
329321
self:PopScope()
330322

331323
if not no_report then
@@ -391,7 +383,7 @@ return function(META--[[#: any]])
391383
end
392384

393385
self:PushScope(function_scope)
394-
self:ApplyMutationsAfterStatement(scope, true, scope:GetTrackedUpvalues(), scope:GetTrackedTables())
386+
self:ApplyMutationsAfterStatement(scope, true, scope:GetTrackedNarrowings())
395387
self:PopScope()
396388
end
397389

0 commit comments

Comments
 (0)