Fix for bug 502730 (ASSERTION: Wrong scope, this is really bad!: 'JS_GetGlobalForObject(cx, obj) == newScope').
authorPeter Van der Beken <peterv@propagandism.org>
Sat, 25 Jul 2009 13:27:42 +0200
changeset 31428 ffc0bbb9ccb474e97ef6e348e65e0ab4640211a7
parent 31427 793a3c7590b7a8c5299fab679f12003a5d9e083b
child 31429 c2cc58728c8bcd0820e360ec99c075d37c791346
push id1
push usersledru@mozilla.com
push dateThu, 04 Dec 2014 17:57:20 +0000
bugs502730
milestone1.9.2a2pre
Fix for bug 502730 (ASSERTION: Wrong scope, this is really bad!: 'JS_GetGlobalForObject(cx, obj) == newScope').
content/base/src/nsDocument.cpp
content/base/src/nsDocument.h
content/html/document/src/nsHTMLDocument.cpp
--- a/content/base/src/nsDocument.cpp
+++ b/content/base/src/nsDocument.cpp
@@ -3545,33 +3545,35 @@ nsDocument::SetScriptGlobalObject(nsIScr
   if (aScriptGlobalObject) {
     mScriptObject = nsnull;
     mHasHadScriptHandlingObject = PR_TRUE;
     // Go back to using the docshell for the layout history state
     mLayoutHistoryState = nsnull;
     mScopeObject = do_GetWeakReference(aScriptGlobalObject);
 
 #ifdef DEBUG
-    // We really shouldn't have a wrapper here but if we do we need to make sure
-    // it has the correct parent.
-    JSObject *obj = GetWrapper();
-    if (obj) {
-      JSObject *newScope = aScriptGlobalObject->GetGlobalJSObject();
-      nsIScriptContext *scx = aScriptGlobalObject->GetContext();
-      JSContext *cx = scx ? (JSContext *)scx->GetNativeContext() : nsnull;
-      if (!cx) {
-        nsContentUtils::ThreadJSContextStack()->Peek(&cx);
+    if (!mWillReparent) {
+      // We really shouldn't have a wrapper here but if we do we need to make sure
+      // it has the correct parent.
+      JSObject *obj = GetWrapper();
+      if (obj) {
+        JSObject *newScope = aScriptGlobalObject->GetGlobalJSObject();
+        nsIScriptContext *scx = aScriptGlobalObject->GetContext();
+        JSContext *cx = scx ? (JSContext *)scx->GetNativeContext() : nsnull;
         if (!cx) {
-          nsContentUtils::ThreadJSContextStack()->GetSafeJSContext(&cx);
-          NS_ASSERTION(cx, "Uhoh, no context, this is bad!");
+          nsContentUtils::ThreadJSContextStack()->Peek(&cx);
+          if (!cx) {
+            nsContentUtils::ThreadJSContextStack()->GetSafeJSContext(&cx);
+            NS_ASSERTION(cx, "Uhoh, no context, this is bad!");
+          }
         }
-      }
-      if (cx) {
-        NS_ASSERTION(JS_GetGlobalForObject(cx, obj) == newScope,
-                     "Wrong scope, this is really bad!");
+        if (cx) {
+          NS_ASSERTION(JS_GetGlobalForObject(cx, obj) == newScope,
+                       "Wrong scope, this is really bad!");
+        }
       }
     }
 #endif
 
     if (mAllowDNSPrefetch) {
       nsCOMPtr<nsIDocShell> docShell = do_QueryReferent(mDocumentContainer);
       if (docShell) {
 #ifdef DEBUG
--- a/content/base/src/nsDocument.h
+++ b/content/base/src/nsDocument.h
@@ -1268,16 +1268,21 @@ private:
   nsExternalResourceMap mExternalResourceMap;
 
   // All images in process of being preloaded
   nsCOMArray<imgIRequest> mPreloadingImages;
 
 #ifdef MOZ_SMIL
   nsAutoPtr<nsSMILAnimationController> mAnimationController;
 #endif // MOZ_SMIL
+
+#ifdef DEBUG
+protected:
+  PRBool mWillReparent;
+#endif
 };
 
 #define NS_DOCUMENT_INTERFACE_TABLE_BEGIN(_class)                             \
   NS_NODE_OFFSET_AND_INTERFACE_TABLE_BEGIN(_class)                            \
   NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsIDOMDocument, nsDocument)      \
   NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsIDOMNSDocument, nsDocument)    \
   NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsIDOMDocumentEvent, nsDocument) \
   NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(_class, nsIDOMDocumentView, nsDocument)  \
--- a/content/html/document/src/nsHTMLDocument.cpp
+++ b/content/html/document/src/nsHTMLDocument.cpp
@@ -1919,19 +1919,28 @@ nsHTMLDocument::OpenCommon(const nsACStr
   nsCOMPtr<nsIDOMDocument> kungFuDeathGrip =
     do_QueryInterface((nsIHTMLDocument*)this);
 
   nsPIDOMWindow *window = GetInnerWindow();
   if (window) {
     // Remember the old scope in case the call to SetNewDocument changes it.
     nsCOMPtr<nsIScriptGlobalObject> oldScope(do_QueryReferent(mScopeObject));
 
+#ifdef DEBUG
+    PRBool willReparent = mWillReparent;
+    mWillReparent = PR_TRUE;
+#endif
+
     rv = window->SetNewDocument(this, nsnull, PR_FALSE);
     NS_ENSURE_SUCCESS(rv, rv);
 
+#ifdef DEBUG
+    mWillReparent = willReparent;
+#endif
+
     // Now make sure we're not flagged as the initial document anymore, now
     // that we've had stuff done to us.  From now on, if anyone tries to
     // document.open() us, they get a new inner window.
     SetIsInitialDocument(PR_FALSE);
 
     nsCOMPtr<nsIScriptGlobalObject> newScope(do_QueryReferent(mScopeObject));
     if (oldScope && newScope != oldScope) {
       nsContentUtils::ReparentContentWrappersInScope(oldScope, newScope);