Bug 1407735 - Make the JS loader XPCOM component use a singleton. r=kmag

mozJSComponentLoader is created using XPCOM. However, you can only
call it once or it'll crash. This patch fixes that by using a
singleton macro.

MozReview-Commit-ID: Bq2k7nv9dKA

--HG--
extra : rebase_source : d2008da7628edf5db283c8a44c17e741f7ae0a96
This commit is contained in:
Andrew McCreight 2017-10-23 13:53:58 -07:00
parent f5fb237b0f
commit cd3f453103
3 changed files with 13 additions and 2 deletions

View File

@ -207,8 +207,16 @@ mozJSComponentLoader::mozJSComponentLoader()
mLoaderGlobal(dom::RootingCx())
{
MOZ_ASSERT(!sSelf, "mozJSComponentLoader should be a singleton");
}
sSelf = this;
// static
already_AddRefed<mozJSComponentLoader>
mozJSComponentLoader::GetOrCreate()
{
if (!sSelf) {
sSelf = new mozJSComponentLoader();
}
return do_AddRef(sSelf);
}
#define ENSURE_DEP(name) { nsresult rv = Ensure##name(); NS_ENSURE_SUCCESS(rv, rv); }

View File

@ -54,6 +54,8 @@ class mozJSComponentLoader final : public mozilla::ModuleLoader,
void FindTargetObject(JSContext* aCx,
JS::MutableHandleObject aTargetObject);
static already_AddRefed<mozJSComponentLoader> GetOrCreate();
static mozJSComponentLoader* Get() { return sSelf; }
nsresult Import(const nsACString& aResourceURI, JS::HandleValue aTargetObj,

View File

@ -24,7 +24,8 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsJSID)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIXPConnect,
nsXPConnect::GetSingleton)
NS_GENERIC_FACTORY_CONSTRUCTOR(mozJSComponentLoader)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(mozJSComponentLoader,
mozJSComponentLoader::GetOrCreate);
NS_GENERIC_FACTORY_CONSTRUCTOR(mozJSSubScriptLoader)
NS_DEFINE_NAMED_CID(NS_JS_ID_CID);