Bug 767938 part 11. Move the "safe JS context" to where it belongs: the CycleCollectedJSRuntime. r=bholley

This commit is contained in:
Boris Zbarsky 2016-06-24 14:19:51 -04:00
Родитель 9b1f1daa04
Коммит 759bad9a0b
7 изменённых файлов: 19 добавлений и 87 удалений

Просмотреть файл

@ -1,46 +0,0 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set ts=8 sts=4 et sw=4 tw=99: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
/* Implement global service to track stack of JSContext. */
#include "xpcprivate.h"
#include "XPCWrapper.h"
#include "nsDOMJSUtils.h"
#include "nsNullPrincipal.h"
#include "mozilla/dom/BindingUtils.h"
using namespace mozilla;
using namespace JS;
using namespace xpc;
/***************************************************************************/
XPCJSContextStack::~XPCJSContextStack()
{
if (mSafeJSContext) {
mSafeJSContext = nullptr;
}
}
JSContext*
XPCJSContextStack::GetSafeJSContext()
{
MOZ_ASSERT(mSafeJSContext);
return mSafeJSContext;
}
void
XPCJSContextStack::InitSafeJSContext()
{
MOZ_ASSERT(!mSafeJSContext);
mSafeJSContext = JS_GetContext(mRuntime->Runtime());
if (!JS::InitSelfHostedCode(mSafeJSContext))
MOZ_CRASH("InitSelfHostedCode failed");
if (!mRuntime->JSContextInitialized(mSafeJSContext))
MOZ_CRASH("JSContextCreated failed");
}

Просмотреть файл

@ -1546,12 +1546,6 @@ CompartmentPrivate::SizeOfIncludingThis(MallocSizeOf mallocSizeOf)
/***************************************************************************/
void XPCJSRuntime::DestroyJSContextStack()
{
delete mJSContextStack;
mJSContextStack = nullptr;
}
void XPCJSRuntime::SystemIsBeingShutDown()
{
for (auto i = mDetachedWrappedNativeProtoMap->Iter(); !i.Done(); i.Next()) {
@ -3386,8 +3380,7 @@ static const JSWrapObjectCallbacks WrapObjectCallbacks = {
};
XPCJSRuntime::XPCJSRuntime()
: mJSContextStack(new XPCJSContextStack(this)),
mCallContext(nullptr),
: mCallContext(nullptr),
mAutoRoots(nullptr),
mResolveName(JSID_VOID),
mResolvingWrapper(nullptr),
@ -3777,6 +3770,7 @@ XPCJSRuntime::DebugDump(int16_t depth)
XPC_LOG_ALWAYS(("XPCJSRuntime @ %x", this));
XPC_LOG_INDENT();
XPC_LOG_ALWAYS(("mJSRuntime @ %x", Runtime()));
XPC_LOG_ALWAYS(("mJSContext @ %x", Context()));
XPC_LOG_ALWAYS(("mWrappedJSClassMap @ %x with %d wrapperclasses(s)",
mWrappedJSClassMap, mWrappedJSClassMap->Count()));
@ -3884,7 +3878,7 @@ void
XPCJSRuntime::InitSingletonScopes()
{
// This all happens very early, so we don't bother with cx pushing.
JSContext* cx = GetJSContextStack()->GetSafeJSContext();
JSContext* cx = Context();
JSAutoRequest ar(cx);
RootedValue v(cx);
nsresult rv;

Просмотреть файл

@ -22,7 +22,6 @@ UNIFIED_SOURCES += [
'XPCConvert.cpp',
'XPCDebug.cpp',
'XPCException.cpp',
'XPCJSContextStack.cpp',
'XPCJSID.cpp',
'XPCJSRuntime.cpp',
'XPCJSWeakReference.cpp',

Просмотреть файл

@ -72,7 +72,6 @@ nsXPConnect::nsXPConnect()
nsXPConnect::~nsXPConnect()
{
mRuntime->DeleteSingletonScopes();
mRuntime->DestroyJSContextStack();
// In order to clean up everything properly, we need to GC twice: once now,
// to clean anything that can go away on its own (like the Junk Scope, which
@ -124,8 +123,10 @@ nsXPConnect::InitStatics()
gScriptSecurityManager->GetSystemPrincipal(&gSystemPrincipal);
MOZ_RELEASE_ASSERT(gSystemPrincipal);
// Initialize the SafeJSContext.
gSelf->mRuntime->GetJSContextStack()->InitSafeJSContext();
if (!JS::InitSelfHostedCode(gSelf->mRuntime->Context()))
MOZ_CRASH("InitSelfHostedCode failed");
if (!gSelf->mRuntime->JSContextInitialized(gSelf->mRuntime->Context()))
MOZ_CRASH("JSContextInitialized failed");
// Initialize our singleton scopes.
gSelf->mRuntime->InitSingletonScopes();
@ -982,7 +983,7 @@ nsXPConnect::JSToVariant(JSContext* ctx, HandleValue value, nsIVariant** _retval
JSContext*
nsXPConnect::GetSafeJSContext()
{
return GetRuntime()->GetJSContextStack()->GetSafeJSContext();
return GetRuntime()->Context();
}
nsIPrincipal*

Просмотреть файл

@ -354,7 +354,6 @@ private:
// In the current xpconnect system there can only be one XPCJSRuntime.
// So, xpconnect can only be used on one JSRuntime within the process.
class XPCJSContextStack;
class WatchdogManager;
enum WatchdogTimestampCategory
@ -420,9 +419,6 @@ public:
static XPCJSRuntime* newXPCJSRuntime();
static XPCJSRuntime* Get() { return nsXPConnect::XPConnect()->GetRuntime(); }
XPCJSContextStack* GetJSContextStack() {return mJSContextStack;}
void DestroyJSContextStack();
void RemoveWrappedJS(nsXPCWrappedJS* wrapper);
void AssertInvalidWrappedJSNotInTable(nsXPCWrappedJS* wrapper) const;
@ -614,7 +610,6 @@ private:
jsid mStrIDs[IDX_TOTAL_COUNT];
JS::Value mStrJSVals[IDX_TOTAL_COUNT];
XPCJSContextStack* mJSContextStack;
XPCCallContext* mCallContext;
AutoMarkingPtr* mAutoRoots;
jsid mResolveName;
@ -2666,27 +2661,6 @@ private:
};
/***************************************************************************/
// XPCJSContextStack is not actually an xpcom object, but xpcom calls are
// delegated to it as an implementation detail.
class XPCJSContextStack
{
public:
explicit XPCJSContextStack(XPCJSRuntime* aRuntime)
: mRuntime(aRuntime)
, mSafeJSContext(nullptr)
{ }
virtual ~XPCJSContextStack();
void InitSafeJSContext();
JSContext* GetSafeJSContext();
private:
XPCJSRuntime* mRuntime;
JSContext* mSafeJSContext;
};
/***************************************************************************/
// 'Components' object implementations. nsXPCComponentsBase has the
// less-privileged stuff that we're willing to expose to XBL.
@ -3172,7 +3146,7 @@ xpc_GetJSPrivate(JSObject* obj)
inline JSContext*
xpc_GetSafeJSContext()
{
return XPCJSRuntime::Get()->GetJSContextStack()->GetSafeJSContext();
return XPCJSRuntime::Get()->Context();
}
namespace xpc {

Просмотреть файл

@ -438,6 +438,7 @@ CycleCollectedJSRuntime::CycleCollectedJSRuntime()
: mGCThingCycleCollectorGlobal(sGCThingCycleCollectorGlobal)
, mJSZoneCycleCollectorGlobal(sJSZoneCycleCollectorGlobal)
, mJSRuntime(nullptr)
, mJSContext(nullptr)
, mPrevGCSliceCallback(nullptr)
, mPrevGCNurseryCollectionCallback(nullptr)
, mJSHolders(256)
@ -479,6 +480,7 @@ CycleCollectedJSRuntime::~CycleCollectedJSRuntime()
JS_DestroyRuntime(mJSRuntime);
mJSRuntime = nullptr;
mJSContext = nullptr;
nsCycleCollector_forgetJSRuntime();
mozilla::dom::DestroyScriptSettings();
@ -509,6 +511,7 @@ CycleCollectedJSRuntime::Initialize(JSRuntime* aParentRuntime,
if (!mJSRuntime) {
return NS_ERROR_OUT_OF_MEMORY;
}
mJSContext = JS_GetContext(mJSRuntime);
if (!JS_AddExtraGCRootsTracer(mJSRuntime, TraceBlackJS, this)) {
MOZ_CRASH("JS_AddExtraGCRootsTracer failed");

Просмотреть файл

@ -328,6 +328,12 @@ public:
return mJSRuntime;
}
JSContext* Context() const
{
MOZ_ASSERT(mJSContext);
return mJSContext;
}
protected:
JSRuntime* MaybeRuntime() const { return mJSRuntime; }
@ -394,6 +400,7 @@ private:
JSZoneParticipant mJSZoneCycleCollectorGlobal;
JSRuntime* mJSRuntime;
JSContext* mJSContext;
JS::GCSliceCallback mPrevGCSliceCallback;
JS::GCNurseryCollectionCallback mPrevGCNurseryCollectionCallback;