Bug 911258: Part 4 - Refactor exception implementations. r=bz
authorKyle Huey <khuey@kylehuey.com>
Sun, 08 Sep 2013 20:28:50 -0700
changeset 146112 3f4223fab461f9a4e5b4fe646db5a0625cedd31e
parent 146111 16cf9da0d24282153c9ea53e49439dcb3aac194f
child 146113 4127ebc547fdf569b326ab0fdb0e7fca94086fc2
push id25240
push useremorley@mozilla.com
push dateMon, 09 Sep 2013 12:09:06 +0000
treeherdermozilla-central@9846f07d5867 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs911258
milestone26.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 911258: Part 4 - Refactor exception implementations. r=bz
dom/base/DOMError.cpp
dom/base/DOMException.cpp
dom/base/DOMException.h
dom/base/moz.build
dom/base/nsDOMClassInfo.cpp
dom/base/nsDOMClassInfoClasses.h
dom/base/nsDOMException.cpp
dom/base/nsDOMException.h
dom/base/nsDOMScriptObjectFactory.cpp
dom/bindings/Bindings.conf
dom/bindings/Exceptions.cpp
dom/bindings/Exceptions.h
dom/bindings/Makefile.in
dom/bindings/moz.build
dom/workers/RuntimeService.cpp
js/xpconnect/idl/xpcexception.idl
js/xpconnect/src/XPCComponents.cpp
js/xpconnect/src/XPCConvert.cpp
js/xpconnect/src/XPCException.cpp
js/xpconnect/src/XPCForwards.h
js/xpconnect/src/XPCJSRuntime.cpp
js/xpconnect/src/XPCModule.h
js/xpconnect/src/XPCQuickStubs.cpp
js/xpconnect/src/XPCStack.cpp
js/xpconnect/src/XPCThrower.cpp
js/xpconnect/src/XPCWrappedJSClass.cpp
js/xpconnect/src/moz.build
js/xpconnect/src/nsXPConnect.cpp
js/xpconnect/src/xpcprivate.h
layout/build/nsLayoutModule.cpp
xpcom/base/CycleCollectedJSRuntime.cpp
xpcom/base/CycleCollectedJSRuntime.h
xpcom/base/nsCycleCollector.cpp
xpcom/base/nsCycleCollector.h
--- a/dom/base/DOMError.cpp
+++ b/dom/base/DOMError.cpp
@@ -1,19 +1,24 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=2 et sw=2 tw=80: */
 /* 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/DOMError.h"
 #include "mozilla/dom/DOMErrorBinding.h"
-#include "nsDOMException.h"
 #include "nsPIDOMWindow.h"
 
+// Implemented in DOMException.cpp
+nsresult
+NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, const char** aName,
+                                   const char** aMessage,
+                                   uint16_t* aCode = nullptr);
+
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(DOMError, mWindow)
 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMError)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMError)
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMError)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
new file mode 100644
--- /dev/null
+++ b/dom/base/DOMException.cpp
@@ -0,0 +1,708 @@
+/* -*- 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/DOMException.h"
+
+#include "jsapi.h"
+#include "jsprf.h"
+#include "js/OldDebugAPI.h"
+#include "mozilla/Util.h"
+#include "nsContentUtils.h"
+#include "nsCOMPtr.h"
+#include "nsCycleCollectionHoldDrop.h"
+#include "nsIClassInfoImpl.h"
+#include "nsIDocument.h"
+#include "nsIDOMDOMException.h"
+#include "nsIException.h"
+#include "nsMemory.h"
+#include "prprf.h"
+#include "xpcprivate.h"
+
+#include "mozilla/dom/DOMExceptionBinding.h"
+
+using namespace mozilla;
+
+enum DOM4ErrorTypeCodeMap {
+  /* DOM4 errors from http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#domexception */
+  IndexSizeError             = nsIDOMDOMException::INDEX_SIZE_ERR,
+  HierarchyRequestError      = nsIDOMDOMException::HIERARCHY_REQUEST_ERR,
+  WrongDocumentError         = nsIDOMDOMException::WRONG_DOCUMENT_ERR,
+  InvalidCharacterError      = nsIDOMDOMException::INVALID_CHARACTER_ERR,
+  NoModificationAllowedError = nsIDOMDOMException::NO_MODIFICATION_ALLOWED_ERR,
+  NotFoundError              = nsIDOMDOMException::NOT_FOUND_ERR,
+  NotSupportedError          = nsIDOMDOMException::NOT_SUPPORTED_ERR,
+  // Can't remove until setNamedItem is removed
+  InUseAttributeError        = nsIDOMDOMException::INUSE_ATTRIBUTE_ERR,
+  InvalidStateError          = nsIDOMDOMException::INVALID_STATE_ERR,
+  SyntaxError                = nsIDOMDOMException::SYNTAX_ERR,
+  InvalidModificationError   = nsIDOMDOMException::INVALID_MODIFICATION_ERR,
+  NamespaceError             = nsIDOMDOMException::NAMESPACE_ERR,
+  InvalidAccessError         = nsIDOMDOMException::INVALID_ACCESS_ERR,
+  TypeMismatchError          = nsIDOMDOMException::TYPE_MISMATCH_ERR,
+  SecurityError              = nsIDOMDOMException::SECURITY_ERR,
+  NetworkError               = nsIDOMDOMException::NETWORK_ERR,
+  AbortError                 = nsIDOMDOMException::ABORT_ERR,
+  URLMismatchError           = nsIDOMDOMException::URL_MISMATCH_ERR,
+  QuotaExceededError         = nsIDOMDOMException::QUOTA_EXCEEDED_ERR,
+  TimeoutError               = nsIDOMDOMException::TIMEOUT_ERR,
+  InvalidNodeTypeError       = nsIDOMDOMException::INVALID_NODE_TYPE_ERR,
+  DataCloneError             = nsIDOMDOMException::DATA_CLONE_ERR,
+  EncodingError              = 0,
+
+  /* XXX Should be JavaScript native errors */
+  TypeError                  = 0,
+  RangeError                 = 0,
+
+  /* IndexedDB errors http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#exceptions */
+  UnknownError             = 0,
+  ConstraintError          = 0,
+  DataError                = 0,
+  TransactionInactiveError = 0,
+  ReadOnlyError            = 0,
+  VersionError             = 0,
+
+  /* File API errors http://dev.w3.org/2006/webapi/FileAPI/#ErrorAndException */
+  NotReadableError         = 0,
+
+  /* FileHandle API errors */
+  LockedFileInactiveError = 0,
+};
+
+#define DOM4_MSG_DEF(name, message, nsresult) {(nsresult), name, #name, message},
+#define DOM_MSG_DEF(val, message) {(val), NS_ERROR_GET_CODE(val), #val, message},
+
+static const struct ResultStruct
+{
+  nsresult mNSResult;
+  uint16_t mCode;
+  const char* mName;
+  const char* mMessage;
+} sDOMErrorMsgMap[] = {
+#include "domerr.msg"
+};
+
+#undef DOM4_MSG_DEF
+#undef DOM_MSG_DEF
+
+static void
+NSResultToNameAndMessage(nsresult aNSResult,
+                         const char** aName,
+                         const char** aMessage,
+                         uint16_t* aCode)
+{
+  *aName = nullptr;
+  *aMessage = nullptr;
+  *aCode = 0;
+  for (uint32_t idx = 0; idx < ArrayLength(sDOMErrorMsgMap); idx++) {
+    if (aNSResult == sDOMErrorMsgMap[idx].mNSResult) {
+      *aName = sDOMErrorMsgMap[idx].mName;
+      *aMessage = sDOMErrorMsgMap[idx].mMessage;
+      *aCode = sDOMErrorMsgMap[idx].mCode;
+      return;
+    }
+  }
+
+  NS_WARNING("Huh, someone is throwing non-DOM errors using the DOM module!");
+
+  return;
+}
+
+nsresult
+NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, const char** aName,
+                                   const char** aMessage, uint16_t* aCode)
+{
+  const char* name = nullptr;
+  const char* message = nullptr;
+  uint16_t code = 0;
+  NSResultToNameAndMessage(aNSResult, &name, &message, &code);
+
+  if (name && message) {
+    *aName = name;
+    *aMessage = message;
+    if (aCode) {
+      *aCode = code;
+    }
+    return NS_OK;
+  }
+
+  return NS_ERROR_NOT_AVAILABLE;
+}
+
+namespace mozilla {
+namespace dom {
+
+bool Exception::sEverMadeOneFromFactory = false;
+
+NS_IMPL_CLASSINFO(Exception, nullptr, nsIClassInfo::DOM_OBJECT,
+                  NS_XPCEXCEPTION_CID)
+NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Exception)
+  NS_INTERFACE_MAP_ENTRY(nsIException)
+  NS_INTERFACE_MAP_ENTRY(nsIXPCException)
+  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIException)
+  NS_IMPL_QUERY_CLASSINFO(Exception)
+NS_INTERFACE_MAP_END
+
+NS_IMPL_CYCLE_COLLECTING_ADDREF(Exception)
+NS_IMPL_CYCLE_COLLECTING_RELEASE(Exception)
+
+NS_IMPL_CYCLE_COLLECTION_CLASS(Exception)
+
+NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Exception)
+  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
+NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
+
+NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Exception)
+  NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
+  NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mThrownJSVal);
+NS_IMPL_CYCLE_COLLECTION_TRACE_END
+
+NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Exception)
+  NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
+NS_IMPL_CYCLE_COLLECTION_UNLINK_END
+
+NS_IMPL_CI_INTERFACE_GETTER1(Exception, nsIXPCException)
+
+Exception::Exception(const char *aMessage,
+                     nsresult aResult,
+                     const char *aName,
+                     nsIStackFrame *aLocation,
+                     nsISupports *aData)
+: mMessage(nullptr),
+  mResult(NS_OK),
+  mName(nullptr),
+  mLocation(nullptr),
+  mData(nullptr),
+  mFilename(nullptr),
+  mLineNumber(0),
+  mInner(nullptr),
+  mInitialized(false),
+  mHoldingJSVal(false)
+{
+  SetIsDOMBinding();
+
+  // A little hack... The nsIGenericModule nsIClassInfo scheme relies on there
+  // having been at least one instance made via the factory. Otherwise, the
+  // shared factory/classinsance object never gets created and our QI getter
+  // for our instance's pointer to our nsIClassInfo will always return null.
+  // This is bad because it means that wrapped exceptions will never have a
+  // shared prototype. So... We force one to be created via the factory
+  // *once* and then go about our business.
+  if (!sEverMadeOneFromFactory) {
+    nsCOMPtr<nsIXPCException> e =
+        do_CreateInstance(XPC_EXCEPTION_CONTRACTID);
+    sEverMadeOneFromFactory = true;
+  }
+
+  nsCOMPtr<nsIStackFrame> location;
+  if (aLocation) {
+    location = aLocation;
+  } else {
+    nsXPConnect* xpc = nsXPConnect::XPConnect();
+    xpc->GetCurrentJSStack(getter_AddRefs(location));
+    // it is legal for there to be no active JS stack, if C++ code
+    // is operating on a JS-implemented interface pointer without
+    // having been called in turn by JS.  This happens in the JS
+    // component loader, and will become more common as additional
+    // components are implemented in JS.
+  }
+  // We want to trim off any leading native 'dataless' frames
+  if (location) {
+    while (1) {
+      uint32_t language;
+      int32_t lineNumber;
+      if (NS_FAILED(location->GetLanguage(&language)) ||
+          language == nsIProgrammingLanguage::JAVASCRIPT ||
+          NS_FAILED(location->GetLineNumber(&lineNumber)) ||
+          lineNumber) {
+        break;
+      }
+      nsCOMPtr<nsIStackFrame> caller;
+      if (NS_FAILED(location->GetCaller(getter_AddRefs(caller))) || !caller) {
+        break;
+      }
+      location = caller;
+    }
+  }
+
+  Initialize(aMessage, aResult, aName, location, aData, nullptr);
+}
+
+Exception::Exception()
+  : mMessage(nullptr),
+    mResult(NS_OK),
+    mName(nullptr),
+    mFilename(nullptr),
+    mLineNumber(-1),
+    mInitialized(false),
+    mHoldingJSVal(false)
+{
+}
+
+Exception::~Exception()
+{
+  if (mMessage) {
+    nsMemory::Free(mMessage);
+    mMessage = nullptr;
+  }
+  if (mName) {
+    nsMemory::Free(mName);
+    mName = nullptr;
+  }
+  if (mFilename) {
+    nsMemory::Free(mFilename);
+    mFilename = nullptr;
+  }
+
+  if (mHoldingJSVal) {
+    MOZ_ASSERT(NS_IsMainThread());
+
+    mozilla::DropJSObjects(this);
+  }
+}
+
+bool
+Exception::StealJSVal(JS::Value* aVp)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  if (mHoldingJSVal) {
+    *aVp = mThrownJSVal;
+    mThrownJSVal = JSVAL_NULL;
+
+    mozilla::DropJSObjects(this);
+    mHoldingJSVal = false;
+    return true;
+  }
+
+  return false;
+}
+
+void
+Exception::StowJSVal(JS::Value& aVp)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  mThrownJSVal = aVp;
+  if (!mHoldingJSVal) {
+    mozilla::HoldJSObjects(this);
+    mHoldingJSVal = true;
+  }
+}
+
+/* readonly attribute string message; */
+NS_IMETHODIMP
+Exception::GetMessageMoz(char** aMessage)
+{
+  NS_ENSURE_ARG_POINTER(aMessage);
+  NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
+
+  if (mMessage) {
+    *aMessage =
+      (char*) nsMemory::Clone(mMessage, sizeof(char)*(strlen(mMessage)+1));
+  } else {
+    *aMessage = nullptr;
+  }
+
+  return NS_OK;
+}
+
+/* readonly attribute nsresult result; */
+NS_IMETHODIMP
+Exception::GetResult(nsresult* aResult)
+{
+  NS_ENSURE_ARG_POINTER(aResult);
+  NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
+
+  *aResult = mResult;
+  return NS_OK;
+}
+
+/* readonly attribute string name; */
+NS_IMETHODIMP
+Exception::GetName(char** aName)
+{
+  NS_ENSURE_ARG_POINTER(aName);
+  NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
+
+  const char* name = mName;
+  if (!name) {
+    nsXPCException::NameAndFormatForNSResult(mResult, &name, nullptr);
+  }
+
+  if (name) {
+    *aName = (char*) nsMemory::Clone(name, sizeof(char)*(strlen(name)+1));
+  } else {
+    *aName = nullptr;
+  }
+
+  return NS_OK;
+}
+
+/* readonly attribute string filename; */
+NS_IMETHODIMP
+Exception::GetFilename(char** aFilename)
+{
+  NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
+    XPC_STRING_GETTER_BODY(aFilename, mFilename);
+}
+
+/* readonly attribute uint32_t lineNumber; */
+NS_IMETHODIMP
+Exception::GetLineNumber(uint32_t *aLineNumber)
+{
+  NS_ENSURE_ARG_POINTER(aLineNumber);
+  NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
+
+  *aLineNumber = mLineNumber;
+  return NS_OK;
+}
+
+/* readonly attribute uint32_t columnNumber; */
+NS_IMETHODIMP
+Exception::GetColumnNumber(uint32_t* aColumnNumber)
+{
+  NS_ENSURE_ARG_POINTER(aColumnNumber);
+  NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
+
+  *aColumnNumber = 0;
+  return NS_OK;
+}
+
+/* readonly attribute nsIStackFrame location; */
+NS_IMETHODIMP
+Exception::GetLocation(nsIStackFrame** aLocation)
+{
+  NS_ENSURE_ARG_POINTER(aLocation);
+  NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
+
+  nsCOMPtr<nsIStackFrame> location = mLocation;
+  location.forget(aLocation);
+  return NS_OK;
+}
+
+/* readonly attribute nsISupports data; */
+NS_IMETHODIMP
+Exception::GetData(nsISupports** aData)
+{
+  NS_ENSURE_ARG_POINTER(aData);
+  NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
+
+  nsCOMPtr<nsISupports> data = mData;
+  data.forget(aData);
+  return NS_OK;
+}
+
+/* readonly attribute nsIException inner; */
+NS_IMETHODIMP
+Exception::GetInner(nsIException** aException)
+{
+  NS_ENSURE_ARG_POINTER(aException);
+  NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
+
+  nsCOMPtr<nsIException> inner = mInner;
+  inner.forget(aException);
+  return NS_OK;
+}
+
+/* string toString (); */
+NS_IMETHODIMP
+Exception::ToString(char **_retval)
+{
+  NS_ENSURE_ARG_POINTER(_retval);
+  NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
+
+  static const char defaultMsg[] = "<no message>";
+  static const char defaultLocation[] = "<unknown>";
+  static const char format[] =
+"[Exception... \"%s\"  nsresult: \"0x%x (%s)\"  location: \"%s\"  data: %s]";
+
+  char* indicatedLocation = nullptr;
+
+  if (mLocation) {
+    // we need to free this if it does not fail
+    nsresult rv = mLocation->ToString(&indicatedLocation);
+    NS_ENSURE_SUCCESS(rv, rv);
+  }
+
+  const char* msg = mMessage ? mMessage : nullptr;
+  const char* location = indicatedLocation ?
+                              indicatedLocation : defaultLocation;
+  const char* resultName = mName;
+  if (!resultName &&
+      !nsXPCException::NameAndFormatForNSResult(mResult, &resultName,
+                                                (!msg) ? &msg : nullptr)) {
+    if (!msg) {
+      msg = defaultMsg;
+    }
+    resultName = "<unknown>";
+  }
+  const char* data = mData ? "yes" : "no";
+
+  char* temp = JS_smprintf(format, msg, mResult, resultName, location, data);
+  if (indicatedLocation) {
+    nsMemory::Free(indicatedLocation);
+  }
+
+  char* final = nullptr;
+  if (temp) {
+    final = (char*) nsMemory::Clone(temp, sizeof(char)*(strlen(temp)+1));
+    JS_smprintf_free(temp);
+  }
+
+  *_retval = final;
+  return final ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
+}
+
+/* void initialize (in string aMessage, in nsresult aResult, in string aName, in nsIStackFrame aLocation, in nsISupports aData, in nsIException aInner); */
+NS_IMETHODIMP
+Exception::Initialize(const char *aMessage, nsresult aResult, const char *aName, nsIStackFrame *aLocation, nsISupports *aData, nsIException *aInner)
+{
+  NS_ENSURE_FALSE(mInitialized, NS_ERROR_ALREADY_INITIALIZED);
+
+  if (aMessage) {
+    mMessage =
+      (char*) nsMemory::Clone(aMessage, sizeof(char)*(strlen(aMessage)+1));
+  }
+
+  if (aName) {
+    mName = (char*) nsMemory::Clone(aName, sizeof(char)*(strlen(aName)+1));
+  }
+
+  mResult = aResult;
+
+  if (aLocation) {
+    mLocation = aLocation;
+    // For now, fill in our location details from our stack frame.
+    // Later we may allow other locations?
+    nsresult rc;
+    if (NS_FAILED(rc = aLocation->GetFilename(&mFilename))) {
+      return rc;
+    }
+    if (NS_FAILED(rc = aLocation->GetLineNumber(&mLineNumber))) {
+      return rc;
+    }
+  } else {
+    nsresult rv;
+    nsXPConnect* xpc = nsXPConnect::XPConnect();
+    rv = xpc->GetCurrentJSStack(getter_AddRefs(mLocation));
+    if (NS_FAILED(rv)) {
+      return rv;
+    }
+  }
+
+  mData = aData;
+  mInner = aInner;
+
+  mInitialized = true;
+  return NS_OK;
+}
+
+JSObject*
+Exception::WrapObject(JSContext* cx, JS::Handle<JSObject*> scope)
+{
+  return ExceptionBinding::Wrap(cx, scope, this);
+}
+
+void
+Exception::GetMessageMoz(nsString& retval)
+{
+  char* str = nullptr;
+#ifdef DEBUG
+  DebugOnly<nsresult> rv = 
+#endif
+  GetMessageMoz(&str);
+  MOZ_ASSERT(NS_SUCCEEDED(rv));
+  CopyUTF8toUTF16(str, retval);
+  nsMemory::Free(str);
+}
+
+uint32_t
+Exception::Result() const
+{
+  return (uint32_t)mResult;
+}
+
+void
+Exception::GetName(nsString& retval)
+{
+  char* str = nullptr;
+#ifdef DEBUG
+  DebugOnly<nsresult> rv =
+#endif
+  GetName(&str);
+  MOZ_ASSERT(NS_SUCCEEDED(rv));
+  CopyUTF8toUTF16(str, retval);
+  nsMemory::Free(str);
+}
+
+void
+Exception::GetFilename(nsString& retval)
+{
+  char* str = nullptr;
+#ifdef DEBUG
+  DebugOnly<nsresult> rv =
+#endif
+  GetFilename(&str);
+  MOZ_ASSERT(NS_SUCCEEDED(rv));
+  CopyUTF8toUTF16(str, retval);
+  nsMemory::Free(str);
+}
+
+uint32_t
+Exception::LineNumber() const
+{
+  return mLineNumber;
+}
+
+uint32_t
+Exception::ColumnNumber() const
+{
+  return 0;
+}
+
+already_AddRefed<nsIStackFrame>
+Exception::GetLocation() const
+{
+  nsCOMPtr<nsIStackFrame> location = mLocation;
+  return location.forget();
+}
+
+already_AddRefed<nsISupports>
+Exception::GetInner() const
+{
+  nsCOMPtr<nsIException> inner = mInner;
+  return inner.forget();
+}
+
+already_AddRefed<nsISupports>
+Exception::GetData() const
+{
+  nsCOMPtr<nsISupports> data = mData;
+  return data.forget();
+}
+
+void
+Exception::Stringify(nsString& retval)
+{
+  char* str = nullptr;
+#ifdef DEBUG
+  DebugOnly<nsresult> rv =
+#endif
+  ToString(&str);
+  MOZ_ASSERT(NS_SUCCEEDED(rv));
+  CopyUTF8toUTF16(str, retval);
+  nsMemory::Free(str);
+}
+
+NS_IMPL_ADDREF_INHERITED(DOMException, Exception)
+NS_IMPL_RELEASE_INHERITED(DOMException, Exception)
+NS_INTERFACE_MAP_BEGIN(DOMException)
+  NS_INTERFACE_MAP_ENTRY(nsIDOMDOMException)
+NS_INTERFACE_MAP_END_INHERITING(Exception)
+
+DOMException::DOMException(nsresult aRv, const char* aMessage,
+                           const char* aName, uint16_t aCode)
+  : Exception(nullptr, aRv, nullptr, nullptr, nullptr),
+    mName(aName),
+    mMessage(aMessage),
+    mCode(aCode)
+{
+  SetIsDOMBinding();
+}
+
+NS_IMETHODIMP
+DOMException::GetCode(uint16_t* aCode)
+{
+  NS_ENSURE_ARG_POINTER(aCode);
+  *aCode = mCode;
+
+  // Warn only when the code was changed (other than DOM Core)
+  // or the code is useless (zero)
+  if (NS_ERROR_GET_MODULE(mResult) != NS_ERROR_MODULE_DOM || !mCode) {
+    nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromCaller();
+    if (doc) {
+      doc->WarnOnceAbout(nsIDocument::eDOMExceptionCode);
+    }
+  }
+
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+DOMException::ToString(char **aReturn)
+{
+  *aReturn = nullptr;
+
+  static const char defaultMsg[] = "<no message>";
+  static const char defaultLocation[] = "<unknown>";
+  static const char defaultName[] = "<unknown>";
+  static const char format[] =
+    "[Exception... \"%s\"  code: \"%d\" nsresult: \"0x%x (%s)\"  location: \"%s\"]";
+
+  nsAutoCString location;
+
+  if (mInner) {
+    nsXPIDLCString filename;
+
+    mInner->GetFilename(getter_Copies(filename));
+
+    if (!filename.IsEmpty()) {
+      uint32_t line_nr = 0;
+
+      mInner->GetLineNumber(&line_nr);
+
+      char *temp = PR_smprintf("%s Line: %d", filename.get(), line_nr);
+      if (temp) {
+        location.Assign(temp);
+        PR_smprintf_free(temp);
+      }
+    }
+  }
+
+  if (location.IsEmpty()) {
+    location = defaultLocation;
+  }
+
+  const char* msg = mMessage ? mMessage : defaultMsg;
+  const char* resultName = mName ? mName : defaultName;
+
+  *aReturn = PR_smprintf(format, msg, mCode, mResult, resultName,
+                         location.get());
+
+  return *aReturn ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
+}
+
+void
+DOMException::GetName(nsString& retval)
+{
+  CopyUTF8toUTF16(mName, retval);
+}
+
+void
+DOMException::GetMessageMoz(nsString& retval)
+{
+  CopyUTF8toUTF16(mMessage, retval);
+}
+
+JSObject*
+DOMException::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
+{
+  return DOMExceptionBinding::Wrap(aCx, aScope, this);
+}
+
+/* static */already_AddRefed<DOMException>
+DOMException::Create(nsresult aRv)
+{
+  const char* name;
+  const char* message;
+  uint16_t code;
+  NSResultToNameAndMessage(aRv, &name, &message, &code);
+  nsRefPtr<DOMException> inst =
+    new DOMException(aRv, message, name, code);
+  return inst.forget();
+}
+
+} // namespace dom
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/base/DOMException.h
@@ -0,0 +1,161 @@
+/* -*- 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/. */
+
+#ifndef mozilla_dom_DOMException_h__
+#define mozilla_dom_DOMException_h__
+
+#ifdef __GNUC__
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Woverloaded-virtual"
+#endif
+
+#include <stdint.h>
+#include "jspubtd.h"
+#include "js/GCAPI.h"
+#include "nsCOMPtr.h"
+#include "nsCycleCollectionParticipant.h"
+#include "nsID.h"
+#include "nsIDOMDOMException.h"
+#include "nsWrapperCache.h"
+#include "xpcexception.h"
+
+class nsIStackFrame;
+class nsString;
+
+nsresult
+NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, const char** aName,
+                                   const char** aMessage,
+                                   uint16_t* aCode = nullptr);
+
+namespace mozilla {
+namespace dom {
+
+#define MOZILLA_DOM_EXCEPTION_IID \
+{ 0x55eda557, 0xeba0, 0x4fe3, \
+  { 0xae, 0x2e, 0xf3, 0x94, 0x49, 0x23, 0x62, 0xd6 } }
+
+class Exception : public nsIXPCException,
+                  public nsWrapperCache
+{
+public:
+  NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_DOM_EXCEPTION_IID)
+
+  NS_DEFINE_STATIC_CID_ACCESSOR(NS_XPCEXCEPTION_CID)
+
+  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Exception)
+  
+  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
+  NS_DECL_NSIEXCEPTION
+  NS_DECL_NSIXPCEXCEPTION
+
+  // Cruft used by XPConnect for exceptions originating in JS implemented
+  // components.
+  bool StealJSVal(JS::Value* aVp);
+  void StowJSVal(JS::Value& aVp);
+
+  // WebIDL API
+  virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> scope)
+    MOZ_OVERRIDE;
+
+  nsISupports* GetParentObject() const { return nullptr; }
+
+  void GetMessageMoz(nsString& retval);
+
+  uint32_t Result() const;
+
+  void GetName(nsString& retval);
+
+  void GetFilename(nsString& retval);
+
+  uint32_t LineNumber() const;
+
+  uint32_t ColumnNumber() const;
+
+  already_AddRefed<nsIStackFrame> GetLocation() const;
+
+  already_AddRefed<nsISupports> GetInner() const;
+
+  already_AddRefed<nsISupports> GetData() const;
+
+  void Stringify(nsString& retval);
+
+  // XPCOM factory ctor.
+  Exception();
+
+  Exception(const char *aMessage,
+            nsresult aResult,
+            const char *aName,
+            nsIStackFrame *aLocation,
+            nsISupports *aData);
+
+protected:
+  virtual ~Exception();
+
+  char*           mMessage;
+  nsresult        mResult;
+  char*           mName;
+  nsCOMPtr<nsIStackFrame> mLocation;
+  nsCOMPtr<nsISupports> mData;
+  char*           mFilename;
+  int             mLineNumber;
+  nsCOMPtr<nsIException> mInner;
+  bool            mInitialized;
+
+  bool mHoldingJSVal;
+  JS::Heap<JS::Value> mThrownJSVal;
+
+private:
+  static bool sEverMadeOneFromFactory;
+};
+
+NS_DEFINE_STATIC_IID_ACCESSOR(Exception, MOZILLA_DOM_EXCEPTION_IID)
+
+class DOMException : public Exception,
+                     public nsIDOMDOMException
+{
+public:
+  DOMException(nsresult aRv, const char* aMessage,
+               const char* aName, uint16_t aCode);
+
+  NS_DECL_ISUPPORTS_INHERITED
+  NS_DECL_NSIDOMDOMEXCEPTION
+
+  // nsIException overrides
+  NS_IMETHOD ToString(char **aReturn) MOZ_OVERRIDE;
+
+  // nsWrapperCache overrides
+  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
+    MOZ_OVERRIDE;
+
+  uint16_t Code() const {
+    return mCode;
+  }
+
+  // Intentionally shadow the nsXPCException version.
+  void GetMessageMoz(nsString& retval);
+  void GetName(nsString& retval);
+
+  static already_AddRefed<DOMException>
+  Create(nsresult aRv);
+
+protected:
+
+  virtual ~DOMException() {}
+
+  // Intentionally shadow the nsXPCException version.
+  const char* mName;
+  const char* mMessage;
+
+  uint16_t mCode;
+};
+
+} // namespace dom
+} // namespace mozilla
+
+#ifdef __GNUC__
+#pragma GCC diagnostic pop
+#endif
+
+#endif
--- a/dom/base/moz.build
+++ b/dom/base/moz.build
@@ -17,17 +17,16 @@ XPIDL_SOURCES += [
 MODULE = 'dom'
 
 EXPORTS += [
     'Crypto.h',
     'nsContentPermissionHelper.h',
     'nsDOMCID.h',
     'nsDOMClassInfoClasses.h',
     'nsDOMClassInfoID.h',
-    'nsDOMException.h',
     'nsDOMJSUtils.h',
     'nsDOMNavigationTiming.h',
     'nsDOMString.h',
     'nsFocusManager.h',
     'nsIDOMClassInfo.h',
     'nsIDOMScriptObjectFactory.h',
     'nsIGlobalObject.h',
     'nsIJSEventListener.h',
@@ -49,36 +48,37 @@ EXPORTS += [
     'nsWrapperCache.h',
     'nsWrapperCacheInlines.h',
 ]
 
 EXPORTS.mozilla.dom += [
     'BarProps.h',
     'DOMCursor.h',
     'DOMError.h',
+    'DOMException.h',
     'DOMRequest.h',
     'MessageChannel.h',
     'MessagePort.h',
     'ScreenOrientation.h',
     'StructuredCloneTags.h',
     'URL.h',
 ]
 
 CPP_SOURCES += [
     'BarProps.cpp',
     'Crypto.cpp',
     'DOMCursor.cpp',
     'DOMError.cpp',
+    'DOMException.cpp',
     'DOMRequest.cpp',
     'Navigator.cpp',
     'MessageChannel.cpp',
     'MessagePort.cpp',
     'nsContentPermissionHelper.cpp',
     'nsDOMClassInfo.cpp',
-    'nsDOMException.cpp',
     'nsDOMNavigationTiming.cpp',
     'nsDOMScriptObjectFactory.cpp',
     'nsDOMWindowList.cpp',
     'nsDOMWindowUtils.cpp',
     'nsFocusManager.cpp',
     'nsGlobalWindow.cpp',
     'nsGlobalWindowCommands.cpp',
     'nsHistory.cpp',
--- a/dom/base/nsDOMClassInfo.cpp
+++ b/dom/base/nsDOMClassInfo.cpp
@@ -72,17 +72,16 @@
 #include "nsPIDOMWindow.h"
 #include "nsIDOMJSWindow.h"
 #include "nsIDOMMediaList.h"
 #include "nsIDOMChromeWindow.h"
 #include "nsIDOMConstructor.h"
 
 // DOM core includes
 #include "nsError.h"
-#include "nsIDOMDOMException.h"
 #include "nsIDOMDOMStringList.h"
 #include "nsIDOMUserDataHandler.h"
 #include "nsIDOMLoadStatus.h"
 #include "nsIDOMXPathNamespace.h"
 #include "nsIDOMXULButtonElement.h"
 #include "nsIDOMXULCheckboxElement.h"
 #include "nsIDOMXULPopupElement.h"
 
@@ -333,20 +332,16 @@ static nsDOMClassInfoData sClassInfoData
                            DOM_BASE_SCRIPTABLE_FLAGS |
                            nsIXPCScriptable::WANT_PRECREATE |
                            nsIXPCScriptable::WANT_NEWRESOLVE |
                            nsIXPCScriptable::WANT_HASINSTANCE |
                            nsIXPCScriptable::WANT_CALL |
                            nsIXPCScriptable::WANT_CONSTRUCT |
                            nsIXPCScriptable::DONT_ENUM_QUERY_INTERFACE)
 
-  // Core classes
-  NS_DEFINE_CLASSINFO_DATA(DOMException, nsDOMGenericSH,
-                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
-
   // Misc Core related classes
 
   NS_DEFINE_CLASSINFO_DATA(DeviceAcceleration, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
   NS_DEFINE_CLASSINFO_DATA(DeviceRotationRate, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
 
   // CSS classes
@@ -1105,21 +1100,16 @@ nsDOMClassInfo::Init()
   DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(DOMPrototype, nsIDOMDOMConstructor)
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMDOMConstructor)
   DOM_CLASSINFO_MAP_END
 
   DOM_CLASSINFO_MAP_BEGIN(DOMConstructor, nsIDOMDOMConstructor)
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMDOMConstructor)
   DOM_CLASSINFO_MAP_END
 
-  DOM_CLASSINFO_MAP_BEGIN(DOMException, nsIDOMDOMException)
-    DOM_CLASSINFO_MAP_ENTRY(nsIDOMDOMException)
-    DOM_CLASSINFO_MAP_ENTRY(nsIException)
-  DOM_CLASSINFO_MAP_END
-
   DOM_CLASSINFO_MAP_BEGIN(DeviceAcceleration, nsIDOMDeviceAcceleration)
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMDeviceAcceleration)
   DOM_CLASSINFO_MAP_END
 
   DOM_CLASSINFO_MAP_BEGIN(DeviceRotationRate, nsIDOMDeviceRotationRate)
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMDeviceRotationRate)
   DOM_CLASSINFO_MAP_END
 
--- a/dom/base/nsDOMClassInfoClasses.h
+++ b/dom/base/nsDOMClassInfoClasses.h
@@ -4,19 +4,16 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 // IWYU pragma: private, include "nsDOMClassInfoID.h"
 
 DOMCI_CLASS(Window)
 DOMCI_CLASS(Location)
 DOMCI_CLASS(DOMPrototype)
 DOMCI_CLASS(DOMConstructor)
 
-// Core classes
-DOMCI_CLASS(DOMException)
-
 DOMCI_CLASS(DeviceAcceleration)
 DOMCI_CLASS(DeviceRotationRate)
 
 // CSS classes
 DOMCI_CLASS(CSSStyleRule)
 DOMCI_CLASS(CSSCharsetRule)
 DOMCI_CLASS(CSSImportRule)
 DOMCI_CLASS(CSSMediaRule)
deleted file mode 100644
--- a/dom/base/nsDOMException.cpp
+++ /dev/null
@@ -1,238 +0,0 @@
-/* -*- 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 "nsDOMException.h"
-
-#include "mozilla/Util.h"
-#include "nsCOMPtr.h"
-#include "nsCRTGlue.h"
-#include "nsContentUtils.h"
-#include "nsDOMClassInfoID.h"
-#include "nsError.h"
-#include "nsIDOMDOMException.h"
-#include "nsIDocument.h"
-#include "nsString.h"
-#include "prprf.h"
-#include "mozilla/dom/DOMExceptionBinding.h"
-
-using namespace mozilla;
-namespace DOMExceptionBinding = mozilla::dom::DOMExceptionBinding;
-
-enum DOM4ErrorTypeCodeMap {
-  /* DOM4 errors from http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#domexception */
-  IndexSizeError             = nsIDOMDOMException::INDEX_SIZE_ERR,
-  HierarchyRequestError      = nsIDOMDOMException::HIERARCHY_REQUEST_ERR,
-  WrongDocumentError         = nsIDOMDOMException::WRONG_DOCUMENT_ERR,
-  InvalidCharacterError      = nsIDOMDOMException::INVALID_CHARACTER_ERR,
-  NoModificationAllowedError = nsIDOMDOMException::NO_MODIFICATION_ALLOWED_ERR,
-  NotFoundError              = nsIDOMDOMException::NOT_FOUND_ERR,
-  NotSupportedError          = nsIDOMDOMException::NOT_SUPPORTED_ERR,
-  // Can't remove until setNamedItem is removed
-  InUseAttributeError        = nsIDOMDOMException::INUSE_ATTRIBUTE_ERR,
-  InvalidStateError          = nsIDOMDOMException::INVALID_STATE_ERR,
-  SyntaxError                = nsIDOMDOMException::SYNTAX_ERR,
-  InvalidModificationError   = nsIDOMDOMException::INVALID_MODIFICATION_ERR,
-  NamespaceError             = nsIDOMDOMException::NAMESPACE_ERR,
-  InvalidAccessError         = nsIDOMDOMException::INVALID_ACCESS_ERR,
-  TypeMismatchError          = nsIDOMDOMException::TYPE_MISMATCH_ERR,
-  SecurityError              = nsIDOMDOMException::SECURITY_ERR,
-  NetworkError               = nsIDOMDOMException::NETWORK_ERR,
-  AbortError                 = nsIDOMDOMException::ABORT_ERR,
-  URLMismatchError           = nsIDOMDOMException::URL_MISMATCH_ERR,
-  QuotaExceededError         = nsIDOMDOMException::QUOTA_EXCEEDED_ERR,
-  TimeoutError               = nsIDOMDOMException::TIMEOUT_ERR,
-  InvalidNodeTypeError       = nsIDOMDOMException::INVALID_NODE_TYPE_ERR,
-  DataCloneError             = nsIDOMDOMException::DATA_CLONE_ERR,
-  EncodingError              = 0,
-
-  /* XXX Should be JavaScript native errors */
-  TypeError                  = 0,
-  RangeError                 = 0,
-
-  /* IndexedDB errors http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#exceptions */
-  UnknownError             = 0,
-  ConstraintError          = 0,
-  DataError                = 0,
-  TransactionInactiveError = 0,
-  ReadOnlyError            = 0,
-  VersionError             = 0,
-
-  /* File API errors http://dev.w3.org/2006/webapi/FileAPI/#ErrorAndException */
-  NotReadableError         = 0,
-
-  /* FileHandle API errors */
-  LockedFileInactiveError = 0,
-};
-
-#define DOM4_MSG_DEF(name, message, nsresult) {(nsresult), name, #name, message},
-#define DOM_MSG_DEF(val, message) {(val), NS_ERROR_GET_CODE(val), #val, message},
-
-static const struct ResultStruct
-{
-  nsresult mNSResult;
-  uint16_t mCode;
-  const char* mName;
-  const char* mMessage;
-} sDOMErrorMsgMap[] = {
-#include "domerr.msg"
-};
-
-#undef DOM4_MSG_DEF
-#undef DOM_MSG_DEF
-
-static void
-NSResultToNameAndMessage(nsresult aNSResult,
-                         const char** aName,
-                         const char** aMessage,
-                         uint16_t* aCode)
-{
-  *aName = nullptr;
-  *aMessage = nullptr;
-  *aCode = 0;
-  for (uint32_t idx = 0; idx < ArrayLength(sDOMErrorMsgMap); idx++) {
-    if (aNSResult == sDOMErrorMsgMap[idx].mNSResult) {
-      *aName = sDOMErrorMsgMap[idx].mName;
-      *aMessage = sDOMErrorMsgMap[idx].mMessage;
-      *aCode = sDOMErrorMsgMap[idx].mCode;
-      return;
-    }
-  }
-
-  NS_WARNING("Huh, someone is throwing non-DOM errors using the DOM module!");
-
-  return;
-}
-
-nsresult
-NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, const char** aName,
-                                   const char** aMessage, uint16_t* aCode)
-{
-  const char* name = nullptr;
-  const char* message = nullptr;
-  uint16_t code = 0;
-  NSResultToNameAndMessage(aNSResult, &name, &message, &code);
-
-  if (name && message) {
-    *aName = name;
-    *aMessage = message;
-    if (aCode) {
-      *aCode = code;
-    }
-    return NS_OK;
-  }
-
-  return NS_ERROR_NOT_AVAILABLE;
-}
-
-DOMCI_DATA(DOMException, nsDOMException) 
-
-NS_IMPL_ADDREF_INHERITED(nsDOMException, nsXPCException)
-NS_IMPL_RELEASE_INHERITED(nsDOMException, nsXPCException)
-NS_INTERFACE_MAP_BEGIN(nsDOMException)
-  NS_INTERFACE_MAP_ENTRY(nsIDOMDOMException)
-  NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(DOMException)
-NS_INTERFACE_MAP_END_INHERITING(nsXPCException)
-
-nsDOMException::nsDOMException(nsresult aRv, const char* aMessage,
-                               const char* aName, uint16_t aCode)
-  : nsXPCException(nullptr, aRv, nullptr, nullptr, nullptr),
-    mName(aName),
-    mMessage(aMessage),
-    mCode(aCode)
-{
-  SetIsDOMBinding();
-}
-
-already_AddRefed<nsDOMException>
-NS_NewDOMException(nsresult aNSResult)
-{
-  const char* name;
-  const char* message;
-  uint16_t code;
-  NSResultToNameAndMessage(aNSResult, &name, &message, &code);
-  nsRefPtr<nsDOMException> inst =
-    new nsDOMException(aNSResult, message, name, code);
-  return inst.forget();
-}
-
-NS_IMETHODIMP
-nsDOMException::GetCode(uint16_t* aCode)
-{
-  NS_ENSURE_ARG_POINTER(aCode);
-  *aCode = mCode;
-
-  // Warn only when the code was changed (other than DOM Core)
-  // or the code is useless (zero)
-  if (NS_ERROR_GET_MODULE(mResult) != NS_ERROR_MODULE_DOM || !mCode) {
-    nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromCaller();
-    if (doc) {
-      doc->WarnOnceAbout(nsIDocument::eDOMExceptionCode);
-    }
-  }
-
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-nsDOMException::ToString(char **aReturn)
-{
-  *aReturn = nullptr;
-
-  static const char defaultMsg[] = "<no message>";
-  static const char defaultLocation[] = "<unknown>";
-  static const char defaultName[] = "<unknown>";
-  static const char format[] =
-    "[Exception... \"%s\"  code: \"%d\" nsresult: \"0x%x (%s)\"  location: \"%s\"]";
-
-  nsAutoCString location;
-
-  if (mInner) {
-    nsXPIDLCString filename;
-
-    mInner->GetFilename(getter_Copies(filename));
-
-    if (!filename.IsEmpty()) {
-      uint32_t line_nr = 0;
-
-      mInner->GetLineNumber(&line_nr);
-
-      char *temp = PR_smprintf("%s Line: %d", filename.get(), line_nr);
-      if (temp) {
-        location.Assign(temp);
-        PR_smprintf_free(temp);
-      }
-    }
-  }
-
-  if (location.IsEmpty()) {
-    location = defaultLocation;
-  }
-
-  const char* msg = mMessage ? mMessage : defaultMsg;
-  const char* resultName = mName ? mName : defaultName;
-
-  *aReturn = PR_smprintf(format, msg, mCode, mResult, resultName,
-                         location.get());
-
-  return *aReturn ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
-}
-
-void
-nsDOMException::GetName(nsString& retval)
-{
-  CopyUTF8toUTF16(mName, retval);
-}
-
-void
-nsDOMException::GetMessageMoz(nsString& retval)
-{
-  CopyUTF8toUTF16(mMessage, retval);
-}
-
-JSObject*
-nsDOMException::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
-{
-  return DOMExceptionBinding::Wrap(aCx, aScope, this);
-}
deleted file mode 100644
--- a/dom/base/nsDOMException.h
+++ /dev/null
@@ -1,67 +0,0 @@
-/* -*- Mode: IDL; 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/. */
-
-// We intentionally shadow non-virtual methods, but gcc gets confused.
-#ifdef __GNUC__
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Woverloaded-virtual"
-#endif
-
-#include "mozilla/NullPtr.h"
-#include "nsError.h"
-#include "nsIDOMDOMException.h"
-#include "nsIException.h"
-#include "nsCOMPtr.h"
-#include "xpcprivate.h"
-
-template<typename> class already_AddRefed;
-
-class nsDOMException : public nsXPCException,
-                       public nsIDOMDOMException
-{
-public:
-  nsDOMException(nsresult aRv, const char* aMessage,
-                 const char* aName, uint16_t aCode);
-
-  NS_DECL_ISUPPORTS_INHERITED
-  NS_DECL_NSIDOMDOMEXCEPTION
-
-  // nsIException overrides
-  NS_IMETHOD ToString(char **aReturn) MOZ_OVERRIDE;
-
-  // nsWrapperCache overrides
-  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
-    MOZ_OVERRIDE;
-
-  uint16_t Code() const {
-    return mCode;
-  }
-
-  // Intentionally shadow the nsXPCException version.
-  void GetMessageMoz(nsString& retval);
-  void GetName(nsString& retval);
-
-protected:
-
-  virtual ~nsDOMException() {}
-
-  // Intentionally shadow the nsXPCException version.
-  const char* mName;
-  const char* mMessage;
-
-  uint16_t mCode;
-};
-
-nsresult
-NS_GetNameAndMessageForDOMNSResult(nsresult aNSResult, const char** aName,
-                                   const char** aMessage,
-                                   uint16_t* aCode = nullptr);
-
-already_AddRefed<nsDOMException>
-NS_NewDOMException(nsresult aNSResult);
-
-#ifdef __GNUC__
-#pragma GCC diagnostic pop
-#endif
--- a/dom/base/nsDOMScriptObjectFactory.cpp
+++ b/dom/base/nsDOMScriptObjectFactory.cpp
@@ -19,17 +19,16 @@
  *                               use in OS2
  */
 
 #include "nsDOMScriptObjectFactory.h"
 #include "nsScriptNameSpaceManager.h"
 #include "nsIObserverService.h"
 #include "nsJSEnvironment.h"
 #include "nsGlobalWindow.h"
-#include "nsDOMException.h"
 #include "nsCRT.h"
 #ifdef MOZ_XUL
 #include "nsXULPrototypeCache.h"
 #endif
 #include "nsThreadUtils.h"
 
 using mozilla::dom::GetNameSpaceManager;
 
--- a/dom/bindings/Bindings.conf
+++ b/dom/bindings/Bindings.conf
@@ -315,17 +315,16 @@ DOMInterfaces = {
     'skipGen': True
 }],
 
 'DocumentFragment': {
     'resultNotAddRefed': [ 'querySelector' ]
 },
 
 'DOMException': {
-    'nativeType': 'nsDOMException',
     'binaryNames': {
         'message': 'messageMoz',
     },
 },
 
 'DOMSettableTokenList': {
     'nativeType': 'nsDOMSettableTokenList',
 },
@@ -394,18 +393,17 @@ DOMInterfaces = {
     'jsImplParent': 'nsDOMEventTargetHelper'
 },
 {
     'workers': True,
     'concrete': False
 }],
 
 'Exception': {
-    'nativeType': 'nsXPCException',
-    'headerFile': 'xpcprivate.h',
+    'headerFile': 'mozilla/dom/DOMException.h',
     'binaryNames': {
         'message': 'messageMoz',
     }
 },
 
 'FileHandle': {
     'nativeType': 'mozilla::dom::file::FileHandle'
 },
new file mode 100644
--- /dev/null
+++ b/dom/bindings/Exceptions.cpp
@@ -0,0 +1,410 @@
+/* -*- 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"
+#include "nsThreadUtils.h"
+#include "XPCWrapper.h"
+
+namespace {
+
+// We can't use nsContentUtils::IsCallerChrome because it might not exist in
+// xpcshell.
+bool
+IsCallerChrome()
+{
+  nsCOMPtr<nsIScriptSecurityManager> secMan;
+  secMan = XPCWrapper::GetSecurityManager();
+
+  if (!secMan) {
+    return false;
+  }
+
+  bool isChrome;
+  return NS_SUCCEEDED(secMan->SubjectPrincipalIsSystem(&isChrome)) && isChrome;
+}
+
+} // anonymous namespace
+
+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());
+
+  JS::RootedObject glob(aCx, JS::CurrentGlobalOrNull(aCx));
+  if (!glob) {
+    return false;
+  }
+
+  nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
+  nsresult rv = nsContentUtils::XPConnect()->WrapNative(aCx, glob, aException,
+                                                        NS_GET_IID(nsIException),
+                                                        getter_AddRefs(holder));
+  if (NS_FAILED(rv) ||! holder) {
+    return false;
+  }
+
+  JS::RootedObject obj(aCx, holder->GetJSObject());
+  if (!obj) {
+    return false;
+  }
+
+  JS_SetPendingException(aCx, OBJECT_TO_JSVAL(obj));
+
+  return true;
+}
+
+bool
+ThrowExceptionObject(JSContext* aCx, Exception* aException)
+{
+  JS::RootedValue thrown(aCx);
+
+  // 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.
+  if (NS_IsMainThread() && !IsCallerChrome() &&
+      aException->StealJSVal(thrown.address())) {
+    if (!JS_WrapValue(aCx, thrown.address())) {
+      return false;
+    }
+    JS_SetPendingException(aCx, thrown);
+    return true;
+  }
+
+  JS::RootedObject glob(aCx, JS::CurrentGlobalOrNull(aCx));
+  if (!glob) {
+    return false;
+  }
+
+  JS::RootedValue val(aCx);
+  if (!mozilla::dom::WrapNewBindingObject(aCx, glob, aException, &val)) {
+    return false;
+  }
+
+  JS_SetPendingException(aCx, val);
+  return true;
+}
+
+void
+Throw(JSContext* cx, nsresult rv, const char* sz)
+{
+  bool success = false;
+
+  if (JS_IsExceptionPending(cx)) {
+    // Don't clobber the existing exception.
+    return;
+  }
+
+  /* no need to set an expection if the security manager already has */
+  if (rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO && JS_IsExceptionPending(cx)) {
+    // This should only ever happen on the main thread.
+    MOZ_ASSERT(NS_IsMainThread());
+    return;
+  }
+
+  CycleCollectedJSRuntime* runtime = CycleCollectedJSRuntime::Get();
+  nsCOMPtr<nsIException> existingException = runtime->GetPendingException();
+  if (existingException) {
+    nsresult nr;
+    if (NS_SUCCEEDED(existingException->GetResult(&nr)) && 
+        rv == nr) {
+      // Just reuse the existing exception.
+      return;
+    }
+  }
+
+  nsRefPtr<Exception> finalException;
+
+  // Do we use DOM exceptions for this error code?
+  switch (NS_ERROR_GET_MODULE(rv)) {
+  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:
+    if (NS_IsMainThread()) {
+      finalException = DOMException::Create(rv);
+    }
+    break;
+
+  default:
+      break;
+  }
+
+  // If not, use the default.
+  if (finalException == nullptr) {
+    finalException = new Exception(sz, rv, nullptr, nullptr, nullptr);
+  }
+
+  MOZ_ASSERT(finalException);
+  success = ThrowExceptionObject(cx, finalException);
+  // If we weren't able to throw an exception we're
+  // most likely out of memory
+  if (!success) {
+    JS_ReportOutOfMemory(cx);
+  }
+}
+
+namespace exceptions {
+
+class JSStackFrame : public nsIStackFrame
+{
+public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSISTACKFRAME
+
+  JSStackFrame();
+  virtual ~JSStackFrame();
+
+  static already_AddRefed<nsIStackFrame>
+  CreateStack(JSContext* cx);
+  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;
+  }
+
+  nsCOMPtr<nsIStackFrame> mCaller;
+
+  char* mFilename;
+  char* mFunname;
+  int32_t mLineno;
+  uint32_t mLanguage;
+};
+
+JSStackFrame::JSStackFrame()
+  : mFilename(nullptr),
+    mFunname(nullptr),
+    mLineno(0),
+    mLanguage(nsIProgrammingLanguage::UNKNOWN)
+{}
+
+JSStackFrame::~JSStackFrame()
+{
+  if (mFilename) {
+    nsMemory::Free(mFilename);
+  }
+  if (mFunname) {
+    nsMemory::Free(mFunname);
+  }
+}
+
+NS_IMPL_ISUPPORTS1(JSStackFrame, nsIStackFrame)
+
+/* readonly attribute uint32_t language; */
+NS_IMETHODIMP JSStackFrame::GetLanguage(uint32_t* aLanguage)
+{
+  *aLanguage = mLanguage;
+  return NS_OK;
+}
+
+/* readonly attribute string languageName; */
+NS_IMETHODIMP JSStackFrame::GetLanguageName(char** aLanguageName)
+{
+  static const char js[] = "JavaScript";
+  static const char cpp[] = "C++";
+
+  if (IsJSFrame()) {
+    *aLanguageName = (char*) nsMemory::Clone(js, sizeof(js));
+  } else {
+    *aLanguageName = (char*) nsMemory::Clone(cpp, sizeof(cpp));
+  }
+
+  return NS_OK;
+}
+
+/* readonly attribute string filename; */
+NS_IMETHODIMP JSStackFrame::GetFilename(char** aFilename)
+{
+  NS_ENSURE_ARG_POINTER(aFilename);
+
+  if (mFilename) {
+    *aFilename = (char*) nsMemory::Clone(mFilename,
+                                         sizeof(char)*(strlen(mFilename)+1));
+  } else {
+    *aFilename = nullptr;
+  }
+
+  return NS_OK;
+}
+
+/* readonly attribute string name; */
+NS_IMETHODIMP JSStackFrame::GetName(char** aFunction)
+{
+  NS_ENSURE_ARG_POINTER(aFunction);
+
+  if (mFunname) {
+    *aFunction = (char*) nsMemory::Clone(mFunname,
+                                         sizeof(char)*(strlen(mFunname)+1));
+  } else {
+    *aFunction = nullptr;
+  }
+
+  return NS_OK;
+}
+
+/* readonly attribute int32_t lineNumber; */
+NS_IMETHODIMP JSStackFrame::GetLineNumber(int32_t* aLineNumber)
+{
+  *aLineNumber = mLineno;
+  return NS_OK;
+}
+
+/* readonly attribute string sourceLine; */
+NS_IMETHODIMP JSStackFrame::GetSourceLine(char** aSourceLine)
+{
+  *aSourceLine = nullptr;
+  return NS_OK;
+}
+
+/* readonly attribute nsIStackFrame caller; */
+NS_IMETHODIMP JSStackFrame::GetCaller(nsIStackFrame** aCaller)
+{
+  NS_IF_ADDREF(*aCaller = mCaller);
+  return NS_OK;
+}
+
+/* string toString (); */
+NS_IMETHODIMP JSStackFrame::ToString(char** _retval)
+{
+  const char* frametype = IsJSFrame() ? "JS" : "native";
+  const char* filename = mFilename ? mFilename : "<unknown filename>";
+  const char* funname = mFunname ? mFunname : "<TOP_LEVEL>";
+  static const char format[] = "%s frame :: %s :: %s :: line %d";
+  int len = sizeof(char)*
+              (strlen(frametype) + strlen(filename) + strlen(funname)) +
+            sizeof(format) + 6 /* space for lineno */;
+
+  char* buf = (char*) nsMemory::Alloc(len);
+  JS_snprintf(buf, len, format, frametype, filename, funname, mLineno);
+  *_retval = buf;
+  return NS_OK;
+}
+
+/* static */ already_AddRefed<nsIStackFrame>
+JSStackFrame::CreateStack(JSContext* cx)
+{
+  static const unsigned MAX_FRAMES = 100;
+
+  nsRefPtr<JSStackFrame> first = new JSStackFrame();
+  nsRefPtr<JSStackFrame> self = first;
+
+  JS::StackDescription* desc = JS::DescribeStack(cx, MAX_FRAMES);
+  if (!desc) {
+    return nullptr;
+  }
+
+  for (size_t i = 0; i < desc->nframes && self; i++) {
+    self->mLanguage = nsIProgrammingLanguage::JAVASCRIPT;
+
+    JSAutoCompartment ac(cx, desc->frames[i].script);
+    const char* filename = JS_GetScriptFilename(cx, desc->frames[i].script);
+    if (filename) {
+      self->mFilename =
+        (char*)nsMemory::Clone(filename, sizeof(char)*(strlen(filename)+1));
+    }
+
+    self->mLineno = desc->frames[i].lineno;
+
+    JSFunction* fun = desc->frames[i].fun;
+    if (fun) {
+      JS::RootedString funid(cx, JS_GetFunctionDisplayId(fun));
+      if (funid) {
+        size_t length = JS_GetStringEncodingLength(cx, funid);
+        if (length != size_t(-1)) {
+          self->mFunname = static_cast<char *>(nsMemory::Alloc(length + 1));
+          if (self->mFunname) {
+            JS_EncodeStringToBuffer(cx, funid, self->mFunname, length);
+            self->mFunname[length] = '\0';
+          }
+        }
+      }
+    }
+
+    nsRefPtr<JSStackFrame> frame = new JSStackFrame();
+    self->mCaller = frame;
+    self = frame.forget();
+  }
+
+  JS::FreeStackDescription(cx, desc);
+
+  return first.forget();
+}
+
+/* static */ already_AddRefed<nsIStackFrame>
+JSStackFrame::CreateStackFrameLocation(uint32_t aLanguage,
+                                       const char* aFilename,
+                                       const char* aFunctionName,
+                                       int32_t aLineNumber,
+                                       nsIStackFrame* aCaller)
+{
+  nsRefPtr<JSStackFrame> self = new JSStackFrame();
+
+  self->mLanguage = aLanguage;
+  self->mLineno = aLineNumber;
+
+  if (aFilename) {
+    self->mFilename =
+      (char*)nsMemory::Clone(aFilename, sizeof(char)*(strlen(aFilename)+1));
+  }
+
+  if (aFunctionName) {
+    self->mFunname = 
+      (char*)nsMemory::Clone(aFunctionName,
+                             sizeof(char)*(strlen(aFunctionName)+1));
+  }
+
+  self->mCaller = aCaller;
+
+  return self.forget();
+}
+
+already_AddRefed<nsIStackFrame>
+CreateStack(JSContext* cx)
+{
+  return JSStackFrame::CreateStack(cx);
+}
+
+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
new file mode 100644
--- /dev/null
+++ b/dom/bindings/Exceptions.h
@@ -0,0 +1,50 @@
+/* -*- 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/. */
+
+#ifndef mozilla_dom_Exceptions_h__
+#define mozilla_dom_Exceptions_h__
+
+// DOM exception throwing machinery (for both main thread and workers).
+
+#include <stdint.h>
+#include "jspubtd.h"
+#include "nsIException.h"
+
+class nsIStackFrame;
+template <class T>
+class already_AddRefed;
+
+namespace mozilla {
+namespace dom {
+
+class Exception;
+
+void
+Throw(JSContext* cx, nsresult rv, const char* sz);
+
+bool
+ThrowExceptionObject(JSContext* aCx, Exception* aException);
+
+bool
+ThrowExceptionObject(JSContext* aCx, nsIException* aException);
+
+// Internal stuff not intended to be widely used.
+namespace exceptions {
+
+already_AddRefed<nsIStackFrame>
+CreateStack(JSContext* cx);
+
+already_AddRefed<nsIStackFrame>
+CreateStackFrameLocation(uint32_t aLanguage,
+                         const char* aFilename,
+                         const char* aFunctionName,
+                         int32_t aLineNumber,
+                         nsIStackFrame* aCaller);
+
+} // namespace exceptions
+} // namespace dom
+} // namespace mozilla
+
+#endif
--- a/dom/bindings/Makefile.in
+++ b/dom/bindings/Makefile.in
@@ -55,16 +55,17 @@ ifneq (export TestExampleInterface-examp
 CPPSRCS = \
   $(linked_binding_cpp_files) \
   $(filter %.cpp, $(globalgen_targets)) \
   BindingUtils.cpp \
   CallbackInterface.cpp \
   CallbackObject.cpp \
   DOMJSProxyHandler.cpp \
   Date.cpp \
+  Exceptions.cpp \
   $(NULL)
 endif
 
 LOCAL_INCLUDES += -I$(topsrcdir)/js/xpconnect/src \
   -I$(topsrcdir)/js/xpconnect/wrappers \
   -I$(topsrcdir)/js/ipc \
   -I$(topsrcdir)/content/canvas/src \
   -I$(topsrcdir)/content/html/content/src \
--- a/dom/bindings/moz.build
+++ b/dom/bindings/moz.build
@@ -16,16 +16,17 @@ EXPORTS.mozilla.dom += [
     'BindingUtils.h',
     'CallbackFunction.h',
     'CallbackInterface.h',
     'CallbackObject.h',
     'DOMJSClass.h',
     'DOMJSProxyHandler.h',
     'Date.h',
     'Errors.msg',
+    'Exceptions.h',
     'JSSlots.h',
     'NonRefcountedDOMObject.h',
     'Nullable.h',
     'PrimitiveConversions.h',
     'TypedArray.h',
 ]
 
 FAIL_ON_WARNINGS = True
--- a/dom/workers/RuntimeService.cpp
+++ b/dom/workers/RuntimeService.cpp
@@ -1111,17 +1111,17 @@ GetWorkerPrivateFromContext(JSContext* a
   NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
   return static_cast<WorkerThreadRuntimePrivate*>(JS_GetRuntimePrivate(JS_GetRuntime(aCx)))->mWorkerPrivate;
 }
 
 bool
 IsCurrentThreadRunningChromeWorker()
 {
   NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
-  CycleCollectedJSRuntime* ccrt = nsCycleCollector_currentJSRuntime();
+  CycleCollectedJSRuntime* ccrt = CycleCollectedJSRuntime::Get();
   if (!ccrt) {
     return false;
   }
 
   JSRuntime* rt = ccrt->Runtime();
   return static_cast<WorkerThreadRuntimePrivate*>(JS_GetRuntimePrivate(rt))->
     mWorkerPrivate->UsesSystemPrincipal();
 }
--- a/js/xpconnect/idl/xpcexception.idl
+++ b/js/xpconnect/idl/xpcexception.idl
@@ -2,38 +2,27 @@
  *
  * 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 "nsISupports.idl"
 #include "nsIException.idl"
 
-%{ C++
-#include "js/TypeDecls.h"
-%}
-
-[ptr] native xpcexJSContextPtr(JSContext);
-      native xpcexJSVal(JS::Value);
-
-[scriptable, uuid(cac29630-7bf2-4e22-811b-46855a7d5af0)]
+[scriptable, uuid(ce83229c-0a82-4ba4-937b-7fd4cd45f34b)]
 interface nsIXPCException : nsIException
 {
     // inherits methods from nsIException
 
     void initialize(in string           aMessage,
                     in nsresult         aResult,
                     in string           aName,
                     in nsIStackFrame    aLocation,
                     in nsISupports      aData,
                     in nsIException     aInner);
-
-    [noscript] xpcexJSVal stealJSVal();
-    [noscript] void stowJSVal(in xpcexJSContextPtr cx,
-                              in xpcexJSVal val);
 };
 
 /* this goes into the C++ header verbatim. */
 %{ C++
 /********************************************************/
 // {5632BF70-51EC-11d3-9896-006008962422}
 #define NS_XPCEXCEPTION_CID  \
 { 0x5632bf70, 0x51ec, 0x11d3, \
--- a/js/xpconnect/src/XPCComponents.cpp
+++ b/js/xpconnect/src/XPCComponents.cpp
@@ -34,22 +34,24 @@
 #include "mozilla/Attributes.h"
 #include "nsIScriptContext.h"
 #include "nsJSEnvironment.h"
 #include "nsXMLHttpRequest.h"
 #include "mozilla/Telemetry.h"
 #include "mozilla/XPTInterfaceInfoManager.h"
 #include "nsDOMClassInfoID.h"
 #include "nsGlobalWindow.h"
+#include "mozilla/dom/DOMException.h"
 #include "mozilla/dom/DOMExceptionBinding.h"
 
 using namespace mozilla;
 using namespace JS;
 using namespace js;
 using namespace xpc;
+using mozilla::dom::Exception;
 
 /***************************************************************************/
 // stuff used by all
 
 nsresult
 xpc::ThrowAndFail(nsresult errNum, JSContext *cx, bool *retval)
 {
     XPCThrower::Throw(errNum, cx);
@@ -1941,32 +1943,30 @@ nsXPCComponents_Exception::CallOrConstru
                                            JSContext *cx, HandleObject obj,
                                            const CallArgs &args, bool *_retval)
 {
     nsXPConnect* xpc = nsXPConnect::XPConnect();
 
     // Do the security check if necessary
 
     nsIXPCSecurityManager* sm = xpc->GetDefaultSecurityManager();
-    if (sm && NS_FAILED(sm->CanCreateInstance(cx, nsXPCException::GetCID()))) {
+    if (sm && NS_FAILED(sm->CanCreateInstance(cx, Exception::GetCID()))) {
         // the security manager vetoed. It should have set an exception.
         *_retval = false;
         return NS_OK;
     }
 
     // Parse the arguments to the Exception constructor.
     ExceptionArgParser parser(cx, xpc);
     if (!parser.parse(args))
         return ThrowAndFail(NS_ERROR_XPC_BAD_CONVERT_JS, cx, _retval);
 
-    nsCOMPtr<nsIException> e = new nsXPCException(parser.eMsg, parser.eResult,
-                                                  nullptr, parser.eStack,
-                                                  parser.eData);
-    if (!e)
-        return ThrowAndFail(NS_ERROR_XPC_UNEXPECTED, cx, _retval);
+    nsCOMPtr<nsIException> e = new Exception(parser.eMsg, parser.eResult,
+                                             nullptr, parser.eStack,
+                                             parser.eData);
 
     nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
     RootedObject newObj(cx);
 
     if (NS_FAILED(xpc->WrapNative(cx, obj, e, NS_GET_IID(nsIXPCException),
                                   getter_AddRefs(holder))) || !holder ||
         // Assign, not compare
         !(newObj = holder->GetJSObject())) {
@@ -1983,17 +1983,17 @@ nsXPCComponents_Exception::HasInstance(n
                                        JSContext * cx, JSObject * obj,
                                        const jsval &val, bool *bp,
                                        bool *_retval)
 {
     using namespace mozilla::dom;
 
     RootedValue v(cx, val);
     if (bp) {
-        nsXPCException* e;
+        Exception* e;
         *bp = NS_SUCCEEDED(UNWRAP_OBJECT(Exception, cx, v.toObjectOrNull(), e)) ||
               JSValIsInterfaceOfType(cx, v, NS_GET_IID(nsIException));
     }
     return NS_OK;
 }
 
 /***************************************************************************/
 // This class is for the thing returned by "new Component.Constructor".
--- a/js/xpconnect/src/XPCConvert.cpp
+++ b/js/xpconnect/src/XPCConvert.cpp
@@ -22,16 +22,17 @@
 #include "nsWrapperCacheInlines.h"
 
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "jsprf.h"
 #include "JavaScriptParent.h"
 
 #include "mozilla/dom/BindingUtils.h"
+#include "mozilla/dom/DOMException.h"
 #include "mozilla/dom/PrimitiveConversions.h"
 
 using namespace xpc;
 using namespace mozilla;
 using namespace mozilla::dom;
 using namespace JS;
 
 //#define STRICT_CHECK_OF_UNICODE
@@ -1075,24 +1076,24 @@ XPCConvert::ConstructException(nsresult 
         }
     }
     if (!msg)
         if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &msg) || ! msg)
             msg = "<error>";
     if (ifaceName && methodName)
         msg = sz = JS_smprintf(format, msg, ifaceName, methodName);
 
-    nsCOMPtr<nsIException> e = new nsXPCException(msg, rv, nullptr, nullptr, data);
+    nsRefPtr<Exception> e = new Exception(msg, rv, nullptr, nullptr, data);
 
-    if (cx && jsExceptionPtr && *exceptn) {
-        nsCOMPtr<nsIXPCException> xpcEx = do_QueryInterface(*exceptn);
-        if (xpcEx)
-            xpcEx->StowJSVal(cx, *jsExceptionPtr);
+    if (cx && jsExceptionPtr) {
+        e->StowJSVal(*jsExceptionPtr);
     }
 
+    e.forget(exceptn);
+
     if (sz)
         JS_smprintf_free(sz);
     return NS_OK;
 }
 
 /********************************/
 
 class MOZ_STACK_CLASS AutoExceptionRestorer
--- a/js/xpconnect/src/XPCException.cpp
+++ b/js/xpconnect/src/XPCException.cpp
@@ -2,22 +2,17 @@
  *
  * 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/. */
 
 /* An implementaion of nsIException. */
 
 #include "xpcprivate.h"
-#include "jsprf.h"
 #include "nsError.h"
-#include "mozilla/dom/DOMExceptionBinding.h"
-
-namespace ExceptionBinding = mozilla::dom::ExceptionBinding;
-using mozilla::DebugOnly;
 
 /***************************************************************************/
 /* Quick and dirty mapping of well known result codes to strings. We only
 *  call this when building an exception object, so iterating the short array
 *  is not too bad.
 *
 *  It sure would be nice to have exceptions declared in idl and available
 *  in some more global way at runtime.
@@ -78,467 +73,8 @@ nsXPCException::IterateNSResults(nsresul
 }
 
 // static
 uint32_t
 nsXPCException::GetNSResultCount()
 {
     return RESULT_COUNT;
 }
-
-/***************************************************************************/
-
-NS_IMPL_CLASSINFO(nsXPCException, NULL, nsIClassInfo::DOM_OBJECT,
-                  NS_XPCEXCEPTION_CID)
-NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXPCException)
-  NS_INTERFACE_MAP_ENTRY(nsIException)
-  NS_INTERFACE_MAP_ENTRY(nsIXPCException)
-  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIException)
-  NS_IMPL_QUERY_CLASSINFO(nsXPCException)
-NS_INTERFACE_MAP_END
-
-NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXPCException)
-NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXPCException)
-
-NS_IMPL_CYCLE_COLLECTION_CLASS(nsXPCException)
-
-NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXPCException)
-  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
-NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
-
-NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(nsXPCException)
-
-NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXPCException)
-  NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
-NS_IMPL_CYCLE_COLLECTION_UNLINK_END
-
-NS_IMPL_CI_INTERFACE_GETTER1(nsXPCException, nsIXPCException)
-
-nsXPCException::nsXPCException(const char *aMessage,
-                               nsresult aResult,
-                               const char *aName,
-                               nsIStackFrame *aLocation,
-                               nsISupports *aData)
-    : mMessage(nullptr),
-      mResult(NS_OK),
-      mName(nullptr),
-      mLocation(nullptr),
-      mData(nullptr),
-      mFilename(nullptr),
-      mLineNumber(0),
-      mInner(nullptr),
-      mInitialized(false)
-{
-    SetIsDOMBinding();
-
-    // A little hack... The nsIGenericModule nsIClassInfo scheme relies on there
-    // having been at least one instance made via the factory. Otherwise, the
-    // shared factory/classinsance object never gets created and our QI getter
-    // for our instance's pointer to our nsIClassInfo will always return null.
-    // This is bad because it means that wrapped exceptions will never have a
-    // shared prototype. So... We force one to be created via the factory
-    // *once* and then go about our business.
-    if (!sEverMadeOneFromFactory) {
-        nsCOMPtr<nsIXPCException> e =
-            do_CreateInstance(XPC_EXCEPTION_CONTRACTID);
-        sEverMadeOneFromFactory = true;
-    }
-
-    nsIStackFrame* location;
-    if (aLocation) {
-        location = aLocation;
-        NS_ADDREF(location);
-    } else {
-        nsXPConnect* xpc = nsXPConnect::XPConnect();
-        xpc->GetCurrentJSStack(&location);
-        // it is legal for there to be no active JS stack, if C++ code
-        // is operating on a JS-implemented interface pointer without
-        // having been called in turn by JS.  This happens in the JS
-        // component loader, and will become more common as additional
-        // components are implemented in JS.
-    }
-    // We want to trim off any leading native 'dataless' frames
-    if (location) {
-        while (1) {
-            uint32_t language;
-            int32_t lineNumber;
-            if (NS_FAILED(location->GetLanguage(&language)) ||
-                language == nsIProgrammingLanguage::JAVASCRIPT ||
-                NS_FAILED(location->GetLineNumber(&lineNumber)) ||
-                lineNumber) {
-                break;
-            }
-            nsCOMPtr<nsIStackFrame> caller;
-            if (NS_FAILED(location->GetCaller(getter_AddRefs(caller))) || !caller)
-                break;
-            NS_RELEASE(location);
-            caller->QueryInterface(NS_GET_IID(nsIStackFrame), (void **)&location);
-        }
-    }
-
-    Initialize(aMessage, aResult, aName, location, aData, nullptr);
-    NS_IF_RELEASE(location);
-}
-
-nsXPCException::nsXPCException()
-    : mMessage(nullptr),
-      mResult(NS_OK),
-      mName(nullptr),
-      mLocation(nullptr),
-      mData(nullptr),
-      mFilename(nullptr),
-      mLineNumber(0),
-      mInner(nullptr),
-      mInitialized(false)
-{ }
-
-nsXPCException::~nsXPCException()
-{
-    Reset();
-}
-
-/* [noscript] xpcexJSVal stealJSVal (); */
-NS_IMETHODIMP
-nsXPCException::StealJSVal(jsval *vp)
-{
-    if (mThrownJSVal.IsHeld()) {
-        *vp = mThrownJSVal.Release();
-        return NS_OK;
-    }
-    return NS_ERROR_FAILURE;
-}
-
-/* [noscript] void stowJSVal (in xpcexJSContextPtr cx, in xpcexJSVal val); */
-NS_IMETHODIMP
-nsXPCException::StowJSVal(JSContext* cx, jsval v)
-{
-    if (mThrownJSVal.Hold(cx)) {
-        mThrownJSVal = v;
-        return NS_OK;
-    }
-    return NS_ERROR_FAILURE;
-}
-
-void
-nsXPCException::Reset()
-{
-    if (mMessage) {
-        nsMemory::Free(mMessage);
-        mMessage = nullptr;
-    }
-    if (mName) {
-        nsMemory::Free(mName);
-        mName = nullptr;
-    }
-    if (mFilename) {
-        nsMemory::Free(mFilename);
-        mFilename = nullptr;
-    }
-    mLineNumber = (uint32_t)-1;
-    NS_IF_RELEASE(mLocation);
-    NS_IF_RELEASE(mData);
-    NS_IF_RELEASE(mInner);
-}
-
-/* readonly attribute string message; */
-NS_IMETHODIMP
-nsXPCException::GetMessageMoz(char * *aMessage)
-{
-    if (!mInitialized)
-        return NS_ERROR_NOT_INITIALIZED;
-    XPC_STRING_GETTER_BODY(aMessage, mMessage);
-}
-
-/* readonly attribute nsresult result; */
-NS_IMETHODIMP
-nsXPCException::GetResult(nsresult *aResult)
-{
-    if (!aResult)
-        return NS_ERROR_NULL_POINTER;
-    if (!mInitialized)
-        return NS_ERROR_NOT_INITIALIZED;
-    *aResult = mResult;
-    return NS_OK;
-}
-
-/* readonly attribute string name; */
-NS_IMETHODIMP
-nsXPCException::GetName(char * *aName)
-{
-    if (!mInitialized)
-        return NS_ERROR_NOT_INITIALIZED;
-
-    const char* name = mName;
-    if (!name)
-        NameAndFormatForNSResult(mResult, &name, nullptr);
-
-    XPC_STRING_GETTER_BODY(aName, name);
-}
-
-/* readonly attribute string filename; */
-NS_IMETHODIMP nsXPCException::GetFilename(char * *aFilename)
-{
-    if (!mInitialized)
-        return NS_ERROR_NOT_INITIALIZED;
-    XPC_STRING_GETTER_BODY(aFilename, mFilename);
-}
-
-/* readonly attribute uint32_t lineNumber; */
-NS_IMETHODIMP nsXPCException::GetLineNumber(uint32_t *aLineNumber)
-{
-    if (!aLineNumber)
-        return NS_ERROR_NULL_POINTER;
-    if (!mInitialized)
-        return NS_ERROR_NOT_INITIALIZED;
-    *aLineNumber = mLineNumber;
-    return NS_OK;
-}
-
-/* readonly attribute uint32_t columnNumber; */
-NS_IMETHODIMP nsXPCException::GetColumnNumber(uint32_t *aColumnNumber)
-{
-    NS_ENSURE_ARG_POINTER(aColumnNumber);
-    if (!mInitialized)
-        return NS_ERROR_NOT_INITIALIZED;
-    *aColumnNumber = 0;
-    return NS_OK;
-}
-
-/* readonly attribute nsIStackFrame location; */
-NS_IMETHODIMP
-nsXPCException::GetLocation(nsIStackFrame * *aLocation)
-{
-    if (!aLocation)
-        return NS_ERROR_NULL_POINTER;
-    if (!mInitialized)
-        return NS_ERROR_NOT_INITIALIZED;
-    *aLocation = mLocation;
-    NS_IF_ADDREF(mLocation);
-    return NS_OK;
-}
-
-/* readonly attribute nsISupports data; */
-NS_IMETHODIMP
-nsXPCException::GetData(nsISupports * *aData)
-{
-    if (!aData)
-        return NS_ERROR_NULL_POINTER;
-    if (!mInitialized)
-        return NS_ERROR_NOT_INITIALIZED;
-    *aData = mData;
-    NS_IF_ADDREF(mData);
-    return NS_OK;
-}
-
-/* readonly attribute nsIException inner; */
-NS_IMETHODIMP
-nsXPCException::GetInner(nsIException* *aException)
-{
-    if (!aException)
-        return NS_ERROR_NULL_POINTER;
-    if (!mInitialized)
-        return NS_ERROR_NOT_INITIALIZED;
-    *aException = mInner;
-    NS_IF_ADDREF(mInner);
-    return NS_OK;
-}
-
-/* void initialize (in string aMessage, in nsresult aResult, in string aName, in nsIStackFrame aLocation, in nsISupports aData, in nsIException aInner); */
-NS_IMETHODIMP
-nsXPCException::Initialize(const char *aMessage, nsresult aResult, const char *aName, nsIStackFrame *aLocation, nsISupports *aData, nsIException *aInner)
-{
-    if (mInitialized)
-        return NS_ERROR_ALREADY_INITIALIZED;
-
-    Reset();
-
-    if (aMessage) {
-        if (!(mMessage = (char*) nsMemory::Clone(aMessage,
-                                                 sizeof(char)*(strlen(aMessage)+1))))
-            return NS_ERROR_OUT_OF_MEMORY;
-    }
-
-    if (aName) {
-        if (!(mName = (char*) nsMemory::Clone(aName,
-                                              sizeof(char)*(strlen(aName)+1))))
-            return NS_ERROR_OUT_OF_MEMORY;
-    }
-
-    mResult = aResult;
-
-    if (aLocation) {
-        mLocation = aLocation;
-        NS_ADDREF(mLocation);
-        // For now, fill in our location details from our stack frame.
-        // Later we may allow other locations?
-        nsresult rc;
-        if (NS_FAILED(rc = aLocation->GetFilename(&mFilename)))
-            return rc;
-        if (NS_FAILED(rc = aLocation->GetLineNumber(&mLineNumber)))
-            return rc;
-    } else {
-        nsresult rv;
-        nsXPConnect* xpc = nsXPConnect::XPConnect();
-        rv = xpc->GetCurrentJSStack(&mLocation);
-        if (NS_FAILED(rv))
-            return rv;
-    }
-
-    if (aData) {
-        mData = aData;
-        NS_ADDREF(mData);
-    }
-    if (aInner) {
-        mInner = aInner;
-        NS_ADDREF(mInner);
-    }
-
-    mInitialized = true;
-    return NS_OK;
-}
-
-/* string toString (); */
-NS_IMETHODIMP
-nsXPCException::ToString(char **_retval)
-{
-    if (!_retval)
-        return NS_ERROR_NULL_POINTER;
-    if (!mInitialized)
-        return NS_ERROR_NOT_INITIALIZED;
-
-    static const char defaultMsg[] = "<no message>";
-    static const char defaultLocation[] = "<unknown>";
-    static const char format[] =
- "[Exception... \"%s\"  nsresult: \"0x%x (%s)\"  location: \"%s\"  data: %s]";
-
-    char* indicatedLocation = nullptr;
-
-    if (mLocation) {
-        // we need to free this if it does not fail
-        nsresult rv = mLocation->ToString(&indicatedLocation);
-        if (NS_FAILED(rv))
-            return rv;
-    }
-
-    const char* msg = mMessage ? mMessage : nullptr;
-    const char* location = indicatedLocation ?
-                                indicatedLocation : defaultLocation;
-    const char* resultName = mName;
-    if (!resultName && !NameAndFormatForNSResult(mResult, &resultName,
-                                                 (!msg) ? &msg : nullptr)) {
-        if (!msg)
-            msg = defaultMsg;
-        resultName = "<unknown>";
-    }
-    const char* data = mData ? "yes" : "no";
-
-    char* temp = JS_smprintf(format, msg, mResult, resultName, location, data);
-    if (indicatedLocation)
-        nsMemory::Free(indicatedLocation);
-
-    char* final = nullptr;
-    if (temp) {
-        final = (char*) nsMemory::Clone(temp, sizeof(char)*(strlen(temp)+1));
-        JS_smprintf_free(temp);
-    }
-
-    *_retval = final;
-    return final ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
-}
-
-JSObject*
-nsXPCException::WrapObject(JSContext* cx, JS::Handle<JSObject*> scope)
-{
-    return ExceptionBinding::Wrap(cx, scope, this);
-}
-
-void
-nsXPCException::GetMessageMoz(nsString& retval)
-{
-    char* str = nullptr;
-#ifdef DEBUG
-    DebugOnly<nsresult> rv = 
-#endif
-    GetMessageMoz(&str);
-    MOZ_ASSERT(NS_SUCCEEDED(rv));
-    CopyUTF8toUTF16(str, retval);
-    nsMemory::Free(str);
-}
-
-uint32_t
-nsXPCException::Result() const
-{
-    return (uint32_t)mResult;
-}
-
-void
-nsXPCException::GetName(nsString& retval)
-{
-    char* str = nullptr;
-#ifdef DEBUG
-    DebugOnly<nsresult> rv =
-#endif
-    GetName(&str);
-    MOZ_ASSERT(NS_SUCCEEDED(rv));
-    CopyUTF8toUTF16(str, retval);
-    nsMemory::Free(str);
-}
-
-void
-nsXPCException::GetFilename(nsString& retval)
-{
-    char* str = nullptr;
-#ifdef DEBUG
-    DebugOnly<nsresult> rv =
-#endif
-    GetFilename(&str);
-    MOZ_ASSERT(NS_SUCCEEDED(rv));
-    CopyUTF8toUTF16(str, retval);
-    nsMemory::Free(str);
-}
-
-uint32_t
-nsXPCException::LineNumber() const
-{
-    return mLineNumber;
-}
-
-uint32_t
-nsXPCException::ColumnNumber() const
-{
-    return 0;
-}
-
-already_AddRefed<nsIStackFrame>
-nsXPCException::GetLocation() const
-{
-    nsCOMPtr<nsIStackFrame> location = mLocation;
-    return location.forget();
-}
-
-already_AddRefed<nsISupports>
-nsXPCException::GetInner() const
-{
-    nsCOMPtr<nsISupports> inner = mInner;
-    return inner.forget();
-}
-
-already_AddRefed<nsISupports>
-nsXPCException::GetData() const
-{
-    nsCOMPtr<nsISupports> data = mData;
-    return data.forget();
-}
-
-void
-nsXPCException::Stringify(nsString& retval)
-{
-    char* str = nullptr;
-#ifdef DEBUG
-    DebugOnly<nsresult> rv =
-#endif
-    ToString(&str);
-    MOZ_ASSERT(NS_SUCCEEDED(rv));
-    CopyUTF8toUTF16(str, retval);
-    nsMemory::Free(str);
-}
-
-bool nsXPCException::sEverMadeOneFromFactory = false;
--- a/js/xpconnect/src/XPCForwards.h
+++ b/js/xpconnect/src/XPCForwards.h
@@ -12,17 +12,16 @@
 // forward declarations of interally used classes...
 
 class nsXPConnect;
 class XPCJSRuntime;
 class XPCContext;
 class XPCCallContext;
 
 class XPCJSThrower;
-class XPCJSStack;
 
 class nsXPCWrappedJS;
 class nsXPCWrappedJSClass;
 
 class XPCNativeMember;
 class XPCNativeInterface;
 class XPCNativeSet;
 
--- a/js/xpconnect/src/XPCJSRuntime.cpp
+++ b/js/xpconnect/src/XPCJSRuntime.cpp
@@ -1388,16 +1388,20 @@ void XPCJSRuntime::SystemIsBeingShutDown
 
     if (mDetachedWrappedNativeProtoMap)
         mDetachedWrappedNativeProtoMap->
             Enumerate(DetachedWrappedNativeProtoShutdownMarker, nullptr);
 }
 
 XPCJSRuntime::~XPCJSRuntime()
 {
+    // Clear any pending exception.  It might be an XPCWrappedJS, and if we try
+    // to destroy it later we will crash.
+    SetPendingException(nullptr);
+
     JS::SetGCSliceCallback(Runtime(), mPrevGCSliceCallback);
 
     xpc_DelocalizeRuntime(Runtime());
 
     if (mWatchdogManager->GetWatchdog())
         mWatchdogManager->StopWatchdog();
 
     if (mCallContext)
--- a/js/xpconnect/src/XPCModule.h
+++ b/js/xpconnect/src/XPCModule.h
@@ -16,17 +16,16 @@
 // {FE4F7592-C1FC-4662-AC83-538841318803}
 #define SCRIPTABLE_INTERFACES_CID                                             \
     {0xfe4f7592, 0xc1fc, 0x4662,                                              \
       { 0xac, 0x83, 0x53, 0x88, 0x41, 0x31, 0x88, 0x3 } }
 
 #define MOZJSSUBSCRIPTLOADER_CONTRACTID "@mozilla.org/moz/jssubscript-loader;1"
 
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsJSID)
-NS_GENERIC_FACTORY_CONSTRUCTOR(nsXPCException)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIXPConnect,
                                          nsXPConnect::GetSingleton)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsScriptError)
 
 NS_GENERIC_FACTORY_CONSTRUCTOR(mozJSComponentLoader)
 NS_GENERIC_FACTORY_CONSTRUCTOR(mozJSSubScriptLoader)
 
 NS_DEFINE_NAMED_CID(NS_JS_ID_CID);
@@ -34,27 +33,25 @@ NS_DEFINE_NAMED_CID(NS_XPCONNECT_CID);
 NS_DEFINE_NAMED_CID(NS_XPCEXCEPTION_CID);
 NS_DEFINE_NAMED_CID(NS_SCRIPTERROR_CID);
 NS_DEFINE_NAMED_CID(MOZJSCOMPONENTLOADER_CID);
 NS_DEFINE_NAMED_CID(MOZ_JSSUBSCRIPTLOADER_CID);
 
 #define XPCONNECT_CIDENTRIES                                                  \
   { &kNS_JS_ID_CID, false, NULL,  nsJSIDConstructor },                        \
   { &kNS_XPCONNECT_CID, false, NULL,  nsIXPConnectConstructor },              \
-  { &kNS_XPCEXCEPTION_CID, false, NULL, nsXPCExceptionConstructor },          \
   { &kNS_SCRIPTERROR_CID, false, NULL, nsScriptErrorConstructor },            \
   { &kMOZJSCOMPONENTLOADER_CID, false, NULL, mozJSComponentLoaderConstructor },\
   { &kMOZ_JSSUBSCRIPTLOADER_CID, false, NULL, mozJSSubScriptLoaderConstructor },
 
 #define XPCONNECT_CONTRACTS                                                   \
   { XPC_ID_CONTRACTID, &kNS_JS_ID_CID },                                      \
   { XPC_XPCONNECT_CONTRACTID, &kNS_XPCONNECT_CID },                           \
   { XPC_CONTEXT_STACK_CONTRACTID, &kNS_XPCONNECT_CID },                       \
   { XPC_RUNTIME_CONTRACTID, &kNS_XPCONNECT_CID },                             \
-  { XPC_EXCEPTION_CONTRACTID, &kNS_XPCEXCEPTION_CID },                        \
   { NS_SCRIPTERROR_CONTRACTID, &kNS_SCRIPTERROR_CID },                        \
   { MOZJSCOMPONENTLOADER_CONTRACTID, &kMOZJSCOMPONENTLOADER_CID },            \
   { MOZJSSUBSCRIPTLOADER_CONTRACTID, &kMOZ_JSSUBSCRIPTLOADER_CID },
 
 #define XPCONNECT_CATEGORIES \
   { "module-loader", "js", MOZJSCOMPONENTLOADER_CONTRACTID },
 
 nsresult xpcModuleCtor();
--- a/js/xpconnect/src/XPCQuickStubs.cpp
+++ b/js/xpconnect/src/XPCQuickStubs.cpp
@@ -8,16 +8,17 @@
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "jsprf.h"
 #include "nsCOMPtr.h"
 #include "xpcprivate.h"
 #include "XPCInlines.h"
 #include "XPCQuickStubs.h"
 #include "mozilla/dom/BindingUtils.h"
+#include "mozilla/dom/Exceptions.h"
 
 using namespace mozilla;
 using namespace JS;
 
 extern const char* xpc_qsStringTable;
 
 static const xpc_qsHashEntry *
 LookupEntry(uint32_t tableSize, const xpc_qsHashEntry *table, const nsID &iid)
@@ -245,17 +246,17 @@ ThrowCallFailed(JSContext *cx, nsresult 
         && name) {
         sz = JS_smprintf("%s 0x%x (%s) [%s.%s]",
                          format, rv, name, ifaceName, memberName);
     } else {
         sz = JS_smprintf("%s 0x%x [%s.%s]",
                          format, rv, ifaceName, memberName);
     }
 
-    XPCThrower::BuildAndThrowException(cx, rv, sz);
+    dom::Throw(cx, rv, sz);
 
     if (sz)
         JS_smprintf_free(sz);
 
     return false;
 }
 
 bool
@@ -331,17 +332,17 @@ ThrowBadArg(JSContext *cx, nsresult rv, 
     if (!memberName) {
         memberName = JSID_IS_STRING(memberId)
                      ? memberNameBytes.encodeLatin1(cx, JSID_TO_STRING(memberId))
                      : "unknown";
     }
     sz = JS_smprintf("%s arg %u [%s.%s]",
                      format, (unsigned int) paramnum, ifaceName, memberName);
 
-    XPCThrower::BuildAndThrowException(cx, rv, sz);
+    dom::Throw(cx, rv, sz);
 
     if (sz)
         JS_smprintf_free(sz);
 }
 
 void
 xpc_qsThrowBadArg(JSContext *cx, nsresult rv, jsval *vp, unsigned paramnum)
 {
deleted file mode 100644
--- a/js/xpconnect/src/XPCStack.cpp
+++ /dev/null
@@ -1,268 +0,0 @@
-/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
- *
- * 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/. */
-
-/* Implements nsIStackFrame. */
-
-#include "xpcprivate.h"
-#include "jsprf.h"
-#include "js/OldDebugAPI.h"
-
-class XPCJSStackFrame : public nsIStackFrame
-{
-public:
-    NS_DECL_THREADSAFE_ISUPPORTS
-    NS_DECL_NSISTACKFRAME
-
-    static nsresult CreateStack(JSContext* cx, XPCJSStackFrame** stack);
-
-    static nsresult CreateStackFrameLocation(uint32_t aLanguage,
-                                             const char* aFilename,
-                                             const char* aFunctionName,
-                                             int32_t aLineNumber,
-                                             nsIStackFrame* aCaller,
-                                             XPCJSStackFrame** stack);
-
-    XPCJSStackFrame();
-    virtual ~XPCJSStackFrame();
-
-    bool IsJSFrame() const
-        {return mLanguage == nsIProgrammingLanguage::JAVASCRIPT;}
-
-private:
-    nsCOMPtr<nsIStackFrame> mCaller;
-
-    char* mFilename;
-    char* mFunname;
-    int32_t mLineno;
-    uint32_t mLanguage;
-};
-
-/**********************************************/
-
-// static
-
-nsresult
-XPCJSStack::CreateStack(JSContext* cx, nsIStackFrame** stack)
-{
-    if (!cx)
-        return NS_ERROR_FAILURE;
-
-    return XPCJSStackFrame::CreateStack(cx, (XPCJSStackFrame**) stack);
-}
-
-// static
-nsresult
-XPCJSStack::CreateStackFrameLocation(uint32_t aLanguage,
-                                     const char* aFilename,
-                                     const char* aFunctionName,
-                                     int32_t aLineNumber,
-                                     nsIStackFrame* aCaller,
-                                     nsIStackFrame** stack)
-{
-    return XPCJSStackFrame::CreateStackFrameLocation(aLanguage,
-                                                     aFilename,
-                                                     aFunctionName,
-                                                     aLineNumber,
-                                                     aCaller,
-                                                     (XPCJSStackFrame**) stack);
-}
-
-
-/**********************************************/
-
-XPCJSStackFrame::XPCJSStackFrame()
-    :   mFilename(nullptr),
-        mFunname(nullptr),
-        mLineno(0),
-        mLanguage(nsIProgrammingLanguage::UNKNOWN)
-{
-}
-
-XPCJSStackFrame::~XPCJSStackFrame()
-{
-    if (mFilename)
-        nsMemory::Free(mFilename);
-    if (mFunname)
-        nsMemory::Free(mFunname);
-}
-
-NS_IMPL_ISUPPORTS1(XPCJSStackFrame, nsIStackFrame)
-
-nsresult
-XPCJSStackFrame::CreateStack(JSContext* cx, XPCJSStackFrame** stack)
-{
-    static const unsigned MAX_FRAMES = 100;
-
-    nsRefPtr<XPCJSStackFrame> first = new XPCJSStackFrame();
-    nsRefPtr<XPCJSStackFrame> self = first;
-
-    JS::StackDescription* desc = JS::DescribeStack(cx, MAX_FRAMES);
-    if (!desc)
-        return NS_ERROR_FAILURE;
-
-    for (size_t i = 0; i < desc->nframes && self; i++) {
-        self->mLanguage = nsIProgrammingLanguage::JAVASCRIPT;
-
-	JSAutoCompartment ac(cx, desc->frames[i].script);
-        const char* filename = JS_GetScriptFilename(cx, desc->frames[i].script);
-        if (filename) {
-            self->mFilename = (char*)
-                nsMemory::Clone(filename,
-                                sizeof(char)*(strlen(filename)+1));
-        }
-
-        self->mLineno = desc->frames[i].lineno;
-
-        JSFunction* fun = desc->frames[i].fun;
-        if (fun) {
-            JS::RootedString funid(cx, JS_GetFunctionDisplayId(fun));
-            if (funid) {
-                size_t length = JS_GetStringEncodingLength(cx, funid);
-                if (length != size_t(-1)) {
-                    self->mFunname = static_cast<char *>(nsMemory::Alloc(length + 1));
-                    if (self->mFunname) {
-                        JS_EncodeStringToBuffer(cx, funid, self->mFunname, length);
-                        self->mFunname[length] = '\0';
-                    }
-                }
-            }
-        }
-
-        XPCJSStackFrame* frame = new XPCJSStackFrame();
-        self->mCaller = frame;
-        self = frame;
-    }
-
-    JS::FreeStackDescription(cx, desc);
-
-    *stack = first.forget().get();
-    return NS_OK;
-}
-
-// static
-nsresult
-XPCJSStackFrame::CreateStackFrameLocation(uint32_t aLanguage,
-                                          const char* aFilename,
-                                          const char* aFunctionName,
-                                          int32_t aLineNumber,
-                                          nsIStackFrame* aCaller,
-                                          XPCJSStackFrame** stack)
-{
-    bool failed = false;
-    XPCJSStackFrame* self = new XPCJSStackFrame();
-    if (self)
-        NS_ADDREF(self);
-    else
-        failed = true;
-
-    if (!failed) {
-        self->mLanguage = aLanguage;
-        self->mLineno = aLineNumber;
-    }
-
-    if (!failed && aFilename) {
-        self->mFilename = (char*)
-                nsMemory::Clone(aFilename,
-                                sizeof(char)*(strlen(aFilename)+1));
-        if (!self->mFilename)
-            failed = true;
-    }
-
-    if (!failed && aFunctionName) {
-        self->mFunname = (char*)
-                nsMemory::Clone(aFunctionName,
-                                sizeof(char)*(strlen(aFunctionName)+1));
-        if (!self->mFunname)
-            failed = true;
-    }
-
-    if (!failed && aCaller) {
-        self->mCaller = aCaller;
-    }
-
-    if (failed && self) {
-        NS_RELEASE(self);   // sets self to nullptr
-    }
-
-    *stack = self;
-    return self ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
-}
-
-/* readonly attribute uint32_t language; */
-NS_IMETHODIMP XPCJSStackFrame::GetLanguage(uint32_t *aLanguage)
-{
-    *aLanguage = mLanguage;
-    return NS_OK;
-}
-
-/* readonly attribute string languageName; */
-NS_IMETHODIMP XPCJSStackFrame::GetLanguageName(char * *aLanguageName)
-{
-    static const char js[] = "JavaScript";
-    static const char cpp[] = "C++";
-    char* temp;
-
-    if (IsJSFrame())
-        *aLanguageName = temp = (char*) nsMemory::Clone(js, sizeof(js));
-    else
-        *aLanguageName = temp = (char*) nsMemory::Clone(cpp, sizeof(cpp));
-
-    return temp ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
-}
-
-/* readonly attribute string filename; */
-NS_IMETHODIMP XPCJSStackFrame::GetFilename(char * *aFilename)
-{
-    XPC_STRING_GETTER_BODY(aFilename, mFilename);
-}
-
-/* readonly attribute string name; */
-NS_IMETHODIMP XPCJSStackFrame::GetName(char * *aFunction)
-{
-    XPC_STRING_GETTER_BODY(aFunction, mFunname);
-}
-
-/* readonly attribute int32_t lineNumber; */
-NS_IMETHODIMP XPCJSStackFrame::GetLineNumber(int32_t *aLineNumber)
-{
-    *aLineNumber = mLineno;
-    return NS_OK;
-}
-
-/* readonly attribute string sourceLine; */
-NS_IMETHODIMP XPCJSStackFrame::GetSourceLine(char * *aSourceLine)
-{
-    *aSourceLine = nullptr;
-    return NS_OK;
-}
-
-/* readonly attribute nsIStackFrame caller; */
-NS_IMETHODIMP XPCJSStackFrame::GetCaller(nsIStackFrame * *aCaller)
-{
-    NS_IF_ADDREF(*aCaller = mCaller);
-    return NS_OK;
-}
-
-/* string toString (); */
-NS_IMETHODIMP XPCJSStackFrame::ToString(char **_retval)
-{
-    const char* frametype = IsJSFrame() ? "JS" : "native";
-    const char* filename = mFilename ? mFilename : "<unknown filename>";
-    const char* funname = mFunname ? mFunname : "<TOP_LEVEL>";
-    static const char format[] = "%s frame :: %s :: %s :: line %d";
-    int len = sizeof(char)*
-                (strlen(frametype) + strlen(filename) + strlen(funname)) +
-              sizeof(format) + 6 /* space for lineno */;
-
-    char* buf = (char*) nsMemory::Alloc(len);
-    if (!buf)
-        return NS_ERROR_OUT_OF_MEMORY;
-
-    JS_snprintf(buf, len, format, frametype, filename, funname, mLineno);
-    *_retval = buf;
-    return NS_OK;
-}
-
--- a/js/xpconnect/src/XPCThrower.cpp
+++ b/js/xpconnect/src/XPCThrower.cpp
@@ -5,31 +5,34 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* Code for throwing errors into JavaScript. */
 
 #include "xpcprivate.h"
 #include "xpcpublic.h"
 #include "XPCWrapper.h"
 #include "jsprf.h"
-#include "nsDOMException.h"
 #include "mozilla/dom/BindingUtils.h"
+#include "mozilla/dom/Exceptions.h"
+
+using namespace mozilla;
+using namespace mozilla::dom;
 
 bool XPCThrower::sVerbose = true;
 
 // static
 void
 XPCThrower::Throw(nsresult rv, JSContext* cx)
 {
     const char* format;
     if (JS_IsExceptionPending(cx))
         return;
     if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &format))
         format = "";
-    BuildAndThrowException(cx, rv, format);
+    dom::Throw(cx, rv, format);
 }
 
 namespace xpc {
 
 bool
 Throw(JSContext *cx, nsresult rv)
 {
     XPCThrower::Throw(rv, cx);
@@ -51,17 +54,17 @@ XPCThrower::CheckForPendingException(nsr
     if (!e)
         return false;
     XPCJSRuntime::Get()->SetPendingException(nullptr);
 
     nsresult e_result;
     if (NS_FAILED(e->GetResult(&e_result)) || e_result != result)
         return false;
 
-    if (!ThrowExceptionObject(cx, static_cast<nsXPCException*>(e.get())))
+    if (!ThrowExceptionObject(cx, e))
         JS_ReportOutOfMemory(cx);
     return true;
 }
 
 // static
 void
 XPCThrower::Throw(nsresult rv, XPCCallContext& ccx)
 {
@@ -74,17 +77,17 @@ XPCThrower::Throw(nsresult rv, XPCCallCo
     if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &format))
         format = "";
 
     sz = (char*) format;
 
     if (sz && sVerbose)
         Verbosify(ccx, &sz, false);
 
-    BuildAndThrowException(ccx, rv, sz);
+    dom::Throw(ccx, rv, sz);
 
     if (sz && sz != format)
         JS_smprintf_free(sz);
 }
 
 
 // static
 void
@@ -112,17 +115,17 @@ XPCThrower::ThrowBadResult(nsresult rv, 
     if (nsXPCException::NameAndFormatForNSResult(result, &name, nullptr) && name)
         sz = JS_smprintf("%s 0x%x (%s)", format, result, name);
     else
         sz = JS_smprintf("%s 0x%x", format, result);
 
     if (sz && sVerbose)
         Verbosify(ccx, &sz, true);
 
-    BuildAndThrowException(ccx, result, sz);
+    dom::Throw(ccx, result, sz);
 
     if (sz)
         JS_smprintf_free(sz);
 }
 
 // static
 void
 XPCThrower::ThrowBadParam(nsresult rv, unsigned paramNum, XPCCallContext& ccx)
@@ -133,17 +136,17 @@ XPCThrower::ThrowBadParam(nsresult rv, u
     if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &format))
         format = "";
 
     sz = JS_smprintf("%s arg %d", format, paramNum);
 
     if (sz && sVerbose)
         Verbosify(ccx, &sz, true);
 
-    BuildAndThrowException(ccx, rv, sz);
+    dom::Throw(ccx, rv, sz);
 
     if (sz)
         JS_smprintf_free(sz);
 }
 
 
 // static
 void
@@ -164,109 +167,8 @@ XPCThrower::Verbosify(XPCCallContext& cc
     }
 
     if (sz) {
         if (own)
             JS_smprintf_free(*psz);
         *psz = sz;
     }
 }
-
-// static
-void
-XPCThrower::BuildAndThrowException(JSContext* cx, nsresult rv, const char* sz)
-{
-    bool success = false;
-
-    /* no need to set an expection if the security manager already has */
-    if (rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO && JS_IsExceptionPending(cx))
-        return;
-
-    XPCJSRuntime* runtime = XPCJSRuntime::Get();
-    nsCOMPtr<nsIException> existingException = runtime->GetPendingException();
-    if (existingException) {
-        nsresult nr;
-        if (NS_SUCCEEDED(existingException->GetResult(&nr)) && 
-            rv == nr) {
-            // Just reuse the existing exception.
-            return;
-        }
-    }
-
-    nsRefPtr<nsXPCException> finalException;
-
-    // Do we use DOM exceptions for this error code?
-    switch (NS_ERROR_GET_MODULE(rv)) {
-    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:
-        if (NS_IsMainThread()) {
-            finalException = NS_NewDOMException(rv);
-        }
-        break;
-
-    default:
-        break;
-    }
-
-    // If not, use the default.
-    if (finalException == nullptr) {
-        finalException = new nsXPCException(sz, rv, nullptr, nullptr, nullptr);
-    }
-
-    MOZ_ASSERT(finalException);
-    success = ThrowExceptionObject(cx, finalException);
-    // If we weren't able to throw an exception we're
-    // most likely out of memory
-    if (!success) {
-        JS_ReportOutOfMemory(cx);
-    }
-}
-
-static bool
-IsCallerChrome(JSContext* cx)
-{
-    nsresult rv;
-
-    nsCOMPtr<nsIScriptSecurityManager> secMan;
-    secMan = XPCWrapper::GetSecurityManager();
-
-    if (!secMan)
-        return false;
-
-    bool isChrome;
-    rv = secMan->SubjectPrincipalIsSystem(&isChrome);
-    return NS_SUCCEEDED(rv) && isChrome;
-}
-
-// static
-bool
-XPCThrower::ThrowExceptionObject(JSContext* cx, nsXPCException* e)
-{
-    bool success = false;
-    if (e) {
-        JS::RootedValue thrown(cx);
-
-        // 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.
-        if (!IsCallerChrome(cx) &&
-            NS_SUCCEEDED(e->StealJSVal(thrown.address()))) {
-            if (!JS_WrapValue(cx, thrown.address()))
-                return false;
-            JS_SetPendingException(cx, thrown);
-            success = true;
-        } else {
-            JS::RootedObject glob(cx, JS::CurrentGlobalOrNull(cx));
-            if (!glob)
-                return false;
-
-            JS::RootedValue val(cx);
-            if (mozilla::dom::WrapNewBindingObject(cx, glob, e, &val)) {
-                JS_SetPendingException(cx, val);
-                success = true;
-            }
-        }
-    }
-    return success;
-}
--- a/js/xpconnect/src/XPCWrappedJSClass.cpp
+++ b/js/xpconnect/src/XPCWrappedJSClass.cpp
@@ -12,16 +12,17 @@
 #include "nsArrayEnumerator.h"
 #include "nsContentUtils.h"
 #include "nsWrapperCache.h"
 #include "XPCWrapper.h"
 #include "AccessCheck.h"
 #include "nsJSUtils.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/dom/BindingUtils.h"
+#include "mozilla/dom/DOMException.h"
 #include "mozilla/dom/DOMExceptionBinding.h"
 
 #include "jsapi.h"
 #include "jsfriendapi.h"
 
 using namespace xpc;
 using namespace JS;
 
@@ -259,17 +260,17 @@ nsXPCWrappedJSClass::CallQueryInterfaceO
             RootedValue jsexception(cx, NullValue());
 
             if (JS_GetPendingException(cx, jsexception.address())) {
                 nsresult rv;
                 if (jsexception.isObject()) {
                     // XPConnect may have constructed an object to represent a
                     // C++ QI failure. See if that is the case.
                     using namespace mozilla::dom;
-                    nsXPCException *e = nullptr;
+                    Exception *e = nullptr;
                     UNWRAP_OBJECT(Exception, cx, &jsexception.toObject(), e);
 
                     if (e &&
                         NS_SUCCEEDED(e->GetResult(&rv)) &&
                         rv == NS_NOINTERFACE) {
                         JS_ClearPendingException(cx);
                     }
                 } else if (JSVAL_IS_NUMBER(jsexception)) {
--- a/js/xpconnect/src/moz.build
+++ b/js/xpconnect/src/moz.build
@@ -29,17 +29,16 @@ CPP_SOURCES += [
     'XPCJSRuntime.cpp',
     'XPCJSWeakReference.cpp',
     'XPCLocale.cpp',
     'XPCLog.cpp',
     'XPCMaps.cpp',
     'XPCModule.cpp',
     'XPCQuickStubs.cpp',
     'XPCRuntimeService.cpp',
-    'XPCStack.cpp',
     'XPCString.cpp',
     'XPCThrower.cpp',
     'XPCVariant.cpp',
     'XPCWrappedJS.cpp',
     'XPCWrappedJSClass.cpp',
     'XPCWrappedNative.cpp',
     'XPCWrappedNativeInfo.cpp',
     'XPCWrappedNativeJSOps.cpp',
--- a/js/xpconnect/src/nsXPConnect.cpp
+++ b/js/xpconnect/src/nsXPConnect.cpp
@@ -31,16 +31,17 @@
 
 #ifdef MOZ_JSDEBUGGER
 #include "jsdIDebuggerService.h"
 #endif
 
 #include "XPCQuickStubs.h"
 
 #include "mozilla/dom/BindingUtils.h"
+#include "mozilla/dom/Exceptions.h"
 #include "mozilla/dom/IDBIndexBinding.h"
 #include "mozilla/dom/IDBObjectStoreBinding.h"
 #include "mozilla/dom/IDBOpenDBRequestBinding.h"
 #include "mozilla/dom/IDBRequestBinding.h"
 #include "mozilla/dom/IDBTransactionBinding.h"
 #include "mozilla/dom/IDBVersionChangeEventBinding.h"
 #include "mozilla/dom/TextDecoderBinding.h"
 #include "mozilla/dom/TextEncoderBinding.h"
@@ -858,35 +859,36 @@ nsXPConnect::CreateStackFrameLocation(ui
                                       const char *aFilename,
                                       const char *aFunctionName,
                                       int32_t aLineNumber,
                                       nsIStackFrame *aCaller,
                                       nsIStackFrame **_retval)
 {
     MOZ_ASSERT(_retval, "bad param");
 
-    return XPCJSStack::CreateStackFrameLocation(aLanguage,
-                                                aFilename,
-                                                aFunctionName,
-                                                aLineNumber,
-                                                aCaller,
-                                                _retval);
+    nsCOMPtr<nsIStackFrame> stackFrame =
+        exceptions::CreateStackFrameLocation(aLanguage,
+                                             aFilename,
+                                             aFunctionName,
+                                             aLineNumber,
+                                             aCaller);
+    stackFrame.forget(_retval);
+    return NS_OK;
 }
 
 /* readonly attribute nsIStackFrame CurrentJSStack; */
 NS_IMETHODIMP
 nsXPConnect::GetCurrentJSStack(nsIStackFrame * *aCurrentJSStack)
 {
     MOZ_ASSERT(aCurrentJSStack, "bad param");
     *aCurrentJSStack = nullptr;
 
     // is there a current context available?
     if (JSContext *cx = GetCurrentJSContext()) {
-        nsCOMPtr<nsIStackFrame> stack;
-        XPCJSStack::CreateStack(cx, getter_AddRefs(stack));
+        nsCOMPtr<nsIStackFrame> stack = exceptions::CreateStack(cx);
         if (stack) {
             // 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))) &&
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -734,27 +734,16 @@ public:
     void DebugDump(int16_t depth);
 
     void SystemIsBeingShutDown();
 
     PRThread* GetThreadRunningGC() const {return mThreadRunningGC;}
 
     ~XPCJSRuntime();
 
-    already_AddRefed<nsIException> GetPendingException()
-    {
-        nsCOMPtr<nsIException> out = mPendingException;
-        return out.forget();
-    }
-
-    void SetPendingException(nsIException* aException)
-    {
-        mPendingException = aException;
-    }
-
     XPCReadableJSStringWrapper *NewStringWrapper(const PRUnichar *str, uint32_t len);
     void DeleteString(nsAString *string);
 
 #ifdef XPC_CHECK_WRAPPERS_AT_SHUTDOWN
    void DEBUG_AddWrappedNative(nsIXPConnectWrappedNative* wrapper)
         {XPCAutoLock lock(GetMapLock());
          PLDHashEntryHdr *entry =
             PL_DHashTableOperate(DEBUG_WrappedNativeHashtable,
@@ -826,18 +815,16 @@ private:
     nsTArray<xpcContextCallback> extraContextCallbacks;
     nsRefPtr<WatchdogManager> mWatchdogManager;
     JS::GCSliceCallback mPrevGCSliceCallback;
     JSObject* mJunkScope;
     nsRefPtr<AsyncFreeSnowWhite> mAsyncSnowWhiteFreer;
 
     mozilla::TimeStamp mSlowScriptCheckpoint;
 
-    nsCOMPtr<nsIException>   mPendingException;
-
 #define XPCCCX_STRING_CACHE_SIZE 2
 
     // String wrapper entry, holds a string, and a boolean that tells
     // whether the string is in use or not.
     //
     // NB: The string is not stored by value so that we avoid the cost of
     // construction/destruction.
     struct StringWrapperEntry
@@ -2938,126 +2925,41 @@ class XPCThrower
 public:
     static void Throw(nsresult rv, JSContext* cx);
     static void Throw(nsresult rv, XPCCallContext& ccx);
     static void ThrowBadResult(nsresult rv, nsresult result, XPCCallContext& ccx);
     static void ThrowBadParam(nsresult rv, unsigned paramNum, XPCCallContext& ccx);
     static bool SetVerbosity(bool state)
         {bool old = sVerbose; sVerbose = state; return old;}
 
-    static void BuildAndThrowException(JSContext* cx, nsresult rv, const char* sz);
     static bool CheckForPendingException(nsresult result, JSContext *cx);
 
 private:
     static void Verbosify(XPCCallContext& ccx,
                           char** psz, bool own);
 
-    static bool ThrowExceptionObject(JSContext* cx, nsXPCException* e);
-
 private:
     static bool sVerbose;
 };
 
-
 /***************************************************************************/
 
-class XPCJSStack
+class nsXPCException
 {
 public:
-    static nsresult
-    CreateStack(JSContext* cx, nsIStackFrame** stack);
-
-    static nsresult
-    CreateStackFrameLocation(uint32_t aLanguage,
-                             const char* aFilename,
-                             const char* aFunctionName,
-                             int32_t aLineNumber,
-                             nsIStackFrame* aCaller,
-                             nsIStackFrame** stack);
-private:
-    XPCJSStack();   // not implemented
-};
-
-/***************************************************************************/
-
-class nsXPCException :
-            public nsIXPCException,
-            public nsWrapperCache
-{
-public:
-    NS_DEFINE_STATIC_CID_ACCESSOR(NS_XPCEXCEPTION_CID)
-
-    NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsXPCException)
-
-    NS_DECL_CYCLE_COLLECTING_ISUPPORTS
-    NS_DECL_NSIEXCEPTION
-    NS_DECL_NSIXPCEXCEPTION
-
     static bool NameAndFormatForNSResult(nsresult rv,
                                          const char** name,
                                          const char** format);
 
     static const void* IterateNSResults(nsresult* rv,
                                         const char** name,
                                         const char** format,
                                         const void** iterp);
 
     static uint32_t GetNSResultCount();
-
-    nsXPCException(const char *aMessage,
-                   nsresult aResult,
-                   const char *aName,
-                   nsIStackFrame *aLocation,
-                   nsISupports *aData);
-    // XPCOM factory ctor.
-    nsXPCException();
-    virtual ~nsXPCException();
-
-    virtual JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> scope)
-      MOZ_OVERRIDE;
-
-    nsISupports* GetParentObject() const { return nullptr; }
-
-    void GetMessageMoz(nsString& retval);
-
-    uint32_t Result() const;
-
-    void GetName(nsString& retval);
-
-    void GetFilename(nsString& retval);
-
-    uint32_t LineNumber() const;
-
-    uint32_t ColumnNumber() const;
-
-    already_AddRefed<nsIStackFrame> GetLocation() const;
-
-    already_AddRefed<nsISupports> GetInner() const;
-
-    already_AddRefed<nsISupports> GetData() const;
-
-    void Stringify(nsString& retval);
-
-protected:
-    void Reset();
-
-    char*           mMessage;
-    nsresult        mResult;
-    char*           mName;
-    nsIStackFrame*  mLocation;
-    nsISupports*    mData;
-    char*           mFilename;
-    int             mLineNumber;
-    nsIException*   mInner;
-    bool            mInitialized;
-
-    nsAutoJSValHolder mThrownJSVal;
-
-private:
-    static bool sEverMadeOneFromFactory;
 };
 
 /***************************************************************************/
 /*
 * nsJSID implements nsIJSID. It is also used by nsJSIID and nsJSCID as a
 * member (as a hidden implementaion detail) to which they delegate many calls.
 */
 
--- a/layout/build/nsLayoutModule.cpp
+++ b/layout/build/nsLayoutModule.cpp
@@ -63,33 +63,33 @@
 #include "nsXMLHttpRequest.h"
 #include "nsChannelPolicy.h"
 
 // view stuff
 #include "nsContentCreatorFunctions.h"
 
 // DOM includes
 #include "nsDOMBlobBuilder.h"
-#include "nsDOMException.h"
 #include "nsDOMFileReader.h"
 
 #include "nsFormData.h"
 #include "nsHostObjectProtocolHandler.h"
 #include "nsHostObjectURI.h"
 #include "nsGlobalWindowCommands.h"
 #include "nsIControllerCommandTable.h"
 #include "nsJSProtocolHandler.h"
 #include "nsScriptNameSpaceManager.h"
 #include "nsIControllerContext.h"
 #include "nsDOMScriptObjectFactory.h"
 #include "DOMStorageManager.h"
 #include "nsJSON.h"
 #include "mozIApplicationClearPrivateDataParams.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/dom/Activity.h"
+#include "mozilla/dom/DOMException.h"
 #include "mozilla/dom/DOMRequest.h"
 #include "mozilla/dom/EventSource.h"
 #include "mozilla/dom/indexedDB/IndexedDatabaseManager.h"
 #include "mozilla/dom/network/TCPSocketChild.h"
 #include "mozilla/dom/network/TCPSocketParent.h"
 #include "mozilla/dom/network/TCPServerSocketChild.h"
 #include "mozilla/dom/quota/QuotaManager.h"
 #include "mozilla/OSFileConstants.h"
@@ -270,16 +270,17 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(Activity)
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsDOMFileReader, Init)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsFormData)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsBlobProtocolHandler)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMediaStreamProtocolHandler)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMediaSourceProtocolHandler)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsFontTableProtocolHandler)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsHostObjectURI)
 NS_GENERIC_FACTORY_CONSTRUCTOR(DOMParser)
+NS_GENERIC_FACTORY_CONSTRUCTOR(Exception)
 NS_GENERIC_FACTORY_CONSTRUCTOR(DOMSessionStorageManager)
 NS_GENERIC_FACTORY_CONSTRUCTOR(DOMLocalStorageManager)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsChannelPolicy)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(IndexedDatabaseManager,
                                          IndexedDatabaseManager::FactoryCreate)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(DOMRequestService,
                                          DOMRequestService::FactoryCreate)
 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(QuotaManager,
@@ -1022,16 +1023,17 @@ static const mozilla::Module::CIDEntry k
   { &kNS_MEDIASTREAMPROTOCOLHANDLER_CID, false, NULL, nsMediaStreamProtocolHandlerConstructor },
   { &kNS_MEDIASOURCEPROTOCOLHANDLER_CID, false, NULL, nsMediaSourceProtocolHandlerConstructor },
   { &kNS_FONTTABLEPROTOCOLHANDLER_CID, false, NULL, nsFontTableProtocolHandlerConstructor },
   { &kNS_HOSTOBJECTURI_CID, false, NULL, nsHostObjectURIConstructor },
   { &kNS_XMLHTTPREQUEST_CID, false, NULL, nsXMLHttpRequestConstructor },
   { &kNS_EVENTSOURCE_CID, false, NULL, EventSourceConstructor },
   { &kNS_DOMACTIVITY_CID, false, NULL, ActivityConstructor },
   { &kNS_DOMPARSER_CID, false, NULL, DOMParserConstructor },
+  { &kNS_XPCEXCEPTION_CID, false, NULL, ExceptionConstructor },
   { &kNS_DOMSESSIONSTORAGEMANAGER_CID, false, NULL, DOMSessionStorageManagerConstructor },
   { &kNS_DOMLOCALSTORAGEMANAGER_CID, false, NULL, DOMLocalStorageManagerConstructor },
   { &kNS_DOMJSON_CID, false, NULL, NS_NewJSON },
   { &kNS_TEXTEDITOR_CID, false, NULL, nsPlaintextEditorConstructor },
   { &kINDEXEDDB_MANAGER_CID, false, NULL, IndexedDatabaseManagerConstructor },
   { &kDOMREQUEST_SERVICE_CID, false, NULL, DOMRequestServiceConstructor },
   { &kQUOTA_MANAGER_CID, false, NULL, QuotaManagerConstructor },
 #ifdef MOZ_B2G_RIL
@@ -1176,16 +1178,17 @@ static const mozilla::Module::ContractID
   { NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX BLOBURI_SCHEME, &kNS_BLOBPROTOCOLHANDLER_CID },
   { NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX MEDIASTREAMURI_SCHEME, &kNS_MEDIASTREAMPROTOCOLHANDLER_CID },
   { NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX MEDIASOURCEURI_SCHEME, &kNS_MEDIASOURCEPROTOCOLHANDLER_CID },
   { NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX FONTTABLEURI_SCHEME, &kNS_FONTTABLEPROTOCOLHANDLER_CID },
   { NS_XMLHTTPREQUEST_CONTRACTID, &kNS_XMLHTTPREQUEST_CID },
   { NS_EVENTSOURCE_CONTRACTID, &kNS_EVENTSOURCE_CID },
   { NS_DOMACTIVITY_CONTRACTID, &kNS_DOMACTIVITY_CID },
   { NS_DOMPARSER_CONTRACTID, &kNS_DOMPARSER_CID },
+  { XPC_EXCEPTION_CONTRACTID, &kNS_XPCEXCEPTION_CID },
   { "@mozilla.org/dom/localStorage-manager;1", &kNS_DOMLOCALSTORAGEMANAGER_CID },
   // Keeping the old ContractID for backward compatibility
   { "@mozilla.org/dom/storagemanager;1", &kNS_DOMLOCALSTORAGEMANAGER_CID },
   { "@mozilla.org/dom/sessionStorage-manager;1", &kNS_DOMSESSIONSTORAGEMANAGER_CID },
   { "@mozilla.org/dom/json;1", &kNS_DOMJSON_CID },
   { "@mozilla.org/editor/texteditor;1", &kNS_TEXTEDITOR_CID },
   { INDEXEDDB_MANAGER_CONTRACTID, &kINDEXEDDB_MANAGER_CID },
   { DOMREQUEST_SERVICE_CONTRACTID, &kDOMREQUEST_SERVICE_CID },
--- a/xpcom/base/CycleCollectedJSRuntime.cpp
+++ b/xpcom/base/CycleCollectedJSRuntime.cpp
@@ -59,16 +59,17 @@
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/dom/BindingUtils.h"
 #include "mozilla/dom/DOMJSClass.h"
 #include "jsprf.h"
 #include "nsCycleCollectionNoteRootCallback.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsCycleCollector.h"
 #include "nsDOMJSUtils.h"
+#include "nsIException.h"
 #include "nsThreadUtils.h"
 #include "xpcpublic.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 namespace mozilla {
 
@@ -458,16 +459,19 @@ CycleCollectedJSRuntime::CycleCollectedJ
   nsCycleCollector_registerJSRuntime(this);
 }
 
 CycleCollectedJSRuntime::~CycleCollectedJSRuntime()
 {
   MOZ_ASSERT(!mDeferredFinalizerTable.Count());
   MOZ_ASSERT(!mDeferredSupports.Length());
 
+  // Clear mPendingException first, since it might be cycle collected.
+  mPendingException = nullptr;
+
   nsCycleCollector_forgetJSRuntime();
 
   JS_DestroyRuntime(mJSRuntime);
   mJSRuntime = nullptr;
 }
 
 size_t
 CycleCollectedJSRuntime::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
@@ -823,16 +827,29 @@ CycleCollectedJSRuntime::AssertNoObjects
 {
   nsScriptObjectTracer* tracer = mJSHolders.Get(aPossibleJSHolder);
   if (tracer) {
     tracer->Trace(aPossibleJSHolder, TraceCallbackFunc(AssertNoGcThing), nullptr);
   }
 }
 #endif
 
+already_AddRefed<nsIException>
+CycleCollectedJSRuntime::GetPendingException() const
+{
+  nsCOMPtr<nsIException> out = mPendingException;
+  return out.forget();
+}
+
+void
+CycleCollectedJSRuntime::SetPendingException(nsIException* aException)
+{
+  mPendingException = aException;
+}
+
 nsCycleCollectionParticipant*
 CycleCollectedJSRuntime::GCThingParticipant()
 {
     return &mGCThingCycleCollectorGlobal;
 }
 
 nsCycleCollectionParticipant*
 CycleCollectedJSRuntime::ZoneParticipant()
--- a/xpcom/base/CycleCollectedJSRuntime.h
+++ b/xpcom/base/CycleCollectedJSRuntime.h
@@ -14,16 +14,17 @@
 #include "nsCycleCollector.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsDataHashtable.h"
 #include "nsHashKeys.h"
 #include "nsTArray.h"
 
 class nsCycleCollectionNoteRootCallback;
 class nsScriptObjectTracer;
+class nsIException;
 
 namespace mozilla {
 
 class JSGCThingParticipant: public nsCycleCollectionParticipant
 {
 public:
   NS_IMETHOD Root(void *n)
   {
@@ -167,16 +168,19 @@ public:
   void AddJSHolder(void* aHolder, nsScriptObjectTracer* aTracer);
   void RemoveJSHolder(void* aHolder);
 #ifdef DEBUG
   bool IsJSHolder(void* aHolder);
   void SetObjectToUnlink(void* aObject) { mObjectToUnlink = aObject; }
   void AssertNoObjectsToTrace(void* aPossibleJSHolder);
 #endif
 
+  already_AddRefed<nsIException> GetPendingException() const;
+  void SetPendingException(nsIException* aException);
+
   nsCycleCollectionParticipant* GCThingParticipant();
   nsCycleCollectionParticipant* ZoneParticipant();
 
   nsresult BeginCycleCollection(nsCycleCollectionNoteRootCallback &aCb);
   bool UsefulToMergeZones() const;
   void FixWeakMappingGrayBits() const;
   bool NeedCollect() const;
   void Collect(uint32_t reason) const;
@@ -193,32 +197,40 @@ public:
   
   virtual void DispatchDeferredDeletion(bool aContinuation) = 0;
 
   JSRuntime* Runtime() const
   {
     MOZ_ASSERT(mJSRuntime);
     return mJSRuntime;
   }
+
+  // Get the current thread's CycleCollectedJSRuntime.  Returns null if there
+  // isn't one.
+  static CycleCollectedJSRuntime*
+  Get();
+
 private:
   JSGCThingParticipant mGCThingCycleCollectorGlobal;
 
   JSZoneParticipant mJSZoneCycleCollectorGlobal;
 
   JSRuntime* mJSRuntime;
 
   nsDataHashtable<nsPtrHashKey<void>, nsScriptObjectTracer*> mJSHolders;
 
   nsTArray<nsISupports*> mDeferredSupports;
   typedef nsDataHashtable<nsFuncPtrHashKey<DeferredFinalizeFunction>, void*>
     DeferredFinalizerTable;
   DeferredFinalizerTable mDeferredFinalizerTable;
 
   nsRefPtr<IncrementalFinalizeRunnable> mFinalizeRunnable;
 
+  nsCOMPtr<nsIException> mPendingException;
+
 #ifdef DEBUG
   void* mObjectToUnlink;
 #endif
 };
 
 } // namespace mozilla
 
 #endif // mozilla_CycleCollectedJSRuntime_h__
--- a/xpcom/base/nsCycleCollector.cpp
+++ b/xpcom/base/nsCycleCollector.cpp
@@ -2898,18 +2898,18 @@ nsCycleCollector_forgetJSRuntime()
         data->mRuntime = nullptr;
     } else {
         data->mRuntime = nullptr;
         delete data;
         sCollectorData.set(nullptr);
     }
 }
 
-mozilla::CycleCollectedJSRuntime*
-nsCycleCollector_currentJSRuntime()
+/* static */ CycleCollectedJSRuntime*
+CycleCollectedJSRuntime::Get()
 {
     CollectorData* data = sCollectorData.get();
     if (data) {
         return data->mRuntime;
     }
     return nullptr;
 }
 
--- a/xpcom/base/nsCycleCollector.h
+++ b/xpcom/base/nsCycleCollector.h
@@ -59,17 +59,16 @@ void nsCycleCollector_collect(bool aManu
                               nsCycleCollectorResults *aResults,
                               nsICycleCollectorListener *aListener);
 uint32_t nsCycleCollector_suspectedCount();
 void nsCycleCollector_shutdown();
 
 // Helpers for interacting with JS
 void nsCycleCollector_registerJSRuntime(mozilla::CycleCollectedJSRuntime *aRt);
 void nsCycleCollector_forgetJSRuntime();
-mozilla::CycleCollectedJSRuntime* nsCycleCollector_currentJSRuntime();
 
 #define NS_CYCLE_COLLECTOR_LOGGER_CID \
 { 0x58be81b4, 0x39d2, 0x437c, \
 { 0x94, 0xea, 0xae, 0xde, 0x2c, 0x62, 0x08, 0xd3 } }
 
 extern nsresult
 nsCycleCollectorLoggerConstructor(nsISupports* outer,
                                   const nsIID& aIID,