Bug 587345 - deAgnostify nsXULPDGlobalObject some; r=jst
☠☠ backed out by 4f0c8bd1a8fa ☠ ☠
authorMs2ger <ms2ger@gmail.com>
Tue, 22 Mar 2011 14:18:17 -0400
changeset 63643 05369f93c49a2616668152a4199953ac00af782a
parent 63642 e6b36ff336bb683b6d7d485101f0b57571ca3a9d
child 63644 8e51d8fc07f4a644f38e39d752efb52d9784bb2b
push idunknown
push userunknown
push dateunknown
reviewersjst
bugs587345
milestone2.0b13pre
Bug 587345 - deAgnostify nsXULPDGlobalObject some; r=jst
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: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* ***** 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>  mScriptContexts[NS_STID_ARRAY_UBOUND];
-    void *                      mScriptGlobals[NS_STID_ARRAY_UBOUND];
+    nsCOMPtr<nsIScriptContext> mContext;
+    JSObject* mJSObject;
 
     nsCOMPtr<nsIPrincipal> mCachedPrincipal;
 
     static JSClass gSharedGlobalClass;
 };
 
 nsIPrincipal* nsXULPrototypeDocument::gSystemPrincipal;
 nsXULPDGlobalObject* nsXULPrototypeDocument::gSystemGlobal;
@@ -627,33 +627,27 @@ 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)
-  {
-    PRUint32 lang_index;
-    NS_STID_FOR_INDEX(lang_index) {
-      cb.NoteXPCOMChild(tmp->mScriptContexts[lang_index]);
-    }
-  }
+  NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mContext)
 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
 
@@ -665,160 +659,143 @@ 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
-  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)
+  if (!aScriptContext) {
     NS_WARNING("Possibly early removal of script object, see bug #41608");
-  else {
+  } else {
     // should probably assert the context is clean???
     aScriptContext->WillInitializeContext();
-    // NOTE: We init this context with a NULL global - this is subtly
-    // different than nsGlobalWindow which passes 'this'
-    rv = aScriptContext->InitContext();
+    nsresult rv = aScriptContext->InitContext();
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
-  NS_ASSERTION(!aScriptContext || !mScriptContexts[lang_ndx],
-               "Bad call to SetContext()!");
+  NS_ASSERTION(!aScriptContext || !mContext, "Bad call to SetContext()!");
 
-  void *script_glob = nsnull;
+  void* script_glob = NULL;
 
   if (aScriptContext) {
     aScriptContext->SetGCOnDestruction(PR_FALSE);
     aScriptContext->DidInitializeContext();
     script_glob = aScriptContext->GetNativeGlobal();
     NS_ASSERTION(script_glob, "GetNativeGlobal returned NULL!");
   }
-  mScriptContexts[lang_ndx] = aScriptContext;
-  mScriptGlobals[lang_ndx] = script_glob;
+  mContext = aScriptContext;
+  mJSObject = static_cast<JSObject*>(script_glob);
   return NS_OK;
 }
 
 nsresult
 nsXULPDGlobalObject::EnsureScriptEnvironment(PRUint32 lang_id)
 {
-  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);
+  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?");
 
-  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<nsIScriptRuntime> languageRuntime;
+  nsresult rv = NS_GetScriptRuntimeByID(nsIProgrammingLanguage::JAVASCRIPT,
+                                        getter_AddRefs(languageRuntime));
+  NS_ENSURE_SUCCESS(rv, NS_OK);
 
-    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);
+  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);
 
-      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, nsnull);
+    rv = xpc_CreateGlobalObject(cx, &gSharedGlobalClass, principal, nsnull,
+                                false, &newGlob, &compartment);
+    NS_ENSURE_SUCCESS(rv, NS_OK);
 
-      ::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(lang_id);
+  nsresult rv = EnsureScriptEnvironment(nsIProgrammingLanguage::JAVASCRIPT);
   if (NS_FAILED(rv)) {
     NS_ERROR("Failed to setup script language");
-    return nsnull;
+    return NULL;
   }
   // Note that EnsureScriptEnvironment has validated lang_id
-  return mScriptContexts[NS_STID_INDEX(lang_id)];
+  return mContext;
 }
 
-void *
+void*
 nsXULPDGlobalObject::GetScriptGlobal(PRUint32 lang_id)
 {
-  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];
+  NS_ABORT_IF_FALSE(lang_id == nsIProgrammingLanguage::JAVASCRIPT,
+                    "We don't support this language ID");
+  return mJSObject;
 }
 
 
 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();
 
-    PRUint32 lang_ndx;
-    NS_STID_FOR_INDEX(lang_ndx) {
-        if (mScriptContexts[lang_ndx]) {
-            mScriptContexts[lang_ndx]->FinalizeContext();
-            mScriptContexts[lang_ndx] = nsnull;
-        }
-    }
+  if (mContext) {
+    mContext->FinalizeContext();
+    mContext = NULL;
+  }
 
-    mGlobalObjectOwner = nsnull;
+  mGlobalObjectOwner = NULL;
 }
 
 
 void
 nsXULPDGlobalObject::OnFinalize(PRUint32 aLangID, void *aObject)
 {
-    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;
+  NS_ABORT_IF_FALSE(aLangId == nsIProgrammingLanguage::JAVASCRIPT,
+                    "We don't support this language ID");
+  mJSObject = NULL;
 }
 
 void
 nsXULPDGlobalObject::SetScriptsEnabled(PRBool aEnabled, PRBool aFireTimeouts)
 {
-    // We don't care...
+  // We don't care...
 }
 
 //----------------------------------------------------------------------
 //
 // nsIScriptObjectPrincipal methods
 //
 
 nsIPrincipal*