diff --git a/js/src/xpconnect/loader/mozJSComponentLoader.cpp b/js/src/xpconnect/loader/mozJSComponentLoader.cpp index 8ccd33ef6d4..6febccbeb08 100644 --- a/js/src/xpconnect/loader/mozJSComponentLoader.cpp +++ b/js/src/xpconnect/loader/mozJSComponentLoader.cpp @@ -303,8 +303,7 @@ class JSCLContextHelper { public: JSCLContextHelper(mozJSComponentLoader* loader); - ~JSCLContextHelper(); - JSBool ok(); + ~JSCLContextHelper() { Pop(); } JSContext* Pop(); @@ -320,13 +319,6 @@ private: const JSCLContextHelper& operator=(const JSCLContextHelper &); // not implemented }; -JSCLContextHelper::~JSCLContextHelper() -{ - if (mContext) { - Pop(); - JS_DestroyContext(mContext); - } -} class JSCLAutoErrorReporterSetter { @@ -570,10 +562,6 @@ mozJSComponentLoader::~mozJSComponentLoader() UnloadModules(); } - if (mContext) { - JS_DestroyContext(mContext); - } - NS_ASSERTION(!mFastLoadTimer, "Fastload file should have been closed via xpcom-shutdown"); @@ -587,13 +575,7 @@ NS_IMPL_ISUPPORTS3(mozJSComponentLoader, nsIModuleLoader, xpcIJSModuleLoader, nsIObserver) - -JSBool -JSCLContextHelper::ok() -{ - return !!mContext; -} - + nsresult mozJSComponentLoader::ReallyInit() { @@ -610,15 +592,38 @@ mozJSComponentLoader::ReallyInit() NS_FAILED(rv = mRuntimeService->GetRuntime(&mRuntime))) return rv; - // This context is for use by UnloadModules - mContext = JS_NewContext(mozJSComponentLoader::sSelf->mRuntime, 256); - if (!mContext) - return NS_ERROR_OUT_OF_MEMORY; - mContextStack = do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv); if (NS_FAILED(rv)) return rv; + // Create our compilation context. + mContext = JS_NewContext(mRuntime, 256); + if (!mContext) + return NS_ERROR_OUT_OF_MEMORY; + + uint32 options = JS_GetOptions(mContext); + JS_SetOptions(mContext, options | JSOPTION_XML); + + // Always use the latest js version + JS_SetVersion(mContext, JSVERSION_LATEST); + + // Limit C stack consumption to a reasonable 512K + int stackDummy; + const jsuword kStackSize = 0x80000; + jsuword stackLimit, currentStackAddr = (jsuword)&stackDummy; + +#if JS_STACK_GROWTH_DIRECTION < 0 + stackLimit = (currentStackAddr > kStackSize) + ? currentStackAddr - kStackSize + : 0; +#else + stackLimit = (currentStackAddr + kStackSize > currentStackAddr) + ? currentStackAddr + kStackSize + : (jsuword) -1; +#endif + + JS_SetThreadStackLimit(mContext, stackLimit); + #ifndef XPCONNECT_STANDALONE nsCOMPtr secman = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID); @@ -719,8 +724,6 @@ mozJSComponentLoader::LoadModule(nsILocalFile* aComponentFile, return rv; JSCLContextHelper cx(this); - if (!cx.ok()) - return NS_ERROR_OUT_OF_MEMORY; JSObject* cm_jsobj; nsCOMPtr cm_holder; @@ -1112,14 +1115,12 @@ mozJSComponentLoader::GlobalForLocation(nsILocalFile *aComponent, JSPrincipals* jsPrincipals = nsnull; JSCLContextHelper cx(this); - if (!cx.ok()) - return NS_ERROR_OUT_OF_MEMORY; #ifndef XPCONNECT_STANDALONE rv = mSystemPrincipal->GetJSPrincipals(cx, &jsPrincipals); NS_ENSURE_SUCCESS(rv, rv); - JSPrincipalsHolder princHolder(cx, jsPrincipals); + JSPrincipalsHolder princHolder(mContext, jsPrincipals); #endif nsCOMPtr backstagePass; @@ -1409,7 +1410,9 @@ mozJSComponentLoader::UnloadModules() mImports.Clear(); mModules.Clear(); - // XXX We want to force a GC. + // Destroying our context will force a GC. + JS_DestroyContext(mContext); + mContext = nsnull; mRuntimeService = nsnull; mContextStack = nsnull; @@ -1581,29 +1584,27 @@ mozJSComponentLoader::ImportInto(const nsACString & aLocation, jsval symbols; if (targetObj) { - JSCLContextHelper cx(this); - if (!cx.ok()) - return NS_ERROR_OUT_OF_MEMORY; + JSCLContextHelper cxhelper(this); - if (!JS_GetProperty(cx, mod->global, + if (!JS_GetProperty(mContext, mod->global, "EXPORTED_SYMBOLS", &symbols)) { - return ReportOnCaller(cx, ERROR_NOT_PRESENT, + return ReportOnCaller(cxhelper, ERROR_NOT_PRESENT, PromiseFlatCString(aLocation).get()); } JSObject *symbolsObj = nsnull; if (!JSVAL_IS_OBJECT(symbols) || !(symbolsObj = JSVAL_TO_OBJECT(symbols)) || - !JS_IsArrayObject(cx, symbolsObj)) { - return ReportOnCaller(cx, ERROR_NOT_AN_ARRAY, + !JS_IsArrayObject(mContext, symbolsObj)) { + return ReportOnCaller(cxhelper, ERROR_NOT_AN_ARRAY, PromiseFlatCString(aLocation).get()); } // Iterate over symbols array, installing symbols on targetObj: jsuint symbolCount = 0; - if (!JS_GetArrayLength(cx, symbolsObj, &symbolCount)) { - return ReportOnCaller(cx, ERROR_GETTING_ARRAY_LENGTH, + if (!JS_GetArrayLength(mContext, symbolsObj, &symbolCount)) { + return ReportOnCaller(cxhelper, ERROR_GETTING_ARRAY_LENGTH, PromiseFlatCString(aLocation).get()); } @@ -1615,23 +1616,23 @@ mozJSComponentLoader::ImportInto(const nsACString & aLocation, jsval val; JSString *symbolName; - if (!JS_GetElement(cx, symbolsObj, i, &val) || + if (!JS_GetElement(mContext, symbolsObj, i, &val) || !JSVAL_IS_STRING(val)) { - return ReportOnCaller(cx, ERROR_ARRAY_ELEMENT, + return ReportOnCaller(cxhelper, ERROR_ARRAY_ELEMENT, PromiseFlatCString(aLocation).get(), i); } symbolName = JSVAL_TO_STRING(val); - if (!JS_GetProperty(cx, mod->global, + if (!JS_GetProperty(mContext, mod->global, JS_GetStringBytes(symbolName), &val)) { - return ReportOnCaller(cx, ERROR_GETTING_SYMBOL, + return ReportOnCaller(cxhelper, ERROR_GETTING_SYMBOL, PromiseFlatCString(aLocation).get(), JS_GetStringBytes(symbolName)); } - if (!JS_SetProperty(cx, targetObj, + if (!JS_SetProperty(mContext, targetObj, JS_GetStringBytes(symbolName), &val)) { - return ReportOnCaller(cx, ERROR_SETTING_SYMBOL, + return ReportOnCaller(cxhelper, ERROR_SETTING_SYMBOL, PromiseFlatCString(aLocation).get(), JS_GetStringBytes(symbolName)); } @@ -1683,43 +1684,14 @@ mozJSComponentLoader::Observe(nsISupports *subject, const char *topic, //---------------------------------------------------------------------- JSCLContextHelper::JSCLContextHelper(mozJSComponentLoader *loader) - : mContextThread(0), + : mContext(loader->mContext), mContextThread(0), mContextStack(loader->mContextStack) { - // Create our compilation context. - mContext = JS_NewContext(mozJSComponentLoader::sSelf->mRuntime, 256); - if (!mContext) - return; - - uint32 options = JS_GetOptions(mContext); - JS_SetOptions(mContext, options | JSOPTION_XML); - - // Always use the latest js version - JS_SetVersion(mContext, JSVERSION_LATEST); - - // Limit C stack consumption to a reasonable 512K - int stackDummy; - const jsuword kStackSize = 0x80000; - - jsuword stackLimit, currentStackAddr = (jsuword)&stackDummy; - -#if JS_STACK_GROWTH_DIRECTION < 0 - stackLimit = (currentStackAddr > kStackSize) - ? currentStackAddr - kStackSize - : 0; -#else - stackLimit = (currentStackAddr + kStackSize > currentStackAddr) - ? currentStackAddr + kStackSize - : (jsuword) -1; -#endif - - JS_SetThreadStackLimit(mContext, stackLimit); - mContextStack->Push(mContext); mContextThread = JS_GetContextThread(mContext); if (mContextThread) { JS_BeginRequest(mContext); - } + } } // Pops the context that was pushed and then returns the context that is now at