User:GijsKruitbosch/JS Debugging/DocumentFromException
From MozillaWiki
Contents
Main aim
Be able to determine the document an error came off from within chrome js/extension/etc.
We'll use plan A unless we run out of time to do this on branch, in which case we have plan B for backup..
Plan A
- Make nsIConsoleMessage implement nsIException (rather, make nsConsoleMessage also implement nsIException if it is from an uncaught exception; the QI should fail if it's a message that isn't from an exception, ideally --shaver)
- Make nsIException have useful stuff in its data field that can be used to determine the document that the message came off. Note: what? (as in, exactly) (How about an nsIDocument? Should check with bz and the docshell mafia here. --shaver)
Plan B
- In the debugger hook that Firebug has, the exception hook gets a JSContext, from there you can get the nsIScriptGlobalObject (http://lxr.mozilla.org/seamonkey/source/dom/src/base/nsJSUtils.cpp#206), and from that you can get a nsIDOMWindowInternal, which has a frameElement from which you can derive a document etc. etc. Note: http://lxr.mozilla.org/seamonkey/source/content/base/src/nsContentUtils.cpp#1001
- Save this info in the internals of the debugger. Note: this would still require binary components...
- Assume that whatever exception comes in through the error console listener will be the last one you stored in the debugger code, meaning you now get a clue of where your error came from.
Discussion
Rob showed me an easier, all JS way:
The starting point is the jsdIStackFrame "frame" parameter in function jsdExecutionHook, from which we obtain the globalObject of its executionContext.
We then continue chasing references from globalObject and finally get to a window thanks to getBaseWindowFromWindow.