diff --git a/js/src/xpconnect/src/xpccomponents.cpp b/js/src/xpconnect/src/xpccomponents.cpp index 6462dac1b417..f017fe11e36c 100644 --- a/js/src/xpconnect/src/xpccomponents.cpp +++ b/js/src/xpconnect/src/xpccomponents.cpp @@ -2246,7 +2246,11 @@ nsXPCComponents_utils_Sandbox::CallOrConstruct(nsIXPConnectWrappedNative *wrappe if(NS_FAILED(rv)) return ThrowAndFail(NS_ERROR_XPC_UNEXPECTED, cx, _retval); - JSObject *sandbox = JS_NewObject(cx, &SandboxClass, nsnull, nsnull); + XPCAutoJSContext tempcx(JS_NewContext(JS_GetRuntime(cx), 1024), PR_FALSE); + if (!tempcx) + return ThrowAndFail(NS_ERROR_OUT_OF_MEMORY, cx, _retval); + + JSObject *sandbox = JS_NewObject(tempcx, &SandboxClass, nsnull, nsnull); if (!sandbox) return ThrowAndFail(NS_ERROR_XPC_UNEXPECTED, cx, _retval); diff --git a/js/src/xpconnect/src/xpcprivate.h b/js/src/xpconnect/src/xpcprivate.h index 3b0bcad174c6..adecabeec7dc 100644 --- a/js/src/xpconnect/src/xpcprivate.h +++ b/js/src/xpconnect/src/xpcprivate.h @@ -388,6 +388,34 @@ 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; +}; + /*************************************************************************** **************************************************************************** *