2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-09-09 07:28:50 +04: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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/Exceptions.h"
|
|
|
|
|
2017-11-16 15:21:08 +03:00
|
|
|
#include "js/RootingAPI.h"
|
2015-02-13 20:21:50 +03:00
|
|
|
#include "js/TypeDecls.h"
|
2013-09-09 07:28:50 +04:00
|
|
|
#include "jsapi.h"
|
2018-08-20 17:45:44 +03:00
|
|
|
#include "js/SavedFrameAPI.h"
|
2018-12-19 22:42:04 +03:00
|
|
|
#include "xpcpublic.h"
|
2016-09-14 16:47:32 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
2013-09-09 07:28:50 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
#include "mozilla/dom/DOMException.h"
|
2014-07-02 14:26:49 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2013-09-09 07:29:21 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-09-09 07:28:50 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "XPCWrapper.h"
|
2013-09-09 07:29:21 +04:00
|
|
|
#include "WorkerPrivate.h"
|
2013-11-01 16:55:35 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2016-08-26 22:38:16 +03:00
|
|
|
// Throw the given exception value if it's safe. If it's not safe, then
|
|
|
|
// synthesize and throw a new exception value for NS_ERROR_UNEXPECTED. The
|
|
|
|
// incoming value must be in the compartment of aCx. This function guarantees
|
|
|
|
// that an exception is pending on aCx when it returns.
|
|
|
|
static void ThrowExceptionValueIfSafe(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> exnVal,
|
2018-02-06 00:34:05 +03:00
|
|
|
Exception* aOriginalException) {
|
2016-08-26 22:38:16 +03:00
|
|
|
MOZ_ASSERT(aOriginalException);
|
|
|
|
|
|
|
|
if (!exnVal.isObject()) {
|
|
|
|
JS_SetPendingException(aCx, exnVal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> exnObj(aCx, &exnVal.toObject());
|
|
|
|
MOZ_ASSERT(js::IsObjectInContextCompartment(exnObj, aCx),
|
|
|
|
"exnObj needs to be in the right compartment for the "
|
2019-02-02 06:23:49 +03:00
|
|
|
"CheckedUnwrapDynamic thing to make sense");
|
2016-08-26 22:38:16 +03:00
|
|
|
|
2019-02-02 06:23:49 +03:00
|
|
|
// aCx's current Realm is where we're throwing, so using it in the
|
|
|
|
// CheckedUnwrapDynamic check makes sense.
|
|
|
|
if (js::CheckedUnwrapDynamic(exnObj, aCx)) {
|
2016-08-26 22:38:16 +03:00
|
|
|
// This is an object we're allowed to work with, so just go ahead and throw
|
|
|
|
// it.
|
|
|
|
JS_SetPendingException(aCx, exnVal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We could probably Throw(aCx, NS_ERROR_UNEXPECTED) here, and it would do the
|
|
|
|
// right thing due to there not being an existing exception on the runtime at
|
|
|
|
// this point, but it's clearer to explicitly do the thing we want done. This
|
|
|
|
// is also why we don't just call ThrowExceptionObject on the Exception we
|
|
|
|
// create: it would do the right thing, but that fact is not obvious.
|
|
|
|
RefPtr<Exception> syntheticException = CreateException(NS_ERROR_UNEXPECTED);
|
|
|
|
JS::Rooted<JS::Value> syntheticVal(aCx);
|
|
|
|
if (!GetOrCreateDOMReflector(aCx, syntheticException, &syntheticVal)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(
|
|
|
|
syntheticVal.isObject() && !js::IsWrapper(&syntheticVal.toObject()),
|
|
|
|
"Must have a reflector here, not a wrapper");
|
|
|
|
JS_SetPendingException(aCx, syntheticVal);
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:28:50 +04:00
|
|
|
void ThrowExceptionObject(JSContext* aCx, Exception* aException) {
|
2013-09-09 07:29:21 +04:00
|
|
|
JS::Rooted<JS::Value> thrown(aCx);
|
2013-09-09 07:28:50 +04:00
|
|
|
|
|
|
|
// If we stored the original thrown JS value in the exception
|
|
|
|
// (see XPCConvert::ConstructException) and we are in a web context
|
|
|
|
// (i.e., not chrome), rethrow the original value. This only applies to JS
|
|
|
|
// implemented components so we only need to check for this on the main
|
|
|
|
// thread.
|
2014-05-07 02:43:03 +04:00
|
|
|
if (NS_IsMainThread() && !nsContentUtils::IsCallerChrome() &&
|
2013-09-09 07:28:50 +04:00
|
|
|
aException->StealJSVal(thrown.address())) {
|
2016-04-22 23:03:05 +03:00
|
|
|
// Now check for the case when thrown is a number which matches
|
|
|
|
// aException->GetResult(). This would indicate that what actually got
|
|
|
|
// thrown was an nsresult value. In that situation, we should go back
|
|
|
|
// through dom::Throw with that nsresult value, because it will make sure to
|
|
|
|
// create the right sort of Exception or DOMException, with the right
|
|
|
|
// global.
|
|
|
|
if (thrown.isNumber()) {
|
2018-02-06 00:34:06 +03:00
|
|
|
nsresult exceptionResult = aException->GetResult();
|
|
|
|
if (double(exceptionResult) == thrown.toNumber()) {
|
2016-04-22 23:03:05 +03:00
|
|
|
Throw(aCx, exceptionResult);
|
2016-08-26 22:38:16 +03:00
|
|
|
return;
|
2016-04-22 23:03:05 +03:00
|
|
|
}
|
|
|
|
}
|
2013-10-26 20:19:05 +04:00
|
|
|
if (!JS_WrapValue(aCx, &thrown)) {
|
2016-08-26 22:38:16 +03:00
|
|
|
return;
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
2016-08-26 22:38:16 +03:00
|
|
|
ThrowExceptionValueIfSafe(aCx, thrown, aException);
|
2016-08-26 22:38:16 +03:00
|
|
|
return;
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
2014-11-26 22:25:20 +03:00
|
|
|
if (!GetOrCreateDOMReflector(aCx, aException, &thrown)) {
|
2016-08-26 22:38:16 +03:00
|
|
|
return;
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
2016-08-26 22:38:16 +03:00
|
|
|
ThrowExceptionValueIfSafe(aCx, thrown, aException);
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
2015-10-09 23:48:10 +03:00
|
|
|
bool Throw(JSContext* aCx, nsresult aRv, const nsACString& aMessage) {
|
2015-02-21 07:58:36 +03:00
|
|
|
if (aRv == NS_ERROR_UNCATCHABLE_EXCEPTION) {
|
|
|
|
// Nuke any existing exception on aCx, to make sure we're uncatchable.
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:29:21 +04:00
|
|
|
if (JS_IsExceptionPending(aCx)) {
|
2013-09-09 07:28:50 +04:00
|
|
|
// Don't clobber the existing exception.
|
2013-09-09 07:29:21 +04:00
|
|
|
return false;
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:47:32 +03:00
|
|
|
CycleCollectedJSContext* context = CycleCollectedJSContext::Get();
|
2018-02-06 00:34:05 +03:00
|
|
|
RefPtr<Exception> existingException = context->GetPendingException();
|
2016-04-22 23:03:05 +03:00
|
|
|
// Make sure to clear the pending exception now. Either we're going to reuse
|
|
|
|
// it (and we already grabbed it), or we plan to throw something else and this
|
|
|
|
// pending exception is no longer relevant.
|
2016-09-14 16:47:32 +03:00
|
|
|
context->SetPendingException(nullptr);
|
2016-04-22 23:03:05 +03:00
|
|
|
|
|
|
|
// Ignore the pending exception if we have a non-default message passed in.
|
|
|
|
if (aMessage.IsEmpty() && existingException) {
|
2018-02-06 00:34:06 +03:00
|
|
|
if (aRv == existingException->GetResult()) {
|
2013-10-10 01:43:15 +04:00
|
|
|
// Reuse the existing exception.
|
2016-08-26 22:38:16 +03:00
|
|
|
ThrowExceptionObject(aCx, existingException);
|
2013-09-09 07:29:21 +04:00
|
|
|
return false;
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 11:03:03 +03:00
|
|
|
RefPtr<Exception> finalException = CreateException(aRv, aMessage);
|
2014-04-16 23:13:41 +04:00
|
|
|
MOZ_ASSERT(finalException);
|
|
|
|
|
2016-08-26 22:38:16 +03:00
|
|
|
ThrowExceptionObject(aCx, finalException);
|
2014-04-16 23:13:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
void ThrowAndReport(nsPIDOMWindowInner* aWindow, nsresult aRv) {
|
2015-02-21 07:58:36 +03:00
|
|
|
MOZ_ASSERT(aRv != NS_ERROR_UNCATCHABLE_EXCEPTION,
|
|
|
|
"Doesn't make sense to report uncatchable exceptions!");
|
2014-07-02 14:26:49 +04:00
|
|
|
AutoJSAPI jsapi;
|
2016-03-10 03:02:25 +03:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(aWindow))) {
|
2014-07-02 14:26:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-09 23:48:10 +03:00
|
|
|
Throw(jsapi.cx(), aRv);
|
2014-07-02 14:26:49 +04:00
|
|
|
}
|
|
|
|
|
2017-01-19 11:03:03 +03:00
|
|
|
already_AddRefed<Exception> CreateException(nsresult aRv,
|
|
|
|
const nsACString& aMessage) {
|
2013-09-09 07:28:50 +04:00
|
|
|
// Do we use DOM exceptions for this error code?
|
2013-09-09 07:29:21 +04:00
|
|
|
switch (NS_ERROR_GET_MODULE(aRv)) {
|
2013-09-09 07:28:50 +04:00
|
|
|
case NS_ERROR_MODULE_DOM:
|
|
|
|
case NS_ERROR_MODULE_SVG:
|
|
|
|
case NS_ERROR_MODULE_DOM_XPATH:
|
|
|
|
case NS_ERROR_MODULE_DOM_INDEXEDDB:
|
|
|
|
case NS_ERROR_MODULE_DOM_FILEHANDLE:
|
2015-10-22 11:22:38 +03:00
|
|
|
case NS_ERROR_MODULE_DOM_ANIM:
|
2016-01-28 19:23:29 +03:00
|
|
|
case NS_ERROR_MODULE_DOM_PUSH:
|
2016-08-24 12:30:58 +03:00
|
|
|
case NS_ERROR_MODULE_DOM_MEDIA:
|
2015-10-09 23:48:10 +03:00
|
|
|
if (aMessage.IsEmpty()) {
|
|
|
|
return DOMException::Create(aRv);
|
|
|
|
}
|
|
|
|
return DOMException::Create(aRv, aMessage);
|
2014-04-17 03:31:41 +04:00
|
|
|
default:
|
2014-04-16 23:13:41 +04:00
|
|
|
break;
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If not, use the default.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Exception> exception =
|
2020-09-23 18:17:15 +03:00
|
|
|
new Exception(aMessage, aRv, ""_ns, nullptr, nullptr);
|
2014-04-16 23:13:41 +04:00
|
|
|
return exception.forget();
|
2013-09-09 07:29:21 +04:00
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
already_AddRefed<nsIStackFrame> GetCurrentJSStack(int32_t aMaxDepth) {
|
2013-09-09 07:29:21 +04:00
|
|
|
// is there a current context available?
|
2018-04-09 03:11:18 +03:00
|
|
|
JSContext* cx = nsContentUtils::GetCurrentJSContext();
|
2013-09-09 07:29:21 +04:00
|
|
|
|
2018-05-31 12:28:48 +03:00
|
|
|
if (!cx || !js::GetContextRealm(cx)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-22 06:41:02 +03:00
|
|
|
static const unsigned MAX_FRAMES = 100;
|
|
|
|
if (aMaxDepth < 0) {
|
|
|
|
aMaxDepth = MAX_FRAMES;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::StackCapture captureMode =
|
|
|
|
aMaxDepth == 0 ? JS::StackCapture(JS::AllFrames())
|
|
|
|
: JS::StackCapture(JS::MaxFrames(aMaxDepth));
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
return dom::exceptions::CreateStack(cx, std::move(captureMode));
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace exceptions {
|
|
|
|
|
2018-12-19 22:42:04 +03:00
|
|
|
class JSStackFrame final : public nsIStackFrame, public xpc::JSStackFrameBase {
|
2013-09-09 07:28:50 +04:00
|
|
|
public:
|
2014-01-08 04:53:18 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(JSStackFrame)
|
2013-09-09 07:28:50 +04:00
|
|
|
NS_DECL_NSISTACKFRAME
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
// aStack must not be null.
|
|
|
|
explicit JSStackFrame(JS::Handle<JSObject*> aStack);
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
private:
|
|
|
|
virtual ~JSStackFrame();
|
2014-07-04 10:23:35 +04:00
|
|
|
|
2018-12-19 22:42:04 +03:00
|
|
|
void Clear() override { mStack = nullptr; }
|
|
|
|
|
|
|
|
// Remove this frame from the per-realm list of live frames,
|
|
|
|
// and clear out the stack pointer.
|
|
|
|
void UnregisterAndClear();
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
JS::Heap<JSObject*> mStack;
|
|
|
|
nsString mFormattedStack;
|
2014-09-22 22:28:31 +04:00
|
|
|
|
2013-09-09 07:28:50 +04:00
|
|
|
nsCOMPtr<nsIStackFrame> mCaller;
|
2015-03-07 16:30:34 +03:00
|
|
|
nsCOMPtr<nsIStackFrame> mAsyncCaller;
|
2014-04-12 06:20:40 +04:00
|
|
|
nsString mFilename;
|
|
|
|
nsString mFunname;
|
2015-03-07 16:30:34 +03:00
|
|
|
nsString mAsyncCause;
|
2019-01-17 01:00:38 +03:00
|
|
|
int32_t mSourceId;
|
2013-09-09 07:28:50 +04:00
|
|
|
int32_t mLineno;
|
2014-09-22 22:28:31 +04:00
|
|
|
int32_t mColNo;
|
2014-01-08 04:53:18 +04:00
|
|
|
|
|
|
|
bool mFilenameInitialized;
|
|
|
|
bool mFunnameInitialized;
|
2019-01-17 01:00:38 +03:00
|
|
|
bool mSourceIdInitialized;
|
2014-01-08 04:53:18 +04:00
|
|
|
bool mLinenoInitialized;
|
2014-09-22 22:28:31 +04:00
|
|
|
bool mColNoInitialized;
|
2015-03-07 16:30:34 +03:00
|
|
|
bool mAsyncCauseInitialized;
|
|
|
|
bool mAsyncCallerInitialized;
|
2014-01-08 04:53:18 +04:00
|
|
|
bool mCallerInitialized;
|
2014-07-04 09:25:16 +04:00
|
|
|
bool mFormattedStackInitialized;
|
2013-09-09 07:28:50 +04:00
|
|
|
};
|
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
JSStackFrame::JSStackFrame(JS::Handle<JSObject*> aStack)
|
|
|
|
: mStack(aStack),
|
2019-01-17 01:00:38 +03:00
|
|
|
mSourceId(0),
|
2016-03-22 20:50:31 +03:00
|
|
|
mLineno(0),
|
|
|
|
mColNo(0),
|
2014-07-04 09:25:16 +04:00
|
|
|
mFilenameInitialized(false),
|
|
|
|
mFunnameInitialized(false),
|
2019-01-17 01:00:38 +03:00
|
|
|
mSourceIdInitialized(false),
|
2014-07-04 09:25:16 +04:00
|
|
|
mLinenoInitialized(false),
|
2014-09-22 22:28:31 +04:00
|
|
|
mColNoInitialized(false),
|
2015-03-07 16:30:34 +03:00
|
|
|
mAsyncCauseInitialized(false),
|
|
|
|
mAsyncCallerInitialized(false),
|
2014-07-04 09:25:16 +04:00
|
|
|
mCallerInitialized(false),
|
2014-07-04 09:25:16 +04:00
|
|
|
mFormattedStackInitialized(false) {
|
2014-07-04 09:25:16 +04:00
|
|
|
MOZ_ASSERT(mStack);
|
2018-07-21 15:33:22 +03:00
|
|
|
MOZ_ASSERT(JS::IsUnwrappedSavedFrame(mStack));
|
2014-07-04 09:25:16 +04:00
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
mozilla::HoldJSObjects(this);
|
2018-12-19 22:42:04 +03:00
|
|
|
|
|
|
|
xpc::RegisterJSStackFrame(js::GetNonCCWObjectRealm(aStack), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSStackFrame::~JSStackFrame() {
|
|
|
|
UnregisterAndClear();
|
|
|
|
mozilla::DropJSObjects(this);
|
2014-01-08 04:53:18 +04:00
|
|
|
}
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2018-12-19 22:42:04 +03:00
|
|
|
void JSStackFrame::UnregisterAndClear() {
|
|
|
|
if (!mStack) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
xpc::UnregisterJSStackFrame(js::GetNonCCWObjectRealm(mStack), this);
|
|
|
|
Clear();
|
|
|
|
}
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(JSStackFrame)
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(JSStackFrame)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCaller)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mAsyncCaller)
|
2018-12-19 22:42:04 +03:00
|
|
|
tmp->UnregisterAndClear();
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(JSStackFrame)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCaller)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAsyncCaller)
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(JSStackFrame)
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mStack)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2014-01-08 04:53:18 +04:00
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(JSStackFrame)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(JSStackFrame)
|
2016-03-22 23:10:37 +03:00
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSStackFrame)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStackFrame)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2018-07-21 15:34:58 +03:00
|
|
|
// Helper method to determine the JSPrincipals* to pass to JS SavedFrame APIs.
|
|
|
|
//
|
|
|
|
// @argument aStack the stack we're working with; must be non-null.
|
|
|
|
// @argument [out] aCanCache whether we can use cached JSStackFrame values.
|
|
|
|
static JSPrincipals* GetPrincipalsForStackGetter(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aStack,
|
|
|
|
bool* aCanCache) {
|
|
|
|
MOZ_ASSERT(JS::IsUnwrappedSavedFrame(aStack));
|
|
|
|
|
|
|
|
JSPrincipals* currentPrincipals =
|
|
|
|
JS::GetRealmPrincipals(js::GetContextRealm(aCx));
|
|
|
|
JSPrincipals* stackPrincipals =
|
|
|
|
JS::GetRealmPrincipals(js::GetNonCCWObjectRealm(aStack));
|
|
|
|
|
|
|
|
// Fast path for when the principals are equal. This check is also necessary
|
|
|
|
// for workers: no nsIPrincipal there so we can't use the code below.
|
|
|
|
if (currentPrincipals == stackPrincipals) {
|
|
|
|
*aCanCache = true;
|
|
|
|
return stackPrincipals;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (nsJSPrincipals::get(currentPrincipals)
|
|
|
|
->Subsumes(nsJSPrincipals::get(stackPrincipals))) {
|
|
|
|
// The current principals subsume the stack's principals. In this case use
|
|
|
|
// the stack's principals: the idea is that this way devtools code that's
|
|
|
|
// asking an exception object for a stack to display will end up with the
|
|
|
|
// stack the web developer would see via doing .stack in a web page, with
|
|
|
|
// Firefox implementation details excluded.
|
|
|
|
|
|
|
|
// Because we use the stack's principals and don't rely on the current
|
|
|
|
// context realm, we can use cached values.
|
|
|
|
*aCanCache = true;
|
|
|
|
return stackPrincipals;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The stack was captured in more-privileged code, so use the less privileged
|
|
|
|
// principals. Don't use cached values because we don't want these values to
|
|
|
|
// depend on the current realm/principals.
|
|
|
|
*aCanCache = false;
|
|
|
|
return currentPrincipals;
|
|
|
|
}
|
|
|
|
|
2015-03-06 09:08:06 +03:00
|
|
|
// Helper method to get the value of a stack property, if it's not already
|
2018-07-21 15:34:58 +03:00
|
|
|
// cached. This will make sure we skip the cache if the property value depends
|
|
|
|
// on the (current) context's realm/principals.
|
2015-03-06 09:08:06 +03:00
|
|
|
//
|
|
|
|
// @argument aStack the stack we're working with; must be non-null.
|
|
|
|
// @argument aPropGetter the getter function to call.
|
|
|
|
// @argument aIsCached whether we've cached this property's value before.
|
|
|
|
//
|
|
|
|
// @argument [out] aCanCache whether the value can get cached.
|
|
|
|
// @argument [out] aUseCachedValue if true, just use the cached value.
|
|
|
|
// @argument [out] aValue the value we got from the stack.
|
|
|
|
template <typename ReturnType, typename GetterOutParamType>
|
2016-10-18 19:58:19 +03:00
|
|
|
static void GetValueIfNotCached(
|
|
|
|
JSContext* aCx, const JS::Heap<JSObject*>& aStack,
|
2015-03-06 09:08:06 +03:00
|
|
|
JS::SavedFrameResult (*aPropGetter)(JSContext*, JSPrincipals*,
|
|
|
|
JS::Handle<JSObject*>,
|
2015-10-21 17:39:00 +03:00
|
|
|
GetterOutParamType,
|
|
|
|
JS::SavedFrameSelfHosted),
|
2015-03-06 09:08:06 +03:00
|
|
|
bool aIsCached, bool* aCanCache, bool* aUseCachedValue, ReturnType aValue) {
|
|
|
|
MOZ_ASSERT(aStack);
|
2018-07-21 15:34:58 +03:00
|
|
|
MOZ_ASSERT(JS::IsUnwrappedSavedFrame(aStack));
|
2015-03-06 09:08:06 +03:00
|
|
|
|
|
|
|
JS::Rooted<JSObject*> stack(aCx, aStack);
|
2018-07-21 15:34:58 +03:00
|
|
|
|
|
|
|
JSPrincipals* principals = GetPrincipalsForStackGetter(aCx, stack, aCanCache);
|
2015-03-06 09:08:06 +03:00
|
|
|
if (*aCanCache && aIsCached) {
|
|
|
|
*aUseCachedValue = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aUseCachedValue = false;
|
|
|
|
|
2018-07-21 15:34:58 +03:00
|
|
|
aPropGetter(aCx, principals, stack, aValue,
|
|
|
|
JS::SavedFrameSelfHosted::Exclude);
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:04 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetFilenameXPCOM(JSContext* aCx,
|
|
|
|
nsAString& aFilename) {
|
|
|
|
GetFilename(aCx, aFilename);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSStackFrame::GetFilename(JSContext* aCx, nsAString& aFilename) {
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
aFilename.Truncate();
|
2018-02-06 00:34:04 +03:00
|
|
|
return;
|
2015-03-27 06:05:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
JS::Rooted<JSString*> filename(aCx);
|
2015-03-06 09:08:06 +03:00
|
|
|
bool canCache = false, useCachedValue = false;
|
2016-03-22 20:50:31 +03:00
|
|
|
GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameSource,
|
2015-03-06 09:08:06 +03:00
|
|
|
mFilenameInitialized, &canCache, &useCachedValue,
|
|
|
|
&filename);
|
|
|
|
if (useCachedValue) {
|
2016-03-22 20:50:31 +03:00
|
|
|
aFilename = mFilename;
|
2018-02-06 00:34:04 +03:00
|
|
|
return;
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
2015-02-13 20:21:50 +03:00
|
|
|
|
2015-03-06 09:08:06 +03:00
|
|
|
nsAutoJSString str;
|
2016-03-22 20:50:31 +03:00
|
|
|
if (!str.init(aCx, filename)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
2015-03-27 06:05:16 +03:00
|
|
|
aFilename.Truncate();
|
2018-02-06 00:34:04 +03:00
|
|
|
return;
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
|
|
|
aFilename = str;
|
|
|
|
|
|
|
|
if (canCache) {
|
2014-07-04 09:25:16 +04:00
|
|
|
mFilename = str;
|
2014-01-08 04:53:18 +04:00
|
|
|
mFilenameInitialized = true;
|
|
|
|
}
|
2014-07-04 09:25:16 +04:00
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:05 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
JSStackFrame::GetNameXPCOM(JSContext* aCx, nsAString& aFunction) {
|
|
|
|
GetName(aCx, aFunction);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSStackFrame::GetName(JSContext* aCx, nsAString& aFunction) {
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
aFunction.Truncate();
|
2018-02-06 00:34:05 +03:00
|
|
|
return;
|
2015-03-27 06:05:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
JS::Rooted<JSString*> name(aCx);
|
2015-03-06 09:08:06 +03:00
|
|
|
bool canCache = false, useCachedValue = false;
|
2016-03-22 20:50:31 +03:00
|
|
|
GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameFunctionDisplayName,
|
2015-03-06 09:08:06 +03:00
|
|
|
mFunnameInitialized, &canCache, &useCachedValue, &name);
|
|
|
|
|
|
|
|
if (useCachedValue) {
|
2016-03-22 20:50:31 +03:00
|
|
|
aFunction = mFunname;
|
2018-02-06 00:34:05 +03:00
|
|
|
return;
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
nsAutoJSString str;
|
2016-03-22 20:50:31 +03:00
|
|
|
if (!str.init(aCx, name)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
2015-03-27 06:05:16 +03:00
|
|
|
aFunction.Truncate();
|
2018-02-06 00:34:05 +03:00
|
|
|
return;
|
2014-01-08 04:53:18 +04:00
|
|
|
}
|
2015-03-06 09:08:06 +03:00
|
|
|
aFunction = str;
|
|
|
|
} else {
|
|
|
|
aFunction.SetIsVoid(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canCache) {
|
|
|
|
mFunname = aFunction;
|
2014-01-08 04:53:18 +04:00
|
|
|
mFunnameInitialized = true;
|
|
|
|
}
|
2014-07-04 09:25:16 +04:00
|
|
|
}
|
|
|
|
|
2019-01-17 01:00:38 +03:00
|
|
|
int32_t JSStackFrame::GetSourceId(JSContext* aCx) {
|
|
|
|
if (!mStack) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t id;
|
|
|
|
bool canCache = false, useCachedValue = false;
|
|
|
|
GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameSourceId,
|
|
|
|
mSourceIdInitialized, &canCache, &useCachedValue, &id);
|
|
|
|
|
|
|
|
if (useCachedValue) {
|
|
|
|
return mSourceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canCache) {
|
|
|
|
mSourceId = id;
|
|
|
|
mSourceIdInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
JSStackFrame::GetSourceIdXPCOM(JSContext* aCx, int32_t* aSourceId) {
|
|
|
|
*aSourceId = GetSourceId(aCx);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:04 +03:00
|
|
|
int32_t JSStackFrame::GetLineNumber(JSContext* aCx) {
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-06 09:08:06 +03:00
|
|
|
uint32_t line;
|
|
|
|
bool canCache = false, useCachedValue = false;
|
2016-03-22 20:50:31 +03:00
|
|
|
GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameLine, mLinenoInitialized,
|
2015-03-06 09:08:06 +03:00
|
|
|
&canCache, &useCachedValue, &line);
|
|
|
|
|
|
|
|
if (useCachedValue) {
|
2016-03-22 20:50:31 +03:00
|
|
|
return mLineno;
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (canCache) {
|
2015-03-06 09:08:06 +03:00
|
|
|
mLineno = line;
|
2014-01-08 04:53:18 +04:00
|
|
|
mLinenoInitialized = true;
|
|
|
|
}
|
|
|
|
|
2015-03-27 06:05:16 +03:00
|
|
|
return line;
|
2014-01-08 04:53:18 +04:00
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:04 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
JSStackFrame::GetLineNumberXPCOM(JSContext* aCx, int32_t* aLineNumber) {
|
|
|
|
*aLineNumber = GetLineNumber(aCx);
|
2015-03-27 06:05:16 +03:00
|
|
|
return NS_OK;
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:04 +03:00
|
|
|
int32_t JSStackFrame::GetColumnNumber(JSContext* aCx) {
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-06 09:08:06 +03:00
|
|
|
uint32_t col;
|
|
|
|
bool canCache = false, useCachedValue = false;
|
2016-03-22 20:50:31 +03:00
|
|
|
GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameColumn, mColNoInitialized,
|
2015-03-06 09:08:06 +03:00
|
|
|
&canCache, &useCachedValue, &col);
|
|
|
|
|
|
|
|
if (useCachedValue) {
|
2016-03-22 20:50:31 +03:00
|
|
|
return mColNo;
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (canCache) {
|
2015-03-06 09:08:06 +03:00
|
|
|
mColNo = col;
|
2014-09-22 22:28:31 +04:00
|
|
|
mColNoInitialized = true;
|
|
|
|
}
|
|
|
|
|
2015-03-27 06:05:16 +03:00
|
|
|
return col;
|
2014-09-22 22:28:31 +04:00
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:04 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
JSStackFrame::GetColumnNumberXPCOM(JSContext* aCx, int32_t* aColumnNumber) {
|
|
|
|
*aColumnNumber = GetColumnNumber(aCx);
|
2015-03-27 06:05:16 +03:00
|
|
|
return NS_OK;
|
2014-09-22 22:28:31 +04:00
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetSourceLine(nsACString& aSourceLine) {
|
2014-01-20 20:51:41 +04:00
|
|
|
aSourceLine.Truncate();
|
2013-09-09 07:28:50 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:04 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
JSStackFrame::GetAsyncCauseXPCOM(JSContext* aCx, nsAString& aAsyncCause) {
|
|
|
|
GetAsyncCause(aCx, aAsyncCause);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSStackFrame::GetAsyncCause(JSContext* aCx, nsAString& aAsyncCause) {
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
aAsyncCause.Truncate();
|
2018-02-06 00:34:04 +03:00
|
|
|
return;
|
2015-03-27 06:05:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
JS::Rooted<JSString*> asyncCause(aCx);
|
2015-03-07 16:30:34 +03:00
|
|
|
bool canCache = false, useCachedValue = false;
|
2016-03-22 20:50:31 +03:00
|
|
|
GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameAsyncCause,
|
2015-03-07 16:30:34 +03:00
|
|
|
mAsyncCauseInitialized, &canCache, &useCachedValue,
|
|
|
|
&asyncCause);
|
|
|
|
|
|
|
|
if (useCachedValue) {
|
2016-03-22 20:50:31 +03:00
|
|
|
aAsyncCause = mAsyncCause;
|
2018-02-06 00:34:04 +03:00
|
|
|
return;
|
2015-03-07 16:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (asyncCause) {
|
|
|
|
nsAutoJSString str;
|
2016-03-22 20:50:31 +03:00
|
|
|
if (!str.init(aCx, asyncCause)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
2015-03-27 06:05:16 +03:00
|
|
|
aAsyncCause.Truncate();
|
2018-02-06 00:34:04 +03:00
|
|
|
return;
|
2015-03-07 16:30:34 +03:00
|
|
|
}
|
|
|
|
aAsyncCause = str;
|
|
|
|
} else {
|
|
|
|
aAsyncCause.SetIsVoid(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canCache) {
|
|
|
|
mAsyncCause = aAsyncCause;
|
|
|
|
mAsyncCauseInitialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:05 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
JSStackFrame::GetAsyncCallerXPCOM(JSContext* aCx,
|
|
|
|
nsIStackFrame** aAsyncCaller) {
|
|
|
|
*aAsyncCaller = GetAsyncCaller(aCx).take();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIStackFrame> JSStackFrame::GetAsyncCaller(JSContext* aCx) {
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
2018-02-06 00:34:05 +03:00
|
|
|
return nullptr;
|
2015-03-27 06:05:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
JS::Rooted<JSObject*> asyncCallerObj(aCx);
|
2015-03-07 16:30:34 +03:00
|
|
|
bool canCache = false, useCachedValue = false;
|
2016-03-22 20:50:31 +03:00
|
|
|
GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameAsyncParent,
|
2015-03-07 16:30:34 +03:00
|
|
|
mAsyncCallerInitialized, &canCache, &useCachedValue,
|
|
|
|
&asyncCallerObj);
|
|
|
|
|
|
|
|
if (useCachedValue) {
|
2018-02-06 00:34:05 +03:00
|
|
|
nsCOMPtr<nsIStackFrame> asyncCaller = mAsyncCaller;
|
|
|
|
return asyncCaller.forget();
|
2015-03-07 16:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStackFrame> asyncCaller =
|
|
|
|
asyncCallerObj ? new JSStackFrame(asyncCallerObj) : nullptr;
|
|
|
|
|
|
|
|
if (canCache) {
|
2018-02-06 00:34:05 +03:00
|
|
|
mAsyncCaller = asyncCaller;
|
2015-03-07 16:30:34 +03:00
|
|
|
mAsyncCallerInitialized = true;
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:05 +03:00
|
|
|
return asyncCaller.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
JSStackFrame::GetCallerXPCOM(JSContext* aCx, nsIStackFrame** aCaller) {
|
|
|
|
*aCaller = GetCaller(aCx).take();
|
2015-03-07 16:30:34 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:05 +03:00
|
|
|
already_AddRefed<nsIStackFrame> JSStackFrame::GetCaller(JSContext* aCx) {
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
2018-02-06 00:34:05 +03:00
|
|
|
return nullptr;
|
2015-03-27 06:05:16 +03:00
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:35 +03:00
|
|
|
JS::Rooted<JSObject*> callerObj(aCx);
|
2015-03-06 09:08:06 +03:00
|
|
|
bool canCache = false, useCachedValue = false;
|
2016-03-22 20:50:35 +03:00
|
|
|
GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameParent, mCallerInitialized,
|
2015-03-06 09:08:06 +03:00
|
|
|
&canCache, &useCachedValue, &callerObj);
|
|
|
|
|
|
|
|
if (useCachedValue) {
|
2018-02-06 00:34:05 +03:00
|
|
|
nsCOMPtr<nsIStackFrame> caller = mCaller;
|
|
|
|
return caller.forget();
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
2015-10-21 09:40:00 +03:00
|
|
|
nsCOMPtr<nsIStackFrame> caller =
|
|
|
|
callerObj ? new JSStackFrame(callerObj) : nullptr;
|
2015-03-06 09:08:06 +03:00
|
|
|
|
|
|
|
if (canCache) {
|
2018-02-06 00:34:05 +03:00
|
|
|
mCaller = caller;
|
2014-01-08 04:53:18 +04:00
|
|
|
mCallerInitialized = true;
|
|
|
|
}
|
2015-03-06 09:08:06 +03:00
|
|
|
|
2018-02-06 00:34:05 +03:00
|
|
|
return caller.forget();
|
2014-07-04 09:25:16 +04:00
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:05 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
JSStackFrame::GetFormattedStackXPCOM(JSContext* aCx, nsAString& aStack) {
|
|
|
|
GetFormattedStack(aCx, aStack);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSStackFrame::GetFormattedStack(JSContext* aCx, nsAString& aStack) {
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
aStack.Truncate();
|
2018-02-06 00:34:05 +03:00
|
|
|
return;
|
2015-03-27 06:05:16 +03:00
|
|
|
}
|
|
|
|
|
2015-03-06 09:08:06 +03:00
|
|
|
// Sadly we can't use GetValueIfNotCached here, because our getter
|
|
|
|
// returns bool, not JS::SavedFrameResult. Maybe it's possible to
|
|
|
|
// make the templates more complicated to deal, but in the meantime
|
|
|
|
// let's just inline GetValueIfNotCached here.
|
|
|
|
|
2018-07-21 15:34:58 +03:00
|
|
|
JS::Rooted<JSObject*> stack(aCx, mStack);
|
|
|
|
|
|
|
|
bool canCache;
|
|
|
|
JSPrincipals* principals = GetPrincipalsForStackGetter(aCx, stack, &canCache);
|
2015-03-06 09:08:06 +03:00
|
|
|
if (canCache && mFormattedStackInitialized) {
|
|
|
|
aStack = mFormattedStack;
|
2018-02-06 00:34:05 +03:00
|
|
|
return;
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
2015-03-06 09:08:06 +03:00
|
|
|
|
2016-03-22 20:50:35 +03:00
|
|
|
JS::Rooted<JSString*> formattedStack(aCx);
|
2018-07-21 15:34:58 +03:00
|
|
|
if (!JS::BuildStackString(aCx, principals, stack, &formattedStack)) {
|
2016-03-22 20:50:35 +03:00
|
|
|
JS_ClearPendingException(aCx);
|
2015-03-27 06:05:16 +03:00
|
|
|
aStack.Truncate();
|
2018-02-06 00:34:05 +03:00
|
|
|
return;
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoJSString str;
|
2016-03-22 20:50:35 +03:00
|
|
|
if (!str.init(aCx, formattedStack)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
2015-03-27 06:05:16 +03:00
|
|
|
aStack.Truncate();
|
2018-02-06 00:34:05 +03:00
|
|
|
return;
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
aStack = str;
|
|
|
|
|
|
|
|
if (canCache) {
|
2014-07-04 09:25:16 +04:00
|
|
|
mFormattedStack = str;
|
|
|
|
mFormattedStackInitialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-07 16:30:34 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetNativeSavedFrame(
|
|
|
|
JS::MutableHandle<JS::Value> aSavedFrame) {
|
|
|
|
aSavedFrame.setObjectOrNull(mStack);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:34:05 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
JSStackFrame::ToStringXPCOM(JSContext* aCx, nsACString& _retval) {
|
|
|
|
ToString(aCx, _retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSStackFrame::ToString(JSContext* aCx, nsACString& _retval) {
|
2014-01-20 20:51:41 +04:00
|
|
|
_retval.Truncate();
|
|
|
|
|
2014-04-12 06:20:40 +04:00
|
|
|
nsString filename;
|
2018-02-06 00:34:04 +03:00
|
|
|
GetFilename(aCx, filename);
|
2014-01-20 20:51:41 +04:00
|
|
|
|
|
|
|
if (filename.IsEmpty()) {
|
2014-04-12 06:20:40 +04:00
|
|
|
filename.AssignLiteral("<unknown filename>");
|
2014-01-08 04:53:18 +04:00
|
|
|
}
|
2014-01-20 20:51:41 +04:00
|
|
|
|
2014-04-12 06:20:40 +04:00
|
|
|
nsString funname;
|
2018-02-06 00:34:05 +03:00
|
|
|
GetName(aCx, funname);
|
2014-01-20 20:51:41 +04:00
|
|
|
|
|
|
|
if (funname.IsEmpty()) {
|
2014-04-12 06:20:40 +04:00
|
|
|
funname.AssignLiteral("<TOP_LEVEL>");
|
2014-01-08 04:53:18 +04:00
|
|
|
}
|
2014-07-04 09:25:16 +04:00
|
|
|
|
2018-02-06 00:34:04 +03:00
|
|
|
int32_t lineno = GetLineNumber(aCx);
|
2014-07-04 09:25:16 +04:00
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
static const char format[] = "JS frame :: %s :: %s :: line %d";
|
2014-04-12 06:20:40 +04:00
|
|
|
_retval.AppendPrintf(format, NS_ConvertUTF16toUTF8(filename).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(funname).get(), lineno);
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
already_AddRefed<nsIStackFrame> CreateStack(JSContext* aCx,
|
2016-07-22 06:41:02 +03:00
|
|
|
JS::StackCapture&& aCaptureMode) {
|
2014-07-04 09:25:16 +04:00
|
|
|
JS::Rooted<JSObject*> stack(aCx);
|
2018-05-30 22:15:35 +03:00
|
|
|
if (!JS::CaptureCurrentStack(aCx, &stack, std::move(aCaptureMode))) {
|
2013-09-09 07:28:50 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-12-07 21:59:14 +03:00
|
|
|
return CreateStack(aCx, stack);
|
|
|
|
}
|
2019-12-07 20:41:36 +03:00
|
|
|
|
2019-12-07 21:59:14 +03:00
|
|
|
already_AddRefed<nsIStackFrame> CreateStack(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aStack) {
|
|
|
|
if (aStack) {
|
|
|
|
return MakeAndAddRef<JSStackFrame>(aStack);
|
|
|
|
}
|
|
|
|
return nullptr;
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace exceptions
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|