Skip to content

Commit

Permalink
fix: don't blow stack on recursive object (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragoncoder047 authored Nov 10, 2024
1 parent 7aea915 commit f00a7d6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 2 additions & 0 deletions examples/prettyDebug.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const pretty = {
"own toString is used": vec2(10, 10),
};

pretty.recursive = pretty;

debug.log("Text in [brackets] doesn't cause issues");

debug.log(pretty);
Expand Down
7 changes: 4 additions & 3 deletions src/gfx/draw/drawDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,16 @@ export function drawDebug() {
}
}

function prettyDebug(object: any | undefined, inside: boolean = false): string {
function prettyDebug(object: any | undefined, inside: boolean = false, seen: Set<any> = new Set): string {
if (seen.has(object)) return "<recursive>";
var outStr = "", tmp;
if (inside && typeof object === "string") {
object = JSON.stringify(object);
}
if (Array.isArray(object)) {
outStr = [
"[",
object.map(e => prettyDebug(e, true)).join(", "),
object.map(e => prettyDebug(e, true, seen.union(new Set([object])))).join(", "),
"]",
].join("");
object = outStr;
Expand All @@ -244,7 +245,7 @@ function prettyDebug(object: any | undefined, inside: boolean = false): string {
(tmp = Object.getOwnPropertyNames(object)
.map(p =>
`${/^\w+$/.test(p) ? p : JSON.stringify(p)}: ${
prettyDebug(object[p], true)
prettyDebug(object[p], true, seen.union(new Set([object])))
}`
)
.join(", "))
Expand Down

0 comments on commit f00a7d6

Please sign in to comment.