Bug 955735 - Reorganize the code in nsScriptLoader to eliminate some rooting hazards; r=smaug

--HG--
extra : rebase_source : db78c06d3eaa5539e906bfc95650abd8aac9be38
This commit is contained in:
Terrence Cole 2013-12-31 08:30:07 -08:00
parent 43f9a33d7a
commit 745f70c9d0
3 changed files with 24 additions and 21 deletions

View File

@ -784,16 +784,18 @@ nsScriptLoader::AttemptAsyncScriptParse(nsScriptLoadRequest* aRequest)
return NS_ERROR_FAILURE;
}
JSObject *unrootedGlobal;
nsCOMPtr<nsIScriptContext> context = GetScriptContext(&unrootedGlobal);
nsCOMPtr<nsIScriptGlobalObject> globalObject = GetScriptGlobalObject();
if (!globalObject) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIScriptContext> context = globalObject->GetScriptContext();
if (!context) {
return NS_ERROR_FAILURE;
}
JSContext* unpushedCx = context->GetNativeContext();
JSAutoRequest ar(unpushedCx);
JS::Rooted<JSObject*> global(unpushedCx, unrootedGlobal);
AutoPushJSContext cx(unpushedCx);
AutoPushJSContext cx(context->GetNativeContext());
JS::Rooted<JSObject*> global(cx, globalObject->GetGlobalJSObject());
JS::CompileOptions options(cx);
FillCompileOptionsForRequest(aRequest, global, &options);
@ -934,8 +936,8 @@ nsScriptLoader::FireScriptEvaluated(nsresult aResult,
aRequest->FireScriptEvaluated(aResult);
}
nsIScriptContext *
nsScriptLoader::GetScriptContext(JSObject **aGlobal)
already_AddRefed<nsIScriptGlobalObject>
nsScriptLoader::GetScriptGlobalObject()
{
nsPIDOMWindow *pwin = mDocument->GetInnerWindow();
if (!pwin) {
@ -951,8 +953,7 @@ nsScriptLoader::GetScriptContext(JSObject **aGlobal)
return nullptr;
}
*aGlobal = globalObject->GetGlobalJSObject();
return globalObject->GetScriptContext();
return globalObject.forget();
}
void
@ -997,19 +998,21 @@ nsScriptLoader::EvaluateScript(nsScriptLoadRequest* aRequest,
// Get the script-type to be used by this element.
NS_ASSERTION(scriptContent, "no content - what is default script-type?");
nsCOMPtr<nsIScriptGlobalObject> globalObject = GetScriptGlobalObject();
if (!globalObject) {
return NS_ERROR_FAILURE;
}
// Make sure context is a strong reference since we access it after
// we've executed a script, which may cause all other references to
// the context to go away.
JSObject *unrootedGlobal;
nsCOMPtr<nsIScriptContext> context = GetScriptContext(&unrootedGlobal);
nsCOMPtr<nsIScriptContext> context = globalObject->GetScriptContext();
if (!context) {
return NS_ERROR_FAILURE;
}
JSContext* unpushedCx = context->GetNativeContext();
JSAutoRequest ar(unpushedCx);
JS::Rooted<JSObject*> global(unpushedCx, unrootedGlobal);
AutoPushJSContext cx(unpushedCx);
AutoPushJSContext cx(context->GetNativeContext());
JS::Rooted<JSObject*> global(cx, globalObject->GetGlobalJSObject());
bool oldProcessingScriptTag = context->GetProcessingScriptTag();
context->SetProcessingScriptTag(true);

View File

@ -65,7 +65,7 @@ public:
{
mObservers.RemoveObject(aObserver);
}
/**
* Process a script element. This will include both loading the
* source of the element if it is not inline and evaluating
@ -280,7 +280,7 @@ private:
const nsAFlatString& aScript,
void **aOffThreadToken);
nsIScriptContext *GetScriptContext(JSObject **aGlobal);
already_AddRefed<nsIScriptGlobalObject> GetScriptGlobalObject();
void FillCompileOptionsForRequest(nsScriptLoadRequest *aRequest,
JS::Handle<JSObject *> scopeChain,
JS::CompileOptions *aOptions);
@ -338,14 +338,14 @@ public:
mLoader->SetEnabled(false);
}
}
~nsAutoScriptLoaderDisabler()
{
if (mWasEnabled) {
mLoader->SetEnabled(true);
}
}
bool mWasEnabled;
nsRefPtr<nsScriptLoader> mLoader;
};

View File

@ -1,3 +1,3 @@
{
"expect-hazards": 3
"expect-hazards": 1
}