Bug 857648 part 4. Add a formattedStack attribute on nsIStackFrame. r=khuey
authorBoris Zbarsky <bzbarsky@mit.edu>
Fri, 04 Jul 2014 01:25:16 -0400
changeset 215737 f33a93527a75cc352375884580fa473753b3575b
parent 215736 842f6ec98e187df7f1b59644456b7a9a189bd3f4
child 215738 5ac6aef80e07f994620ea1f7f902f3da365fe4da
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey
bugs857648
milestone33.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 857648 part 4. Add a formattedStack attribute on nsIStackFrame. r=khuey
dom/bindings/Exceptions.cpp
xpcom/base/nsIException.idl
--- a/dom/bindings/Exceptions.cpp
+++ b/dom/bindings/Exceptions.cpp
@@ -272,41 +272,45 @@ public:
 
   static already_AddRefed<nsIStackFrame>
   CreateStack(JSContext* aCx, int32_t aMaxDepth = -1);
 
   NS_IMETHOD GetLanguageName(nsACString& aLanguageName) MOZ_OVERRIDE;
   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 {
     return true;
   }
 
   virtual nsresult GetLineno(int32_t* aLineNo) MOZ_OVERRIDE;
 
 private:
   virtual ~JSStackFrame();
 
   JS::Heap<JSObject*> mStack;
+  nsString mFormattedStack;
 
   bool mFilenameInitialized;
   bool mFunnameInitialized;
   bool mLinenoInitialized;
   bool mCallerInitialized;
+  bool mFormattedStackInitialized;
 };
 
 JSStackFrame::JSStackFrame(JS::Handle<JSObject*> aStack)
   : mStack(aStack)
   , mFilenameInitialized(false)
   , mFunnameInitialized(false)
   , mLinenoInitialized(false)
   , mCallerInitialized(false)
+  , mFormattedStackInitialized(false)
 {
   MOZ_ASSERT(mStack);
 
   mozilla::HoldJSObjects(this);
   mLineno = 0;
   mLanguage = nsIProgrammingLanguage::JAVASCRIPT;
 }
 
@@ -490,16 +494,45 @@ NS_IMETHODIMP JSStackFrame::GetCaller(ns
 }
 
 NS_IMETHODIMP StackFrame::GetCaller(nsIStackFrame** aCaller)
 {
   NS_IF_ADDREF(*aCaller = mCaller);
   return NS_OK;
 }
 
+NS_IMETHODIMP JSStackFrame::GetFormattedStack(nsAString& aStack)
+{
+  if (!mFormattedStackInitialized) {
+    ThreadsafeAutoJSContext cx;
+    JS::Rooted<JS::Value> stack(cx, JS::ObjectValue(*mStack));
+    JS::ExposeObjectToActiveJS(mStack);
+    JSAutoCompartment ac(cx, mStack);
+    JS::Rooted<JSString*> 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)
 {
   _retval.Truncate();
 
   const char* frametype = IsJSFrame() ? "JS" : "native";
 
   nsString filename;
--- a/xpcom/base/nsIException.idl
+++ b/xpcom/base/nsIException.idl
@@ -5,29 +5,34 @@
 
 /*
  * Interfaces for representing cross-language exceptions and stack traces.
  */
 
 
 #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
     readonly attribute uint32_t                language;
     readonly attribute AUTF8String             languageName;
     readonly attribute AString                 filename;
     readonly attribute AString                 name;
     // Valid line numbers begin at '1'. '0' indicates unknown.
     readonly attribute int32_t                 lineNumber;
     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();
 };
 
 [scriptable, uuid(1caf1461-be1d-4b79-a552-5292b6bf3c35)]
 interface nsIException : nsISupports
 {
     // A custom message set by the thrower.
     [binaryname(MessageMoz)] readonly attribute AUTF8String message;