Bug 811784: Account for subscripts when figuring out what object to stick properties on. r=mrbkap

--HG--
extra : rebase_source : 6da8a33c512b2e5758094f4a2108e5c342c7c28c
This commit is contained in:
Kyle Huey 2012-11-16 08:54:58 -08:00
Родитель 59d3bd6ffb
Коммит c728f95e75
4 изменённых файлов: 57 добавлений и 12 удалений

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

@ -625,6 +625,16 @@ mozJSComponentLoader::FindTargetObject(JSContext* aCx,
return NS_OK;
}
void
mozJSComponentLoader::NoteSubScript(JSScript* aScript, JSObject* aThisObject)
{
if (!mInitialized && NS_FAILED(ReallyInit())) {
MOZ_NOT_REACHED();
}
mThisObjects.Put(aScript, aThisObject);
}
// Some stack based classes for cleaning up on early return
#ifdef HAVE_PR_MEMMAP
class FileAutoCloser

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

@ -54,6 +54,8 @@ class mozJSComponentLoader : public mozilla::ModuleLoader,
static mozJSComponentLoader* Get() { return sSelf; }
void NoteSubScript(JSScript* aScript, JSObject* aThisObject);
protected:
static mozJSComponentLoader* sSelf;

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

@ -31,6 +31,7 @@
#include "mozilla/scache/StartupCache.h"
#include "mozilla/scache/StartupCacheUtils.h"
#include "mozilla/Preferences.h"
using namespace mozilla::scache;
@ -57,6 +58,7 @@ mozJSSubScriptLoader::mozJSSubScriptLoader() : mSystemPrincipal(nullptr)
// Force construction of the JS component loader. We may need it later.
nsCOMPtr<xpcIJSModuleLoader> componentLoader =
do_GetService(MOZJSCOMPONENTLOADER_CONTRACTID);
mReuseLoaderGlobal = mozilla::Preferences::GetBool("jsloader.reuseGlobal");
}
mozJSSubScriptLoader::~mozJSSubScriptLoader()
@ -77,12 +79,15 @@ nsresult
mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_obj,
const nsAString& charset, const char *uriStr,
nsIIOService *serv, nsIPrincipal *principal,
JSScript **scriptp)
JSScript **scriptp, JSFunction **functionp)
{
nsCOMPtr<nsIChannel> chan;
nsCOMPtr<nsIInputStream> instream;
JSErrorReporter er;
*scriptp = nullptr;
*functionp = nullptr;
nsresult rv;
// Instead of calling NS_OpenURI, we create the channel ourselves and call
// SetContentType, to avoid expensive MIME type lookups (bug 632490).
@ -120,7 +125,9 @@ mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_ob
JS::CompileOptions options(cx);
options.setPrincipals(nsJSPrincipals::get(principal))
.setFileAndLine(uriStr, 1)
.setSourcePolicy(JS::CompileOptions::LAZY_SOURCE);
.setSourcePolicy(mReuseLoaderGlobal ?
JS::CompileOptions::NO_SOURCE :
JS::CompileOptions::LAZY_SOURCE);
js::RootedObject target_obj_root(cx, target_obj);
if (!charset.IsVoid()) {
nsString script;
@ -131,10 +138,24 @@ mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_ob
return ReportError(cx, LOAD_ERROR_BADCHARSET);
}
*scriptp = JS::Compile(cx, target_obj_root, options,
reinterpret_cast<const jschar*>(script.get()), script.Length());
if (!mReuseLoaderGlobal) {
*scriptp = JS::Compile(cx, target_obj_root, options,
reinterpret_cast<const jschar*>(script.get()),
script.Length());
} else {
*functionp = JS::CompileFunction(cx, target_obj_root, options,
nullptr, 0, nullptr,
reinterpret_cast<const jschar*>(script.get()),
script.Length());
}
} else {
*scriptp = JS::Compile(cx, target_obj_root, options, buf.get(), len);
if (!mReuseLoaderGlobal) {
*scriptp = JS::Compile(cx, target_obj_root, options, buf.get(), len);
} else {
*functionp = JS::CompileFunction(cx, target_obj_root, options,
nullptr, 0, nullptr, buf.get(),
len);
}
}
/* repent for our evil deeds */
@ -181,10 +202,9 @@ mozJSSubScriptLoader::LoadSubScript(const nsAString& url,
if (!JS_ValueToObject(cx, target, &targetObj))
return NS_ERROR_ILLEGAL_VALUE;
mozJSComponentLoader* loader = mozJSComponentLoader::Get();
if (!targetObj) {
// If the user didn't provide an object to eval onto, find one.
mozJSComponentLoader* loader = mozJSComponentLoader::Get();
rv = loader->FindTargetObject(cx, &targetObj);
NS_ENSURE_SUCCESS(rv, rv);
}
@ -272,20 +292,32 @@ mozJSSubScriptLoader::LoadSubScript(const nsAString& url,
cachePath.AppendPrintf("jssubloader/%d", version);
PathifyURI(uri, cachePath);
JSFunction* function = nullptr;
script = nullptr;
if (cache)
rv = ReadCachedScript(cache, cachePath, cx, mSystemPrincipal, &script);
if (!script) {
rv = ReadScript(uri, cx, targetObj, charset,
static_cast<const char*>(uriStr.get()), serv,
principal, &script);
writeScript = true;
principal, &script, &function);
writeScript = !!script;
}
if (NS_FAILED(rv) || !script)
if (NS_FAILED(rv) || (!script && !function))
return rv;
bool ok = JS_ExecuteScriptVersion(cx, targetObj, script, retval, version);
if (function) {
script = JS_GetFunctionScript(cx, function);
}
loader->NoteSubScript(script, targetObj);
bool ok = false;
if (function) {
ok = JS_CallFunction(cx, targetObj, function, 0, nullptr, retval);
} else {
ok = JS_ExecuteScriptVersion(cx, targetObj, script, retval, version);
}
if (ok) {
JSAutoCompartment rac(cx, result_obj);

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

@ -33,7 +33,8 @@ private:
nsresult ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_obj,
const nsAString& charset, const char *uriStr,
nsIIOService *serv, nsIPrincipal *principal,
JSScript **scriptp);
JSScript **scriptp, JSFunction **functionp);
bool mReuseLoaderGlobal;
nsCOMPtr<nsIPrincipal> mSystemPrincipal;
};