From 8b48b15f08e9230095e6d05e52bb8a6dfb47d4b7 Mon Sep 17 00:00:00 2001 From: Blake Kaplan Date: Wed, 17 Mar 2010 17:38:16 -0700 Subject: [PATCH] Bug 552516 - Don't create a temporary context for the new sandbox object. r=igor --HG-- extra : rebase_source : 78b20a13d174f1ddf0b47556e9ea39fbe724c2cb --- js/src/xpconnect/src/xpccomponents.cpp | 24 ++++++++++++---------- js/src/xpconnect/src/xpcprivate.h | 28 -------------------------- 2 files changed, 13 insertions(+), 39 deletions(-) diff --git a/js/src/xpconnect/src/xpccomponents.cpp b/js/src/xpconnect/src/xpccomponents.cpp index 8b1e2ff1774..e1995d9aef7 100644 --- a/js/src/xpconnect/src/xpccomponents.cpp +++ b/js/src/xpconnect/src/xpccomponents.cpp @@ -3217,16 +3217,11 @@ xpc_CreateSandboxObject(JSContext * cx, jsval * vp, nsISupports *prinOrSop) if(NS_FAILED(rv)) return NS_ERROR_XPC_UNEXPECTED; - XPCAutoJSContext tempcx(JS_NewContext(JS_GetRuntime(cx), 1024), PR_FALSE); - if (!tempcx) - return NS_ERROR_OUT_OF_MEMORY; - - AutoJSRequestWithNoCallContext req(tempcx); - JSObject *sandbox = JS_NewObject(tempcx, &SandboxClass, nsnull, nsnull); + JSObject *sandbox = JS_NewObjectWithGivenProto(cx, &SandboxClass, + nsnull, nsnull); if (!sandbox) return NS_ERROR_XPC_UNEXPECTED; - - JS_SetGlobalObject(tempcx, sandbox); + JSAutoTempValueRooter tvr(cx, sandbox); nsCOMPtr sop(do_QueryInterface(prinOrSop)); @@ -3398,6 +3393,7 @@ class ContextHolder : public nsISupports { public: ContextHolder(JSContext *aOuterCx, JSObject *aSandbox); + virtual ~ContextHolder(); JSContext * GetJSContext() { @@ -3408,15 +3404,15 @@ public: private: static JSBool ContextHolderOperationCallback(JSContext *cx); - - XPCAutoJSContext mJSContext; + + JSContext* mJSContext; JSContext* mOrigCx; }; NS_IMPL_ISUPPORTS0(ContextHolder) ContextHolder::ContextHolder(JSContext *aOuterCx, JSObject *aSandbox) - : mJSContext(JS_NewContext(JS_GetRuntime(aOuterCx), 1024), JS_FALSE), + : mJSContext(JS_NewContext(JS_GetRuntime(aOuterCx), 1024)), mOrigCx(aOuterCx) { if(mJSContext) @@ -3431,6 +3427,12 @@ ContextHolder::ContextHolder(JSContext *aOuterCx, JSObject *aSandbox) } } +ContextHolder::~ContextHolder() +{ + if(mJSContext) + JS_DestroyContextNoGC(mJSContext); +} + JSBool ContextHolder::ContextHolderOperationCallback(JSContext *cx) { diff --git a/js/src/xpconnect/src/xpcprivate.h b/js/src/xpconnect/src/xpcprivate.h index b8dbb2a37da..9beed895381 100644 --- a/js/src/xpconnect/src/xpcprivate.h +++ b/js/src/xpconnect/src/xpcprivate.h @@ -399,34 +399,6 @@ private: static void operator delete(void* /*memory*/) {} }; -// A helper class to deal with temporary JS contexts. It destroys the context -// when it goes out of scope. -class XPCAutoJSContext -{ -public: - XPCAutoJSContext(JSContext *aContext, PRBool aGCOnDestroy) - : mContext(aContext), mGCOnDestroy(aGCOnDestroy) - { - } - - ~XPCAutoJSContext() - { - if(!mContext) - return; - - if(mGCOnDestroy) - JS_DestroyContext(mContext); - else - JS_DestroyContextNoGC(mContext); - } - - operator JSContext * () {return mContext;} - -private: - JSContext *mContext; - PRBool mGCOnDestroy; -}; - /*************************************************************************** **************************************************************************** *