2013-09-09 07:28:50 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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"
|
|
|
|
#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"
|
|
|
|
#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-04-09 02:27:19 +04:00
|
|
|
if (!WrapNewBindingObject(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
|
|
|
|
Throw(JSContext* aCx, nsresult aRv, const char* aMessage)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
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;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 23:13:41 +04:00
|
|
|
nsRefPtr<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
|
|
|
|
ThrowAndReport(nsPIDOMWindow* aWindow, nsresult aRv, const char* aMessage)
|
|
|
|
{
|
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.InitWithLegacyErrorReporting(aWindow))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Throw(jsapi.cx(), aRv, aMessage);
|
|
|
|
(void) JS_ReportPendingException(jsapi.cx());
|
|
|
|
}
|
|
|
|
|
2014-04-16 23:13:41 +04:00
|
|
|
already_AddRefed<Exception>
|
|
|
|
CreateException(JSContext* aCx, nsresult aRv, const char* 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:
|
2014-08-25 10:51:20 +04:00
|
|
|
case NS_ERROR_MODULE_DOM_BLUETOOTH:
|
2014-04-16 23:13:41 +04:00
|
|
|
return DOMException::Create(aRv);
|
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.
|
2014-04-16 23:13:41 +04:00
|
|
|
// aMessage can be null, so we can't use nsDependentCString on it.
|
|
|
|
nsRefPtr<Exception> exception =
|
|
|
|
new Exception(nsCString(aMessage), aRv,
|
|
|
|
EmptyCString(), nullptr, nullptr);
|
|
|
|
return exception.forget();
|
2013-09-09 07:29:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIStackFrame>
|
|
|
|
GetCurrentJSStack()
|
|
|
|
{
|
|
|
|
// is there a current context available?
|
|
|
|
JSContext* cx = nullptr;
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
2014-05-07 02:43:03 +04:00
|
|
|
MOZ_ASSERT(nsContentUtils::XPConnect());
|
|
|
|
cx = nsContentUtils::GetCurrentJSContext();
|
2013-09-09 07:29:21 +04:00
|
|
|
} else {
|
|
|
|
cx = workers::GetCurrentThreadJSContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStackFrame> stack = exceptions::CreateStack(cx);
|
|
|
|
if (!stack) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-04 09:25:15 +04:00
|
|
|
// Note that CreateStack only returns JS frames, so we're done here.
|
2013-09-09 07:29:21 +04:00
|
|
|
return stack.forget();
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace exceptions {
|
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
class StackFrame : public nsIStackFrame
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-01-08 04:53:18 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(StackFrame)
|
2013-09-09 07:28:50 +04:00
|
|
|
NS_DECL_NSISTACKFRAME
|
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
StackFrame(uint32_t aLanguage,
|
|
|
|
const char* aFilename,
|
|
|
|
const char* aFunctionName,
|
|
|
|
int32_t aLineNumber,
|
|
|
|
nsIStackFrame* aCaller);
|
|
|
|
|
|
|
|
StackFrame()
|
|
|
|
: mLineno(0)
|
2014-09-22 22:28:31 +04:00
|
|
|
, mColNo(0)
|
2014-07-04 09:25:16 +04:00
|
|
|
, mLanguage(nsIProgrammingLanguage::UNKNOWN)
|
|
|
|
{
|
|
|
|
}
|
2013-09-09 07:28:50 +04:00
|
|
|
|
|
|
|
static already_AddRefed<nsIStackFrame>
|
|
|
|
CreateStackFrameLocation(uint32_t aLanguage,
|
|
|
|
const char* aFilename,
|
|
|
|
const char* aFunctionName,
|
|
|
|
int32_t aLineNumber,
|
|
|
|
nsIStackFrame* aCaller);
|
2014-07-04 09:25:16 +04:00
|
|
|
protected:
|
|
|
|
virtual ~StackFrame();
|
2013-09-09 07:28:50 +04:00
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
virtual bool IsJSFrame() const
|
|
|
|
{
|
|
|
|
return false;
|
2014-07-04 09:25:16 +04:00
|
|
|
}
|
2014-01-08 04:53:18 +04:00
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
virtual nsresult GetLineno(int32_t* aLineNo)
|
2014-07-04 09:25:16 +04:00
|
|
|
{
|
2014-07-04 09:25:16 +04:00
|
|
|
*aLineNo = mLineno;
|
|
|
|
return NS_OK;
|
2014-07-04 09:25:16 +04:00
|
|
|
}
|
2014-07-04 10:23:35 +04:00
|
|
|
|
2014-09-22 22:28:31 +04:00
|
|
|
virtual nsresult GetColNo(int32_t* aColNo)
|
|
|
|
{
|
|
|
|
*aColNo = mColNo;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-09 07:28:50 +04:00
|
|
|
nsCOMPtr<nsIStackFrame> mCaller;
|
2014-04-12 06:20:40 +04:00
|
|
|
nsString mFilename;
|
|
|
|
nsString mFunname;
|
2013-09-09 07:28:50 +04:00
|
|
|
int32_t mLineno;
|
2014-09-22 22:28:31 +04:00
|
|
|
int32_t mColNo;
|
2013-09-09 07:28:50 +04:00
|
|
|
uint32_t mLanguage;
|
2014-07-04 09:25:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
StackFrame::StackFrame(uint32_t aLanguage,
|
|
|
|
const char* aFilename,
|
|
|
|
const char* aFunctionName,
|
|
|
|
int32_t aLineNumber,
|
|
|
|
nsIStackFrame* aCaller)
|
|
|
|
: mCaller(aCaller)
|
|
|
|
, mLineno(aLineNumber)
|
|
|
|
, mLanguage(aLanguage)
|
|
|
|
{
|
|
|
|
CopyUTF8toUTF16(aFilename, mFilename);
|
|
|
|
CopyUTF8toUTF16(aFunctionName, mFunname);
|
|
|
|
}
|
|
|
|
|
|
|
|
StackFrame::~StackFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(StackFrame, mCaller)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(StackFrame)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(StackFrame)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StackFrame)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStackFrame)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
class JSStackFrame : public StackFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(JSStackFrame,
|
|
|
|
StackFrame)
|
2014-07-04 09:25:16 +04:00
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
// aStack must not be null.
|
2014-09-02 02:26:43 +04:00
|
|
|
explicit JSStackFrame(JS::Handle<JSObject*> aStack);
|
2014-07-04 09:25:16 +04:00
|
|
|
|
|
|
|
static already_AddRefed<nsIStackFrame>
|
|
|
|
CreateStack(JSContext* aCx, int32_t aMaxDepth = -1);
|
|
|
|
|
|
|
|
NS_IMETHOD GetLanguageName(nsACString& aLanguageName) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD GetFilename(nsAString& aFilename) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD GetName(nsAString& aFunction) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD GetCaller(nsIStackFrame** aCaller) MOZ_OVERRIDE;
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMETHOD GetFormattedStack(nsAString& aStack) MOZ_OVERRIDE;
|
2014-07-04 09:25:16 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool IsJSFrame() const MOZ_OVERRIDE {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
virtual nsresult GetLineno(int32_t* aLineNo) MOZ_OVERRIDE;
|
2014-09-22 22:28:31 +04:00
|
|
|
virtual nsresult GetColNo(int32_t* aColNo) MOZ_OVERRIDE;
|
2014-07-04 09:25:16 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~JSStackFrame();
|
2014-01-08 04:53:18 +04:00
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
JS::Heap<JSObject*> mStack;
|
2014-07-04 09:25:16 +04:00
|
|
|
nsString mFormattedStack;
|
2014-01-08 04:53:18 +04:00
|
|
|
|
|
|
|
bool mFilenameInitialized;
|
|
|
|
bool mFunnameInitialized;
|
|
|
|
bool mLinenoInitialized;
|
2014-09-22 22:28:31 +04:00
|
|
|
bool mColNoInitialized;
|
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)
|
2014-07-04 09:25:16 +04:00
|
|
|
, mFilenameInitialized(false)
|
|
|
|
, mFunnameInitialized(false)
|
|
|
|
, mLinenoInitialized(false)
|
2014-09-22 22:28:31 +04:00
|
|
|
, mColNoInitialized(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-07-04 09:25:16 +04:00
|
|
|
mLineno = 0;
|
|
|
|
mLanguage = nsIProgrammingLanguage::JAVASCRIPT;
|
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)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(JSStackFrame, StackFrame)
|
|
|
|
tmp->mStack = nullptr;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(JSStackFrame, StackFrame)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(JSStackFrame, StackFrame)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mStack)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2014-01-08 04:53:18 +04:00
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(JSStackFrame, StackFrame)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(JSStackFrame, StackFrame)
|
2014-01-08 04:53:18 +04:00
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(JSStackFrame)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(StackFrame)
|
2013-09-09 07:28:50 +04:00
|
|
|
|
|
|
|
/* readonly attribute uint32_t language; */
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMETHODIMP StackFrame::GetLanguage(uint32_t* aLanguage)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
|
|
|
*aLanguage = mLanguage;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute string languageName; */
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMETHODIMP StackFrame::GetLanguageName(nsACString& aLanguageName)
|
2014-07-04 09:25:16 +04:00
|
|
|
{
|
2014-07-04 09:25:16 +04:00
|
|
|
aLanguageName.AssignLiteral("C++");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-12 06:20:40 +04:00
|
|
|
/* readonly attribute AString filename; */
|
|
|
|
NS_IMETHODIMP JSStackFrame::GetFilename(nsAString& aFilename)
|
2014-01-08 04:53:18 +04:00
|
|
|
{
|
2014-07-26 09:41:18 +04:00
|
|
|
// We can get called after unlink; in that case we can't do much
|
|
|
|
// about producing a useful value.
|
|
|
|
if (!mFilenameInitialized && mStack) {
|
2014-07-04 09:25:16 +04:00
|
|
|
ThreadsafeAutoJSContext cx;
|
|
|
|
JS::Rooted<JSObject*> stack(cx, mStack);
|
|
|
|
JS::ExposeObjectToActiveJS(mStack);
|
|
|
|
JSAutoCompartment ac(cx, stack);
|
|
|
|
JS::Rooted<JS::Value> filenameVal(cx);
|
|
|
|
if (!JS_GetProperty(cx, stack, "source", &filenameVal) ||
|
|
|
|
!filenameVal.isString()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-01-08 04:53:18 +04:00
|
|
|
}
|
2014-07-04 09:25:16 +04:00
|
|
|
nsAutoJSString str;
|
|
|
|
if (!str.init(cx, filenameVal.toString())) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mFilename = str;
|
2014-01-08 04:53:18 +04:00
|
|
|
mFilenameInitialized = true;
|
|
|
|
}
|
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
return StackFrame::GetFilename(aFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP StackFrame::GetFilename(nsAString& aFilename)
|
|
|
|
{
|
2014-01-20 20:51:41 +04:00
|
|
|
// The filename must be set to null if empty.
|
|
|
|
if (mFilename.IsEmpty()) {
|
|
|
|
aFilename.SetIsVoid(true);
|
2013-09-09 07:28:50 +04:00
|
|
|
} else {
|
2014-01-20 20:51:41 +04:00
|
|
|
aFilename.Assign(mFilename);
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-12 06:20:40 +04:00
|
|
|
/* readonly attribute AString name; */
|
|
|
|
NS_IMETHODIMP JSStackFrame::GetName(nsAString& aFunction)
|
2014-01-08 04:53:18 +04:00
|
|
|
{
|
2014-07-26 09:41:18 +04:00
|
|
|
// We can get called after unlink; in that case we can't do much
|
|
|
|
// about producing a useful value.
|
|
|
|
if (!mFunnameInitialized && mStack) {
|
2014-07-04 09:25:16 +04:00
|
|
|
ThreadsafeAutoJSContext cx;
|
|
|
|
JS::Rooted<JSObject*> stack(cx, mStack);
|
|
|
|
JS::ExposeObjectToActiveJS(mStack);
|
|
|
|
JSAutoCompartment ac(cx, stack);
|
|
|
|
JS::Rooted<JS::Value> nameVal(cx);
|
|
|
|
// functionDisplayName can be null
|
|
|
|
if (!JS_GetProperty(cx, stack, "functionDisplayName", &nameVal) ||
|
|
|
|
(!nameVal.isString() && !nameVal.isNull())) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
if (nameVal.isString()) {
|
|
|
|
nsAutoJSString str;
|
|
|
|
if (!str.init(cx, nameVal.toString())) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mFunname = str;
|
2014-01-08 04:53:18 +04:00
|
|
|
}
|
|
|
|
mFunnameInitialized = true;
|
|
|
|
}
|
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
return StackFrame::GetName(aFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP StackFrame::GetName(nsAString& aFunction)
|
|
|
|
{
|
2014-01-20 20:51:41 +04:00
|
|
|
// The function name must be set to null if empty.
|
|
|
|
if (mFunname.IsEmpty()) {
|
|
|
|
aFunction.SetIsVoid(true);
|
2013-09-09 07:28:50 +04:00
|
|
|
} else {
|
2014-01-20 20:51:41 +04:00
|
|
|
aFunction.Assign(mFunname);
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
// virtual
|
2014-07-04 09:25:16 +04:00
|
|
|
nsresult
|
|
|
|
JSStackFrame::GetLineno(int32_t* aLineNo)
|
2014-01-08 04:53:18 +04:00
|
|
|
{
|
2014-07-26 09:41:18 +04:00
|
|
|
// We can get called after unlink; in that case we can't do much
|
|
|
|
// about producing a useful value.
|
|
|
|
if (!mLinenoInitialized && mStack) {
|
2014-07-04 09:25:16 +04:00
|
|
|
ThreadsafeAutoJSContext cx;
|
|
|
|
JS::Rooted<JSObject*> stack(cx, mStack);
|
|
|
|
JS::ExposeObjectToActiveJS(mStack);
|
|
|
|
JSAutoCompartment ac(cx, stack);
|
|
|
|
JS::Rooted<JS::Value> lineVal(cx);
|
|
|
|
if (!JS_GetProperty(cx, stack, "line", &lineVal) ||
|
|
|
|
!lineVal.isNumber()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
mLineno = lineVal.toNumber();
|
2014-01-08 04:53:18 +04:00
|
|
|
mLinenoInitialized = true;
|
|
|
|
}
|
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
return StackFrame::GetLineno(aLineNo);
|
2014-01-08 04:53:18 +04:00
|
|
|
}
|
|
|
|
|
2013-09-09 07:28:50 +04:00
|
|
|
/* readonly attribute int32_t lineNumber; */
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMETHODIMP StackFrame::GetLineNumber(int32_t* aLineNumber)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
2014-07-04 09:25:16 +04:00
|
|
|
return GetLineno(aLineNumber);
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 22:28:31 +04:00
|
|
|
// virtual
|
|
|
|
nsresult
|
|
|
|
JSStackFrame::GetColNo(int32_t* aColNo)
|
|
|
|
{
|
|
|
|
// We can get called after unlink; in that case we can't do much
|
|
|
|
// about producing a useful value.
|
|
|
|
if (!mColNoInitialized && mStack) {
|
|
|
|
ThreadsafeAutoJSContext cx;
|
|
|
|
JS::Rooted<JSObject*> stack(cx, mStack);
|
|
|
|
JS::ExposeObjectToActiveJS(mStack);
|
|
|
|
JSAutoCompartment ac(cx, stack);
|
|
|
|
JS::Rooted<JS::Value> colVal(cx);
|
|
|
|
if (!JS_GetProperty(cx, stack, "column", &colVal) ||
|
|
|
|
!colVal.isNumber()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
mColNo = colVal.toNumber();
|
|
|
|
mColNoInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return StackFrame::GetColNo(aColNo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute int32_t columnNumber; */
|
|
|
|
NS_IMETHODIMP StackFrame::GetColumnNumber(int32_t* aColumnNumber)
|
|
|
|
{
|
|
|
|
return GetColNo(aColumnNumber);
|
|
|
|
}
|
|
|
|
|
2014-01-20 20:51:41 +04:00
|
|
|
/* readonly attribute AUTF8String sourceLine; */
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMETHODIMP StackFrame::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute nsIStackFrame caller; */
|
|
|
|
NS_IMETHODIMP JSStackFrame::GetCaller(nsIStackFrame** aCaller)
|
|
|
|
{
|
2014-07-26 09:41:18 +04:00
|
|
|
// We can get called after unlink; in that case we can't do much
|
|
|
|
// about producing a useful value.
|
|
|
|
if (!mCallerInitialized && mStack) {
|
2014-07-04 09:25:16 +04:00
|
|
|
ThreadsafeAutoJSContext cx;
|
|
|
|
JS::Rooted<JSObject*> stack(cx, mStack);
|
|
|
|
JS::ExposeObjectToActiveJS(mStack);
|
|
|
|
JSAutoCompartment ac(cx, stack);
|
|
|
|
JS::Rooted<JS::Value> callerVal(cx);
|
|
|
|
if (!JS_GetProperty(cx, stack, "parent", &callerVal) ||
|
|
|
|
!callerVal.isObjectOrNull()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callerVal.isObject()) {
|
|
|
|
JS::Rooted<JSObject*> caller(cx, &callerVal.toObject());
|
|
|
|
mCaller = new JSStackFrame(caller);
|
2014-07-04 09:25:16 +04:00
|
|
|
} else {
|
|
|
|
// Do we really need this dummy frame? If so, we should document why... I
|
|
|
|
// guess for symmetry with the "nothing on the stack" case, which returns
|
|
|
|
// a single dummy frame?
|
|
|
|
mCaller = new StackFrame();
|
|
|
|
}
|
2014-01-08 04:53:18 +04:00
|
|
|
mCallerInitialized = true;
|
|
|
|
}
|
2014-07-04 09:25:16 +04:00
|
|
|
return StackFrame::GetCaller(aCaller);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP StackFrame::GetCaller(nsIStackFrame** aCaller)
|
|
|
|
{
|
2013-09-09 07:28:50 +04:00
|
|
|
NS_IF_ADDREF(*aCaller = mCaller);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetFormattedStack(nsAString& aStack)
|
|
|
|
{
|
2014-07-26 09:41:18 +04:00
|
|
|
// We can get called after unlink; in that case we can't do much
|
|
|
|
// about producing a useful value.
|
|
|
|
if (!mFormattedStackInitialized && mStack) {
|
2014-07-04 09:25:16 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-20 20:51:41 +04:00
|
|
|
/* AUTF8String toString (); */
|
2014-07-04 09:25:16 +04:00
|
|
|
NS_IMETHODIMP StackFrame::ToString(nsACString& _retval)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
2014-01-20 20:51:41 +04:00
|
|
|
_retval.Truncate();
|
|
|
|
|
2013-09-09 07:28:50 +04:00
|
|
|
const char* frametype = IsJSFrame() ? "JS" : "native";
|
2014-01-20 20:51:41 +04:00
|
|
|
|
2014-04-12 06:20:40 +04:00
|
|
|
nsString filename;
|
2014-01-20 20:51:41 +04:00
|
|
|
nsresult rv = GetFilename(filename);
|
|
|
|
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;
|
2014-01-20 20:51:41 +04:00
|
|
|
rv = GetName(funname);
|
|
|
|
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
|
|
|
|
|
|
|
int32_t lineno;
|
|
|
|
rv = GetLineno(&lineno);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-09-09 07:28:50 +04:00
|
|
|
static const char format[] = "%s frame :: %s :: %s :: line %d";
|
2014-04-12 06:20:40 +04:00
|
|
|
_retval.AppendPrintf(format, frametype,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<nsIStackFrame>
|
2014-01-26 16:35:17 +04:00
|
|
|
JSStackFrame::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
|
|
|
nsCOMPtr<nsIStackFrame> first;
|
2014-07-04 09:25:16 +04:00
|
|
|
if (!stack) {
|
2014-07-04 09:25:16 +04:00
|
|
|
first = new StackFrame();
|
|
|
|
} else {
|
2014-07-04 09:25:16 +04:00
|
|
|
first = new JSStackFrame(stack);
|
2014-07-04 09:25:16 +04:00
|
|
|
}
|
2013-09-09 07:28:50 +04:00
|
|
|
return first.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<nsIStackFrame>
|
2014-07-04 09:25:16 +04:00
|
|
|
StackFrame::CreateStackFrameLocation(uint32_t aLanguage,
|
|
|
|
const char* aFilename,
|
|
|
|
const char* aFunctionName,
|
|
|
|
int32_t aLineNumber,
|
|
|
|
nsIStackFrame* aCaller)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
2014-07-04 09:25:16 +04:00
|
|
|
nsRefPtr<StackFrame> self =
|
|
|
|
new StackFrame(aLanguage, aFilename, aFunctionName, aLineNumber, aCaller);
|
2013-09-09 07:28:50 +04:00
|
|
|
return self.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIStackFrame>
|
2014-01-26 16:35:17 +04:00
|
|
|
CreateStack(JSContext* aCx, int32_t aMaxDepth)
|
2013-09-09 07:28:50 +04:00
|
|
|
{
|
2014-01-26 16:35:17 +04:00
|
|
|
return JSStackFrame::CreateStack(aCx, aMaxDepth);
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIStackFrame>
|
|
|
|
CreateStackFrameLocation(uint32_t aLanguage,
|
|
|
|
const char* aFilename,
|
|
|
|
const char* aFunctionName,
|
|
|
|
int32_t aLineNumber,
|
|
|
|
nsIStackFrame* aCaller)
|
|
|
|
{
|
2014-07-04 09:25:16 +04:00
|
|
|
return StackFrame::CreateStackFrameLocation(aLanguage, aFilename,
|
|
|
|
aFunctionName, aLineNumber,
|
|
|
|
aCaller);
|
2013-09-09 07:28:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace exceptions
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|