Zotero.debug and infinite loops
Hi, I am sure this question must have been raised, but I cannot find any traces. The Zotero.debug method, when applied to objects, calls Zotero.varDump, which will recursively call itself in order to dump the object tree. This will obviously lead to an infinite loop, if a child object references the containing object, or any of its outer objects. The following will cause a stack overflow:
var test = {};
test.child = {
parent : test
};
Zotero.debug(test);
While this is a silly example, the situation is not uncommon or unreasonable in real-world projects.
As far as I can see (but then, this does not mean very much), there is no clean way to handle this, as JavaScript is lacking the concept of unique object handles, ids, hashes (or whatever it takes to build a set of objects visited during the current recursion). All I can think of is introducing a configurable maximum depth of recursion for varDump. This may be sufficient for most practical purposes.
I apologize if this question has been handled. Best, Florian
var test = {};
test.child = {
parent : test
};
Zotero.debug(test);
While this is a silly example, the situation is not uncommon or unreasonable in real-world projects.
As far as I can see (but then, this does not mean very much), there is no clean way to handle this, as JavaScript is lacking the concept of unique object handles, ids, hashes (or whatever it takes to build a set of objects visited during the current recursion). All I can think of is introducing a configurable maximum depth of recursion for varDump. This may be sufficient for most practical purposes.
I apologize if this question has been handled. Best, Florian
This is an old discussion that has not been active in a long time. Instead of commenting here, you should start a new discussion. If you think the content of this discussion is still relevant, you can link to it from your new discussion.
The Zotero codebase is littered with Zotero.debug() and this has pretty much never been an issue for us—the only time I've ever experienced it is trying to dump some complicated Mozilla XPCOM object, in which case I use Zotero.safeDebug(), which forces properties to strings and catches errors. Feel free to provide a patch if it's bothering you—it's unlikely to be changed otherwise.