Bug 989373, part 4 - Remove reuse global flag from subscript loader. r=kmag
authorAndrew McCreight <continuation@gmail.com>
Mon, 15 May 2017 15:04:45 -0700
changeset 359835 d76a99d0f147a39e07083dc2b9e654a61dd0b276
parent 359834 d9f3972b7d978da13ce781ae42a9140f97a50c9c
child 359836 60a6e210bb3b8e3e09d6807d727ad63cd5b12901
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 4 - Remove reuse global flag from subscript loader. r=kmag FindTargetObject in DoLoadSubScriptWithOptions will always return a global object, so the boolean values we pass around to determine if a global was being reused will always be false. The next patch will eliminate the various |function| arguments that are now unused. MozReview-Commit-ID: GvPNFGluRub
js/xpconnect/loader/mozJSSubScriptLoader.cpp
js/xpconnect/loader/mozJSSubScriptLoader.h
--- a/js/xpconnect/loader/mozJSSubScriptLoader.cpp
+++ b/js/xpconnect/loader/mozJSSubScriptLoader.cpp
@@ -127,25 +127,22 @@ ReportError(JSContext* cx, const char* o
 static bool
 PrepareScript(nsIURI* uri,
               JSContext* cx,
               HandleObject targetObj,
               const char* uriStr,
               const nsAString& charset,
               const char* buf,
               int64_t len,
-              bool reuseGlobal,
               bool wantReturnValue,
               MutableHandleScript script,
               MutableHandleFunction function)
 {
     JS::CompileOptions options(cx);
-    // Use line 0 to make the function body starts from line 1 when
-    // |reuseGlobal == true|.
-    options.setFileAndLine(uriStr, reuseGlobal ? 0 : 1)
+    options.setFileAndLine(uriStr, 1)
            .setVersion(JSVERSION_LATEST)
            .setNoScriptRval(!wantReturnValue);
     if (!charset.IsVoid()) {
         char16_t* scriptBuf = nullptr;
         size_t scriptLength = 0;
 
         nsresult rv =
             ScriptLoader::ConvertToUTF16(nullptr, reinterpret_cast<const uint8_t*>(buf), len,
@@ -154,44 +151,28 @@ PrepareScript(nsIURI* uri,
         JS::SourceBufferHolder srcBuf(scriptBuf, scriptLength,
                                       JS::SourceBufferHolder::GiveOwnership);
 
         if (NS_FAILED(rv)) {
             ReportError(cx, LOAD_ERROR_BADCHARSET, uri);
             return false;
         }
 
-        if (!reuseGlobal) {
-            if (JS_IsGlobalObject(targetObj)) {
-                return JS::Compile(cx, options, srcBuf, script);
-            }
-            return JS::CompileForNonSyntacticScope(cx, options, srcBuf, script);
+        if (JS_IsGlobalObject(targetObj)) {
+            return JS::Compile(cx, options, srcBuf, script);
         }
-        AutoObjectVector envChain(cx);
-        if (!JS_IsGlobalObject(targetObj) && !envChain.append(targetObj)) {
-            return false;
-        }
-        return JS::CompileFunction(cx, envChain, options, nullptr, 0, nullptr,
-                                   srcBuf, function);
+        return JS::CompileForNonSyntacticScope(cx, options, srcBuf, script);
     }
     // We only use lazy source when no special encoding is specified because
     // the lazy source loader doesn't know the encoding.
-    if (!reuseGlobal) {
-        options.setSourceIsLazy(true);
-        if (JS_IsGlobalObject(targetObj)) {
-            return JS::Compile(cx, options, buf, len, script);
-        }
-        return JS::CompileForNonSyntacticScope(cx, options, buf, len, script);
+    options.setSourceIsLazy(true);
+    if (JS_IsGlobalObject(targetObj)) {
+        return JS::Compile(cx, options, buf, len, script);
     }
-    AutoObjectVector envChain(cx);
-    if (!JS_IsGlobalObject(targetObj) && !envChain.append(targetObj)) {
-        return false;
-    }
-    return JS::CompileFunction(cx, envChain, options, nullptr, 0, nullptr,
-                               buf, len, function);
+    return JS::CompileForNonSyntacticScope(cx, options, buf, len, script);
 }
 
 static bool
 EvalScript(JSContext* cx,
            HandleObject targetObj,
            MutableHandleValue retval,
            nsIURI* uri,
            bool startupCache,
@@ -265,41 +246,39 @@ EvalScript(JSContext* cx,
 class AsyncScriptLoader : public nsIIncrementalStreamLoaderObserver
 {
 public:
     NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     NS_DECL_NSIINCREMENTALSTREAMLOADEROBSERVER
 
     NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(AsyncScriptLoader)
 
-    AsyncScriptLoader(nsIChannel* aChannel, bool aReuseGlobal, bool aWantReturnValue,
+    AsyncScriptLoader(nsIChannel* aChannel, bool aWantReturnValue,
                       JSObject* aTargetObj, const nsAString& aCharset,
                       bool aCache, Promise* aPromise)
         : mChannel(aChannel)
         , mTargetObj(aTargetObj)
         , mPromise(aPromise)
         , mCharset(aCharset)
-        , mReuseGlobal(aReuseGlobal)
         , mWantReturnValue(aWantReturnValue)
         , mCache(aCache)
     {
         // Needed for the cycle collector to manage mTargetObj.
         mozilla::HoldJSObjects(this);
     }
 
 private:
     virtual ~AsyncScriptLoader() {
         mozilla::DropJSObjects(this);
     }
 
     RefPtr<nsIChannel> mChannel;
     Heap<JSObject*> mTargetObj;
     RefPtr<Promise> mPromise;
     nsString mCharset;
-    bool mReuseGlobal;
     bool mWantReturnValue;
     bool mCache;
 };
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(AsyncScriptLoader)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AsyncScriptLoader)
   NS_INTERFACE_MAP_ENTRY(nsIIncrementalStreamLoaderObserver)
@@ -399,17 +378,17 @@ AsyncScriptLoader::OnStreamComplete(nsII
     nsAutoCString spec;
     nsresult rv = uri->GetSpec(spec);
     NS_ENSURE_SUCCESS(rv, rv);
 
     RootedObject targetObj(cx, mTargetObj);
 
     if (!PrepareScript(uri, cx, targetObj, spec.get(), mCharset,
                        reinterpret_cast<const char*>(aBuf), aLength,
-                       mReuseGlobal, mWantReturnValue, &script, &function))
+                       mWantReturnValue, &script, &function))
     {
         return NS_OK;
     }
 
     JS::Rooted<JS::Value> retval(cx);
     if (EvalScript(cx, targetObj, &retval, uri, mCache,
                    mCache && !mWantReturnValue,
                    &script, function)) {
@@ -419,17 +398,16 @@ AsyncScriptLoader::OnStreamComplete(nsII
     return NS_OK;
 }
 
 nsresult
 mozJSSubScriptLoader::ReadScriptAsync(nsIURI* uri,
                                       HandleObject targetObj,
                                       const nsAString& charset,
                                       nsIIOService* serv,
-                                      bool reuseGlobal,
                                       bool wantReturnValue,
                                       bool cache,
                                       MutableHandleValue retval)
 {
     nsCOMPtr<nsIGlobalObject> globalObject = xpc::NativeGlobal(targetObj);
     ErrorResult result;
 
     AutoJSAPI jsapi;
@@ -462,17 +440,16 @@ mozJSSubScriptLoader::ReadScriptAsync(ns
     if (!NS_SUCCEEDED(rv)) {
         return rv;
     }
 
     channel->SetContentType(NS_LITERAL_CSTRING("application/javascript"));
 
     RefPtr<AsyncScriptLoader> loadObserver =
         new AsyncScriptLoader(channel,
-                              reuseGlobal,
                               wantReturnValue,
                               targetObj,
                               charset,
                               cache,
                               promise);
 
     nsCOMPtr<nsIIncrementalStreamLoader> loader;
     rv = NS_NewIncrementalStreamLoader(getter_AddRefs(loader), loadObserver);
@@ -484,17 +461,16 @@ mozJSSubScriptLoader::ReadScriptAsync(ns
 
 bool
 mozJSSubScriptLoader::ReadScript(nsIURI* uri,
                                  JSContext* cx,
                                  HandleObject targetObj,
                                  const nsAString& charset,
                                  const char* uriStr,
                                  nsIIOService* serv,
-                                 bool reuseGlobal,
                                  bool wantReturnValue,
                                  MutableHandleScript script,
                                  MutableHandleFunction function)
 {
     script.set(nullptr);
     function.set(nullptr);
 
     // We create a channel and call SetContentType, to avoid expensive MIME type
@@ -535,18 +511,17 @@ mozJSSubScriptLoader::ReadScript(nsIURI*
         return false;
     }
 
     nsCString buf;
     rv = NS_ReadInputStreamToString(instream, buf, len);
     NS_ENSURE_SUCCESS(rv, false);
 
     return PrepareScript(uri, cx, targetObj, uriStr, charset,
-                         buf.get(), len,
-                         reuseGlobal, wantReturnValue,
+                         buf.get(), len, wantReturnValue,
                          script, function);
 }
 
 NS_IMETHODIMP
 mozJSSubScriptLoader::LoadSubScript(const nsAString& url,
                                     HandleValue target,
                                     const nsAString& charset,
                                     JSContext* cx,
@@ -600,25 +575,23 @@ mozJSSubScriptLoader::DoLoadSubScriptWit
             return NS_OK;
 
         rv = secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
         if (NS_FAILED(rv) || !mSystemPrincipal)
             return rv;
     }
 
     RootedObject targetObj(cx);
-    mozJSComponentLoader* loader = mozJSComponentLoader::Get();
-    loader->FindTargetObject(cx, &targetObj);
-
-    // We base reusingGlobal off of what the loader told us, but we may not
-    // actually be using that object.
-    bool reusingGlobal = !JS_IsGlobalObject(targetObj);
-
-    if (options.target)
+    if (options.target) {
         targetObj = options.target;
+    } else {
+        mozJSComponentLoader* loader = mozJSComponentLoader::Get();
+        loader->FindTargetObject(cx, &targetObj);
+        MOZ_ASSERT(JS_IsGlobalObject(targetObj));
+    }
 
     // Remember an object out of the calling compartment so that we
     // can properly wrap the result later.
     nsCOMPtr<nsIPrincipal> principal = mSystemPrincipal;
     RootedObject result_obj(cx, targetObj);
     targetObj = JS_FindCompilationScope(cx, targetObj);
     if (!targetObj)
         return NS_ERROR_FAILURE;
@@ -705,30 +678,28 @@ mozJSSubScriptLoader::DoLoadSubScriptWit
             // ReadCachedScript may have set a pending exception.
             JS_ClearPendingException(cx);
         }
     }
 
     // If we are doing an async load, trigger it and bail out.
     if (!script && options.async) {
         return ReadScriptAsync(uri, targetObj, options.charset, serv,
-                               reusingGlobal, options.wantReturnValue,
-                               !!cache, retval);
+                               options.wantReturnValue, !!cache, retval);
     }
 
-    if (!script) {
-        if (!ReadScript(uri, cx, targetObj, options.charset,
-                        static_cast<const char*>(uriStr.get()), serv,
-                        reusingGlobal, options.wantReturnValue, &script,
-                        &function))
-        {
-            return NS_OK;
-        }
-    } else {
+    if (script) {
+        // |script| came from the cache, so don't bother writing it
+        // |back there.
         cache = nullptr;
+    } else if (!ReadScript(uri, cx, targetObj, options.charset,
+                           static_cast<const char*>(uriStr.get()), serv,
+                           options.wantReturnValue, &script,
+                           &function)) {
+        return NS_OK;
     }
 
     Unused << EvalScript(cx, targetObj, retval, uri, !!cache,
                          !ignoreCache && !options.wantReturnValue,
                          &script, function);
     return NS_OK;
 }
 
--- a/js/xpconnect/loader/mozJSSubScriptLoader.h
+++ b/js/xpconnect/loader/mozJSSubScriptLoader.h
@@ -31,24 +31,26 @@ public:
     NS_DECL_MOZIJSSUBSCRIPTLOADER
 
 private:
     virtual ~mozJSSubScriptLoader();
 
     bool ReadScript(nsIURI* uri, JSContext* cx, JS::HandleObject targetObj,
                     const nsAString& charset, const char* uriStr,
                     nsIIOService* serv,
-                    bool reuseGlobal, bool wantReturnValue,
+                    bool wantReturnValue,
                     JS::MutableHandleScript script,
                     JS::MutableHandleFunction function);
 
-    nsresult ReadScriptAsync(nsIURI* uri, JS::HandleObject targetObj,
+    nsresult ReadScriptAsync(nsIURI* uri,
+                             JS::HandleObject targetObj,
                              const nsAString& charset,
-                             nsIIOService* serv, bool reuseGlobal,
-                             bool wantReturnValue, bool cache,
+                             nsIIOService* serv,
+                             bool wantReturnValue,
+                             bool cache,
                              JS::MutableHandleValue retval);
 
     nsresult DoLoadSubScriptWithOptions(const nsAString& url,
                                         LoadSubScriptOptions& options,
                                         JSContext* cx,
                                         JS::MutableHandleValue retval);
 
     nsCOMPtr<nsIPrincipal> mSystemPrincipal;