Bug 857648 part 4. Add a formattedStack attribute on nsIStackFrame. r=khuey

This commit is contained in:
Boris Zbarsky 2014-07-04 01:25:16 -04:00
parent c685ec72ad
commit 538e69f63d
2 changed files with 39 additions and 1 deletions

View File

@ -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<JSObject*> mStack;
nsString mFormattedStack;
bool mFilenameInitialized;
bool mFunnameInitialized;
bool mLinenoInitialized;
bool mCallerInitialized;
bool mFormattedStackInitialized;
};
JSStackFrame::JSStackFrame(JS::Handle<JSObject*> aStack)
@ -302,6 +305,7 @@ JSStackFrame::JSStackFrame(JS::Handle<JSObject*> 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<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)
{

View File

@ -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();
};