Bug 1282150 part 2. Get rid of GetDefaultJSContextForThread. r=bholley
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 07 Jul 2016 20:08:26 -0400
changeset 304146 57628a2c33d651e1e2e5925eb44a3e266528551f
parent 304145 e21358504aa9e822d3cee4c43becaad5010c03a5
child 304147 9870763b6a4c1d196562f45cbbaa0c5875ad3a07
push id30414
push usercbook@mozilla.com
push dateFri, 08 Jul 2016 09:59:01 +0000
treeherdermozilla-central@45682df2d2d4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs1282150
milestone50.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 1282150 part 2. Get rid of GetDefaultJSContextForThread. r=bholley
dom/base/ScriptSettings.cpp
dom/base/nsContentUtils.cpp
dom/base/nsContentUtils.h
dom/bindings/BindingUtils.cpp
dom/bindings/SimpleGlobalObject.cpp
--- a/dom/base/ScriptSettings.cpp
+++ b/dom/base/ScriptSettings.cpp
@@ -329,17 +329,17 @@ void
 WarningOnlyErrorReporter(JSContext* aCx, const char* aMessage,
                          JSErrorReport* aRep);
 
 void
 AutoJSAPI::InitInternal(nsIGlobalObject* aGlobalObject, JSObject* aGlobal,
                         JSContext* aCx, bool aIsMainThread)
 {
   MOZ_ASSERT(aCx);
-  MOZ_ASSERT(aCx == nsContentUtils::GetDefaultJSContextForThread());
+  MOZ_ASSERT(aCx == danger::GetJSContext());
   MOZ_ASSERT(aIsMainThread == NS_IsMainThread());
   MOZ_ASSERT(bool(aGlobalObject) == bool(aGlobal));
   MOZ_ASSERT_IF(aGlobalObject, aGlobalObject->GetGlobalJSObject() == aGlobal);
 #ifdef DEBUG
   bool haveException = JS_IsExceptionPending(aCx);
 #endif // DEBUG
 
   mCx = aCx;
@@ -432,28 +432,26 @@ AutoJSAPI::AutoJSAPI(nsIGlobalObject* aG
   : ScriptSettingsStackEntry(aGlobalObject, aType)
   , mIsMainThread(aIsMainThread)
 {
   MOZ_ASSERT(aGlobalObject);
   MOZ_ASSERT(aGlobalObject->GetGlobalJSObject(), "Must have a JS global");
   MOZ_ASSERT(aIsMainThread == NS_IsMainThread());
 
   InitInternal(aGlobalObject, aGlobalObject->GetGlobalJSObject(),
-               nsContentUtils::GetDefaultJSContextForThread(),
-               aIsMainThread);
+               danger::GetJSContext(), aIsMainThread);
 }
 
 void
 AutoJSAPI::Init()
 {
   MOZ_ASSERT(!mCx, "An AutoJSAPI should only be initialised once");
 
   InitInternal(/* aGlobalObject */ nullptr, /* aGlobal */ nullptr,
-               nsContentUtils::GetDefaultJSContextForThread(),
-               NS_IsMainThread());
+               danger::GetJSContext(), NS_IsMainThread());
 }
 
 bool
 AutoJSAPI::Init(nsIGlobalObject* aGlobalObject, JSContext* aCx)
 {
   MOZ_ASSERT(!mCx, "An AutoJSAPI should only be initialised once");
   MOZ_ASSERT(aCx);
 
@@ -468,17 +466,17 @@ AutoJSAPI::Init(nsIGlobalObject* aGlobal
 
   InitInternal(aGlobalObject, global, aCx, NS_IsMainThread());
   return true;
 }
 
 bool
 AutoJSAPI::Init(nsIGlobalObject* aGlobalObject)
 {
-  return Init(aGlobalObject, nsContentUtils::GetDefaultJSContextForThread());
+  return Init(aGlobalObject, danger::GetJSContext());
 }
 
 bool
 AutoJSAPI::Init(JSObject* aObject)
 {
   return Init(xpc::NativeGlobal(aObject));
 }
 
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -5555,28 +5555,16 @@ nsContentUtils::GetSafeJSContext()
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(IsInitialized());
   return sXPConnect->GetSafeJSContext();
 }
 
 /* static */
 JSContext *
-nsContentUtils::GetDefaultJSContextForThread()
-{
-  MOZ_ASSERT(IsInitialized());
-  if (MOZ_LIKELY(NS_IsMainThread())) {
-    return GetSafeJSContext();
-  } else {
-    return workers::GetCurrentThreadJSContext();
-  }
-}
-
-/* static */
-JSContext *
 nsContentUtils::GetCurrentJSContextForThread()
 {
   MOZ_ASSERT(IsInitialized());
   if (MOZ_LIKELY(NS_IsMainThread())) {
     return GetCurrentJSContext();
   } else {
     return workers::GetCurrentThreadJSContext();
   }
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -1719,19 +1719,20 @@ public:
                                       const nsAString &viewportInfo);
 
   static nsIScriptContext* GetContextForEventHandlers(nsINode* aNode,
                                                       nsresult* aRv);
 
   static JSContext *GetCurrentJSContext();
   static JSContext *GetSafeJSContext();
   static JSContext *GetCurrentJSContextForThread();
-  static JSContext *GetDefaultJSContextForThread();
   inline static JSContext *RootingCx() { return GetSafeJSContext(); }
-  inline static JSContext *RootingCxForThread() { return GetDefaultJSContextForThread(); }
+  inline static JSContext *RootingCxForThread() {
+    return mozilla::dom::danger::GetJSContext();
+  }
 
   /**
    * Case insensitive comparison between two strings. However it only ignores
    * case for ASCII characters a-z.
    */
   static bool EqualsIgnoreASCIICase(const nsAString& aStr1,
                                     const nsAString& aStr2);
 
--- a/dom/bindings/BindingUtils.cpp
+++ b/dom/bindings/BindingUtils.cpp
@@ -354,17 +354,17 @@ ErrorResult::ClearDOMExceptionInfo()
   mUnionState = HasNothing;
 #endif // DEBUG
 }
 
 void
 ErrorResult::ClearUnionData()
 {
   if (IsJSException()) {
-    JSContext* cx = nsContentUtils::GetDefaultJSContextForThread();
+    JSContext* cx = nsContentUtils::RootingCxForThread();
     MOZ_ASSERT(cx);
     mJSException.setUndefined();
     js::RemoveRawValueRoot(cx, &mJSException);
 #ifdef DEBUG
     mUnionState = HasNothing;
 #endif // DEBUG
   } else if (IsErrorWithMessage()) {
     ClearMessage();
@@ -393,17 +393,17 @@ ErrorResult::operator=(ErrorResult&& aRH
 #ifdef DEBUG
   mMightHaveUnreportedJSException = aRHS.mMightHaveUnreportedJSException;
   aRHS.mMightHaveUnreportedJSException = false;
 #endif
   if (aRHS.IsErrorWithMessage()) {
     mMessage = aRHS.mMessage;
     aRHS.mMessage = nullptr;
   } else if (aRHS.IsJSException()) {
-    JSContext* cx = nsContentUtils::GetDefaultJSContextForThread();
+    JSContext* cx = nsContentUtils::RootingCxForThread();
     MOZ_ASSERT(cx);
     mJSException.setUndefined();
     if (!js::AddRawValueRoot(cx, &mJSException, "ErrorResult::mJSException")) {
       MOZ_CRASH("Could not root mJSException, we're about to OOM");
     }
     mJSException = aRHS.mJSException;
     aRHS.mJSException.setUndefined();
     js::RemoveRawValueRoot(cx, &aRHS.mJSException);
--- a/dom/bindings/SimpleGlobalObject.cpp
+++ b/dom/bindings/SimpleGlobalObject.cpp
@@ -4,23 +4,24 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/dom/SimpleGlobalObject.h"
 
 #include "jsapi.h"
 #include "js/Class.h"
 
-#include "nsContentUtils.h"
 #include "nsJSPrincipals.h"
 #include "nsNullPrincipal.h"
 #include "nsThreadUtils.h"
 
 #include "xpcprivate.h"
 
+#include "mozilla/dom/ScriptSettings.h"
+
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(SimpleGlobalObject)
 
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(SimpleGlobalObject)
   NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
   tmp->UnlinkHostObjectURIs();
@@ -85,18 +86,19 @@ const js::Class SimpleGlobalClass = {
     &SimpleGlobalClassExtension,
     JS_NULL_OBJECT_OPS
 };
 
 // static
 JSObject*
 SimpleGlobalObject::Create(GlobalType globalType, JS::Handle<JS::Value> proto)
 {
-  JSContext* cx = nsContentUtils::GetDefaultJSContextForThread();
-  JSAutoRequest ar(cx);
+  AutoJSAPI jsapi;
+  jsapi.Init();
+  JSContext* cx = jsapi.cx();
 
   JS::CompartmentOptions options;
   options.creationOptions().setInvisibleToDebugger(true);
 
   JS::Rooted<JSObject*> global(cx);
 
   if (NS_IsMainThread()) {
     nsCOMPtr<nsIPrincipal> principal = nsNullPrincipal::Create();
@@ -106,17 +108,17 @@ SimpleGlobalObject::Create(GlobalType gl
                                      options);
   } else {
     global = JS_NewGlobalObject(cx, js::Jsvalify(&SimpleGlobalClass),
                                 nullptr,
                                 JS::DontFireOnNewGlobalHook, options);
   }
 
   if (!global) {
-    JS_ClearPendingException(cx);
+    jsapi.ClearException();
     return nullptr;
   }
 
   JSAutoCompartment ac(cx, global);
 
   // It's important to create the nsIGlobalObject for our new global before we
   // start trying to wrap things like the prototype into its compartment,
   // because the wrap operation relies on the global having its nsIGlobalObject
@@ -125,22 +127,22 @@ SimpleGlobalObject::Create(GlobalType gl
     new SimpleGlobalObject(global, globalType);
 
   // Pass on ownership of globalObject to |global|.
   JS_SetPrivate(global, globalObject.forget().take());
 
   if (proto.isObjectOrNull()) {
     JS::Rooted<JSObject*> protoObj(cx, proto.toObjectOrNull());
     if (!JS_WrapObject(cx, &protoObj)) {
-      JS_ClearPendingException(cx);
+      jsapi.ClearException();
       return nullptr;
     }
 
     if (!JS_SplicePrototype(cx, global, protoObj)) {
-      JS_ClearPendingException(cx);
+      jsapi.ClearException();
       return nullptr;
     }
   } else if (!proto.isUndefined()) {
     // Bogus proto.
     return nullptr;
   }
 
   JS_FireOnNewGlobalObject(cx, global);