Skip to content

Commit 28c5d0f

Browse files
cscheidjjallaire
authored andcommitted
make tdump resilient to circular structures
1 parent 3dd256b commit 28c5d0f

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/resources/filters/common/debug.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ function dump(o)
1111
end
1212

1313
-- improved formatting for dumping tables
14-
function tdump (tbl, indent)
14+
function tdump (tbl, indent, refs)
15+
if not refs then refs = {} end
1516
if not indent then indent = 0 end
17+
local address = string.format("%p", tbl)
18+
if refs[address] ~= nil then
19+
print(string.rep(" ", indent) .. "(circular reference to " .. address .. ")")
20+
return
21+
end
22+
1623
if tbl.t then
1724
print(string.rep(" ", indent) .. tbl.t)
1825
end
@@ -22,7 +29,8 @@ function tdump (tbl, indent)
2229
formatting = string.rep(" ", indent) .. k .. ": "
2330
if type(v) == "table" then
2431
print(formatting)
25-
tdump(v, indent+1)
32+
refs[address] = true
33+
tdump(v, indent+1, refs)
2634
elseif type(v) == 'boolean' then
2735
print(formatting .. tostring(v))
2836
elseif (v ~= nil) then

src/resources/pandoc/datadir/_utils.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
-- debug.lua
22
-- Copyright (C) 2020 by RStudio, PBC
33
-- improved formatting for dumping tables
4-
function tdump (tbl, indent)
4+
function tdump (tbl, indent, refs)
5+
if not refs then refs = {} end
56
if not indent then indent = 0 end
7+
local address = string.format("%p", tbl)
8+
if refs[address] ~= nil then
9+
print(string.rep(" ", indent) .. "(circular reference to " .. address .. ")")
10+
return
11+
end
12+
613
if tbl.t then
714
print(string.rep(" ", indent) .. tbl.t)
815
end
@@ -11,8 +18,9 @@ function tdump (tbl, indent)
1118
empty = false
1219
formatting = string.rep(" ", indent) .. k .. ": "
1320
if type(v) == "table" then
14-
print(formatting)
15-
tdump(v, indent+1)
21+
print(formatting .. "table: " .. address)
22+
refs[address] = true
23+
tdump(v, indent+1, refs)
1624
elseif type(v) == 'boolean' then
1725
print(formatting .. tostring(v))
1826
elseif (v ~= nil) then

0 commit comments

Comments
 (0)