Skip to content

Commit

Permalink
Handle non-circular internal references in serialiseValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Derpius committed Dec 23, 2023
1 parent 8dbdc8b commit e18118e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/lest/src/lua/utils/serialise-value.lua
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ serialiseValue = function(value, visitedTables)
return "<circular reference>"
end

visitedTables[value] = true
return serialiseTable(value, visitedTables)
local visitedTablesCopy = {}
for k, v in pairs(visitedTables) do
visitedTablesCopy[k] = v
end
visitedTablesCopy[value] = true

return serialiseTable(value, visitedTablesCopy)
end

if type(value) == "function" then
Expand Down
9 changes: 9 additions & 0 deletions packages/lest/src/lua/utils/tests/serialise-value.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ test("serialises table with circular reference", function()

expect(serialised).toBe('{bar = <circular reference>, foo = "123"}')
end)

test("serialises table with non-circular internal reference", function()
local tbl = { a = {}, b = { foo = "bar" } }
tbl.a.b = tbl.b

local serialised = serialiseValue(tbl)

expect(serialised).toBe('{a = {b = {foo = "bar"}}, b = {foo = "bar"}}')
end)

0 comments on commit e18118e

Please sign in to comment.