Back out changeset 05369f93c49a (bug 587345) because it breaks the build
authorEhsan Akhgari <ehsan@mozilla.com>
Tue, 22 Mar 2011 14:54:24 -0400
changeset 63647 4f0c8bd1a8fa3032e3ee3a59057d0ccb79c548ac
parent 63646 b141be7077348301e2f8d648e510c46df5c415d8
child 63648 c2d545094c631da47a3d9299d09f4e8158dd26f5
push idunknown
push userunknown
push dateunknown
bugs587345
milestone2.0b13pre
backs out05369f93c49a2616668152a4199953ac00af782a
Back out changeset 05369f93c49a (bug 587345) because it breaks the build
content/xul/document/src/nsXULPrototypeDocument.cpp
--- a/content/xul/document/src/nsXULPrototypeDocument.cpp
+++ b/content/xul/document/src/nsXULPrototypeDocument.cpp
@@ -1,9 +1,9 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* ***** BEGIN LICENSE BLOCK *****
  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  *
  * The contents of this file are subject to the Mozilla Public License Version
  * 1.1 (the "License"); you may not use this file except in compliance with
  * the License. You may obtain a copy of the License at
  * http://www.mozilla.org/MPL/
  *
@@ -98,18 +98,18 @@ public:
 
     void ClearGlobalObjectOwner();
 
 protected:
     virtual ~nsXULPDGlobalObject();
 
     nsXULPrototypeDocument* mGlobalObjectOwner; // weak reference
 
-    nsCOMPtr<nsIScriptContext> mContext;
-    JSObject* mJSObject;
+    nsCOMPtr<nsIScriptContext>  mScriptContexts[NS_STID_ARRAY_UBOUND];
+    void *                      mScriptGlobals[NS_STID_ARRAY_UBOUND];
 
     nsCOMPtr<nsIPrincipal> mCachedPrincipal;
 
     static JSClass gSharedGlobalClass;
 };
 
 nsIPrincipal* nsXULPrototypeDocument::gSystemPrincipal;
 nsXULPDGlobalObject* nsXULPrototypeDocument::gSystemGlobal;
@@ -627,27 +627,33 @@ nsXULPrototypeDocument::GetScriptGlobalO
 //----------------------------------------------------------------------
 //
 // nsXULPDGlobalObject
 //
 
 nsXULPDGlobalObject::nsXULPDGlobalObject(nsXULPrototypeDocument* owner)
     :  mGlobalObjectOwner(owner)
 {
+  memset(mScriptGlobals, 0, sizeof(mScriptGlobals));
 }
 
 
 nsXULPDGlobalObject::~nsXULPDGlobalObject()
 {
 }
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsXULPDGlobalObject)
 NS_IMPL_CYCLE_COLLECTION_UNLINK_0(nsXULPDGlobalObject)
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXULPDGlobalObject)
-  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mContext)
+  {
+    PRUint32 lang_index;
+    NS_STID_FOR_INDEX(lang_index) {
+      cb.NoteXPCOMChild(tmp->mScriptContexts[lang_index]);
+    }
+  }
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXULPDGlobalObject)
   NS_INTERFACE_MAP_ENTRY(nsIScriptGlobalObject)
   NS_INTERFACE_MAP_ENTRY(nsIScriptObjectPrincipal)
   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIScriptGlobalObject)
 NS_INTERFACE_MAP_END
 
@@ -659,143 +665,160 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE_AMBIGUO
 //----------------------------------------------------------------------
 //
 // nsIScriptGlobalObject methods
 //
 
 nsresult
 nsXULPDGlobalObject::SetScriptContext(PRUint32 lang_id, nsIScriptContext *aScriptContext)
 {
-  NS_ABORT_IF_FALSE(lang_id == nsIProgrammingLanguage::JAVASCRIPT,
-                    "We don't support this language ID");
   // almost a clone of nsGlobalWindow
-  if (!aScriptContext) {
+  nsresult rv;
+
+  PRBool ok = NS_STID_VALID(lang_id);
+  NS_ASSERTION(ok, "Invalid programming language ID requested");
+  NS_ENSURE_TRUE(ok, NS_ERROR_INVALID_ARG);
+  PRUint32 lang_ndx = NS_STID_INDEX(lang_id);
+
+  if (!aScriptContext)
     NS_WARNING("Possibly early removal of script object, see bug #41608");
-  } else {
+  else {
     // should probably assert the context is clean???
     aScriptContext->WillInitializeContext();
-    nsresult rv = aScriptContext->InitContext();
+    // NOTE: We init this context with a NULL global - this is subtly
+    // different than nsGlobalWindow which passes 'this'
+    rv = aScriptContext->InitContext();
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
-  NS_ASSERTION(!aScriptContext || !mContext, "Bad call to SetContext()!");
+  NS_ASSERTION(!aScriptContext || !mScriptContexts[lang_ndx],
+               "Bad call to SetContext()!");
 
-  void* script_glob = NULL;
+  void *script_glob = nsnull;
 
   if (aScriptContext) {
     aScriptContext->SetGCOnDestruction(PR_FALSE);
     aScriptContext->DidInitializeContext();
     script_glob = aScriptContext->GetNativeGlobal();
     NS_ASSERTION(script_glob, "GetNativeGlobal returned NULL!");
   }
-  mContext = aScriptContext;
-  mJSObject = static_cast<JSObject*>(script_glob);
+  mScriptContexts[lang_ndx] = aScriptContext;
+  mScriptGlobals[lang_ndx] = script_glob;
   return NS_OK;
 }
 
 nsresult
 nsXULPDGlobalObject::EnsureScriptEnvironment(PRUint32 lang_id)
 {
-  NS_ABORT_IF_FALSE(lang_id == nsIProgrammingLanguage::JAVASCRIPT,
-                    "We don't support this language ID");
-  if (mContext) {
-    return NS_OK;
-  }
-  NS_ASSERTION(!mJSObject, "Have global without context?");
+  PRBool ok = NS_STID_VALID(lang_id);
+  NS_ASSERTION(ok, "Invalid programming language ID requested");
+  NS_ENSURE_TRUE(ok, NS_ERROR_INVALID_ARG);
+  PRUint32 lang_ndx = NS_STID_INDEX(lang_id);
 
-  nsCOMPtr<nsIScriptRuntime> languageRuntime;
-  nsresult rv = NS_GetScriptRuntimeByID(nsIProgrammingLanguage::JAVASCRIPT,
-                                        getter_AddRefs(languageRuntime));
-  NS_ENSURE_SUCCESS(rv, NS_OK);
+  if (mScriptContexts[lang_ndx] == nsnull) {
+    nsresult rv;
+    NS_ASSERTION(mScriptGlobals[lang_ndx] == nsnull, "Have global without context?");
+
+    nsCOMPtr<nsIScriptRuntime> languageRuntime;
+    rv = NS_GetScriptRuntimeByID(lang_id, getter_AddRefs(languageRuntime));
+    NS_ENSURE_SUCCESS(rv, nsnull);
 
-  nsCOMPtr<nsIScriptContext> ctxNew;
-  rv = languageRuntime->CreateContext(getter_AddRefs(ctxNew));
-  // We have to setup a special global object.  We do this then
-  // attach it as the global for this context.  Then, ::SetScriptContext
-  // will re-fetch the global and set it up in our language globals array.
-  {
-    JSContext *cx = (JSContext *)ctxNew->GetNativeContext();
-    JSAutoRequest ar(cx);
+    nsCOMPtr<nsIScriptContext> ctxNew;
+    rv = languageRuntime->CreateContext(getter_AddRefs(ctxNew));
+    // For JS, we have to setup a special global object.  We do this then
+    // attach it as the global for this context.  Then, ::SetScriptContext
+    // will re-fetch the global and set it up in our language globals array.
+    if (lang_id == nsIProgrammingLanguage::JAVASCRIPT) {
+      // some special JS specific code we should abstract
+      JSContext *cx = (JSContext *)ctxNew->GetNativeContext();
+      JSAutoRequest ar(cx);
 
-    nsIPrincipal *principal = GetPrincipal();
-    JSObject *newGlob;
-    JSCompartment *compartment;
+      nsIPrincipal *principal = GetPrincipal();
+      JSObject *newGlob;
+      JSCompartment *compartment;
 
-    rv = xpc_CreateGlobalObject(cx, &gSharedGlobalClass, principal, nsnull,
-                                false, &newGlob, &compartment);
-    NS_ENSURE_SUCCESS(rv, NS_OK);
+      rv = xpc_CreateGlobalObject(cx, &gSharedGlobalClass, principal, nsnull,
+                                  false, &newGlob, &compartment);
+      NS_ENSURE_SUCCESS(rv, nsnull);
 
-    ::JS_SetGlobalObject(cx, newGlob);
+      ::JS_SetGlobalObject(cx, newGlob);
 
-    // Add an owning reference from JS back to us. This'll be
-    // released when the JSObject is finalized.
-    ::JS_SetPrivate(cx, newGlob, this);
-    NS_ADDREF(this);
+      // Add an owning reference from JS back to us. This'll be
+      // released when the JSObject is finalized.
+      ::JS_SetPrivate(cx, newGlob, this);
+      NS_ADDREF(this);
+    }
+
+    NS_ENSURE_SUCCESS(rv, nsnull);
+    rv = SetScriptContext(lang_id, ctxNew);
+    NS_ENSURE_SUCCESS(rv, nsnull);
   }
-
-  NS_ENSURE_SUCCESS(rv, NS_OK);
-  rv = SetScriptContext(lang_id, ctxNew);
-  NS_ENSURE_SUCCESS(rv, NS_OK);
   return NS_OK;
 }
 
-nsIScriptContext*
+nsIScriptContext *
 nsXULPDGlobalObject::GetScriptContext(PRUint32 lang_id)
 {
-  NS_ABORT_IF_FALSE(lang_id == nsIProgrammingLanguage::JAVASCRIPT,
-                    "We don't support this language ID");
   // This global object creates a context on demand - do that now.
-  nsresult rv = EnsureScriptEnvironment(nsIProgrammingLanguage::JAVASCRIPT);
+  nsresult rv = EnsureScriptEnvironment(lang_id);
   if (NS_FAILED(rv)) {
     NS_ERROR("Failed to setup script language");
-    return NULL;
+    return nsnull;
   }
   // Note that EnsureScriptEnvironment has validated lang_id
-  return mContext;
+  return mScriptContexts[NS_STID_INDEX(lang_id)];
 }
 
-void*
+void *
 nsXULPDGlobalObject::GetScriptGlobal(PRUint32 lang_id)
 {
-  NS_ABORT_IF_FALSE(lang_id == nsIProgrammingLanguage::JAVASCRIPT,
-                    "We don't support this language ID");
-  return mJSObject;
+  PRBool ok = NS_STID_VALID(lang_id);
+  NS_ASSERTION(ok, "Invalid programming language ID requested");
+  NS_ENSURE_TRUE(ok, nsnull);
+  PRUint32 lang_ndx = NS_STID_INDEX(lang_id);
+
+  NS_ASSERTION(mScriptContexts[lang_ndx] != nsnull, "Querying for global before setting up context?");
+  return mScriptGlobals[lang_ndx];
 }
 
 
 void
 nsXULPDGlobalObject::ClearGlobalObjectOwner()
 {
-  NS_ASSERTION(!mCachedPrincipal, "This shouldn't ever be set until now!");
+    NS_ASSERTION(!mCachedPrincipal, "This shouldn't ever be set until now!");
 
-  // Cache mGlobalObjectOwner's principal if possible.
-  if (this != nsXULPrototypeDocument::gSystemGlobal)
-    mCachedPrincipal = mGlobalObjectOwner->DocumentPrincipal();
+    // Cache mGlobalObjectOwner's principal if possible.
+    if (this != nsXULPrototypeDocument::gSystemGlobal)
+        mCachedPrincipal = mGlobalObjectOwner->DocumentPrincipal();
 
-  if (mContext) {
-    mContext->FinalizeContext();
-    mContext = NULL;
-  }
+    PRUint32 lang_ndx;
+    NS_STID_FOR_INDEX(lang_ndx) {
+        if (mScriptContexts[lang_ndx]) {
+            mScriptContexts[lang_ndx]->FinalizeContext();
+            mScriptContexts[lang_ndx] = nsnull;
+        }
+    }
 
-  mGlobalObjectOwner = NULL;
+    mGlobalObjectOwner = nsnull;
 }
 
 
 void
 nsXULPDGlobalObject::OnFinalize(PRUint32 aLangID, void *aObject)
 {
-  NS_ABORT_IF_FALSE(aLangId == nsIProgrammingLanguage::JAVASCRIPT,
-                    "We don't support this language ID");
-  mJSObject = NULL;
+    NS_ASSERTION(NS_STID_VALID(aLangID), "Invalid language ID");
+    NS_ASSERTION(aObject == mScriptGlobals[NS_STID_INDEX(aLangID)],
+                 "Wrong object finalized!");
+    mScriptGlobals[NS_STID_INDEX(aLangID)] = nsnull;
 }
 
 void
 nsXULPDGlobalObject::SetScriptsEnabled(PRBool aEnabled, PRBool aFireTimeouts)
 {
-  // We don't care...
+    // We don't care...
 }
 
 //----------------------------------------------------------------------
 //
 // nsIScriptObjectPrincipal methods
 //
 
 nsIPrincipal*