From 75933a51dc2ac357db39687a08789130295d3c6e Mon Sep 17 00:00:00 2001 From: "bzbarsky%mit.edu" Date: Wed, 6 Jul 2005 15:46:12 +0000 Subject: [PATCH] Make sure to notify XPConnect if an XBL constructor or destructor throws an exception so they get reported in a timely manner. Bug 299741, r+sr=jst, a=chofmann --- content/base/public/nsContentUtils.h | 5 +++++ content/base/src/nsContentUtils.cpp | 14 +++++++++++++ content/xbl/src/nsXBLProtoImplMethod.cpp | 4 ++++ dom/src/base/nsJSEnvironment.cpp | 25 +++++------------------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/content/base/public/nsContentUtils.h b/content/base/public/nsContentUtils.h index d65d0c97b55a..46b9f26097bc 100644 --- a/content/base/public/nsContentUtils.h +++ b/content/base/public/nsContentUtils.h @@ -539,6 +539,11 @@ public: */ static PRBool IsChromeDoc(nsIDocument *aDocument); + /** + * Notify XPConnect if an exception is pending on aCx. + */ + static void NotifyXPCIfExceptionPending(JSContext *aCx); + private: static nsresult doReparentContentWrapper(nsIContent *aChild, nsIDocument *aNewDocument, diff --git a/content/base/src/nsContentUtils.cpp b/content/base/src/nsContentUtils.cpp index 6efc71980bfb..61e642f15763 100644 --- a/content/base/src/nsContentUtils.cpp +++ b/content/base/src/nsContentUtils.cpp @@ -2334,3 +2334,17 @@ nsContentUtils::IsChromeDoc(nsIDocument *aDocument) return principal == systemPrincipal; } + +void +nsContentUtils::NotifyXPCIfExceptionPending(JSContext* aCx) +{ + if (!::JS_IsExceptionPending(aCx)) { + return; + } + + nsCOMPtr nccx; + XPConnect()->GetCurrentNativeCallContext(getter_AddRefs(nccx)); + if (nccx) { + nccx->SetExceptionWasThrown(PR_TRUE); + } +} diff --git a/content/xbl/src/nsXBLProtoImplMethod.cpp b/content/xbl/src/nsXBLProtoImplMethod.cpp index 0155c4dd8f54..38220786cd1b 100644 --- a/content/xbl/src/nsXBLProtoImplMethod.cpp +++ b/content/xbl/src/nsXBLProtoImplMethod.cpp @@ -329,6 +329,10 @@ nsXBLProtoImplAnonymousMethod::Execute(nsIContent* aBoundElement) } if (!ok) { + // Tell XPConnect about any pending exceptions. This is needed + // to avoid dropping JS exceptions in case we got here through + // nested calls through XPConnect. + nsContentUtils::NotifyXPCIfExceptionPending(cx); return NS_ERROR_FAILURE; } diff --git a/dom/src/base/nsJSEnvironment.cpp b/dom/src/base/nsJSEnvironment.cpp index 2a7884d04655..e0912b3d4d96 100644 --- a/dom/src/base/nsJSEnvironment.cpp +++ b/dom/src/base/nsJSEnvironment.cpp @@ -476,21 +476,6 @@ LocaleCompare(JSContext *cx, JSString *src1, JSString *src2, jsval *rval) return JS_TRUE; } -static void -NotifyXPCIfExceptionPending(JSContext *cx) -{ - if (!::JS_IsExceptionPending(cx)) { - return; - } - - nsCOMPtr nccx; - nsContentUtils::XPConnect()-> - GetCurrentNativeCallContext(getter_AddRefs(nccx)); - if (nccx) { - nccx->SetExceptionWasThrown(PR_TRUE); - } -} - // The number of branch callbacks between calls to JS_MaybeGC #define MAYBE_GC_BRANCH_COUNT_MASK 0x00000fff // 4095 @@ -888,7 +873,7 @@ nsJSContext::EvaluateStringWithValue(const nsAString& aScript, // to avoid dropping JS exceptions in case we got here through // nested calls through XPConnect. - NotifyXPCIfExceptionPending(mContext); + nsContentUtils::NotifyXPCIfExceptionPending(mContext); } } } @@ -975,7 +960,7 @@ JSValueToAString(JSContext *cx, jsval val, nsAString *result, // avoid dropping JS exceptions in case we got here through nested // calls through XPConnect. - NotifyXPCIfExceptionPending(cx); + nsContentUtils::NotifyXPCIfExceptionPending(cx); } return NS_OK; @@ -1083,7 +1068,7 @@ nsJSContext::EvaluateString(const nsAString& aScript, // to avoid dropping JS exceptions in case we got here through // nested calls through XPConnect. - NotifyXPCIfExceptionPending(mContext); + nsContentUtils::NotifyXPCIfExceptionPending(mContext); } } } @@ -1246,7 +1231,7 @@ nsJSContext::ExecuteScript(void* aScriptObject, // avoid dropping JS exceptions in case we got here through nested // calls through XPConnect. - NotifyXPCIfExceptionPending(mContext); + nsContentUtils::NotifyXPCIfExceptionPending(mContext); } // Pop here, after JS_ValueToString and any other possible evaluation. @@ -1419,7 +1404,7 @@ nsJSContext::CallEventHandler(JSObject *aTarget, JSObject *aHandler, // to avoid dropping JS exceptions in case we got here through // nested calls through XPConnect. - NotifyXPCIfExceptionPending(mContext); + nsContentUtils::NotifyXPCIfExceptionPending(mContext); // Don't pass back results from failed calls. *rval = JSVAL_VOID;