Bug 989373, part 7 - Add new method to create a loader global. r=kmag
authorAndrew McCreight <continuation@gmail.com>
Mon, 15 May 2017 15:46:13 -0700
changeset 359838 249446e1ed682cc2f16b2a80473a2546fa80e2f7
parent 359837 67231a14bdc916eb8a0b673e0eeb34ada188a3dc
child 359839 da7db7d677e7a1284645c4c729060892c5e60bfe
push id31859
push userihsiao@mozilla.com
push dateMon, 22 May 2017 03:28:26 +0000
treeherdermozilla-central@367944041b55 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs989373
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 989373, part 7 - Add new method to create a loader global. r=kmag This makes the code a little nicer to read, and means there will be less code churn if we later add back the ability to share globals. The holder also gets changed to an actual JS object. mLoaderGlobal is always null, but the simplification for that will be made in a later patch. MozReview-Commit-ID: 7Qg7JSgIxxm
js/xpconnect/loader/mozJSComponentLoader.cpp
js/xpconnect/loader/mozJSComponentLoader.h
--- a/js/xpconnect/loader/mozJSComponentLoader.cpp
+++ b/js/xpconnect/loader/mozJSComponentLoader.cpp
@@ -454,16 +454,63 @@ mozJSComponentLoader::SizeOfIncludingThi
 {
     size_t n = aMallocSizeOf(this);
     n += SizeOfTableExcludingThis(mModules, aMallocSizeOf);
     n += SizeOfTableExcludingThis(mImports, aMallocSizeOf);
     n += SizeOfTableExcludingThis(mInProgressImports, aMallocSizeOf);
     return n;
 }
 
+void
+mozJSComponentLoader::CreateLoaderGlobal(JSContext* aCx,
+                                         JSAddonId* aAddonID,
+                                         MutableHandleObject aGlobal)
+{
+    RefPtr<BackstagePass> backstagePass;
+    nsresult rv = NS_NewBackstagePass(getter_AddRefs(backstagePass));
+    NS_ENSURE_SUCCESS_VOID(rv);
+
+    CompartmentOptions options;
+
+    options.creationOptions()
+           .setSystemZone()
+           .setAddonId(aAddonID);
+
+    options.behaviors().setVersion(JSVERSION_LATEST);
+
+    if (xpc::SharedMemoryEnabled())
+        options.creationOptions().setSharedMemoryAndAtomicsEnabled(true);
+
+    // Defer firing OnNewGlobalObject until after the __URI__ property has
+    // been defined so the JS debugger can tell what module the global is
+    // for
+    nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
+    rv = nsXPConnect::XPConnect()->
+        InitClassesWithNewWrappedGlobal(aCx,
+                                        static_cast<nsIGlobalObject*>(backstagePass),
+                                        mSystemPrincipal,
+                                        nsIXPConnect::DONT_FIRE_ONNEWGLOBALHOOK,
+                                        options,
+                                        getter_AddRefs(holder));
+    NS_ENSURE_SUCCESS_VOID(rv);
+
+    RootedObject global(aCx, holder->GetJSObject());
+    NS_ENSURE_TRUE_VOID(global);
+
+    backstagePass->SetGlobalObject(global);
+
+    JSAutoCompartment ac(aCx, global);
+    if (!JS_DefineFunctions(aCx, global, gGlobalFun) ||
+        !JS_DefineProfilingFunctions(aCx, global)) {
+        return;
+    }
+
+    aGlobal.set(global);
+}
+
 // Some stack based classes for cleaning up on early return
 class FileAutoCloser
 {
  public:
     explicit FileAutoCloser(PRFileDesc* file) : mFile(file) {}
     ~FileAutoCloser() { PR_Close(mFile); }
  private:
     PRFileDesc* mFile;
@@ -479,75 +526,40 @@ class FileMapAutoCloser
 };
 
 JSObject*
 mozJSComponentLoader::PrepareObjectForLocation(JSContext* aCx,
                                                nsIFile* aComponentFile,
                                                nsIURI* aURI,
                                                bool* aRealFile)
 {
-    nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
-    nsresult rv = NS_OK;
     bool createdNewGlobal = false;
+    RootedObject globalObj(aCx);
 
     if (!mLoaderGlobal) {
-        RefPtr<BackstagePass> backstagePass;
-        rv = NS_NewBackstagePass(getter_AddRefs(backstagePass));
-        NS_ENSURE_SUCCESS(rv, nullptr);
-
-        CompartmentOptions options;
-
-        options.creationOptions()
-               .setSystemZone()
-               .setAddonId(MapURIToAddonID(aURI));
-
-        options.behaviors().setVersion(JSVERSION_LATEST);
-
-        if (xpc::SharedMemoryEnabled())
-            options.creationOptions().setSharedMemoryAndAtomicsEnabled(true);
-
-        // Defer firing OnNewGlobalObject until after the __URI__ property has
-        // been defined so the JS debugger can tell what module the global is
-        // for
-        rv = nsXPConnect::XPConnect()->
-            InitClassesWithNewWrappedGlobal(aCx,
-                                            static_cast<nsIGlobalObject*>(backstagePass),
-                                            mSystemPrincipal,
-                                            nsIXPConnect::DONT_FIRE_ONNEWGLOBALHOOK,
-                                            options,
-                                            getter_AddRefs(holder));
-        NS_ENSURE_SUCCESS(rv, nullptr);
-        createdNewGlobal = true;
-
-        RootedObject global(aCx, holder->GetJSObject());
-        NS_ENSURE_TRUE(global, nullptr);
-
-        backstagePass->SetGlobalObject(global);
-
-        JSAutoCompartment ac(aCx, global);
-        if (!JS_DefineFunctions(aCx, global, gGlobalFun) ||
-            !JS_DefineProfilingFunctions(aCx, global)) {
-            return nullptr;
-        }
+        CreateLoaderGlobal(aCx, MapURIToAddonID(aURI), &globalObj);
+        if (globalObj)
+            createdNewGlobal = true;
     }
 
     // |thisObj| is the object we set properties on for a particular .jsm.
     // XXX Right now, thisObj is always globalObj, but if we start
     // sharing globals between jsms, they won't be the same.
     // See bug 1186409.
-    RootedObject thisObj(aCx, holder->GetJSObject());
+    RootedObject thisObj(aCx, globalObj);
     NS_ENSURE_TRUE(thisObj, nullptr);
 
     JSAutoCompartment ac(aCx, thisObj);
 
     *aRealFile = false;
 
     // need to be extra careful checking for URIs pointing to files
     // EnsureFile may not always get called, especially on resource URIs
     // so we need to call GetFile to make sure this is a valid file
+    nsresult rv = NS_OK;
     nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aURI, &rv);
     nsCOMPtr<nsIFile> testFile;
     if (NS_SUCCEEDED(rv)) {
         fileURL->GetFile(getter_AddRefs(testFile));
     }
 
     if (testFile) {
         *aRealFile = true;
@@ -576,20 +588,19 @@ mozJSComponentLoader::PrepareObjectForLo
     NS_ENSURE_TRUE(exposedUri, nullptr);
 
     if (!JS_DefineProperty(aCx, thisObj, "__URI__", exposedUri, 0))
         return nullptr;
 
     if (createdNewGlobal) {
         // AutoEntryScript required to invoke debugger hook, which is a
         // Gecko-specific concept at present.
-        dom::AutoEntryScript aes(holder->GetJSObject(),
+        dom::AutoEntryScript aes(globalObj,
                                  "component loader report global");
-        RootedObject global(aes.cx(), holder->GetJSObject());
-        JS_FireOnNewGlobalObject(aes.cx(), global);
+        JS_FireOnNewGlobalObject(aes.cx(), globalObj);
     }
 
     return thisObj;
 }
 
 nsresult
 mozJSComponentLoader::ObjectForLocation(ComponentLoaderInfo& aInfo,
                                         nsIFile* aComponentFile,
--- a/js/xpconnect/loader/mozJSComponentLoader.h
+++ b/js/xpconnect/loader/mozJSComponentLoader.h
@@ -57,16 +57,20 @@ class mozJSComponentLoader : public mozi
  protected:
     virtual ~mozJSComponentLoader();
 
     static mozJSComponentLoader* sSelf;
 
     nsresult ReallyInit();
     void UnloadModules();
 
+    void CreateLoaderGlobal(JSContext* aCx,
+                            JSAddonId* aAddonID,
+                            JS::MutableHandleObject aGlobal);
+
     JSObject* PrepareObjectForLocation(JSContext* aCx,
                                        nsIFile* aComponentFile,
                                        nsIURI* aComponent,
                                        bool* aRealFile);
 
     nsresult ObjectForLocation(ComponentLoaderInfo& aInfo,
                                nsIFile* aComponentFile,
                                JS::MutableHandleObject aObject,