Bug 1466458 part 1 - Remove JSContext::enterRealmDepth_. r=luke
authorJan de Mooij <jdemooij@mozilla.com>
Thu, 07 Jun 2018 12:17:08 +0200
changeset 805261 b968ba27d88c395cd6221545fbd412543cbedce2
parent 805260 3f0ec67f9c5d2300758c05858ac23cc8b89b8d02
child 805262 60828108f4b8492d4de455e949be190b8e173af9
push id112610
push userbmo:gl@mozilla.com
push dateThu, 07 Jun 2018 15:48:24 +0000
reviewersluke
bugs1466458
milestone62.0a1
Bug 1466458 part 1 - Remove JSContext::enterRealmDepth_. r=luke
dom/base/nsJSUtils.cpp
js/src/jsfriendapi.cpp
js/src/jsfriendapi.h
js/src/vm/JSContext-inl.h
js/src/vm/JSContext.cpp
js/src/vm/JSContext.h
--- a/dom/base/nsJSUtils.cpp
+++ b/dom/base/nsJSUtils.cpp
@@ -96,17 +96,17 @@ nsJSUtils::CompileFunction(AutoJSAPI& js
                            JS::CompileOptions& aOptions,
                            const nsACString& aName,
                            uint32_t aArgCount,
                            const char** aArgArray,
                            const nsAString& aBody,
                            JSObject** aFunctionObject)
 {
   JSContext* cx = jsapi.cx();
-  MOZ_ASSERT(js::GetEnterRealmDepth(cx) > 0);
+  MOZ_ASSERT(js::GetContextRealm(cx));
   MOZ_ASSERT_IF(aScopeChain.length() != 0,
                 js::IsObjectInContextCompartment(aScopeChain[0], cx));
 
   // Do the junk Gecko is supposed to do before calling into JSAPI.
   for (size_t i = 0; i < aScopeChain.length(); ++i) {
     JS::ExposeObjectToActiveJS(aScopeChain[i]);
   }
 
--- a/js/src/jsfriendapi.cpp
+++ b/js/src/jsfriendapi.cpp
@@ -1331,24 +1331,16 @@ js::GetTestingFunctions(JSContext* cx)
         return nullptr;
 
     if (!DefineTestingFunctions(cx, obj, false, false))
         return nullptr;
 
     return obj;
 }
 
-#ifdef DEBUG
-JS_FRIEND_API(unsigned)
-js::GetEnterRealmDepth(JSContext* cx)
-{
-  return cx->getEnterRealmDepth();
-}
-#endif
-
 JS_FRIEND_API(void)
 js::SetDOMCallbacks(JSContext* cx, const DOMCallbacks* callbacks)
 {
     cx->runtime()->DOMcallbacks = callbacks;
 }
 
 JS_FRIEND_API(const DOMCallbacks*)
 js::GetDOMCallbacks(JSContext* cx)
--- a/js/src/jsfriendapi.h
+++ b/js/src/jsfriendapi.h
@@ -1212,21 +1212,16 @@ CastToJSFreeOp(FreeOp* fop)
 
 /**
  * Get an error type name from a JSExnType constant.
  * Returns nullptr for invalid arguments and JSEXN_INTERNALERR
  */
 extern JS_FRIEND_API(JSFlatString*)
 GetErrorTypeName(JSContext* cx, int16_t exnType);
 
-#ifdef JS_DEBUG
-extern JS_FRIEND_API(unsigned)
-GetEnterRealmDepth(JSContext* cx);
-#endif
-
 extern JS_FRIEND_API(RegExpShared*)
 RegExpToSharedNonInline(JSContext* cx, JS::HandleObject regexp);
 
 /* Implemented in CrossCompartmentWrapper.cpp. */
 typedef enum NukeReferencesToWindow {
     NukeWindowReferences,
     DontNukeWindowReferences
 } NukeReferencesToWindow;
--- a/js/src/vm/JSContext-inl.h
+++ b/js/src/vm/JSContext-inl.h
@@ -458,19 +458,16 @@ JSContext::runningWithTrustedPrincipals(
 }
 
 inline void
 JSContext::enterRealm(JS::Realm* realm)
 {
     // We should never enter a realm while in the atoms zone.
     MOZ_ASSERT_IF(zone(), !zone()->isAtomsZone());
 
-#ifdef DEBUG
-    enterRealmDepth_++;
-#endif
     realm->enter();
     setRealm(realm);
 }
 
 inline void
 JSContext::enterAtomsZone(const js::AutoLockForExclusiveAccess& lock)
 {
     // Only one thread can be in the atoms zone at a time.
@@ -490,30 +487,22 @@ JSContext::enterRealmOf(const T& target)
 }
 
 inline void
 JSContext::enterNullRealm()
 {
     // We should never enter a realm while in the atoms zone.
     MOZ_ASSERT_IF(zone(), !zone()->isAtomsZone());
 
-#ifdef DEBUG
-    enterRealmDepth_++;
-#endif
     setRealm(nullptr);
 }
 
 inline void
 JSContext::leaveRealm(JS::Realm* oldRealm)
 {
-    MOZ_ASSERT(hasEnteredRealm());
-#ifdef DEBUG
-    enterRealmDepth_--;
-#endif
-
     // Only call leave() after we've setRealm()-ed away from the current realm.
     JS::Realm* startingRealm = realm_;
     setRealm(oldRealm);
     if (startingRealm)
         startingRealm->leave();
 }
 
 inline void
--- a/js/src/vm/JSContext.cpp
+++ b/js/src/vm/JSContext.cpp
@@ -1214,19 +1214,16 @@ JSContext::alreadyReportedError()
 }
 
 JSContext::JSContext(JSRuntime* runtime, const JS::ContextOptions& options)
   : runtime_(runtime),
     kind_(ContextKind::HelperThread),
     helperThread_(nullptr),
     options_(options),
     arenas_(nullptr),
-#ifdef DEBUG
-    enterRealmDepth_(0),
-#endif
     jitActivation(nullptr),
     activation_(nullptr),
     profilingActivation_(nullptr),
     nativeStackBase(GetNativeStackBase()),
     entryMonitor(nullptr),
     noExecuteDebuggerTop(nullptr),
     activityCallback(nullptr),
     activityCallbackArg(nullptr),
--- a/js/src/vm/JSContext.h
+++ b/js/src/vm/JSContext.h
@@ -191,40 +191,24 @@ struct JSContext : public JS::RootingCon
     /*
      * "Entering" a realm changes cx->realm (which changes cx->global). Note
      * that this does not push an Activation so it's possible for the caller's
      * realm to be != cx->realm(). This is not a problem since, in general, most
      * places in the VM cannot know that they were called from script (e.g.,
      * they may have been called through the JSAPI via JS_CallFunction) and thus
      * cannot expect there is a scripted caller.
      *
-     * Realms should be entered/left in a LIFO fasion. The depth of this
-     * enter/leave stack is maintained by enterRealmDepth_ and queried by
-     * hasEnteredRealm.
+     * Realms should be entered/left in a LIFO fasion. To enter a realm, code
+     * should prefer using AutoRealm over JS::EnterRealm/JS::LeaveRealm.
      *
-     * To enter a realm, code should prefer using AutoRealm over
-     * manually calling cx->enterRealm/leaveRealm.
+     * Also note that the JIT can enter (same-compartment) realms without going
+     * through these methods - it will update cx->realm_ directly.
      */
-  protected:
-#ifdef DEBUG
-    js::ThreadData<unsigned> enterRealmDepth_;
-#endif
-
+  private:
     inline void setRealm(JS::Realm* realm);
-  public:
-#ifdef DEBUG
-    bool hasEnteredRealm() const {
-        return enterRealmDepth_ > 0;
-    }
-    unsigned getEnterRealmDepth() const {
-        return enterRealmDepth_;
-    }
-#endif
-
-  private:
     inline void enterRealm(JS::Realm* realm);
     inline void enterAtomsZone(const js::AutoLockForExclusiveAccess& lock);
 
     friend class js::AutoAtomsZone;
     friend class js::AutoRealm;
 
   public:
     template <typename T>