Bug 1089026 part 4. Change the CompileFunction calls in the component loader and subscript loader to pass in their desired scope chains. r=bholley

This commit is contained in:
Boris Zbarsky 2014-10-30 19:40:29 -04:00
Родитель 5c64bd8645
Коммит 8c3d40d482
2 изменённых файлов: 55 добавлений и 17 удалений

Просмотреть файл

@ -457,7 +457,7 @@ mozJSComponentLoader::FindTargetObject(JSContext* aCx,
if (mReuseLoaderGlobal) { if (mReuseLoaderGlobal) {
JSFunction *fun = js::GetOutermostEnclosingFunctionOfScriptedCaller(aCx); JSFunction *fun = js::GetOutermostEnclosingFunctionOfScriptedCaller(aCx);
if (fun) { if (fun) {
JSObject *funParent = js::GetObjectParent(JS_GetFunctionObject(fun)); JSObject *funParent = js::GetObjectEnvironmentObjectForFunction(fun);
if (JS_GetClass(funParent) == &kFakeBackstagePassJSClass) if (JS_GetClass(funParent) == &kFakeBackstagePassJSClass)
targetObject = funParent; targetObject = funParent;
} }
@ -783,9 +783,14 @@ mozJSComponentLoader::ObjectForLocation(ComponentLoaderInfo &aInfo,
if (!mReuseLoaderGlobal) { if (!mReuseLoaderGlobal) {
Compile(cx, obj, options, buf, fileSize32, &script); Compile(cx, obj, options, buf, fileSize32, &script);
} else { } else {
CompileFunction(cx, obj, options, // Note: exceptions will get handled further down;
nullptr, 0, nullptr, // don't early return for them here.
buf, fileSize32, &function); AutoObjectVector scopeChain(cx);
if (scopeChain.append(obj)) {
CompileFunction(cx, scopeChain,
options, nullptr, 0, nullptr,
buf, fileSize32, &function);
}
} }
PR_MemUnmap(buf, fileSize32); PR_MemUnmap(buf, fileSize32);
@ -829,9 +834,14 @@ mozJSComponentLoader::ObjectForLocation(ComponentLoaderInfo &aInfo,
script = Compile(cx, obj, options, buf, script = Compile(cx, obj, options, buf,
fileSize32); fileSize32);
} else { } else {
function = CompileFunction(cx, obj, options, // Note: exceptions will get handled further down;
nullptr, 0, nullptr, // don't early return for them here.
buf, fileSize32); AutoObjectVector scopeChain(cx);
if (scopeChain.append(obj)) {
CompileFunction(cx, scopeChain,
options, nullptr, 0, nullptr,
buf, fileSize32, &function);
}
} }
free(buf); free(buf);
@ -869,9 +879,14 @@ mozJSComponentLoader::ObjectForLocation(ComponentLoaderInfo &aInfo,
if (!mReuseLoaderGlobal) { if (!mReuseLoaderGlobal) {
Compile(cx, obj, options, buf, bytesRead, &script); Compile(cx, obj, options, buf, bytesRead, &script);
} else { } else {
CompileFunction(cx, obj, options, // Note: exceptions will get handled further down;
nullptr, 0, nullptr, // don't early return for them here.
buf, bytesRead, &function); AutoObjectVector scopeChain(cx);
if (scopeChain.append(obj)) {
CompileFunction(cx, scopeChain,
options, nullptr, 0, nullptr,
buf, bytesRead, &function);
}
} }
} }
// Propagate the exception, if one exists. Also, don't leave the stale // Propagate the exception, if one exists. Also, don't leave the stale

Просмотреть файл

@ -90,6 +90,22 @@ ReportError(JSContext *cx, const char *msg)
return NS_OK; return NS_OK;
} }
// There probably aren't actually any consumers that rely on having the full
// scope chain up the parent chain of "obj" (instead of just having obj and then
// the global), but we do this for now to preserve backwards compat.
static bool
BuildScopeChainForObject(JSContext *cx, HandleObject obj,
AutoObjectVector &scopeChain)
{
RootedObject cur(cx, obj);
for ( ; cur && !JS_IsGlobalObject(cur); cur = JS_GetParent(cur)) {
if (!scopeChain.append(cur)) {
return false;
}
}
return true;
}
nsresult nsresult
mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *targetObjArg, mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *targetObjArg,
const nsAString &charset, const char *uriStr, const nsAString &charset, const char *uriStr,
@ -161,10 +177,13 @@ mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *targetObj
if (!reuseGlobal) { if (!reuseGlobal) {
JS::Compile(cx, target_obj, options, srcBuf, script); JS::Compile(cx, target_obj, options, srcBuf, script);
} else { } else {
JS::CompileFunction(cx, target_obj, options, AutoObjectVector scopeChain(cx);
nullptr, 0, nullptr, if (!BuildScopeChainForObject(cx, target_obj, scopeChain)) {
srcBuf, return NS_ERROR_OUT_OF_MEMORY;
function); }
// XXXbz do we really not care if the compile fails???
JS::CompileFunction(cx, scopeChain, options, nullptr, 0, nullptr,
srcBuf, function);
} }
} else { } else {
// We only use lazy source when no special encoding is specified because // We only use lazy source when no special encoding is specified because
@ -173,9 +192,13 @@ mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *targetObj
options.setSourceIsLazy(true); options.setSourceIsLazy(true);
JS::Compile(cx, target_obj, options, buf.get(), len, script); JS::Compile(cx, target_obj, options, buf.get(), len, script);
} else { } else {
JS::CompileFunction(cx, target_obj, options, AutoObjectVector scopeChain(cx);
nullptr, 0, nullptr, buf.get(), if (!BuildScopeChainForObject(cx, target_obj, scopeChain)) {
len, function); return NS_ERROR_OUT_OF_MEMORY;
}
// XXXbz do we really not care if the compile fails???
JS::CompileFunction(cx, scopeChain, options, nullptr, 0, nullptr,
buf.get(), len, function);
} }
} }