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

This commit is contained in:
Kyle Huey 2012-11-26 14:41:55 -08:00
parent 0466aaefe7
commit 9048d562b8
4 changed files with 66 additions and 18 deletions

View File

@ -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

View File

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

View File

@ -31,6 +31,7 @@
#include "mozilla/scache/StartupCache.h"
#include "mozilla/scache/StartupCacheUtils.h"
#include "mozilla/Preferences.h"
using namespace mozilla::scache;
@ -77,12 +78,16 @@ nsresult
mozJSSubScriptLoader::ReadScript(nsIURI *uri, JSContext *cx, JSObject *target_obj,
const nsAString& charset, const char *uriStr,
nsIIOService *serv, nsIPrincipal *principal,
JSScript **scriptp)
bool reuseGlobal, 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).
@ -130,13 +135,27 @@ 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 (!reuseGlobal) {
*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 {
// We only use LAZY_SOURCE when no special encoding is specified because
// the lazy source loader doesn't know the encoding.
options.setSourcePolicy(JS::CompileOptions::LAZY_SOURCE);
*scriptp = JS::Compile(cx, target_obj_root, options, buf.get(), len);
if (!reuseGlobal) {
options.setSourcePolicy(JS::CompileOptions::LAZY_SOURCE);
*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 */
@ -180,16 +199,20 @@ mozJSSubScriptLoader::LoadSubScript(const nsAString& url,
JSAutoRequest ar(cx);
JSObject* targetObj;
if (!JS_ValueToObject(cx, target, &targetObj))
mozJSComponentLoader* loader = mozJSComponentLoader::Get();
rv = loader->FindTargetObject(cx, &targetObj);
NS_ENSURE_SUCCESS(rv, rv);
bool reusingGlobal = !JS_IsGlobalObject(targetObj);
// We base reusingGlobal off of what the loader told us, but we may not
// actually be using that object.
JSObject* passedObj;
if (!JS_ValueToObject(cx, target, &passedObj))
return NS_ERROR_ILLEGAL_VALUE;
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);
}
if (passedObj)
targetObj = passedObj;
// Remember an object out of the calling compartment so that we
// can properly wrap the result later.
@ -274,20 +297,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, reusingGlobal, &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);

View File

@ -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);
bool reuseGlobal, JSScript **scriptp,
JSFunction **functionp);
nsCOMPtr<nsIPrincipal> mSystemPrincipal;
};