diff --git a/mozilla/dom/src/base/nsJSEnvironment.cpp b/mozilla/dom/src/base/nsJSEnvironment.cpp index 5e9e8a33c5a..508e984c9d2 100644 --- a/mozilla/dom/src/base/nsJSEnvironment.cpp +++ b/mozilla/dom/src/base/nsJSEnvironment.cpp @@ -417,6 +417,9 @@ nsJSContext::~nsJSContext() this); } + // Release mGlobalWrapperRef before the context is destroyed + mGlobalWrapperRef = nsnull; + // Let xpconnect destroy the JSContext when it thinks the time is right. nsCOMPtr xpc(do_GetService(nsIXPConnect::GetCID())); if (xpc) { @@ -1103,12 +1106,12 @@ nsJSContext::InitContext(nsIScriptGlobalObject *aGlobalObject) JSObject *global = ::JS_GetGlobalObject(mContext); + nsCOMPtr holder; + // If there's already a global object in mContext we won't tell // XPConnect to wrap aGlobalObject since it's already wrapped. if (!global) { - nsCOMPtr holder; - rv = xpc->InitClassesWithNewWrappedGlobal(mContext, aGlobalObject, NS_GET_IID(nsISupports), PR_FALSE, @@ -1125,8 +1128,6 @@ nsJSContext::InitContext(nsIScriptGlobalObject *aGlobalObject) nsCOMPtr ci(do_QueryInterface(aGlobalObject)); if (ci) { - nsCOMPtr holder; - rv = xpc->WrapNative(mContext, global, aGlobalObject, NS_GET_IID(nsISupports), getter_AddRefs(holder)); @@ -1140,6 +1141,11 @@ nsJSContext::InitContext(nsIScriptGlobalObject *aGlobalObject) } } + // Hold a strong reference to the wrapper for the global to avoid + // rooting and unrooting the global object every time its AddRef() + // or Release() methods are called + mGlobalWrapperRef = holder; + rv = InitClasses(); // this will complete global object initialization NS_ENSURE_SUCCESS(rv, rv); diff --git a/mozilla/dom/src/base/nsJSEnvironment.h b/mozilla/dom/src/base/nsJSEnvironment.h index 3d626c7aeed..09c4801b440 100644 --- a/mozilla/dom/src/base/nsJSEnvironment.h +++ b/mozilla/dom/src/base/nsJSEnvironment.h @@ -145,10 +145,17 @@ private: PRPackedBool mIsInitialized; PRPackedBool mScriptsEnabled; PRPackedBool mGCOnDestruction; + PRPackedBool mProcessingScriptTag; PRUint32 mBranchCallbackCount; PRUint32 mDefaultJSOptions; - PRBool mProcessingScriptTag; + + // mGlobalWrapperRef is used only to hold a strong reference to the + // global object wrapper while the nsJSContext is alive. This cuts + // down on the number of rooting and unrooting calls XPConnect has + // to make when the global object is touched in JS. + + nsCOMPtr mGlobalWrapperRef; static int PR_CALLBACK JSOptionChangedCallback(const char *pref, void *data);