From f56f915374f8b8de0746a1e68e0cf61ad74f2d14 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Sun, 20 Apr 2014 11:48:13 -0700 Subject: [PATCH] Bug 993772 - Introduce a singleton compilation scope. r=mrbkap --- js/xpconnect/src/XPCJSRuntime.cpp | 32 +++++++++++++++++++++++++++++-- js/xpconnect/src/nsXPConnect.cpp | 2 +- js/xpconnect/src/xpcprivate.h | 4 +++- js/xpconnect/src/xpcpublic.h | 8 ++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/js/xpconnect/src/XPCJSRuntime.cpp b/js/xpconnect/src/XPCJSRuntime.cpp index 8a6cff93596..770d336b3a0 100644 --- a/js/xpconnect/src/XPCJSRuntime.cpp +++ b/js/xpconnect/src/XPCJSRuntime.cpp @@ -587,6 +587,14 @@ GetJunkScopeGlobal() return GetNativeForGlobal(junkScope); } +JSObject * +GetCompilationScope() +{ + XPCJSRuntime *self = nsXPConnect::GetRuntimeInstance(); + NS_ENSURE_TRUE(self, nullptr); + return self->GetCompilationScope(); +} + JSObject * GetSafeJSContextGlobal() { @@ -3045,6 +3053,7 @@ XPCJSRuntime::XPCJSRuntime(nsXPConnect* aXPConnect) mObjectHolderRoots(nullptr), mWatchdogManager(new WatchdogManager(MOZ_THIS_IN_INITIALIZER_LIST())), mJunkScope(MOZ_THIS_IN_INITIALIZER_LIST()->Runtime(), nullptr), + mCompilationScope(MOZ_THIS_IN_INITIALIZER_LIST()->Runtime(), nullptr), mAsyncSnowWhiteFreer(new AsyncFreeSnowWhite()) { DOM_InitInterfaces(); @@ -3476,7 +3485,7 @@ XPCJSRuntime::GetJunkScope() if (!mJunkScope) { AutoSafeJSContext cx; SandboxOptions options; - options.sandboxName.AssignASCII("XPConnect Junk Compartment"); + options.sandboxName.AssignLiteral("XPConnect Junk Compartment"); RootedValue v(cx); nsresult rv = CreateSandboxObject(cx, &v, nsContentUtils::GetSystemPrincipal(), options); NS_ENSURE_SUCCESS(rv, nullptr); @@ -3486,8 +3495,27 @@ XPCJSRuntime::GetJunkScope() return mJunkScope; } +JSObject * +XPCJSRuntime::GetCompilationScope() +{ + if (!mCompilationScope) { + AutoSafeJSContext cx; + SandboxOptions options; + options.sandboxName.AssignLiteral("XPConnect Compilation Compartment"); + options.invisibleToDebugger = true; + RootedValue v(cx); + nsresult rv = CreateSandboxObject(cx, &v, /* principal = */ nullptr, options); + NS_ENSURE_SUCCESS(rv, nullptr); + + mCompilationScope = js::UncheckedUnwrap(&v.toObject()); + } + return mCompilationScope; +} + + void -XPCJSRuntime::DeleteJunkScope() +XPCJSRuntime::DeleteSingletonScopes() { mJunkScope = nullptr; + mCompilationScope = nullptr; } diff --git a/js/xpconnect/src/nsXPConnect.cpp b/js/xpconnect/src/nsXPConnect.cpp index e9e50bb7f90..b6f671a53de 100644 --- a/js/xpconnect/src/nsXPConnect.cpp +++ b/js/xpconnect/src/nsXPConnect.cpp @@ -88,7 +88,7 @@ nsXPConnect::nsXPConnect() nsXPConnect::~nsXPConnect() { - mRuntime->DeleteJunkScope(); + mRuntime->DeleteSingletonScopes(); mRuntime->DestroyJSContextStack(); // In order to clean up everything properly, we need to GC twice: once now, diff --git a/js/xpconnect/src/xpcprivate.h b/js/xpconnect/src/xpcprivate.h index 54d1c8cc081..ef0696381e0 100644 --- a/js/xpconnect/src/xpcprivate.h +++ b/js/xpconnect/src/xpcprivate.h @@ -557,7 +557,8 @@ public: AutoMarkingPtr** GetAutoRootsAdr() {return &mAutoRoots;} JSObject* GetJunkScope(); - void DeleteJunkScope(); + JSObject* GetCompilationScope(); + void DeleteSingletonScopes(); PRTime GetWatchdogTimestamp(WatchdogTimestampCategory aCategory); void OnAfterProcessNextEvent() { mSlowScriptCheckpoint = mozilla::TimeStamp(); } @@ -598,6 +599,7 @@ private: nsRefPtr mWatchdogManager; JS::GCSliceCallback mPrevGCSliceCallback; JS::PersistentRootedObject mJunkScope; + JS::PersistentRootedObject mCompilationScope; nsRefPtr mAsyncSnowWhiteFreer; mozilla::TimeStamp mSlowScriptCheckpoint; diff --git a/js/xpconnect/src/xpcpublic.h b/js/xpconnect/src/xpcpublic.h index 14f51b4b44d..4a01a97ba78 100644 --- a/js/xpconnect/src/xpcpublic.h +++ b/js/xpconnect/src/xpcpublic.h @@ -435,6 +435,14 @@ GetJunkScope(); nsIGlobalObject * GetJunkScopeGlobal(); +/** + * Shared compilation scope for XUL prototype documents and XBL + * precompilation. This compartment has a null principal. No code may run, and + * it is invisible to the debugger. + */ +JSObject * +GetCompilationScope(); + /** * If |aObj| is a window, returns the associated nsGlobalWindow. * Otherwise, returns null.