2013-09-08 20:28:50 -07: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 "js/OldDebugAPI.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsprf.h"
|
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
|
|
|
#include "mozilla/dom/DOMException.h"
|
2013-09-08 20:29:21 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-09-08 20:28:50 -07:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "XPCWrapper.h"
|
2013-09-08 20:29:21 -07:00
|
|
|
#include "WorkerPrivate.h"
|
2013-11-01 05:55:35 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-09-08 20:28:50 -07: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-08 20:29:21 -07:00
|
|
|
JS::Rooted<JSObject*> glob(aCx, JS::CurrentGlobalOrNull(aCx));
|
2013-09-08 20:28:50 -07:00
|
|
|
if (!glob) {
|
2014-04-09 21:58:43 -07:00
|
|
|
// XXXbz Can this really be null here?
|
2013-09-08 20:28:50 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-22 20:35:09 -07:00
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
2014-04-09 21:58:43 -07:00
|
|
|
if (!WrapObject(aCx, aException, &NS_GET_IID(nsIException), &val)) {
|
2013-09-08 20:28:50 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-22 20:35:09 -07:00
|
|
|
JS_SetPendingException(aCx, val);
|
2013-09-08 20:28:50 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ThrowExceptionObject(JSContext* aCx, Exception* aException)
|
|
|
|
{
|
2013-09-08 20:29:21 -07:00
|
|
|
JS::Rooted<JS::Value> thrown(aCx);
|
2013-09-08 20:28:50 -07: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-06 15:43:03 -07:00
|
|
|
if (NS_IsMainThread() && !nsContentUtils::IsCallerChrome() &&
|
2013-09-08 20:28:50 -07:00
|
|
|
aException->StealJSVal(thrown.address())) {
|
2013-10-26 09:19:05 -07:00
|
|
|
if (!JS_WrapValue(aCx, &thrown)) {
|
2013-09-08 20:28:50 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
JS_SetPendingException(aCx, thrown);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-08 20:29:21 -07:00
|
|
|
JS::Rooted<JSObject*> glob(aCx, JS::CurrentGlobalOrNull(aCx));
|
2013-09-08 20:28:50 -07:00
|
|
|
if (!glob) {
|
2014-04-08 15:27:19 -07:00
|
|
|
// XXXbz Can this actually be null here?
|
2013-09-08 20:28:50 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-08 15:27:19 -07:00
|
|
|
if (!WrapNewBindingObject(aCx, aException, &thrown)) {
|
2013-09-08 20:28:50 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-08 20:29:21 -07:00
|
|
|
JS_SetPendingException(aCx, thrown);
|
2013-09-08 20:28:50 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-08 20:29:21 -07:00
|
|
|
bool
|
|
|
|
Throw(JSContext* aCx, nsresult aRv, const char* aMessage)
|
2013-09-08 20:28:50 -07:00
|
|
|
{
|
2013-09-08 20:29:21 -07:00
|
|
|
if (JS_IsExceptionPending(aCx)) {
|
2013-09-08 20:28:50 -07:00
|
|
|
// Don't clobber the existing exception.
|
2013-09-08 20:29:21 -07:00
|
|
|
return false;
|
2013-09-08 20:28:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CycleCollectedJSRuntime* runtime = CycleCollectedJSRuntime::Get();
|
|
|
|
nsCOMPtr<nsIException> existingException = runtime->GetPendingException();
|
|
|
|
if (existingException) {
|
|
|
|
nsresult nr;
|
|
|
|
if (NS_SUCCEEDED(existingException->GetResult(&nr)) &&
|
2013-09-08 20:29:21 -07:00
|
|
|
aRv == nr) {
|
2013-10-09 14:43:15 -07: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-08 20:29:21 -07:00
|
|
|
return false;
|
2013-09-08 20:28:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 12:13:41 -07:00
|
|
|
nsRefPtr<Exception> finalException = CreateException(aCx, aRv, aMessage);
|
2013-09-08 20:28:50 -07:00
|
|
|
|
2014-04-16 12:13:41 -07: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Exception>
|
|
|
|
CreateException(JSContext* aCx, nsresult aRv, const char* aMessage)
|
|
|
|
{
|
2013-09-08 20:28:50 -07:00
|
|
|
// Do we use DOM exceptions for this error code?
|
2013-09-08 20:29:21 -07:00
|
|
|
switch (NS_ERROR_GET_MODULE(aRv)) {
|
2013-09-08 20:28:50 -07: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-04-16 12:13:41 -07:00
|
|
|
return DOMException::Create(aRv);
|
2014-04-16 16:31:41 -07:00
|
|
|
default:
|
2014-04-16 12:13:41 -07:00
|
|
|
break;
|
2013-09-08 20:28:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If not, use the default.
|
2014-04-16 12:13:41 -07: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-08 20:29:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIStackFrame>
|
|
|
|
GetCurrentJSStack()
|
|
|
|
{
|
|
|
|
// is there a current context available?
|
|
|
|
JSContext* cx = nullptr;
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
2014-05-06 15:43:03 -07:00
|
|
|
MOZ_ASSERT(nsContentUtils::XPConnect());
|
|
|
|
cx = nsContentUtils::GetCurrentJSContext();
|
2013-09-08 20:29:21 -07:00
|
|
|
} else {
|
|
|
|
cx = workers::GetCurrentThreadJSContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cx) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStackFrame> stack = exceptions::CreateStack(cx);
|
|
|
|
if (!stack) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// peel off native frames...
|
|
|
|
uint32_t language;
|
|
|
|
nsCOMPtr<nsIStackFrame> caller;
|
|
|
|
while (stack &&
|
|
|
|
NS_SUCCEEDED(stack->GetLanguage(&language)) &&
|
|
|
|
language != nsIProgrammingLanguage::JAVASCRIPT &&
|
|
|
|
NS_SUCCEEDED(stack->GetCaller(getter_AddRefs(caller))) &&
|
|
|
|
caller) {
|
|
|
|
stack = caller;
|
2013-09-08 20:28:50 -07:00
|
|
|
}
|
2013-09-08 20:29:21 -07:00
|
|
|
return stack.forget();
|
2013-09-08 20:28:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace exceptions {
|
|
|
|
|
2014-01-07 16:53:17 -08:00
|
|
|
class StackDescriptionOwner {
|
|
|
|
public:
|
|
|
|
StackDescriptionOwner(JS::StackDescription* aDescription)
|
|
|
|
: mDescription(aDescription)
|
|
|
|
{
|
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~StackDescriptionOwner()
|
|
|
|
{
|
|
|
|
// Make sure to set mDescription to null before calling DropJSObjects, since
|
|
|
|
// in debug builds DropJSObjects try to trace us and we don't want to trace
|
|
|
|
// a dead StackDescription.
|
|
|
|
if (mDescription) {
|
|
|
|
JS::FreeStackDescription(nullptr, mDescription);
|
|
|
|
mDescription = nullptr;
|
|
|
|
}
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(StackDescriptionOwner)
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(StackDescriptionOwner)
|
|
|
|
|
|
|
|
JS::FrameDescription& FrameAt(size_t aIndex)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aIndex < mDescription->nframes);
|
|
|
|
return mDescription->frames[aIndex];
|
|
|
|
}
|
|
|
|
|
2014-01-07 16:53:18 -08:00
|
|
|
unsigned NumFrames()
|
|
|
|
{
|
|
|
|
return mDescription->nframes;
|
|
|
|
}
|
|
|
|
|
2014-01-07 16:53:17 -08:00
|
|
|
private:
|
|
|
|
JS::StackDescription* mDescription;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(StackDescriptionOwner, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(StackDescriptionOwner, Release)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(StackDescriptionOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(StackDescriptionOwner)
|
|
|
|
if (tmp->mDescription) {
|
|
|
|
JS::FreeStackDescription(nullptr, tmp->mDescription);
|
|
|
|
tmp->mDescription = nullptr;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(StackDescriptionOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(StackDescriptionOwner)
|
|
|
|
JS::StackDescription* desc = tmp->mDescription;
|
|
|
|
if (tmp->mDescription) {
|
|
|
|
for (size_t i = 0; i < desc->nframes; ++i) {
|
2014-02-25 07:43:14 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mDescription->frames[i].markedLocation1());
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mDescription->frames[i].markedLocation2());
|
2014-01-07 16:53:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2013-09-08 20:28:50 -07:00
|
|
|
class JSStackFrame : public nsIStackFrame
|
|
|
|
{
|
|
|
|
public:
|
2014-01-07 16:53:18 -08:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(JSStackFrame)
|
2013-09-08 20:28:50 -07:00
|
|
|
NS_DECL_NSISTACKFRAME
|
|
|
|
|
2014-01-07 16:53:18 -08:00
|
|
|
// A null aStackDescription or an aIndex that's out of range for the
|
|
|
|
// number of frames aStackDescription has will mean that the
|
|
|
|
// JSStackFrame will never look at the stack description. Instead,
|
|
|
|
// it is expected to be initialized by the caller as needed.
|
|
|
|
JSStackFrame(StackDescriptionOwner* aStackDescription, size_t aIndex);
|
2013-09-08 20:28:50 -07:00
|
|
|
virtual ~JSStackFrame();
|
|
|
|
|
|
|
|
static already_AddRefed<nsIStackFrame>
|
2014-01-26 04:35:17 -08:00
|
|
|
CreateStack(JSContext* aCx, int32_t aMaxDepth = -1);
|
2013-09-08 20:28:50 -07:00
|
|
|
static already_AddRefed<nsIStackFrame>
|
|
|
|
CreateStackFrameLocation(uint32_t aLanguage,
|
|
|
|
const char* aFilename,
|
|
|
|
const char* aFunctionName,
|
|
|
|
int32_t aLineNumber,
|
|
|
|
nsIStackFrame* aCaller);
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool IsJSFrame() const {
|
|
|
|
return mLanguage == nsIProgrammingLanguage::JAVASCRIPT;
|
|
|
|
}
|
|
|
|
|
2014-01-07 16:53:18 -08:00
|
|
|
int32_t GetLineno();
|
|
|
|
|
|
|
|
nsRefPtr<StackDescriptionOwner> mStackDescription;
|
2013-09-08 20:28:50 -07:00
|
|
|
nsCOMPtr<nsIStackFrame> mCaller;
|
|
|
|
|
2014-01-07 16:53:18 -08:00
|
|
|
// Cached values
|
2014-04-11 19:20:40 -07:00
|
|
|
nsString mFilename;
|
|
|
|
nsString mFunname;
|
2013-09-08 20:28:50 -07:00
|
|
|
int32_t mLineno;
|
|
|
|
uint32_t mLanguage;
|
2014-01-07 16:53:18 -08:00
|
|
|
|
|
|
|
size_t mIndex;
|
|
|
|
|
|
|
|
bool mFilenameInitialized;
|
|
|
|
bool mFunnameInitialized;
|
|
|
|
bool mLinenoInitialized;
|
2014-01-07 16:53:18 -08:00
|
|
|
bool mCallerInitialized;
|
2013-09-08 20:28:50 -07:00
|
|
|
};
|
|
|
|
|
2014-01-07 16:53:18 -08:00
|
|
|
JSStackFrame::JSStackFrame(StackDescriptionOwner* aStackDescription,
|
|
|
|
size_t aIndex)
|
2014-01-20 08:51:41 -08:00
|
|
|
: mLineno(0)
|
2014-01-07 16:53:18 -08:00
|
|
|
{
|
|
|
|
if (aStackDescription && aIndex < aStackDescription->NumFrames()) {
|
|
|
|
mStackDescription = aStackDescription;
|
|
|
|
mIndex = aIndex;
|
|
|
|
mFilenameInitialized = false;
|
|
|
|
mFunnameInitialized = false;
|
|
|
|
mLinenoInitialized = false;
|
2014-01-07 16:53:18 -08:00
|
|
|
mCallerInitialized = false;
|
2014-01-07 16:53:18 -08:00
|
|
|
mLanguage = nsIProgrammingLanguage::JAVASCRIPT;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(!mStackDescription);
|
|
|
|
mIndex = 0;
|
|
|
|
mFilenameInitialized = true;
|
|
|
|
mFunnameInitialized = true;
|
|
|
|
mLinenoInitialized = true;
|
2014-01-07 16:53:18 -08:00
|
|
|
mCallerInitialized = true;
|
2014-01-07 16:53:18 -08:00
|
|
|
mLanguage = nsIProgrammingLanguage::UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
2013-09-08 20:28:50 -07:00
|
|
|
|
|
|
|
JSStackFrame::~JSStackFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(JSStackFrame, mStackDescription, mCaller)
|
2014-01-07 16:53:18 -08:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(JSStackFrame)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(JSStackFrame)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSStackFrame)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStackFrame)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
2013-09-08 20:28:50 -07:00
|
|
|
|
|
|
|
/* readonly attribute uint32_t language; */
|
|
|
|
NS_IMETHODIMP JSStackFrame::GetLanguage(uint32_t* aLanguage)
|
|
|
|
{
|
|
|
|
*aLanguage = mLanguage;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute string languageName; */
|
2014-01-20 08:51:41 -08:00
|
|
|
NS_IMETHODIMP JSStackFrame::GetLanguageName(nsACString& aLanguageName)
|
2013-09-08 20:28:50 -07:00
|
|
|
{
|
|
|
|
static const char js[] = "JavaScript";
|
|
|
|
static const char cpp[] = "C++";
|
|
|
|
|
|
|
|
if (IsJSFrame()) {
|
2014-01-20 08:51:41 -08:00
|
|
|
aLanguageName.AssignASCII(js);
|
2013-09-08 20:28:50 -07:00
|
|
|
} else {
|
2014-01-20 08:51:41 -08:00
|
|
|
aLanguageName.AssignASCII(cpp);
|
2013-09-08 20:28:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-11 19:20:40 -07:00
|
|
|
/* readonly attribute AString filename; */
|
|
|
|
NS_IMETHODIMP JSStackFrame::GetFilename(nsAString& aFilename)
|
2014-01-07 16:53:18 -08:00
|
|
|
{
|
|
|
|
if (!mFilenameInitialized) {
|
|
|
|
JS::FrameDescription& desc = mStackDescription->FrameAt(mIndex);
|
2014-02-25 07:43:14 -08:00
|
|
|
if (const char *filename = desc.filename()) {
|
2014-04-11 19:20:40 -07:00
|
|
|
CopyUTF8toUTF16(filename, mFilename);
|
2014-01-07 16:53:18 -08:00
|
|
|
}
|
|
|
|
mFilenameInitialized = true;
|
|
|
|
}
|
|
|
|
|
2014-01-20 08:51:41 -08:00
|
|
|
// The filename must be set to null if empty.
|
|
|
|
if (mFilename.IsEmpty()) {
|
|
|
|
aFilename.SetIsVoid(true);
|
2013-09-08 20:28:50 -07:00
|
|
|
} else {
|
2014-01-20 08:51:41 -08:00
|
|
|
aFilename.Assign(mFilename);
|
2013-09-08 20:28:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-11 19:20:40 -07:00
|
|
|
/* readonly attribute AString name; */
|
|
|
|
NS_IMETHODIMP JSStackFrame::GetName(nsAString& aFunction)
|
2014-01-07 16:53:18 -08:00
|
|
|
{
|
|
|
|
if (!mFunnameInitialized) {
|
|
|
|
JS::FrameDescription& desc = mStackDescription->FrameAt(mIndex);
|
2014-02-25 07:43:14 -08:00
|
|
|
if (JSFlatString *name = desc.funDisplayName()) {
|
2014-04-11 19:20:40 -07:00
|
|
|
mFunname.Assign(JS_GetFlatStringChars(name),
|
|
|
|
// XXXbz Can't JS_GetStringLength on JSFlatString!
|
|
|
|
JS_GetStringLength(JS_FORGET_STRING_FLATNESS(name)));
|
2014-01-07 16:53:18 -08:00
|
|
|
}
|
|
|
|
mFunnameInitialized = true;
|
|
|
|
}
|
|
|
|
|
2014-01-20 08:51:41 -08:00
|
|
|
// The function name must be set to null if empty.
|
|
|
|
if (mFunname.IsEmpty()) {
|
|
|
|
aFunction.SetIsVoid(true);
|
2013-09-08 20:28:50 -07:00
|
|
|
} else {
|
2014-01-20 08:51:41 -08:00
|
|
|
aFunction.Assign(mFunname);
|
2013-09-08 20:28:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-07 16:53:18 -08:00
|
|
|
int32_t
|
|
|
|
JSStackFrame::GetLineno()
|
|
|
|
{
|
|
|
|
if (!mLinenoInitialized) {
|
|
|
|
JS::FrameDescription& desc = mStackDescription->FrameAt(mIndex);
|
|
|
|
mLineno = desc.lineno();
|
|
|
|
mLinenoInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mLineno;
|
|
|
|
}
|
|
|
|
|
2013-09-08 20:28:50 -07:00
|
|
|
/* readonly attribute int32_t lineNumber; */
|
|
|
|
NS_IMETHODIMP JSStackFrame::GetLineNumber(int32_t* aLineNumber)
|
|
|
|
{
|
2014-01-07 16:53:18 -08:00
|
|
|
*aLineNumber = GetLineno();
|
2013-09-08 20:28:50 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-20 08:51:41 -08:00
|
|
|
/* readonly attribute AUTF8String sourceLine; */
|
|
|
|
NS_IMETHODIMP JSStackFrame::GetSourceLine(nsACString& aSourceLine)
|
2013-09-08 20:28:50 -07:00
|
|
|
{
|
2014-01-20 08:51:41 -08:00
|
|
|
aSourceLine.Truncate();
|
2013-09-08 20:28:50 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute nsIStackFrame caller; */
|
|
|
|
NS_IMETHODIMP JSStackFrame::GetCaller(nsIStackFrame** aCaller)
|
|
|
|
{
|
2014-01-07 16:53:18 -08:00
|
|
|
if (!mCallerInitialized) {
|
|
|
|
mCaller = new JSStackFrame(mStackDescription, mIndex+1);
|
|
|
|
mCallerInitialized = true;
|
|
|
|
}
|
2013-09-08 20:28:50 -07:00
|
|
|
NS_IF_ADDREF(*aCaller = mCaller);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-20 08:51:41 -08:00
|
|
|
/* AUTF8String toString (); */
|
|
|
|
NS_IMETHODIMP JSStackFrame::ToString(nsACString& _retval)
|
2013-09-08 20:28:50 -07:00
|
|
|
{
|
2014-01-20 08:51:41 -08:00
|
|
|
_retval.Truncate();
|
|
|
|
|
2013-09-08 20:28:50 -07:00
|
|
|
const char* frametype = IsJSFrame() ? "JS" : "native";
|
2014-01-20 08:51:41 -08:00
|
|
|
|
2014-04-11 19:20:40 -07:00
|
|
|
nsString filename;
|
2014-01-20 08:51:41 -08:00
|
|
|
nsresult rv = GetFilename(filename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (filename.IsEmpty()) {
|
2014-04-11 19:20:40 -07:00
|
|
|
filename.AssignLiteral("<unknown filename>");
|
2014-01-07 16:53:18 -08:00
|
|
|
}
|
2014-01-20 08:51:41 -08:00
|
|
|
|
2014-04-11 19:20:40 -07:00
|
|
|
nsString funname;
|
2014-01-20 08:51:41 -08:00
|
|
|
rv = GetName(funname);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (funname.IsEmpty()) {
|
2014-04-11 19:20:40 -07:00
|
|
|
funname.AssignLiteral("<TOP_LEVEL>");
|
2014-01-07 16:53:18 -08:00
|
|
|
}
|
2013-09-08 20:28:50 -07:00
|
|
|
static const char format[] = "%s frame :: %s :: %s :: line %d";
|
2014-04-11 19:20:40 -07:00
|
|
|
_retval.AppendPrintf(format, frametype,
|
|
|
|
NS_ConvertUTF16toUTF8(filename).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(funname).get(),
|
|
|
|
GetLineno());
|
2013-09-08 20:28:50 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<nsIStackFrame>
|
2014-01-26 04:35:17 -08:00
|
|
|
JSStackFrame::CreateStack(JSContext* aCx, int32_t aMaxDepth)
|
2013-09-08 20:28:50 -07:00
|
|
|
{
|
|
|
|
static const unsigned MAX_FRAMES = 100;
|
2014-01-26 04:35:17 -08:00
|
|
|
if (aMaxDepth < 0) {
|
|
|
|
aMaxDepth = MAX_FRAMES;
|
|
|
|
}
|
2013-09-08 20:28:50 -07:00
|
|
|
|
2014-01-26 04:35:17 -08:00
|
|
|
JS::StackDescription* desc = JS::DescribeStack(aCx, aMaxDepth);
|
2013-09-08 20:28:50 -07:00
|
|
|
if (!desc) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-01-07 16:53:17 -08:00
|
|
|
nsRefPtr<StackDescriptionOwner> descOwner = new StackDescriptionOwner(desc);
|
|
|
|
|
2014-01-07 16:53:18 -08:00
|
|
|
nsRefPtr<JSStackFrame> first = new JSStackFrame(descOwner, 0);
|
2013-09-08 20:28:50 -07:00
|
|
|
return first.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<nsIStackFrame>
|
|
|
|
JSStackFrame::CreateStackFrameLocation(uint32_t aLanguage,
|
|
|
|
const char* aFilename,
|
|
|
|
const char* aFunctionName,
|
|
|
|
int32_t aLineNumber,
|
|
|
|
nsIStackFrame* aCaller)
|
|
|
|
{
|
2014-01-07 16:53:18 -08:00
|
|
|
nsRefPtr<JSStackFrame> self = new JSStackFrame(nullptr, 0);
|
2013-09-08 20:28:50 -07:00
|
|
|
|
|
|
|
self->mLanguage = aLanguage;
|
|
|
|
self->mLineno = aLineNumber;
|
2014-04-11 19:20:40 -07:00
|
|
|
CopyUTF8toUTF16(aFilename, self->mFilename);
|
|
|
|
CopyUTF8toUTF16(aFunctionName, self->mFunname);
|
2013-09-08 20:28:50 -07:00
|
|
|
|
|
|
|
self->mCaller = aCaller;
|
|
|
|
|
|
|
|
return self.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIStackFrame>
|
2014-01-26 04:35:17 -08:00
|
|
|
CreateStack(JSContext* aCx, int32_t aMaxDepth)
|
2013-09-08 20:28:50 -07:00
|
|
|
{
|
2014-01-26 04:35:17 -08:00
|
|
|
return JSStackFrame::CreateStack(aCx, aMaxDepth);
|
2013-09-08 20:28:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIStackFrame>
|
|
|
|
CreateStackFrameLocation(uint32_t aLanguage,
|
|
|
|
const char* aFilename,
|
|
|
|
const char* aFunctionName,
|
|
|
|
int32_t aLineNumber,
|
|
|
|
nsIStackFrame* aCaller)
|
|
|
|
{
|
|
|
|
return JSStackFrame::CreateStackFrameLocation(aLanguage, aFilename,
|
|
|
|
aFunctionName, aLineNumber,
|
|
|
|
aCaller);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace exceptions
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|