Bug 1225219 Implement ErrorResult::CloneTo(). r=bz
authorBen Kelly <ben@wanderview.com>
Wed, 18 Nov 2015 11:07:58 -0800
changeset 309812 1b5636e3136518de47d25306bb5061e42b99d991
parent 309811 52d6ee1e55c922c78367528e8752fabe64ebab71
child 309813 cc82d9b8f949a60ba3ad9b88c8fca77513e4fa68
push id7649
push usermartin.thomson@gmail.com
push dateThu, 19 Nov 2015 00:06:17 +0000
reviewersbz
bugs1225219
milestone45.0a1
Bug 1225219 Implement ErrorResult::CloneTo(). r=bz
dom/bindings/BindingUtils.cpp
dom/bindings/ErrorResult.h
--- a/dom/bindings/BindingUtils.cpp
+++ b/dom/bindings/BindingUtils.cpp
@@ -462,16 +462,48 @@ ErrorResult::operator=(ErrorResult&& aRH
   // Note: It's important to do this last, since this affects the condition
   // checks above!
   mResult = aRHS.mResult;
   aRHS.mResult = NS_OK;
   return *this;
 }
 
 void
+ErrorResult::CloneTo(ErrorResult& aRv) const
+{
+  aRv.ClearUnionData();
+  aRv.mResult = mResult;
+#ifdef DEBUG
+  aRv.mMightHaveUnreportedJSException = mMightHaveUnreportedJSException;
+#endif
+
+  if (IsErrorWithMessage()) {
+#ifdef DEBUG
+    aRv.mUnionState = HasMessage;
+#endif
+    aRv.mMessage = new Message();
+    aRv.mMessage->mArgs = mMessage->mArgs;
+    aRv.mMessage->mErrorNumber = mMessage->mErrorNumber;
+  } else if (IsDOMException()) {
+#ifdef DEBUG
+    aRv.mUnionState = HasDOMExceptionInfo;
+#endif
+    aRv.mDOMExceptionInfo = new DOMExceptionInfo(mDOMExceptionInfo->mRv,
+                                                 mDOMExceptionInfo->mMessage);
+  } else if (IsJSException()) {
+#ifdef DEBUG
+    aRv.mUnionState = HasJSException;
+#endif
+    JSContext* cx = nsContentUtils::RootingCxForThread();
+    JS::Rooted<JS::Value> exception(cx, mJSException);
+    aRv.ThrowJSException(cx, exception);
+  }
+}
+
+void
 ErrorResult::SuppressException()
 {
   WouldReportJSException();
   ClearUnionData();
   // We don't use AssignErrorCode, because we want to override existing error
   // states, which AssignErrorCode is not allowed to do.
   mResult = NS_OK;
 }
--- a/dom/bindings/ErrorResult.h
+++ b/dom/bindings/ErrorResult.h
@@ -110,16 +110,21 @@ public:
     AssignErrorCode(aRv);
   }
 
   void Throw(nsresult rv) {
     MOZ_ASSERT(NS_FAILED(rv), "Please don't try throwing success");
     AssignErrorCode(rv);
   }
 
+  // Duplicate our current state on the given ErrorResult object.  Any existing
+  // errors or messages on the target will be suppressed before cloning.  Our
+  // own error state remains unchanged.
+  void CloneTo(ErrorResult& aRv) const;
+
   // Use SuppressException when you want to suppress any exception that might be
   // on the ErrorResult.  After this call, the ErrorResult will be back a "no
   // exception thrown" state.
   void SuppressException();
 
   // Use StealNSResult() when you want to safely convert the ErrorResult to an
   // nsresult that you will then return to a caller.  This will
   // SuppressException(), since there will no longer be a way to report it.