2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2015-07-09 14:02:00 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsScriptErrorWithStack implementation.
|
2015-07-28 23:51:10 +03:00
|
|
|
* a main-thread-only, cycle-collected subclass of nsScriptErrorBase
|
2015-07-09 14:02:00 +03:00
|
|
|
* that can store a SavedFrame stack trace object.
|
|
|
|
*/
|
|
|
|
|
2017-01-27 18:42:47 +03:00
|
|
|
#include "nsScriptError.h"
|
2015-07-09 14:02:00 +03:00
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
2017-01-27 18:42:47 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2015-07-09 14:02:00 +03:00
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
|
2017-01-27 18:42:47 +03:00
|
|
|
using namespace mozilla::dom;
|
2015-09-16 13:16:56 +03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-07-21 15:34:58 +03:00
|
|
|
static nsCString FormatStackString(JSContext* cx, JSPrincipals* aPrincipals,
|
|
|
|
JS::HandleObject aStack) {
|
2015-09-16 13:16:56 +03:00
|
|
|
JS::RootedString formattedStack(cx);
|
2018-07-21 15:34:58 +03:00
|
|
|
if (!JS::BuildStackString(cx, aPrincipals, aStack, &formattedStack)) {
|
2015-09-16 13:16:56 +03:00
|
|
|
return nsCString();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-09-16 13:16:56 +03:00
|
|
|
nsAutoJSString stackJSString;
|
|
|
|
if (!stackJSString.init(cx, formattedStack)) {
|
|
|
|
return nsCString();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-09-16 13:16:56 +03:00
|
|
|
return NS_ConvertUTF16toUTF8(stackJSString.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-07-09 14:02:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsScriptErrorWithStack)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsScriptErrorWithStack)
|
|
|
|
tmp->mStack = nullptr;
|
2018-07-21 15:34:37 +03:00
|
|
|
tmp->mStackGlobal = nullptr;
|
2015-07-09 14:02:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsScriptErrorWithStack)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsScriptErrorWithStack)
|
2016-02-11 21:58:02 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mStack)
|
2018-07-21 15:34:37 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mStackGlobal)
|
2015-07-09 14:02:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsScriptErrorWithStack)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsScriptErrorWithStack)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsScriptErrorWithStack)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIConsoleMessage)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIScriptError)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2018-07-21 15:34:37 +03:00
|
|
|
nsScriptErrorWithStack::nsScriptErrorWithStack(JS::HandleObject aStack,
|
|
|
|
JS::HandleObject aStackGlobal)
|
|
|
|
: mStack(aStack), mStackGlobal(aStackGlobal) {
|
2015-07-21 17:50:09 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "You can't use this class on workers.");
|
2018-07-21 15:34:37 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(JS_IsGlobalObject(mStackGlobal));
|
|
|
|
js::AssertSameCompartment(mStack, mStackGlobal);
|
|
|
|
|
2015-07-09 14:02:00 +03:00
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsScriptErrorWithStack::~nsScriptErrorWithStack() {
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptErrorWithStack::GetStack(JS::MutableHandleValue aStack) {
|
|
|
|
aStack.setObjectOrNull(mStack);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-09-16 13:16:56 +03:00
|
|
|
|
2018-08-04 16:30:40 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptErrorWithStack::GetStackGlobal(JS::MutableHandleValue aStackGlobal) {
|
|
|
|
aStackGlobal.setObjectOrNull(mStackGlobal);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-16 13:16:56 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsScriptErrorWithStack::ToString(nsACString& /*UTF8*/ aResult) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCString message;
|
|
|
|
nsresult rv = nsScriptErrorBase::ToString(message);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!mStack) {
|
|
|
|
aResult.Assign(message);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoJSAPI jsapi;
|
2018-07-21 15:34:37 +03:00
|
|
|
if (!jsapi.Init(mStackGlobal)) {
|
2015-09-16 13:16:56 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-07-21 15:34:58 +03:00
|
|
|
JSPrincipals* principals =
|
|
|
|
JS::GetRealmPrincipals(js::GetNonCCWObjectRealm(mStackGlobal));
|
|
|
|
|
2015-09-16 13:16:56 +03:00
|
|
|
JSContext* cx = jsapi.cx();
|
2017-10-26 13:12:44 +03:00
|
|
|
JS::RootedObject stack(cx, mStack);
|
2018-07-21 15:34:58 +03:00
|
|
|
nsCString stackString = FormatStackString(cx, principals, stack);
|
2015-09-16 13:16:56 +03:00
|
|
|
nsCString combined = message + NS_LITERAL_CSTRING("\n") + stackString;
|
|
|
|
aResult.Assign(combined);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|