Bug 959787 - Handlify JS_ExecuteScript, JS_EvaluateScript and other JS APIs r=sfink r=bz

This commit is contained in:
Jon Coppeard
2014-03-17 16:17:58 +00:00
parent 4ce4b39185
commit a3b2ee7cf7
12 changed files with 67 additions and 84 deletions

View File

@@ -211,7 +211,8 @@ nsJSUtils::EvaluateString(JSContext* aCx,
JS::Rooted<JSObject*> rootedScope(aCx, aScopeObject);
if (aOffThreadToken) {
JSScript *script = JS::FinishOffThreadScript(aCx, JS_GetRuntime(aCx), *aOffThreadToken);
JS::Rooted<JSScript*>
script(aCx, JS::FinishOffThreadScript(aCx, JS_GetRuntime(aCx), *aOffThreadToken));
*aOffThreadToken = nullptr; // Mark the token as having been finished.
if (script) {
ok = JS_ExecuteScript(aCx, rootedScope, script, aRetValue);

View File

@@ -1185,9 +1185,8 @@ nsXBLBinding::LookupMemberInternal(JSContext* aCx, nsString& aName,
// Look for the property on this binding. If it's not there, try the next
// binding on the chain.
nsXBLProtoImpl* impl = mPrototypeBinding->GetImplementation();
if (impl && !impl->LookupMember(aCx, aName, aNameAsId, aDesc,
&classObject.toObject()))
{
JS::Rooted<JSObject*> object(aCx, &classObject.toObject());
if (impl && !impl->LookupMember(aCx, aName, aNameAsId, aDesc, object)) {
return false;
}
if (aDesc.object() || !mNextBinding) {

View File

@@ -239,7 +239,7 @@ bool
nsXBLProtoImpl::LookupMember(JSContext* aCx, nsString& aName,
JS::Handle<jsid> aNameAsId,
JS::MutableHandle<JSPropertyDescriptor> aDesc,
JSObject* aClassObject)
JS::Handle<JSObject*> aClassObject)
{
for (nsXBLProtoImplMember* m = mMembers; m; m = m->GetNext()) {
if (aName.Equals(m->GetName())) {

View File

@@ -50,7 +50,7 @@ public:
bool LookupMember(JSContext* aCx, nsString& aName, JS::Handle<jsid> aNameAsId,
JS::MutableHandle<JSPropertyDescriptor> aDesc,
JSObject* aClassObject);
JS::Handle<JSObject*> aClassObject);
void SetMemberList(nsXBLProtoImplMember* aMemberList)
{

View File

@@ -165,8 +165,7 @@ Load(JSContext *cx,
JS::CompileOptions options(cx);
options.setUTF8(true)
.setFileAndLine(filename.ptr(), 1);
JS::RootedObject rootedObj(cx, obj);
JSScript *script = JS::Compile(cx, rootedObj, options, file);
JS::Rooted<JSScript*> script(cx, JS::Compile(cx, obj, options, file));
fclose(file);
if (!script)
return false;
@@ -298,7 +297,6 @@ XPCShellEnvironment::ProcessFile(JSContext *cx,
{
XPCShellEnvironment* env = this;
JSScript *script;
JS::Rooted<JS::Value> result(cx);
int lineno, startline;
bool ok, hitEOF;
@@ -333,7 +331,7 @@ XPCShellEnvironment::ProcessFile(JSContext *cx,
JS::CompileOptions options(cx);
options.setUTF8(true)
.setFileAndLine(filename, 1);
JSScript* script = JS::Compile(cx, obj, options, file);
JS::Rooted<JSScript*> script(cx, JS::Compile(cx, obj, options, file));
if (script)
(void)JS_ExecuteScript(cx, obj, script, result.address());
@@ -370,7 +368,8 @@ XPCShellEnvironment::ProcessFile(JSContext *cx,
JS_ClearPendingException(cx);
JS::CompileOptions options(cx);
options.setFileAndLine("typein", startline);
script = JS_CompileScript(cx, obj, buffer, strlen(buffer), options);
JS::Rooted<JSScript*> script(cx,
JS_CompileScript(cx, obj, buffer, strlen(buffer), options));
if (script) {
JSErrorReporter older;
@@ -578,8 +577,8 @@ XPCShellEnvironment::EvaluateString(const nsString& aString,
JS::CompileOptions options(cx);
options.setFileAndLine("typein", 0);
JSScript* script = JS_CompileUCScript(cx, global, aString.get(),
aString.Length(), options);
JS::Rooted<JSScript*> script(cx, JS_CompileUCScript(cx, global, aString.get(),
aString.Length(), options));
if (!script) {
return false;
}

View File

@@ -56,13 +56,15 @@ LinkFail(JSContext *cx, const char *str)
}
static bool
GetDataProperty(JSContext *cx, const Value &objVal, HandlePropertyName field, MutableHandleValue v)
GetDataProperty(JSContext *cx, HandleValue objVal, HandlePropertyName field, MutableHandleValue v)
{
if (!objVal.isObject())
return LinkFail(cx, "accessing property of non-object");
Rooted<JSPropertyDescriptor> desc(cx);
if (!JS_GetPropertyDescriptorById(cx, &objVal.toObject(), NameToId(field), 0, &desc))
RootedObject obj(cx, &objVal.toObject());
RootedId id(cx, NameToId(field));
if (!JS_GetPropertyDescriptorById(cx, obj, id, 0, &desc))
return false;
if (!desc.object())

View File

@@ -3096,11 +3096,8 @@ JS_DefineUCProperty(JSContext *cx, JSObject *objArg, const jschar *name, size_t
}
JS_PUBLIC_API(bool)
JS_DefineOwnProperty(JSContext *cx, JSObject *objArg, jsid idArg, jsval descriptorArg, bool *bp)
JS_DefineOwnProperty(JSContext *cx, HandleObject obj, HandleId id, HandleValue descriptor, bool *bp)
{
RootedObject obj(cx, objArg);
RootedId id(cx, idArg);
RootedValue descriptor(cx, descriptorArg);
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
assertSameCompartment(cx, obj, id, descriptor);
@@ -3136,9 +3133,8 @@ JS_DefineObject(JSContext *cx, JSObject *objArg, const char *name, const JSClass
}
JS_PUBLIC_API(bool)
JS_DefineConstDoubles(JSContext *cx, JSObject *objArg, const JSConstDoubleSpec *cds)
JS_DefineConstDoubles(JSContext *cx, HandleObject obj, const JSConstDoubleSpec *cds)
{
RootedObject obj(cx, objArg);
bool ok;
unsigned attrs;
@@ -3159,9 +3155,8 @@ JS_DefineConstDoubles(JSContext *cx, JSObject *objArg, const JSConstDoubleSpec *
}
JS_PUBLIC_API(bool)
JS_DefineProperties(JSContext *cx, JSObject *objArg, const JSPropertySpec *ps)
JS_DefineProperties(JSContext *cx, HandleObject obj, const JSPropertySpec *ps)
{
RootedObject obj(cx, objArg);
bool ok;
for (ok = true; ps->name; ps++) {
if (ps->flags & JSPROP_NATIVE_ACCESSORS) {
@@ -3245,11 +3240,9 @@ GetPropertyDescriptorById(JSContext *cx, HandleObject obj, HandleId id, unsigned
}
JS_PUBLIC_API(bool)
JS_GetOwnPropertyDescriptorById(JSContext *cx, JSObject *objArg, jsid idArg, unsigned flags,
JS_GetOwnPropertyDescriptorById(JSContext *cx, HandleObject obj, HandleId id, unsigned flags,
MutableHandle<JSPropertyDescriptor> desc)
{
RootedObject obj(cx, objArg);
RootedId id(cx, idArg);
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
@@ -3257,30 +3250,32 @@ JS_GetOwnPropertyDescriptorById(JSContext *cx, JSObject *objArg, jsid idArg, uns
}
JS_PUBLIC_API(bool)
JS_GetOwnPropertyDescriptor(JSContext *cx, JSObject *objArg, const char *name, unsigned flags,
JS_GetOwnPropertyDescriptor(JSContext *cx, HandleObject obj, const char *name, unsigned flags,
MutableHandle<JSPropertyDescriptor> desc)
{
RootedObject obj(cx, objArg);
JSAtom *atom = Atomize(cx, name, strlen(name));
return atom && JS_GetOwnPropertyDescriptorById(cx, obj, AtomToId(atom), flags, desc);
if (!atom)
return false;
RootedId id(cx, AtomToId(atom));
return JS_GetOwnPropertyDescriptorById(cx, obj, id, flags, desc);
}
JS_PUBLIC_API(bool)
JS_GetPropertyDescriptorById(JSContext *cx, JSObject *objArg, jsid idArg, unsigned flags,
JS_GetPropertyDescriptorById(JSContext *cx, HandleObject obj, HandleId id, unsigned flags,
MutableHandle<JSPropertyDescriptor> desc)
{
RootedObject obj(cx, objArg);
RootedId id(cx, idArg);
return GetPropertyDescriptorById(cx, obj, id, flags, false, desc);
}
JS_PUBLIC_API(bool)
JS_GetPropertyDescriptor(JSContext *cx, JSObject *objArg, const char *name, unsigned flags,
JS_GetPropertyDescriptor(JSContext *cx, HandleObject obj, const char *name, unsigned flags,
MutableHandle<JSPropertyDescriptor> desc)
{
RootedObject obj(cx, objArg);
JSAtom *atom = Atomize(cx, name, strlen(name));
return atom && JS_GetPropertyDescriptorById(cx, obj, AtomToId(atom), flags, desc);
if (!atom)
return false;
RootedId id(cx, AtomToId(atom));
return atom && JS_GetPropertyDescriptorById(cx, obj, id, flags, desc);
}
JS_PUBLIC_API(bool)
@@ -4712,9 +4707,8 @@ JS_DecompileFunctionBody(JSContext *cx, HandleFunction fun, unsigned indent)
}
MOZ_NEVER_INLINE JS_PUBLIC_API(bool)
JS_ExecuteScript(JSContext *cx, JSObject *objArg, JSScript *scriptArg, jsval *rval)
JS_ExecuteScript(JSContext *cx, HandleObject obj, HandleScript scriptArg, jsval *rval)
{
RootedObject obj(cx, objArg);
RootedScript script(cx, scriptArg);
JS_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment()));
@@ -4745,10 +4739,9 @@ JS_ExecuteScript(JSContext *cx, JSObject *objArg, JSScript *scriptArg, jsval *rv
}
JS_PUBLIC_API(bool)
JS_ExecuteScriptVersion(JSContext *cx, JSObject *objArg, JSScript *script, jsval *rval,
JS_ExecuteScriptVersion(JSContext *cx, HandleObject obj, HandleScript script, jsval *rval,
JSVersion version)
{
RootedObject obj(cx, objArg);
return JS_ExecuteScript(cx, obj, script, rval);
}
@@ -4839,10 +4832,9 @@ JS_EvaluateUCScript(JSContext *cx, HandleObject obj, const jschar *chars, unsign
}
JS_PUBLIC_API(bool)
JS_EvaluateScript(JSContext *cx, JSObject *objArg, const char *bytes, unsigned nbytes,
JS_EvaluateScript(JSContext *cx, HandleObject obj, const char *bytes, unsigned nbytes,
const char *filename, unsigned lineno, jsval *rval)
{
RootedObject obj(cx, objArg);
CompileOptions options(cx);
options.setFileAndLine(filename, lineno);

View File

@@ -2770,10 +2770,10 @@ JS_DefineObject(JSContext *cx, JSObject *obj, const char *name, const JSClass *c
JSObject *proto, unsigned attrs);
extern JS_PUBLIC_API(bool)
JS_DefineConstDoubles(JSContext *cx, JSObject *obj, const JSConstDoubleSpec *cds);
JS_DefineConstDoubles(JSContext *cx, JS::HandleObject obj, const JSConstDoubleSpec *cds);
extern JS_PUBLIC_API(bool)
JS_DefineProperties(JSContext *cx, JSObject *obj, const JSPropertySpec *ps);
JS_DefineProperties(JSContext *cx, JS::HandleObject obj, const JSPropertySpec *ps);
extern JS_PUBLIC_API(bool)
JS_DefineProperty(JSContext *cx, JSObject *obj, const char *name, jsval value,
@@ -2784,7 +2784,8 @@ JS_DefinePropertyById(JSContext *cx, JSObject *obj, jsid id, jsval value,
JSPropertyOp getter, JSStrictPropertyOp setter, unsigned attrs);
extern JS_PUBLIC_API(bool)
JS_DefineOwnProperty(JSContext *cx, JSObject *obj, jsid id, jsval descriptor, bool *bp);
JS_DefineOwnProperty(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
JS::HandleValue descriptor, bool *bp);
extern JS_PUBLIC_API(bool)
JS_AlreadyHasOwnProperty(JSContext *cx, JS::HandleObject obj, const char *name,
@@ -2960,11 +2961,11 @@ class MutableHandleBase<JSPropertyDescriptor>
} /* namespace js */
extern JS_PUBLIC_API(bool)
JS_GetOwnPropertyDescriptorById(JSContext *cx, JSObject *objArg, jsid id, unsigned flags,
JS::MutableHandle<JSPropertyDescriptor> desc);
JS_GetOwnPropertyDescriptorById(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
unsigned flags, JS::MutableHandle<JSPropertyDescriptor> desc);
extern JS_PUBLIC_API(bool)
JS_GetOwnPropertyDescriptor(JSContext *cx, JSObject *objArg, const char *name, unsigned flags,
JS_GetOwnPropertyDescriptor(JSContext *cx, JS::HandleObject obj, const char *name, unsigned flags,
JS::MutableHandle<JSPropertyDescriptor> desc);
/*
@@ -2973,11 +2974,11 @@ JS_GetOwnPropertyDescriptor(JSContext *cx, JSObject *objArg, const char *name, u
* then this property was not found on the prototype chain.
*/
extern JS_PUBLIC_API(bool)
JS_GetPropertyDescriptorById(JSContext *cx, JSObject *obj, jsid id, unsigned flags,
JS_GetPropertyDescriptorById(JSContext *cx, JS::HandleObject obj, JS::HandleId id, unsigned flags,
JS::MutableHandle<JSPropertyDescriptor> desc);
extern JS_PUBLIC_API(bool)
JS_GetPropertyDescriptor(JSContext *cx, JSObject *obj, const char *name, unsigned flags,
JS_GetPropertyDescriptor(JSContext *cx, JS::HandleObject obj, const char *name, unsigned flags,
JS::MutableHandle<JSPropertyDescriptor> desc);
extern JS_PUBLIC_API(bool)
@@ -3747,14 +3748,14 @@ JS_DecompileFunctionBody(JSContext *cx, JS::Handle<JSFunction*> fun, unsigned in
* etc., entry points.
*/
extern JS_PUBLIC_API(bool)
JS_ExecuteScript(JSContext *cx, JSObject *obj, JSScript *script, jsval *rval);
JS_ExecuteScript(JSContext *cx, JS::HandleObject obj, JS::HandleScript script, jsval *rval);
extern JS_PUBLIC_API(bool)
JS_ExecuteScriptVersion(JSContext *cx, JSObject *obj, JSScript *script, jsval *rval,
JS_ExecuteScriptVersion(JSContext *cx, JS::HandleObject obj, JS::HandleScript script, jsval *rval,
JSVersion version);
extern JS_PUBLIC_API(bool)
JS_EvaluateScript(JSContext *cx, JSObject *obj,
JS_EvaluateScript(JSContext *cx, JS::HandleObject obj,
const char *bytes, unsigned length,
const char *filename, unsigned lineno,
jsval *rval);

View File

@@ -2887,8 +2887,8 @@ WorkerMain(void *arg)
options.setFileAndLine("<string>", 1)
.setCompileAndGo(true);
JSScript *script = JS::Compile(cx, global, options,
input->chars, input->length);
RootedScript script(cx, JS::Compile(cx, global, options,
input->chars, input->length));
if (!script)
break;
RootedValue result(cx);

View File

@@ -339,8 +339,7 @@ Load(JSContext *cx, unsigned argc, jsval *vp)
JS::CompileOptions options(cx);
options.setUTF8(true)
.setFileAndLine(filename.ptr(), 1);
JS::RootedObject rootedObj(cx, obj);
JSScript *script = JS::Compile(cx, rootedObj, options, file);
JS::Rooted<JSScript*> script(cx, JS::Compile(cx, obj, options, file));
fclose(file);
if (!script)
return false;
@@ -892,8 +891,8 @@ static void
ProcessFile(JSContext *cx, JS::Handle<JSObject*> obj, const char *filename, FILE *file,
bool forceTTY)
{
JSScript *script;
JS::Rooted<JS::Value> result(cx);
JS::RootedScript script(cx);
JS::RootedValue result(cx);
int lineno, startline;
bool ok, hitEOF;
char *bufp, buffer[4096];

View File

@@ -613,8 +613,9 @@ ProxyAutoConfig::SetupJS()
if (!mJSRuntime)
return NS_ERROR_FAILURE;
JSAutoRequest ar(mJSRuntime->Context());
JSAutoCompartment ac(mJSRuntime->Context(), mJSRuntime->Global());
JSContext* cx = mJSRuntime->Context();
JSAutoRequest ar(cx);
JSAutoCompartment ac(cx, mJSRuntime->Global());
// check if this is a data: uri so that we don't spam the js console with
// huge meaningless strings. this is not on the main thread, so it can't
@@ -622,14 +623,12 @@ ProxyAutoConfig::SetupJS()
bool isDataURI = nsDependentCSubstring(mPACURI, 0, 5).LowerCaseEqualsASCII("data:", 5);
sRunning = this;
JS::Rooted<JSObject *> global(mJSRuntime->Context(), mJSRuntime->Global());
JS::CompileOptions options(mJSRuntime->Context());
JS::Rooted<JSObject*> global(cx, mJSRuntime->Global());
JS::CompileOptions options(cx);
options.setFileAndLine(mPACURI.get(), 1);
JSScript *script = JS_CompileScript(mJSRuntime->Context(), global,
mPACScript.get(), mPACScript.Length(),
options);
if (!script ||
!JS_ExecuteScript(mJSRuntime->Context(), mJSRuntime->Global(), script, nullptr)) {
JS::Rooted<JSScript*> script(cx, JS_CompileScript(cx, global, mPACScript.get(),
mPACScript.Length(), options));
if (!script || !JS_ExecuteScript(cx, global, script, nullptr)) {
nsString alertMessage(NS_LITERAL_STRING("PAC file failed to install from "));
if (isDataURI) {
alertMessage += NS_LITERAL_STRING("data: URI");

View File

@@ -167,11 +167,11 @@ typedef struct nsKeyPairInfoStr {
//to the nsCryptoRunnable event.
class nsCryptoRunArgs : public nsISupports {
public:
nsCryptoRunArgs();
nsCryptoRunArgs(JSContext *aCx);
virtual ~nsCryptoRunArgs();
nsCOMPtr<nsISupports> m_kungFuDeathGrip;
JSContext *m_cx;
JSObject *m_scope;
JS::PersistentRooted<JSObject*> m_scope;
nsCOMPtr<nsIPrincipal> m_principals;
nsXPIDLCString m_jsCallback;
NS_DECL_ISUPPORTS
@@ -2054,9 +2054,8 @@ nsCrypto::GenerateCRMFRequest(JSContext* aContext,
return nullptr;
}
nsCryptoRunArgs *args = new nsCryptoRunArgs();
nsCryptoRunArgs *args = new nsCryptoRunArgs(aContext);
args->m_cx = aContext;
args->m_kungFuDeathGrip = GetISupportsFromContext(aContext);
args->m_scope = JS_GetParent(script_obj);
if (!aJsCallback.IsVoid()) {
@@ -2169,11 +2168,9 @@ nsP12Runnable::Run()
return NS_OK;
}
nsCryptoRunArgs::nsCryptoRunArgs()
{
}
nsCryptoRunArgs::~nsCryptoRunArgs() {}
nsCryptoRunArgs::nsCryptoRunArgs(JSContext *cx) : m_cx(cx), m_scope(cx) {}
nsCryptoRunArgs::~nsCryptoRunArgs() {}
nsCryptoRunnable::nsCryptoRunnable(nsCryptoRunArgs *args)
{
@@ -2181,18 +2178,11 @@ nsCryptoRunnable::nsCryptoRunnable(nsCryptoRunArgs *args)
NS_ASSERTION(args,"Passed nullptr to nsCryptoRunnable constructor.");
m_args = args;
NS_IF_ADDREF(m_args);
JS_AddNamedObjectRoot(args->m_cx, &args->m_scope,"nsCryptoRunnable::mScope");
}
nsCryptoRunnable::~nsCryptoRunnable()
{
nsNSSShutDownPreventionLock locker;
{
JSAutoRequest ar(m_args->m_cx);
JS_RemoveObjectRoot(m_args->m_cx, &m_args->m_scope);
}
NS_IF_RELEASE(m_args);
}
@@ -2204,10 +2194,11 @@ nsCryptoRunnable::Run()
nsNSSShutDownPreventionLock locker;
AutoPushJSContext cx(m_args->m_cx);
JSAutoRequest ar(cx);
JSAutoCompartment ac(cx, m_args->m_scope);
JS::Rooted<JSObject*> scope(cx, m_args->m_scope);
JSAutoCompartment ac(cx, scope);
bool ok =
JS_EvaluateScript(cx, m_args->m_scope, m_args->m_jsCallback,
JS_EvaluateScript(cx, scope, m_args->m_jsCallback,
strlen(m_args->m_jsCallback), nullptr, 0, nullptr);
return ok ? NS_OK : NS_ERROR_FAILURE;
}