diff --git a/dom/bindings/Exceptions.cpp b/dom/bindings/Exceptions.cpp index d4f382382d92..9b709b426e22 100644 --- a/dom/bindings/Exceptions.cpp +++ b/dom/bindings/Exceptions.cpp @@ -277,6 +277,7 @@ public: NS_IMETHOD GetFilename(nsAString& aFilename) MOZ_OVERRIDE; NS_IMETHOD GetName(nsAString& aFunction) MOZ_OVERRIDE; NS_IMETHOD GetCaller(nsIStackFrame** aCaller) MOZ_OVERRIDE; + NS_IMETHOD GetFormattedStack(nsAString& aStack) MOZ_OVERRIDE; protected: virtual bool IsJSFrame() const MOZ_OVERRIDE { @@ -289,11 +290,13 @@ private: virtual ~JSStackFrame(); JS::Heap mStack; + nsString mFormattedStack; bool mFilenameInitialized; bool mFunnameInitialized; bool mLinenoInitialized; bool mCallerInitialized; + bool mFormattedStackInitialized; }; JSStackFrame::JSStackFrame(JS::Handle aStack) @@ -302,6 +305,7 @@ JSStackFrame::JSStackFrame(JS::Handle aStack) , mFunnameInitialized(false) , mLinenoInitialized(false) , mCallerInitialized(false) + , mFormattedStackInitialized(false) { MOZ_ASSERT(mStack); @@ -495,6 +499,35 @@ NS_IMETHODIMP StackFrame::GetCaller(nsIStackFrame** aCaller) return NS_OK; } +NS_IMETHODIMP JSStackFrame::GetFormattedStack(nsAString& aStack) +{ + if (!mFormattedStackInitialized) { + ThreadsafeAutoJSContext cx; + JS::Rooted stack(cx, JS::ObjectValue(*mStack)); + JS::ExposeObjectToActiveJS(mStack); + JSAutoCompartment ac(cx, mStack); + JS::Rooted formattedStack(cx, JS::ToString(cx, stack)); + if (!formattedStack) { + return NS_ERROR_UNEXPECTED; + } + nsAutoJSString str; + if (!str.init(cx, formattedStack)) { + return NS_ERROR_OUT_OF_MEMORY; + } + mFormattedStack = str; + mFormattedStackInitialized = true; + } + + aStack = mFormattedStack; + return NS_OK; +} + +NS_IMETHODIMP StackFrame::GetFormattedStack(nsAString& aStack) +{ + aStack.Truncate(); + return NS_OK; +} + /* AUTF8String toString (); */ NS_IMETHODIMP StackFrame::ToString(nsACString& _retval) { diff --git a/xpcom/base/nsIException.idl b/xpcom/base/nsIException.idl index 897b7f8fb2f5..f6e9952680d3 100644 --- a/xpcom/base/nsIException.idl +++ b/xpcom/base/nsIException.idl @@ -10,7 +10,7 @@ #include "nsISupports.idl" -[scriptable, uuid(3bc4793f-e6be-44d6-b839-d6b9e85e5346)] +[scriptable, uuid(13b75be1-f950-497b-81e4-a0214a14e5ae)] interface nsIStackFrame : nsISupports { // see nsIProgrammingLanguage for list of language consts @@ -23,6 +23,11 @@ interface nsIStackFrame : nsISupports readonly attribute AUTF8String sourceLine; readonly attribute nsIStackFrame caller; + // Returns a formatted stack string that looks like the sort of + // string that would be returned by .stack on JS Error objects. + // Only works on JS-language stack frames. + readonly attribute AString formattedStack; + AUTF8String toString(); };