/src/mozilla-central/dom/bindings/nsScriptErrorWithStack.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* vim: set ts=8 sts=4 et sw=4 tw=99: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | /* |
8 | | * nsScriptErrorWithStack implementation. |
9 | | * a main-thread-only, cycle-collected subclass of nsScriptErrorBase |
10 | | * that can store a SavedFrame stack trace object. |
11 | | */ |
12 | | |
13 | | #include "nsScriptError.h" |
14 | | #include "MainThreadUtils.h" |
15 | | #include "mozilla/Assertions.h" |
16 | | #include "mozilla/dom/ScriptSettings.h" |
17 | | #include "nsGlobalWindow.h" |
18 | | #include "nsCycleCollectionParticipant.h" |
19 | | |
20 | | using namespace mozilla::dom; |
21 | | |
22 | | namespace { |
23 | | |
24 | | static nsCString |
25 | | FormatStackString(JSContext* cx, JSPrincipals* aPrincipals, JS::HandleObject aStack) |
26 | 0 | { |
27 | 0 | JS::RootedString formattedStack(cx); |
28 | 0 | if (!JS::BuildStackString(cx, aPrincipals, aStack, &formattedStack)) { |
29 | 0 | return nsCString(); |
30 | 0 | } |
31 | 0 | |
32 | 0 | nsAutoJSString stackJSString; |
33 | 0 | if (!stackJSString.init(cx, formattedStack)) { |
34 | 0 | return nsCString(); |
35 | 0 | } |
36 | 0 | |
37 | 0 | return NS_ConvertUTF16toUTF8(stackJSString.get()); |
38 | 0 | } |
39 | | |
40 | | } |
41 | | |
42 | | |
43 | | NS_IMPL_CYCLE_COLLECTION_CLASS(nsScriptErrorWithStack) |
44 | | |
45 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsScriptErrorWithStack) |
46 | 0 | tmp->mStack = nullptr; |
47 | 0 | tmp->mStackGlobal = nullptr; |
48 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
49 | | |
50 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsScriptErrorWithStack) |
51 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
52 | | |
53 | 0 | NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsScriptErrorWithStack) |
54 | 0 | NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mStack) |
55 | 0 | NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mStackGlobal) |
56 | 0 | NS_IMPL_CYCLE_COLLECTION_TRACE_END |
57 | | |
58 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(nsScriptErrorWithStack) |
59 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(nsScriptErrorWithStack) |
60 | | |
61 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsScriptErrorWithStack) |
62 | 0 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
63 | 0 | NS_INTERFACE_MAP_ENTRY(nsIConsoleMessage) |
64 | 0 | NS_INTERFACE_MAP_ENTRY(nsIScriptError) |
65 | 0 | NS_INTERFACE_MAP_END |
66 | | |
67 | | nsScriptErrorWithStack::nsScriptErrorWithStack(JS::HandleObject aStack, |
68 | | JS::HandleObject aStackGlobal) |
69 | | : mStack(aStack) |
70 | | , mStackGlobal(aStackGlobal) |
71 | 0 | { |
72 | 0 | MOZ_ASSERT(NS_IsMainThread(), "You can't use this class on workers."); |
73 | 0 |
|
74 | 0 | MOZ_ASSERT(JS_IsGlobalObject(mStackGlobal)); |
75 | 0 | js::AssertSameCompartment(mStack, mStackGlobal); |
76 | 0 |
|
77 | 0 | mozilla::HoldJSObjects(this); |
78 | 0 | } |
79 | | |
80 | 0 | nsScriptErrorWithStack::~nsScriptErrorWithStack() { |
81 | 0 | mozilla::DropJSObjects(this); |
82 | 0 | } |
83 | | |
84 | | NS_IMETHODIMP |
85 | 0 | nsScriptErrorWithStack::GetStack(JS::MutableHandleValue aStack) { |
86 | 0 | aStack.setObjectOrNull(mStack); |
87 | 0 | return NS_OK; |
88 | 0 | } |
89 | | |
90 | | NS_IMETHODIMP |
91 | | nsScriptErrorWithStack::GetStackGlobal(JS::MutableHandleValue aStackGlobal) |
92 | 0 | { |
93 | 0 | aStackGlobal.setObjectOrNull(mStackGlobal); |
94 | 0 | return NS_OK; |
95 | 0 | } |
96 | | |
97 | | NS_IMETHODIMP |
98 | | nsScriptErrorWithStack::ToString(nsACString& /*UTF8*/ aResult) |
99 | 0 | { |
100 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
101 | 0 |
|
102 | 0 | nsCString message; |
103 | 0 | nsresult rv = nsScriptErrorBase::ToString(message); |
104 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
105 | 0 |
|
106 | 0 | if (!mStack) { |
107 | 0 | aResult.Assign(message); |
108 | 0 | return NS_OK; |
109 | 0 | } |
110 | 0 | |
111 | 0 | AutoJSAPI jsapi; |
112 | 0 | if (!jsapi.Init(mStackGlobal)) { |
113 | 0 | return NS_ERROR_FAILURE; |
114 | 0 | } |
115 | 0 | |
116 | 0 | JSPrincipals* principals = |
117 | 0 | JS::GetRealmPrincipals(js::GetNonCCWObjectRealm(mStackGlobal)); |
118 | 0 |
|
119 | 0 | JSContext* cx = jsapi.cx(); |
120 | 0 | JS::RootedObject stack(cx, mStack); |
121 | 0 | nsCString stackString = FormatStackString(cx, principals, stack); |
122 | 0 | nsCString combined = message + NS_LITERAL_CSTRING("\n") + stackString; |
123 | 0 | aResult.Assign(combined); |
124 | 0 |
|
125 | 0 | return NS_OK; |
126 | 0 | } |