Skip to content

Commit

Permalink
Bug 1292892 part 1 - Stop using JSRuntime outside SpiderMonkey. r=bz,…
Browse files Browse the repository at this point in the history
…terrence,fitzgen,kanru
  • Loading branch information
jandem committed Aug 11, 2016
1 parent 9e5cf92 commit 0ad1251
Show file tree
Hide file tree
Showing 83 changed files with 427 additions and 482 deletions.
21 changes: 10 additions & 11 deletions caps/nsScriptSecurityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ using namespace mozilla::dom;

nsIIOService *nsScriptSecurityManager::sIOService = nullptr;
nsIStringBundle *nsScriptSecurityManager::sStrBundle = nullptr;
JSRuntime *nsScriptSecurityManager::sRuntime = 0;
JSContext *nsScriptSecurityManager::sContext = nullptr;
bool nsScriptSecurityManager::sStrictFileOriginPolicy = true;

///////////////////////////
Expand Down Expand Up @@ -1413,19 +1413,18 @@ nsresult nsScriptSecurityManager::Init()

//-- Register security check callback in the JS engine
// Currently this is used to control access to function.caller
sRuntime = xpc::GetJSRuntime();
sContext = danger::GetJSContext();

static const JSSecurityCallbacks securityCallbacks = {
ContentSecurityPolicyPermitsJSAction,
JSPrincipalsSubsume,
};

JSContext* cx = JS_GetContext(sRuntime);
MOZ_ASSERT(!JS_GetSecurityCallbacks(cx));
JS_SetSecurityCallbacks(cx, &securityCallbacks);
JS_InitDestroyPrincipalsCallback(cx, nsJSPrincipals::Destroy);
MOZ_ASSERT(!JS_GetSecurityCallbacks(sContext));
JS_SetSecurityCallbacks(sContext, &securityCallbacks);
JS_InitDestroyPrincipalsCallback(sContext, nsJSPrincipals::Destroy);

JS_SetTrustedPrincipals(cx, system);
JS_SetTrustedPrincipals(sContext, system);

return NS_OK;
}
Expand All @@ -1448,10 +1447,10 @@ nsScriptSecurityManager::~nsScriptSecurityManager(void)
void
nsScriptSecurityManager::Shutdown()
{
if (sRuntime) {
JS_SetSecurityCallbacks(JS_GetContext(sRuntime), nullptr);
JS_SetTrustedPrincipals(JS_GetContext(sRuntime), nullptr);
sRuntime = nullptr;
if (sContext) {
JS_SetSecurityCallbacks(sContext, nullptr);
JS_SetTrustedPrincipals(sContext, nullptr);
sContext = nullptr;
}

NS_IF_RELEASE(sIOService);
Expand Down
2 changes: 1 addition & 1 deletion caps/nsScriptSecurityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class nsScriptSecurityManager final : public nsIScriptSecurityManager,

static nsIIOService *sIOService;
static nsIStringBundle *sStrBundle;
static JSRuntime *sRuntime;
static JSContext *sContext;
};

namespace mozilla {
Expand Down
15 changes: 5 additions & 10 deletions devtools/shared/heapsnapshot/tests/gtest/DevTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,26 @@ using namespace testing;
// GTest fixture class that all of our tests derive from.
struct DevTools : public ::testing::Test {
bool _initialized;
JSRuntime* rt;
JSContext* cx;
JSCompartment* compartment;
JS::Zone* zone;
JS::PersistentRootedObject global;

DevTools()
: _initialized(false),
rt(nullptr),
cx(nullptr)
{ }

virtual void SetUp() {
MOZ_ASSERT(!_initialized);

rt = getRuntime();
if (!rt)
cx = getContext();
if (!cx)
return;

MOZ_RELEASE_ASSERT(!cx);
cx = JS_GetContext(rt);

JS_BeginRequest(cx);

global.init(rt, createGlobal());
global.init(cx, createGlobal());
if (!global)
return;
JS_EnterCompartment(cx, global);
Expand All @@ -64,8 +59,8 @@ struct DevTools : public ::testing::Test {
_initialized = true;
}

JSRuntime* getRuntime() {
return CycleCollectedJSRuntime::Get()->Runtime();
JSContext* getContext() {
return CycleCollectedJSRuntime::Get()->Context();
}

static void reportError(JSContext* cx, const char* message, JSErrorReport* report) {
Expand Down
4 changes: 2 additions & 2 deletions dom/base/DOMRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using mozilla::dom::DOMRequestService;
using mozilla::dom::DOMCursor;
using mozilla::dom::Promise;
using mozilla::dom::AutoJSAPI;
using mozilla::dom::GetJSRuntime;
using mozilla::dom::RootingCx;

DOMRequest::DOMRequest(nsPIDOMWindowInner* aWindow)
: DOMEventTargetHelper(aWindow)
Expand Down Expand Up @@ -302,7 +302,7 @@ class FireSuccessAsyncTask : public mozilla::Runnable
FireSuccessAsyncTask(DOMRequest* aRequest,
const JS::Value& aResult) :
mReq(aRequest),
mResult(GetJSRuntime(), aResult)
mResult(RootingCx(), aResult)
{
}

Expand Down
8 changes: 4 additions & 4 deletions dom/base/ScriptSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ GetJSContext()
}
} // namespace danger

JSRuntime*
GetJSRuntime()
js::RootingContext*
RootingCx()
{
return CycleCollectedJSRuntime::Get()->Runtime();
return CycleCollectedJSRuntime::Get()->RootingCx();
}

AutoJSAPI::AutoJSAPI()
Expand Down Expand Up @@ -590,7 +590,7 @@ AutoJSAPI::ReportException()
nsContentUtils::IsCallerChrome(),
inner ? inner->WindowID() : 0);
if (inner && jsReport.report()->errorNumber != JSMSG_OUT_OF_MEMORY) {
DispatchScriptErrorEvent(inner, JS_GetRuntime(cx()), xpcReport, exn);
DispatchScriptErrorEvent(inner, cx(), xpcReport, exn);
} else {
JS::Rooted<JSObject*> stack(cx(),
xpc::FindExceptionStackForConsoleReport(inner, exn));
Expand Down
2 changes: 1 addition & 1 deletion dom/base/ScriptSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ JSContext* GetJSContext();

} // namespace danger

JSRuntime* GetJSRuntime();
js::RootingContext* RootingCx();

class ScriptSettingsStack;
class ScriptSettingsStackEntry {
Expand Down
5 changes: 4 additions & 1 deletion dom/base/nsContentUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2790,7 +2790,10 @@ nsIPrincipal*
nsContentUtils::ObjectPrincipal(JSObject* aObj)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(JS_GetObjectRuntime(aObj) == CycleCollectedJSRuntime::Get()->Runtime());

#ifdef DEBUG
JS::AssertObjectBelongsToCurrentThread(aObj);
#endif

// This is duplicated from nsScriptSecurityManager. We don't call through there
// because the API unnecessarily requires a JSContext for historical reasons.
Expand Down
6 changes: 3 additions & 3 deletions dom/base/nsFrameMessageManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1711,8 +1711,8 @@ nsMessageManagerScriptExecutor::LoadScriptInternal(const nsAString& aURL,
return;
}

JSRuntime* rt = CycleCollectedJSRuntime::Get()->Runtime();
JS::Rooted<JSScript*> script(rt);
js::RootingContext* rcx = RootingCx();
JS::Rooted<JSScript*> script(rcx);

nsMessageManagerScriptHolder* holder = sCachedScripts->Get(aURL);
if (holder && holder->WillRunInGlobalScope() == aRunInGlobalScope) {
Expand All @@ -1725,7 +1725,7 @@ nsMessageManagerScriptExecutor::LoadScriptInternal(const nsAString& aURL,
shouldCache, &script);
}

JS::Rooted<JSObject*> global(rt, mGlobal->GetJSObject());
JS::Rooted<JSObject*> global(rcx, mGlobal->GetJSObject());
if (global) {
AutoEntryScript aes(global, "message manager script load");
JSContext* cx = aes.cx();
Expand Down
2 changes: 1 addition & 1 deletion dom/base/nsGlobalWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12209,7 +12209,7 @@ nsGlobalWindow::RunTimeoutHandler(nsTimeout* aTimeout,
// Hold strong ref to ourselves while we call the callback.
nsCOMPtr<nsISupports> me(static_cast<nsIDOMWindow *>(this));
ErrorResult rv;
JS::Rooted<JS::Value> ignoredVal(CycleCollectedJSRuntime::Get()->Runtime());
JS::Rooted<JS::Value> ignoredVal(RootingCx());
callback->Call(me, handler->GetArgs(), &ignoredVal, rv, reason);
if (rv.IsUncatchableException()) {
abortIntervalHandler = true;
Expand Down
3 changes: 1 addition & 2 deletions dom/base/nsINode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1531,8 +1531,7 @@ static nsresult
CheckForOutdatedParent(nsINode* aParent, nsINode* aNode)
{
if (JSObject* existingObjUnrooted = aNode->GetWrapper()) {
JSRuntime* runtime = JS_GetObjectRuntime(existingObjUnrooted);
JS::Rooted<JSObject*> existingObj(runtime, existingObjUnrooted);
JS::Rooted<JSObject*> existingObj(RootingCx(), existingObjUnrooted);

AutoJSContext cx;
nsIGlobalObject* global = aParent->OwnerDoc()->GetScopeObject();
Expand Down
24 changes: 10 additions & 14 deletions dom/base/nsJSEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ static nsScriptNameSpaceManager *gNameSpaceManager;

static PRTime sFirstCollectionTime;

static JSRuntime* sRuntime;
static JSContext* sContext;

static bool sIsInitialized;
Expand Down Expand Up @@ -246,8 +245,8 @@ FindExceptionStackForConsoleReport(nsPIDOMWindowInner* win,
return nullptr;
}

JSRuntime* rt = GetJSRuntime();
JS::RootedObject exceptionObject(rt, &exceptionValue.toObject());
js::RootingContext* rcx = RootingCx();
JS::RootedObject exceptionObject(rcx, &exceptionValue.toObject());
JSObject* stackObject = ExceptionStackOrNull(exceptionObject);
if (stackObject) {
return stackObject;
Expand All @@ -268,7 +267,7 @@ FindExceptionStackForConsoleReport(nsPIDOMWindowInner* win,
if (!stack) {
return nullptr;
}
JS::RootedValue value(rt);
JS::RootedValue value(rcx);
stack->GetNativeSavedFrame(&value);
if (value.isObject()) {
return &value.toObject();
Expand Down Expand Up @@ -422,12 +421,12 @@ class ScriptErrorEvent : public Runnable
{
public:
ScriptErrorEvent(nsPIDOMWindowInner* aWindow,
JSRuntime* aRuntime,
JSContext* aContext,
xpc::ErrorReport* aReport,
JS::Handle<JS::Value> aError)
: mWindow(aWindow)
, mReport(aReport)
, mError(aRuntime, aError)
, mError(aContext, aError)
{}

NS_IMETHOD Run() override
Expand Down Expand Up @@ -496,10 +495,10 @@ bool ScriptErrorEvent::sHandlingScriptError = false;
namespace xpc {

void
DispatchScriptErrorEvent(nsPIDOMWindowInner *win, JSRuntime *rt, xpc::ErrorReport *xpcReport,
DispatchScriptErrorEvent(nsPIDOMWindowInner *win, JSContext *cx, xpc::ErrorReport *xpcReport,
JS::Handle<JS::Value> exception)
{
nsContentUtils::AddScriptRunner(new ScriptErrorEvent(win, rt, xpcReport, exception));
nsContentUtils::AddScriptRunner(new ScriptErrorEvent(win, cx, xpcReport, exception));
}

} /* namespace xpc */
Expand Down Expand Up @@ -1196,7 +1195,7 @@ nsJSContext::GarbageCollectNow(JS::gcreason::Reason aReason,
sPendingLoadCount = 0;
sLoadingInProgress = false;

if (!nsContentUtils::XPConnect() || !sRuntime) {
if (!nsContentUtils::XPConnect() || !sContext) {
return;
}

Expand Down Expand Up @@ -2234,7 +2233,6 @@ mozilla::dom::StartupJSEnvironment()
sNeedsFullGC = false;
sNeedsGCAfterCC = false;
gNameSpaceManager = nullptr;
sRuntime = nullptr;
sContext = nullptr;
sIsInitialized = false;
sDidShutdown = false;
Expand Down Expand Up @@ -2378,13 +2376,11 @@ nsJSContext::EnsureStatics()
MOZ_CRASH();
}

sRuntime = xpc::GetJSRuntime();
if (!sRuntime) {
sContext = danger::GetJSContext();
if (!sContext) {
MOZ_CRASH();
}

sContext = JS_GetContext(sRuntime);

// Let's make sure that our main thread is the same as the xpcom main thread.
MOZ_ASSERT(NS_IsMainThread());

Expand Down
2 changes: 1 addition & 1 deletion dom/base/nsScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ nsScriptLoadRequest::MaybeCancelOffThreadScript()
return;
}

JSContext* cx = JS_GetContext(xpc::GetJSRuntime());
JSContext* cx = danger::GetJSContext();
JS::CancelOffThreadScript(cx, mOffThreadToken);
mOffThreadToken = nullptr;
}
Expand Down
2 changes: 1 addition & 1 deletion dom/bindings/BindingUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2448,7 +2448,7 @@ ConstructJSImplementation(const char* aContractId,
do_QueryInterface(implISupports);
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal);
if (gpi) {
JS::Rooted<JS::Value> initReturn(GetJSRuntime());
JS::Rooted<JS::Value> initReturn(RootingCx());
rv = gpi->Init(window, &initReturn);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
Expand Down
7 changes: 3 additions & 4 deletions dom/bindings/BindingUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,8 @@ struct VerifyTraceProtoAndIfaceCacheCalledTracer : public JS::CallbackTracer
{
bool ok;

explicit VerifyTraceProtoAndIfaceCacheCalledTracer(JSRuntime *rt)
: JS::CallbackTracer(rt), ok(false)
explicit VerifyTraceProtoAndIfaceCacheCalledTracer(JSContext* cx)
: JS::CallbackTracer(cx), ok(false)
{}

void onChild(const JS::GCCellPtr&) override {
Expand Down Expand Up @@ -3243,8 +3243,7 @@ WrappedJSToDictionary(nsISupports* aObject, T& aDictionary)
{
nsCOMPtr<nsIXPConnectWrappedJS> wrappedObj = do_QueryInterface(aObject);
NS_ENSURE_TRUE(wrappedObj, false);
JS::Rooted<JSObject*> obj(CycleCollectedJSRuntime::Get()->Runtime(),
wrappedObj->GetJSObject());
JS::Rooted<JSObject*> obj(RootingCx(), wrappedObj->GetJSObject());
NS_ENSURE_TRUE(obj, false);

nsIGlobalObject* global = xpc::NativeGlobal(obj);
Expand Down
2 changes: 1 addition & 1 deletion dom/devicestorage/nsDeviceStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3977,7 +3977,7 @@ DeviceStorageRequestManager::Resolve(uint32_t aId, uint64_t aValue,
return NS_OK;
}

JS::RootedValue value(GetJSRuntime(), JS_NumberValue((double)aValue));
JS::RootedValue value(RootingCx(), JS_NumberValue((double)aValue));
return ResolveInternal(i, value);
}

Expand Down
4 changes: 2 additions & 2 deletions dom/events/JSEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ JSEventHandler::HandleEvent(nsIDOMEvent* aEvent)
columnNumber.Construct();
columnNumber.Value() = scriptEvent->Colno();

error.Construct(GetJSRuntime());
error.Construct(RootingCx());
scriptEvent->GetError(&error.Value());
} else {
msgOrEvent.SetAsEvent() = aEvent->InternalDOMEvent();
Expand Down Expand Up @@ -210,7 +210,7 @@ JSEventHandler::HandleEvent(nsIDOMEvent* aEvent)
MOZ_ASSERT(mTypedHandler.Type() == TypedEventHandler::eNormal);
ErrorResult rv;
RefPtr<EventHandlerNonNull> handler = mTypedHandler.NormalEventHandler();
JS::Rooted<JS::Value> retval(CycleCollectedJSRuntime::Get()->Runtime());
JS::Rooted<JS::Value> retval(RootingCx());
handler->Call(mTarget, *(aEvent->InternalDOMEvent()), &retval, rv);
if (rv.Failed()) {
return rv.StealNSResult();
Expand Down
2 changes: 1 addition & 1 deletion dom/ipc/nsIContentChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace dom {
PJavaScriptChild*
nsIContentChild::AllocPJavaScriptChild()
{
return NewJavaScriptChild(xpc::GetJSRuntime());
return NewJavaScriptChild(danger::GetJSContext());
}

bool
Expand Down
2 changes: 1 addition & 1 deletion dom/ipc/nsIContentParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ nsIContentParent::AsContentBridgeParent()
PJavaScriptParent*
nsIContentParent::AllocPJavaScriptParent()
{
return NewJavaScriptParent(xpc::GetJSRuntime());
return NewJavaScriptParent(danger::GetJSContext());
}

bool
Expand Down
4 changes: 2 additions & 2 deletions dom/plugins/base/nsJSNPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ RegisterGCCallbacks()
}

// Register a callback to trace wrapped JSObjects.
JSContext* cx = JS_GetContext(xpc::GetJSRuntime());
JSContext* cx = dom::danger::GetJSContext();
if (!JS_AddExtraGCRootsTracer(cx, TraceJSObjWrappers, nullptr)) {
return false;
}
Expand All @@ -356,7 +356,7 @@ UnregisterGCCallbacks()
MOZ_ASSERT(sCallbackIsRegistered);

// Remove tracing callback.
JSContext* cx = JS_GetContext(xpc::GetJSRuntime());
JSContext* cx = dom::danger::GetJSContext();
JS_RemoveExtraGCRootsTracer(cx, TraceJSObjWrappers, nullptr);

// Remove delayed destruction callback.
Expand Down
Loading

0 comments on commit 0ad1251

Please sign in to comment.