diff --git a/js/xpconnect/src/nsCxPusher.cpp b/js/xpconnect/src/nsCxPusher.cpp index c206d171cbea..f24a00066400 100644 --- a/js/xpconnect/src/nsCxPusher.cpp +++ b/js/xpconnect/src/nsCxPusher.cpp @@ -115,6 +115,7 @@ AutoCxPusher::AutoCxPusher(JSContext* cx, bool allowNull) if (!stack->Push(cx)) { MOZ_CRASH(); } + mStackDepthAfterPush = stack->Count(); #ifdef DEBUG mPushedContext = cx; @@ -163,6 +164,14 @@ AutoCxPusher::~AutoCxPusher() mScx = nullptr; } +bool +AutoCxPusher::IsStackTop() +{ + uint32_t currentDepth = XPCJSRuntime::Get()->GetJSContextStack()->Count(); + MOZ_ASSERT(currentDepth >= mStackDepthAfterPush); + return currentDepth == mStackDepthAfterPush; +} + AutoJSContext::AutoJSContext(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL) : mCx(nullptr) { diff --git a/js/xpconnect/src/nsCxPusher.h b/js/xpconnect/src/nsCxPusher.h index 0517147b1e88..a9a0fa64fd23 100644 --- a/js/xpconnect/src/nsCxPusher.h +++ b/js/xpconnect/src/nsCxPusher.h @@ -31,10 +31,15 @@ public: nsIScriptContext* GetScriptContext() { return mScx; } + // Returns true if this AutoCxPusher performed the push that is currently at + // the top of the cx stack. + bool IsStackTop(); + private: mozilla::Maybe mAutoRequest; mozilla::Maybe mAutoCompartment; nsCOMPtr mScx; + uint32_t mStackDepthAfterPush; #ifdef DEBUG JSContext* mPushedContext; unsigned mCompartmentDepthOnEntry;