From 2e6de46ff151c9eb191b20d30ada7b7b5f742929 Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Wed, 5 Nov 2008 12:36:19 +1300 Subject: [PATCH] Backing out changeset dc1aff36a411 (bug 462389) to try to fix bustage --- dom/src/threads/nsDOMWorkerScriptLoader.cpp | 2 +- dom/src/threads/nsDOMWorkerScriptLoader.h | 8 +- js/src/xpconnect/public/Makefile.in | 1 - js/src/xpconnect/public/nsAutoJSValHolder.h | 112 ++++++++++---------- js/src/xpconnect/src/XPCDispTearOff.cpp | 2 +- js/src/xpconnect/src/xpcconvert.cpp | 25 ++--- js/src/xpconnect/src/xpcexception.cpp | 14 +-- js/src/xpconnect/src/xpcprivate.h | 12 +-- js/src/xpconnect/src/xpcthrower.cpp | 2 +- js/src/xpconnect/src/xpcwrappedjsclass.cpp | 2 +- 10 files changed, 89 insertions(+), 91 deletions(-) diff --git a/dom/src/threads/nsDOMWorkerScriptLoader.cpp b/dom/src/threads/nsDOMWorkerScriptLoader.cpp index 98a8d7bb629a..4cd5ffdc098c 100644 --- a/dom/src/threads/nsDOMWorkerScriptLoader.cpp +++ b/dom/src/threads/nsDOMWorkerScriptLoader.cpp @@ -672,7 +672,7 @@ nsDOMWorkerScriptLoader:: ScriptCompiler::ScriptCompiler(nsDOMWorkerScriptLoader* aLoader, const nsString& aScriptText, const nsCString& aFilename, - nsAutoJSValHolder& aScriptObj) + nsAutoJSObjectHolder& aScriptObj) : ScriptLoaderRunnable(aLoader), mScriptText(aScriptText), mFilename(aFilename), diff --git a/dom/src/threads/nsDOMWorkerScriptLoader.h b/dom/src/threads/nsDOMWorkerScriptLoader.h index 331bc6c611a5..e6ff88bc6d67 100644 --- a/dom/src/threads/nsDOMWorkerScriptLoader.h +++ b/dom/src/threads/nsDOMWorkerScriptLoader.h @@ -50,7 +50,7 @@ // Other includes #include "jsapi.h" #include "nsAutoPtr.h" -#include "nsAutoJSValHolder.h" +#include "nsAutoJSObjectHolder.h" #include "nsCOMPtr.h" #include "nsStringGlue.h" #include "nsTArray.h" @@ -162,12 +162,12 @@ private: ScriptCompiler(nsDOMWorkerScriptLoader* aLoader, const nsString& aScriptText, const nsCString& aFilename, - nsAutoJSValHolder& aScriptObj); + nsAutoJSObjectHolder& aScriptObj); private: nsString mScriptText; nsCString mFilename; - nsAutoJSValHolder& mScriptObj; + nsAutoJSObjectHolder& mScriptObj; }; class ScriptLoaderDone : public ScriptLoaderRunnable @@ -202,7 +202,7 @@ private: nsresult result; nsCOMPtr finalURI; nsCOMPtr channel; - nsAutoJSValHolder scriptObj; + nsAutoJSObjectHolder scriptObj; }; nsIThread* mTarget; diff --git a/js/src/xpconnect/public/Makefile.in b/js/src/xpconnect/public/Makefile.in index 9fb08aa49deb..213b7903d169 100644 --- a/js/src/xpconnect/public/Makefile.in +++ b/js/src/xpconnect/public/Makefile.in @@ -48,7 +48,6 @@ MODULE = xpconnect EXPORTS = \ nsAXPCNativeCallContext.h \ xpc_map_end.h \ - nsAutoJSValHolder.h \ $(NULL) include $(topsrcdir)/config/rules.mk diff --git a/js/src/xpconnect/public/nsAutoJSValHolder.h b/js/src/xpconnect/public/nsAutoJSValHolder.h index 142c35dda9ce..c6753bdfef6a 100644 --- a/js/src/xpconnect/public/nsAutoJSValHolder.h +++ b/js/src/xpconnect/public/nsAutoJSValHolder.h @@ -21,7 +21,6 @@ * * Contributor(s): * Ben Turner (Original Author) - * Ben Newman * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -37,33 +36,55 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef __NSAUTOJSVALHOLDER_H__ -#define __NSAUTOJSVALHOLDER_H__ +#ifndef __NSAUTOJSOBJECTHOLDER_H__ +#define __NSAUTOJSOBJECTHOLDER_H__ #include "jsapi.h" /** - * Simple class that looks and acts like a jsval except that it unroots + * Simple class that looks and acts like a JSObject* except that it unroots * itself automatically if Root() is ever called. Designed to be rooted on the - * context or runtime (but not both!). + * context or runtime (but not both!). Also automatically nulls its JSObject* + * on Unroot and asserts that Root has been called prior to assigning an object. */ -class nsAutoJSValHolder +class nsAutoJSObjectHolder { public: + /** + * Default constructor, no holding. + */ + nsAutoJSObjectHolder() + : mRt(NULL), mObj(NULL), mHeld(PR_FALSE) { } - nsAutoJSValHolder() - : mRt(NULL) - , mVal(JSVAL_NULL) - , mGCThing(NULL) - , mHeld(JS_FALSE) - { - // nothing to do + /** + * Hold by rooting on the context's runtime in the constructor, passing the + * result out. + */ + nsAutoJSObjectHolder(JSContext* aCx, JSBool* aRv = NULL, + JSObject* aObj = NULL) + : mRt(NULL), mObj(aObj), mHeld(JS_FALSE) { + JSBool rv = Hold(aCx); + if (aRv) { + *aRv = rv; + } + } + + /** + * Hold by rooting on the runtime in the constructor, passing the result out. + */ + nsAutoJSObjectHolder(JSRuntime* aRt, JSBool* aRv = NULL, + JSObject* aObj = NULL) + : mRt(aRt), mObj(aObj), mHeld(JS_FALSE) { + JSBool rv = Hold(aRt); + if (aRv) { + *aRv = rv; + } } /** * Always release on destruction. */ - virtual ~nsAutoJSValHolder() { + ~nsAutoJSObjectHolder() { Release(); } @@ -76,39 +97,29 @@ public: /** * Hold by rooting on the runtime. - * Note that mGCThing may be JSVAL_NULL, which is not a problem. */ JSBool Hold(JSRuntime* aRt) { if (!mHeld) { - if (JS_AddNamedRootRT(aRt, &mGCThing, "nsAutoJSValHolder")) { + mHeld = JS_AddNamedRootRT(aRt, &mObj, "nsAutoRootedJSObject"); + if (mHeld) { mRt = aRt; - mHeld = JS_TRUE; - } else { - Release(); // out of memory } } return mHeld; } /** - * Manually release, nullifying mVal, mGCThing, and mRt, but returning - * the original jsval. + * Manually release. */ - jsval Release() { + void Release() { NS_ASSERTION(!mHeld || mRt, "Bad!"); - - jsval oldval = mVal; - if (mHeld) { - JS_RemoveRootRT(mRt, &mGCThing); // infallible - mHeld = JS_FALSE; + mHeld = !JS_RemoveRootRT(mRt, &mObj); + if (!mHeld) { + mRt = NULL; + } + mObj = NULL; } - - mVal = JSVAL_NULL; - mGCThing = NULL; - mRt = NULL; - - return oldval; } /** @@ -121,44 +132,33 @@ public: /** * Pretend to be a JSObject*. */ - operator JSObject*() const { - return JSVAL_IS_OBJECT(mVal) - ? JSVAL_TO_OBJECT(mVal) - : JSVAL_NULL; + JSObject* get() const { + return mObj; } /** - * Pretend to be a jsval. + * Pretend to be a JSObject*. */ - operator jsval() const { return mVal; } - - nsAutoJSValHolder &operator=(JSObject* aOther) { -#ifdef DEBUG - if (aOther) { - NS_ASSERTION(mHeld, "Not rooted!"); - } -#endif - return *this = OBJECT_TO_JSVAL(aOther); + operator JSObject*() const { + return get(); } - nsAutoJSValHolder &operator=(jsval aOther) { + /** + * Pretend to be a JSObject*. Assert if not held. + */ + JSObject* operator=(JSObject* aOther) { #ifdef DEBUG if (aOther) { NS_ASSERTION(mHeld, "Not rooted!"); } #endif - mVal = aOther; - mGCThing = JSVAL_IS_GCTHING(aOther) - ? JSVAL_TO_GCTHING(aOther) - : NULL; - return *this; + return mObj = aOther; } private: JSRuntime* mRt; - jsval mVal; - void* mGCThing; + JSObject* mObj; JSBool mHeld; }; -#endif /* __NSAUTOJSVALHOLDER_H__ */ +#endif /* __NSAUTOJSOBJECTHOLDER_H__ */ diff --git a/js/src/xpconnect/src/XPCDispTearOff.cpp b/js/src/xpconnect/src/XPCDispTearOff.cpp index 595354b6e780..6e81df4394fa 100644 --- a/js/src/xpconnect/src/XPCDispTearOff.cpp +++ b/js/src/xpconnect/src/XPCDispTearOff.cpp @@ -465,7 +465,7 @@ pre_call_clean_up: nsCOMPtr e; XPCConvert::ConstructException(code, sz, "IDispatch", name.get(), - nsnull, getter_AddRefs(e), nsnull, nsnull); + nsnull, getter_AddRefs(e), nsnull); xpcc->SetException(e); if(sz) JS_smprintf_free(sz); diff --git a/js/src/xpconnect/src/xpcconvert.cpp b/js/src/xpconnect/src/xpcconvert.cpp index 69efbaba484a..c6c87a9e85c8 100644 --- a/js/src/xpconnect/src/xpcconvert.cpp +++ b/js/src/xpconnect/src/xpcconvert.cpp @@ -1384,11 +1384,8 @@ XPCConvert::ConstructException(nsresult rv, const char* message, const char* ifaceName, const char* methodName, nsISupports* data, nsIException** exceptn, - JSContext* cx, - jsval* jsExceptionPtr) + const jsval *jsExceptionPtr) { - NS_ASSERTION(!cx == !jsExceptionPtr, "Expected cx and jsExceptionPtr to cooccur."); - static const char format[] = "\'%s\' when calling method: [%s::%s]"; const char * msg = message; char* sz = nsnull; @@ -1410,11 +1407,11 @@ XPCConvert::ConstructException(nsresult rv, const char* message, nsresult res = nsXPCException::NewException(msg, rv, nsnull, data, exceptn); - if(NS_SUCCEEDED(res) && cx && jsExceptionPtr && *exceptn) + if(NS_SUCCEEDED(res) && jsExceptionPtr && *exceptn) { nsCOMPtr xpcEx = do_QueryInterface(*exceptn); if(xpcEx) - xpcEx->StowThrownJSVal(cx, *jsExceptionPtr); + xpcEx->SetThrownJSVal(*jsExceptionPtr); } if(sz) @@ -1485,7 +1482,7 @@ XPCConvert::JSValToXPCException(XPCCallContext& ccx, // it is a wrapped native, but not an exception! return ConstructException(NS_ERROR_XPC_JS_THREW_NATIVE_OBJECT, nsnull, ifaceName, methodName, supports, - exceptn, nsnull, nsnull); + exceptn, nsnull); } } else @@ -1543,7 +1540,7 @@ XPCConvert::JSValToXPCException(XPCCallContext& ccx, return ConstructException(NS_ERROR_XPC_JS_THREW_JS_OBJECT, JS_GetStringBytes(str), ifaceName, methodName, nsnull, - exceptn, cx, &s); + exceptn, &s); } } @@ -1551,7 +1548,7 @@ XPCConvert::JSValToXPCException(XPCCallContext& ccx, { return ConstructException(NS_ERROR_XPC_JS_THREW_NULL, nsnull, ifaceName, methodName, nsnull, - exceptn, cx, &s); + exceptn, &s); } if(JSVAL_IS_NUMBER(s)) @@ -1584,7 +1581,7 @@ XPCConvert::JSValToXPCException(XPCCallContext& ccx, if(isResult) return ConstructException(rv, nsnull, ifaceName, methodName, - nsnull, exceptn, cx, &s); + nsnull, exceptn, &s); else { // XXX all this nsISupportsDouble code seems a little redundant @@ -1600,7 +1597,7 @@ XPCConvert::JSValToXPCException(XPCCallContext& ccx, return NS_ERROR_FAILURE; data->SetData(number); rv = ConstructException(NS_ERROR_XPC_JS_THREW_NUMBER, nsnull, - ifaceName, methodName, data, exceptn, cx, &s); + ifaceName, methodName, data, exceptn, &s); NS_RELEASE(data); return rv; } @@ -1614,7 +1611,7 @@ XPCConvert::JSValToXPCException(XPCCallContext& ccx, return ConstructException(NS_ERROR_XPC_JS_THREW_STRING, JS_GetStringBytes(str), ifaceName, methodName, nsnull, - exceptn, cx, &s); + exceptn, &s); return NS_ERROR_FAILURE; } @@ -1668,7 +1665,7 @@ XPCConvert::JSErrorToXPCException(XPCCallContext& ccx, rv = ConstructException(NS_ERROR_XPC_JAVASCRIPT_ERROR_WITH_DETAILS, formattedMsg.get(), ifaceName, methodName, data, - exceptn, nsnull, nsnull); + exceptn, nsnull); NS_RELEASE(data); } @@ -1676,7 +1673,7 @@ XPCConvert::JSErrorToXPCException(XPCCallContext& ccx, { rv = ConstructException(NS_ERROR_XPC_JAVASCRIPT_ERROR, nsnull, ifaceName, methodName, nsnull, - exceptn, nsnull, nsnull); + exceptn, nsnull); } return rv; } diff --git a/js/src/xpconnect/src/xpcexception.cpp b/js/src/xpconnect/src/xpcexception.cpp index 513e85622cd4..e7407cbe75d3 100644 --- a/js/src/xpconnect/src/xpcexception.cpp +++ b/js/src/xpconnect/src/xpcexception.cpp @@ -152,21 +152,23 @@ nsXPCException::~nsXPCException() } PRBool -nsXPCException::StealThrownJSVal(jsval *vp) +nsXPCException::GetThrownJSVal(jsval *vp) const { - if(mThrownJSVal.IsHeld()) + if(mThrownJSVal) { - *vp = mThrownJSVal.Release(); + if(vp) + *vp = mThrownJSVal->GetJSVal(); return PR_TRUE; } return PR_FALSE; } void -nsXPCException::StowThrownJSVal(JSContext *cx, jsval v) +nsXPCException::SetThrownJSVal(jsval v) { - if (mThrownJSVal.Hold(cx)) - mThrownJSVal = v; + mThrownJSVal = JSVAL_IS_TRACEABLE(v) + ? new XPCTraceableVariant(nsXPConnect::GetRuntimeInstance(), v) + : new XPCVariant(v); } void diff --git a/js/src/xpconnect/src/xpcprivate.h b/js/src/xpconnect/src/xpcprivate.h index b7aa0e90213a..ba1a321807c8 100644 --- a/js/src/xpconnect/src/xpcprivate.h +++ b/js/src/xpconnect/src/xpcprivate.h @@ -95,7 +95,6 @@ #include "nsString.h" #include "nsReadableUtils.h" #include "nsXPIDLString.h" -#include "nsAutoJSValHolder.h" #include "nsThreadUtils.h" #include "nsIJSContextStack.h" @@ -2835,8 +2834,7 @@ public: const char* methodName, nsISupports* data, nsIException** exception, - JSContext* cx, - jsval *jsExceptionPtr); + const jsval *jsExceptionPtr); static void RemoveXPCOMUCStringFinalizer(); @@ -2922,6 +2920,8 @@ private: /***************************************************************************/ +class XPCVariant; + class nsXPCException : public nsIXPCException { @@ -2954,8 +2954,8 @@ public: static void InitStatics() { sEverMadeOneFromFactory = JS_FALSE; } - PRBool StealThrownJSVal(jsval* vp); - void StowThrownJSVal(JSContext* cx, jsval v); + PRBool GetThrownJSVal(jsval *vp) const; + void SetThrownJSVal(jsval v); protected: void Reset(); @@ -2970,7 +2970,7 @@ private: nsIException* mInner; PRBool mInitialized; - nsAutoJSValHolder mThrownJSVal; + nsCOMPtr mThrownJSVal; static JSBool sEverMadeOneFromFactory; }; diff --git a/js/src/xpconnect/src/xpcthrower.cpp b/js/src/xpconnect/src/xpcthrower.cpp index 120bd7deeac6..e929a0982bec 100644 --- a/js/src/xpconnect/src/xpcthrower.cpp +++ b/js/src/xpconnect/src/xpcthrower.cpp @@ -284,7 +284,7 @@ XPCThrower::ThrowExceptionObject(JSContext* cx, nsIException* e) // (see XPCConvert::ConstructException) and we are in a web // context (i.e., not chrome), rethrow the original value. if((xpcEx = do_QueryInterface(e)) && - xpcEx->StealThrownJSVal(&thrown) && + xpcEx->GetThrownJSVal(&thrown) && !IsCallerChrome()) { JS_SetPendingException(cx, thrown); diff --git a/js/src/xpconnect/src/xpcwrappedjsclass.cpp b/js/src/xpconnect/src/xpcwrappedjsclass.cpp index d8702b218686..89fe91c180fa 100644 --- a/js/src/xpconnect/src/xpcwrappedjsclass.cpp +++ b/js/src/xpconnect/src/xpcwrappedjsclass.cpp @@ -1566,7 +1566,7 @@ pre_call_clean_up: nsCOMPtr e; XPCConvert::ConstructException(code, sz, GetInterfaceName(), name, - nsnull, getter_AddRefs(e), nsnull, nsnull); + nsnull, getter_AddRefs(e), nsnull); xpcc->SetException(e); if(sz) JS_smprintf_free(sz);