JS log-dumping an object and circular references problem

"R.Wieser" <[email protected]> Mon, 8 Feb 2021 09:43:30 +0100
Newsgroups gmane.comp.mozilla.general
Message-ID <[email protected]>
Hello all,

I'm trying my hand at a XUL style addon (for FF v52), in which I have set a 
"http-on-examine-response" observer.  In the callback I'm now trying to 
log-dump the received "request" object (the first argument).

For that I took some simple code from the internet :

https://stackoverflow.com/questions/749266/object-dump-javascript
Function "mydump" by zhongshu (Jun 10 '10 at 4:04)

I've got two problems

1) Somethimes the code throws, for a to me unknown reason, an error on the 
"var value = arr[item];" assignment.  I don't know why, but a decided to, 
for the moment, use quick "try" "catch".

2) The request object seems to have a circular reference.  In the response 
section John Henckel (Jan 19 '18 at 17:30) gave a fix of sorts (limiting the 
depth), but it still causes duplicated data to be generated.

To solve that I thought of using a WeakList, checking for and adding each 
visited object just after the second "if(typeof(value) == 'object')" 
(skipping the two lines below it if its already in the WeakList).

The problem with that is that any assignment to that list, 
"seen.add(value))", throws an error.  Not the "seen.has(value)" checking, 
just the addition to the list.

And now I'm stumped : What am I, if anything, doing wrong there ?

Regards,
Rudy Wieser

P.s.
I've also tried the JSON serialize -> deserialize trick, but that barfed on 
the circular reference.   The solution for that would be include a filter. 
But that filter, you guessed it, also uses a WeakList. :-)