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"
|
|
|
|
|
|
|
|
#include "js/GCAPI.h"
|
2015-02-13 20:21:50 +03:00
|
|
|
#include "js/TypeDecls.h"
|
2013-09-09 07:28:50 +04:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsprf.h"
|
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
#include "mozilla/dom/DOMException.h"
|
2014-07-02 14:26:49 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2015-03-30 20:45:39 +03:00
|
|
|
#include "nsIProgrammingLanguage.h"
|
2014-07-02 14:26:49 +04:00
|
|
|
#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
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThrowExceptionObject(JSContext* aCx, nsIException* aException)
|
|
|
|
{
|
|
|
|
// See if we really have an Exception.
|
|
|
|
nsCOMPtr<Exception> exception = do_QueryInterface(aException);
|
|
|
|
if (exception) {
|
|
|
|
return ThrowExceptionObject(aCx, exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only have an nsIException (probably an XPCWrappedJS). Fall back on old
|
|
|
|
// wrapping.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2013-09-09 07:29:21 +04:00
|
|
|
JS::Rooted<JSObject*> glob(aCx, JS::CurrentGlobalOrNull(aCx));
|
2013-09-09 07:28:50 +04:00
|
|
|
if (!glob) {
|
2014-04-10 08:58:43 +04:00
|
|
|
// XXXbz Can this really be null here?
|
2013-09-09 07:28:50 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-23 07:35:09 +04:00
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
2014-04-10 08:58:43 +04:00
|
|
|
if (!WrapObject(aCx, aException, &NS_GET_IID(nsIException), &val)) {
|
2013-09-09 07:28:50 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-23 07:35:09 +04:00
|
|
|
JS_SetPendingException(aCx, val);
|
2013-09-09 07:28:50 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
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())) {
|
2013-10-26 20:19:05 +04:00
|
|
|
if (!JS_WrapValue(aCx, &thrown)) {
|
2013-09-09 07:28:50 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JS_SetPendingException(aCx, thrown);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:29:21 +04:00
|
|
|
JS::Rooted<JSObject*> glob(aCx, JS::CurrentGlobalOrNull(aCx));
|
2013-09-09 07:28:50 +04:00
|
|
|
if (!glob) {
|
2014-04-09 02:27:19 +04:00
|
|
|
// XXXbz Can this actually be null here?
|
2013-09-09 07:28:50 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-26 22:25:20 +03:00
|
|
|
if (!GetOrCreateDOMReflector(aCx, aException, &thrown)) {
|
2013-09-09 07:28:50 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:29:21 +04:00
|
|
|
JS_SetPendingException(aCx, thrown);
|
2013-09-09 07:28:50 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:29:21 +04:00
|
|
|
bool
|
2015-10-09 23:48:10 +03:00
|
|
|
Throw(JSContext* aCx, nsresult aRv, const nsACString& aMessage)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
CycleCollectedJSRuntime* runtime = CycleCollectedJSRuntime::Get();
|
|
|
|
nsCOMPtr<nsIException> existingException = runtime->GetPendingException();
|
|
|
|
if (existingException) {
|
|
|
|
nsresult nr;
|
2015-10-21 17:39:00 +03:00
|
|
|
if (NS_SUCCEEDED(existingException->GetResult(&nr)) &&
|
2013-09-09 07:29:21 +04:00
|
|
|
aRv == nr) {
|
2013-10-10 01:43:15 +04:00
|
|
|
// Reuse the existing exception.
|
|
|
|
|
|
|
|
// Clear pending exception
|
|
|
|
runtime->SetPendingException(nullptr);
|
|
|
|
|
|
|
|
if (!ThrowExceptionObject(aCx, existingException)) {
|
|
|
|
// If we weren't able to throw an exception we're
|
|
|
|
// most likely out of memory
|
|
|
|
JS_ReportOutOfMemory(aCx);
|
|
|
|
}
|
2013-09-09 07:29:21 +04:00
|
|
|
return false;
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Exception> finalException = CreateException(aCx, aRv, aMessage);
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2014-04-16 23:13:41 +04:00
|
|
|
MOZ_ASSERT(finalException);
|
|
|
|
if (!ThrowExceptionObject(aCx, finalException)) {
|
|
|
|
// If we weren't able to throw an exception we're
|
|
|
|
// most likely out of memory
|
|
|
|
JS_ReportOutOfMemory(aCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-02 14:26:49 +04:00
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
ThrowAndReport(nsPIDOMWindowInner* aWindow, nsresult aRv)
|
2014-07-02 14:26:49 +04:00
|
|
|
{
|
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-06-16 03:11:54 +03:00
|
|
|
jsapi.TakeOwnershipOfErrorReporting();
|
2014-07-02 14:26:49 +04:00
|
|
|
|
2015-10-09 23:48:10 +03:00
|
|
|
Throw(jsapi.cx(), aRv);
|
2014-07-02 14:26:49 +04:00
|
|
|
}
|
|
|
|
|
2014-04-16 23:13:41 +04:00
|
|
|
already_AddRefed<Exception>
|
2015-10-09 23:48:10 +03:00
|
|
|
CreateException(JSContext* aCx, nsresult aRv, const nsACString& aMessage)
|
2014-04-16 23:13:41 +04:00
|
|
|
{
|
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:
|
2014-08-25 10:51:20 +04:00
|
|
|
case NS_ERROR_MODULE_DOM_BLUETOOTH:
|
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:
|
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 =
|
2015-10-09 23:48:10 +03:00
|
|
|
new Exception(aMessage, aRv, EmptyCString(), nullptr, nullptr);
|
2014-04-16 23:13:41 +04:00
|
|
|
return exception.forget();
|
2013-09-09 07:29:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIStackFrame>
|
2016-03-22 20:50:31 +03:00
|
|
|
GetCurrentJSStack(int32_t aMaxDepth)
|
2013-09-09 07:29:21 +04:00
|
|
|
{
|
|
|
|
// is there a current context available?
|
2016-03-22 20:50:31 +03:00
|
|
|
JSContext* cx = nsContentUtils::GetCurrentJSContextForThread();
|
2013-09-09 07:29:21 +04:00
|
|
|
|
2015-07-09 10:29:17 +03:00
|
|
|
if (!cx || !js::GetContextCompartment(cx)) {
|
2013-09-09 07:29:21 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
return exceptions::CreateStack(cx, aMaxDepth);
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
2015-01-15 00:43:27 +03:00
|
|
|
AutoForceSetExceptionOnContext::AutoForceSetExceptionOnContext(JSContext* aCx)
|
|
|
|
: mCx(aCx)
|
|
|
|
{
|
|
|
|
mOldValue = JS::ContextOptionsRef(mCx).autoJSAPIOwnsErrorReporting();
|
|
|
|
JS::ContextOptionsRef(mCx).setAutoJSAPIOwnsErrorReporting(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoForceSetExceptionOnContext::~AutoForceSetExceptionOnContext()
|
|
|
|
{
|
|
|
|
JS::ContextOptionsRef(mCx).setAutoJSAPIOwnsErrorReporting(mOldValue);
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:28:50 +04:00
|
|
|
namespace exceptions {
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
class JSStackFrame : public nsIStackFrame
|
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
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
protected:
|
2016-03-22 20:50:31 +03:00
|
|
|
int32_t GetLineno(JSContext* aCx);
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
int32_t GetColNo(JSContext* aCx);
|
2014-01-08 04:53:18 +04:00
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
private:
|
|
|
|
virtual ~JSStackFrame();
|
2014-07-04 10:23:35 +04:00
|
|
|
|
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;
|
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;
|
|
|
|
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)
|
2016-03-22 20:50:31 +03:00
|
|
|
, mLineno(0)
|
|
|
|
, mColNo(0)
|
2014-07-04 09:25:16 +04:00
|
|
|
, mFilenameInitialized(false)
|
|
|
|
, mFunnameInitialized(false)
|
|
|
|
, 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-01-08 04:53:18 +04:00
|
|
|
{
|
2014-07-04 09:25:16 +04:00
|
|
|
MOZ_ASSERT(mStack);
|
2014-07-04 09:25:16 +04:00
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
mozilla::HoldJSObjects(this);
|
2014-01-08 04:53:18 +04:00
|
|
|
}
|
2013-09-09 07:28:50 +04:00
|
|
|
|
|
|
|
JSStackFrame::~JSStackFrame()
|
|
|
|
{
|
2014-07-04 09:25:16 +04:00
|
|
|
mozilla::DropJSObjects(this);
|
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)
|
2014-07-04 09:25:16 +04:00
|
|
|
tmp->mStack = nullptr;
|
|
|
|
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_SCRIPT_OBJECTS
|
|
|
|
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
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetLanguage(uint32_t* aLanguage)
|
2014-07-04 09:25:16 +04:00
|
|
|
{
|
2016-03-22 20:50:31 +03:00
|
|
|
*aLanguage = nsIProgrammingLanguage::JAVASCRIPT;
|
2014-07-04 09:25:16 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-07-04 10:23:35 +04:00
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetLanguageName(nsACString& aLanguageName)
|
|
|
|
{
|
|
|
|
aLanguageName.AssignLiteral("JavaScript");
|
2013-09-09 07:28:50 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-06 09:08:06 +03:00
|
|
|
// Helper method to get the value of a stack property, if it's not already
|
|
|
|
// cached. This will make sure we skip the cache if the access is happening
|
|
|
|
// over Xrays.
|
|
|
|
//
|
|
|
|
// @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>
|
|
|
|
static void
|
|
|
|
GetValueIfNotCached(JSContext* aCx, JSObject* aStack,
|
|
|
|
JS::SavedFrameResult (*aPropGetter)(JSContext*,
|
|
|
|
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);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> stack(aCx, aStack);
|
|
|
|
// Allow caching if aCx and stack are same-compartment. Otherwise take the
|
|
|
|
// slow path.
|
|
|
|
*aCanCache = js::GetContextCompartment(aCx) == js::GetObjectCompartment(stack);
|
|
|
|
if (*aCanCache && aIsCached) {
|
|
|
|
*aUseCachedValue = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aUseCachedValue = false;
|
|
|
|
JS::ExposeObjectToActiveJS(stack);
|
|
|
|
|
2015-10-21 17:39:00 +03:00
|
|
|
aPropGetter(aCx, stack, aValue, JS::SavedFrameSelfHosted::Exclude);
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetFilename(JSContext* aCx, nsAString& aFilename)
|
2014-01-08 04:53:18 +04:00
|
|
|
{
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
aFilename.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
mFilenameInitialized,
|
2015-03-06 09:08:06 +03:00
|
|
|
&canCache, &useCachedValue, &filename);
|
|
|
|
if (useCachedValue) {
|
2016-03-22 20:50:31 +03:00
|
|
|
aFilename = mFilename;
|
|
|
|
return NS_OK;
|
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();
|
|
|
|
return NS_OK;
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-06 09:08:06 +03:00
|
|
|
return NS_OK;
|
2014-07-04 09:25:16 +04:00
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetName(JSContext* aCx, nsAString& aFunction)
|
2014-01-08 04:53:18 +04:00
|
|
|
{
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
aFunction.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return NS_OK;
|
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();
|
|
|
|
return NS_OK;
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-06 09:08:06 +03:00
|
|
|
return NS_OK;
|
2014-07-04 09:25:16 +04:00
|
|
|
}
|
|
|
|
|
2015-03-27 06:05:16 +03:00
|
|
|
int32_t
|
2016-03-22 20:50:31 +03:00
|
|
|
JSStackFrame::GetLineno(JSContext* aCx)
|
2014-01-08 04:53:18 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetLineNumber(JSContext* aCx, int32_t* aLineNumber)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
2016-03-22 20:50:31 +03:00
|
|
|
*aLineNumber = GetLineno(aCx);
|
2015-03-27 06:05:16 +03:00
|
|
|
return NS_OK;
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
2015-03-27 06:05:16 +03:00
|
|
|
int32_t
|
2016-03-22 20:50:31 +03:00
|
|
|
JSStackFrame::GetColNo(JSContext* aCx)
|
2014-09-22 22:28:31 +04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetColumnNumber(JSContext* aCx,
|
|
|
|
int32_t* aColumnNumber)
|
2014-09-22 22:28:31 +04:00
|
|
|
{
|
2016-03-22 20:50:31 +03:00
|
|
|
*aColumnNumber = GetColNo(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)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
2014-01-20 20:51:41 +04:00
|
|
|
aSourceLine.Truncate();
|
2013-09-09 07:28:50 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetAsyncCause(JSContext* aCx,
|
|
|
|
nsAString& aAsyncCause)
|
2015-03-07 16:30:34 +03:00
|
|
|
{
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
aAsyncCause.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return NS_OK;
|
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();
|
|
|
|
return NS_OK;
|
2015-03-07 16:30:34 +03:00
|
|
|
}
|
|
|
|
aAsyncCause = str;
|
|
|
|
} else {
|
|
|
|
aAsyncCause.SetIsVoid(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canCache) {
|
|
|
|
mAsyncCause = aAsyncCause;
|
|
|
|
mAsyncCauseInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetAsyncCaller(JSContext* aCx,
|
|
|
|
nsIStackFrame** aAsyncCaller)
|
2015-03-07 16:30:34 +03:00
|
|
|
{
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
*aAsyncCaller = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IF_ADDREF(*aAsyncCaller = mAsyncCaller);
|
|
|
|
return NS_OK;
|
2015-03-07 16:30:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStackFrame> asyncCaller =
|
|
|
|
asyncCallerObj ? new JSStackFrame(asyncCallerObj) : nullptr;
|
|
|
|
asyncCaller.forget(aAsyncCaller);
|
|
|
|
|
|
|
|
if (canCache) {
|
|
|
|
mAsyncCaller = *aAsyncCaller;
|
|
|
|
mAsyncCallerInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:35 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetCaller(JSContext* aCx, nsIStackFrame** aCaller)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
*aCaller = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IF_ADDREF(*aCaller = mCaller);
|
|
|
|
return NS_OK;
|
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
|
|
|
caller.forget(aCaller);
|
|
|
|
|
|
|
|
if (canCache) {
|
|
|
|
mCaller = *aCaller;
|
2014-01-08 04:53:18 +04:00
|
|
|
mCallerInitialized = true;
|
|
|
|
}
|
2015-03-06 09:08:06 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2014-07-04 09:25:16 +04:00
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:35 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetFormattedStack(JSContext* aCx, nsAString& aStack)
|
2014-07-04 09:25:16 +04:00
|
|
|
{
|
2015-03-27 06:05:16 +03:00
|
|
|
if (!mStack) {
|
|
|
|
aStack.Truncate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
2016-03-22 20:50:35 +03:00
|
|
|
// Allow caching if aCx and stack are same-compartment. Otherwise take the
|
2015-03-06 09:08:06 +03:00
|
|
|
// slow path.
|
|
|
|
bool canCache =
|
2016-03-22 20:50:35 +03:00
|
|
|
js::GetContextCompartment(aCx) == js::GetObjectCompartment(mStack);
|
2015-03-06 09:08:06 +03:00
|
|
|
if (canCache && mFormattedStackInitialized) {
|
|
|
|
aStack = mFormattedStack;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-03-06 09:08:06 +03:00
|
|
|
|
2015-03-06 09:08:06 +03:00
|
|
|
JS::ExposeObjectToActiveJS(mStack);
|
2016-03-22 20:50:35 +03:00
|
|
|
JS::Rooted<JSObject*> stack(aCx, mStack);
|
2015-03-06 09:08:06 +03:00
|
|
|
|
2016-03-22 20:50:35 +03:00
|
|
|
JS::Rooted<JSString*> formattedStack(aCx);
|
|
|
|
if (!JS::BuildStackString(aCx, stack, &formattedStack)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
2015-03-27 06:05:16 +03:00
|
|
|
aStack.Truncate();
|
|
|
|
return NS_OK;
|
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();
|
|
|
|
return NS_OK;
|
2015-03-06 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
aStack = str;
|
|
|
|
|
|
|
|
if (canCache) {
|
2014-07-04 09:25:16 +04:00
|
|
|
mFormattedStack = str;
|
|
|
|
mFormattedStackInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-07 16:30:34 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetNativeSavedFrame(JS::MutableHandle<JS::Value> aSavedFrame)
|
|
|
|
{
|
|
|
|
aSavedFrame.setObjectOrNull(mStack);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
NS_IMETHODIMP JSStackFrame::ToString(JSContext* aCx, nsACString& _retval)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
2014-01-20 20:51:41 +04:00
|
|
|
_retval.Truncate();
|
|
|
|
|
2014-04-12 06:20:40 +04:00
|
|
|
nsString filename;
|
2016-03-22 20:50:31 +03:00
|
|
|
nsresult rv = GetFilename(aCx, filename);
|
2014-01-20 20:51:41 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
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;
|
2016-03-22 20:50:31 +03:00
|
|
|
rv = GetName(aCx, funname);
|
2014-01-20 20:51:41 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
int32_t lineno = GetLineno(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";
|
|
|
|
_retval.AppendPrintf(format,
|
2014-04-12 06:20:40 +04:00
|
|
|
NS_ConvertUTF16toUTF8(filename).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(funname).get(),
|
2014-07-04 09:25:16 +04:00
|
|
|
lineno);
|
2013-09-09 07:28:50 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
already_AddRefed<nsIStackFrame>
|
|
|
|
CreateStack(JSContext* aCx, int32_t aMaxDepth)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
|
|
|
static const unsigned MAX_FRAMES = 100;
|
2014-01-26 16:35:17 +04:00
|
|
|
if (aMaxDepth < 0) {
|
|
|
|
aMaxDepth = MAX_FRAMES;
|
|
|
|
}
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
JS::Rooted<JSObject*> stack(aCx);
|
|
|
|
if (!JS::CaptureCurrentStack(aCx, &stack, aMaxDepth)) {
|
2013-09-09 07:28:50 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
if (!stack) {
|
2016-03-22 20:50:31 +03:00
|
|
|
return nullptr;
|
2014-07-04 09:25:16 +04:00
|
|
|
}
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
nsCOMPtr<nsIStackFrame> frame = new JSStackFrame(stack);
|
|
|
|
return frame.forget();
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace exceptions
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|