Bug 959787 - Handlify JS_ExecuteScript, JS_EvaluateScript and other JS APIs r=sfink r=bz
authorJon Coppeard <jcoppeard@mozilla.com>
Mon, 17 Mar 2014 16:17:58 +0000
changeset 185972 9f41c59d6a59f8f0241c14b9b63d00902a54d958
parent 184932 233a33ca8ef81fca3d2b08f5f1a085ab6385fd39
child 185973 0ae9c4eef8d6ec3c7ddf977ca9a9efd1758183ec
push idunknown
push userunknown
push dateunknown
reviewerssfink, bz
bugs959787
milestone30.0a1
Bug 959787 - Handlify JS_ExecuteScript, JS_EvaluateScript and other JS APIs r=sfink r=bz
dom/base/nsJSUtils.cpp
dom/xbl/nsXBLBinding.cpp
dom/xbl/nsXBLProtoImpl.cpp
dom/xbl/nsXBLProtoImpl.h
ipc/testshell/XPCShellEnvironment.cpp
js/src/jit/AsmJSLink.cpp
js/src/jsapi.cpp
js/src/jsapi.h
js/src/shell/js.cpp
js/xpconnect/src/XPCShellImpl.cpp
netwerk/base/src/ProxyAutoConfig.cpp
security/manager/ssl/src/nsCrypto.cpp
--- a/dom/base/nsJSUtils.cpp
+++ b/dom/base/nsJSUtils.cpp
@@ -206,17 +206,18 @@ nsJSUtils::EvaluateString(JSContext* aCx
 
   // Scope the JSAutoCompartment so that we can later wrap the return value
   // into the caller's cx.
   {
     JSAutoCompartment ac(aCx, aScopeObject);
 
     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);
       } else {
         ok = false;
       }
     } else {
       ok = JS::Evaluate(aCx, rootedScope, aCompileOptions,
--- a/dom/xbl/nsXBLBinding.cpp
+++ b/dom/xbl/nsXBLBinding.cpp
@@ -1180,19 +1180,18 @@ nsXBLBinding::LookupMemberInternal(JSCon
     return true;
   }
 
   MOZ_ASSERT(classObject.isObject());
 
   // 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) {
     return true;
   }
 
   return mNextBinding->LookupMemberInternal(aCx, aName, aNameAsId, aDesc,
                                             aXBLScope);
--- a/dom/xbl/nsXBLProtoImpl.cpp
+++ b/dom/xbl/nsXBLProtoImpl.cpp
@@ -234,17 +234,17 @@ nsXBLProtoImpl::CompilePrototypeMembers(
 
   return NS_OK;
 }
 
 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())) {
       return JS_GetPropertyDescriptorById(aCx, aClassObject, aNameAsId, 0, aDesc);
     }
   }
   return true;
 }
--- a/dom/xbl/nsXBLProtoImpl.h
+++ b/dom/xbl/nsXBLProtoImpl.h
@@ -45,17 +45,17 @@ private:
                              JS::MutableHandle<JSObject*> aTargetClassObject,
                              bool* aTargetIsNew);
 
 public:
   nsresult CompilePrototypeMembers(nsXBLPrototypeBinding* aBinding);
 
   bool LookupMember(JSContext* aCx, nsString& aName, JS::Handle<jsid> aNameAsId,
                     JS::MutableHandle<JSPropertyDescriptor> aDesc,
-                    JSObject* aClassObject);
+                    JS::Handle<JSObject*> aClassObject);
 
   void SetMemberList(nsXBLProtoImplMember* aMemberList)
   {
     delete mMembers;
     mMembers = aMemberList;
   }
 
   void SetFieldList(nsXBLProtoImplField* aFieldList)
--- a/ipc/testshell/XPCShellEnvironment.cpp
+++ b/ipc/testshell/XPCShellEnvironment.cpp
@@ -160,18 +160,17 @@ Load(JSContext *cx,
         if (!file) {
             JS_ReportError(cx, "cannot open file '%s' for reading", filename.ptr());
             return false;
         }
         Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(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;
 
         JS::Rooted<JS::Value> result(cx);
         if (!JS_ExecuteScript(cx, obj, script, result.address())) {
             return false;
         }
@@ -293,17 +292,16 @@ void
 XPCShellEnvironment::ProcessFile(JSContext *cx,
                                  JS::Handle<JSObject*> obj,
                                  const char *filename,
                                  FILE *file,
                                  bool forceTTY)
 {
     XPCShellEnvironment* env = this;
 
-    JSScript *script;
     JS::Rooted<JS::Value> result(cx);
     int lineno, startline;
     bool ok, hitEOF;
     char *bufp, buffer[4096];
     JSString *str;
 
     if (forceTTY) {
         file = stdin;
@@ -328,17 +326,17 @@ XPCShellEnvironment::ProcessFile(JSConte
         ungetc(ch, file);
 
         JSAutoRequest ar(cx);
         JSAutoCompartment ac(cx, obj);
 
         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());
 
         return;
     }
 
     /* It's an interactive filehandle; drop into read-eval-print loop. */
     lineno = 1;
@@ -365,17 +363,18 @@ XPCShellEnvironment::ProcessFile(JSConte
             bufp += strlen(bufp);
             lineno++;
         } while (!JS_BufferIsCompilableUnit(cx, obj, buffer, strlen(buffer)));
 
         /* Clear any pending exception from previous failed compiles.  */
         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;
 
             ok = JS_ExecuteScript(cx, obj, script, result.address());
             if (ok && result != JSVAL_VOID) {
                 /* Suppress error reports from JS::ToString(). */
                 older = JS_SetErrorReporter(cx, nullptr);
                 str = JS::ToString(cx, result);
@@ -573,18 +572,18 @@ XPCShellEnvironment::EvaluateString(cons
                                     nsString* aResult)
 {
   AutoSafeJSContext cx;
   JS::Rooted<JSObject*> global(cx, GetGlobalObject());
   JSAutoCompartment ac(cx, global);
 
   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;
   }
 
   if (aResult) {
       aResult->Truncate();
   }
 
--- a/js/src/jit/AsmJSLink.cpp
+++ b/js/src/jit/AsmJSLink.cpp
@@ -51,23 +51,25 @@ static bool
 LinkFail(JSContext *cx, const char *str)
 {
     JS_ReportErrorFlagsAndNumber(cx, JSREPORT_WARNING, js_GetErrorMessage,
                                  nullptr, JSMSG_USE_ASM_LINK_FAIL, str);
     return false;
 }
 
 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())
         return LinkFail(cx, "property not present on object");
 
     if (desc.hasGetterOrSetterObject())
         return LinkFail(cx, "property is not a data property");
 
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -3091,21 +3091,18 @@ JS_DefineUCProperty(JSContext *cx, JSObj
                     jsval valueArg, JSPropertyOp getter, JSStrictPropertyOp setter, unsigned attrs)
 {
     RootedObject obj(cx, objArg);
     RootedValue value(cx, valueArg);
     return DefineUCProperty(cx, obj, name, namelen, value, getter, setter, attrs, 0);
 }
 
 JS_PUBLIC_API(bool)
-JS_DefineOwnProperty(JSContext *cx, JSObject *objArg, jsid idArg, jsval descriptorArg, bool *bp)
-{
-    RootedObject obj(cx, objArg);
-    RootedId id(cx, idArg);
-    RootedValue descriptor(cx, descriptorArg);
+JS_DefineOwnProperty(JSContext *cx, HandleObject obj, HandleId id, HandleValue descriptor, bool *bp)
+{
     AssertHeapIsIdle(cx);
     CHECK_REQUEST(cx);
     assertSameCompartment(cx, obj, id, descriptor);
 
     return DefineOwnProperty(cx, obj, id, descriptor, bp);
 }
 
 JS_PUBLIC_API(JSObject *)
@@ -3131,19 +3128,18 @@ JS_DefineObject(JSContext *cx, JSObject 
                         attrs, 0)) {
         return nullptr;
     }
 
     return nobj;
 }
 
 JS_PUBLIC_API(bool)
-JS_DefineConstDoubles(JSContext *cx, JSObject *objArg, const JSConstDoubleSpec *cds)
-{
-    RootedObject obj(cx, objArg);
+JS_DefineConstDoubles(JSContext *cx, HandleObject obj, const JSConstDoubleSpec *cds)
+{
     bool ok;
     unsigned attrs;
 
     AssertHeapIsIdle(cx);
     CHECK_REQUEST(cx);
     JSPropertyOpWrapper noget = GetterWrapper(nullptr);
     JSStrictPropertyOpWrapper noset = SetterWrapper(nullptr);
     for (ok = true; cds->name; cds++) {
@@ -3154,19 +3150,18 @@ JS_DefineConstDoubles(JSContext *cx, JSO
         ok = DefineProperty(cx, obj, cds->name, value, noget, noset, attrs, 0);
         if (!ok)
             break;
     }
     return ok;
 }
 
 JS_PUBLIC_API(bool)
-JS_DefineProperties(JSContext *cx, JSObject *objArg, const JSPropertySpec *ps)
-{
-    RootedObject obj(cx, objArg);
+JS_DefineProperties(JSContext *cx, HandleObject obj, const JSPropertySpec *ps)
+{
     bool ok;
     for (ok = true; ps->name; ps++) {
         if (ps->flags & JSPROP_NATIVE_ACCESSORS) {
             // If you declare native accessors, then you should have a native
             // getter.
             JS_ASSERT(ps->getter.propertyOp.op);
             // If you do not have a self-hosted getter, you should not have a
             // self-hosted setter. This is the closest approximation to that
@@ -3240,52 +3235,52 @@ GetPropertyDescriptorById(JSContext *cx,
         JS_ASSERT(desc.getter() == nullptr);
         JS_ASSERT(desc.setter() == nullptr);
         JS_ASSERT(desc.value().isUndefined());
     }
     return true;
 }
 
 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);
 
     return GetPropertyDescriptorById(cx, obj, id, flags, true, desc);
 }
 
 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)
 JS_GetPropertyById(JSContext *cx, HandleObject obj, HandleId id, MutableHandleValue vp)
 {
     return JS_ForwardGetPropertyTo(cx, obj, id, obj, vp);
 }
 
@@ -4707,19 +4702,18 @@ JS_DecompileFunctionBody(JSContext *cx, 
     JS_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment()));
     AssertHeapIsIdle(cx);
     CHECK_REQUEST(cx);
     assertSameCompartment(cx, fun);
     return FunctionToString(cx, fun, true, !(indent & JS_DONT_PRETTY_PRINT));
 }
 
 MOZ_NEVER_INLINE JS_PUBLIC_API(bool)
-JS_ExecuteScript(JSContext *cx, JSObject *objArg, JSScript *scriptArg, jsval *rval)
-{
-    RootedObject obj(cx, objArg);
+JS_ExecuteScript(JSContext *cx, HandleObject obj, HandleScript scriptArg, jsval *rval)
+{
     RootedScript script(cx, scriptArg);
 
     JS_ASSERT(!cx->runtime()->isAtomsCompartment(cx->compartment()));
     AssertHeapIsIdle(cx);
     CHECK_REQUEST(cx);
     assertSameCompartment(cx, obj);
     if (cx->compartment() != obj->compartment())
         *(volatile int *) 0 = 0xf0;
@@ -4740,20 +4734,19 @@ JS_ExecuteScript(JSContext *cx, JSObject
     } else {
         script = scriptArg;
     }
 
     return Execute(cx, script, *obj, rval);
 }
 
 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);
 }
 
 static const unsigned LARGE_SCRIPT_LENGTH = 500*1024;
 
 extern JS_PUBLIC_API(bool)
 JS::Evaluate(JSContext *cx, HandleObject obj, const ReadOnlyCompileOptions &optionsArg,
              const jschar *chars, size_t length, jsval *rval)
@@ -4834,20 +4827,19 @@ JS_EvaluateUCScript(JSContext *cx, Handl
 {
     CompileOptions options(cx);
     options.setFileAndLine(filename, lineno);
 
     return Evaluate(cx, obj, options, chars, length, rval.address());
 }
 
 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);
 
     return Evaluate(cx, obj, options, bytes, nbytes, rval);
 }
 
 JS_PUBLIC_API(bool)
 JS_CallFunction(JSContext *cx, HandleObject obj, HandleFunction fun, const HandleValueArray& args,
--- a/js/src/jsapi.h
+++ b/js/src/jsapi.h
@@ -2765,31 +2765,32 @@ JS_PreventExtensions(JSContext *cx, JS::
 extern JS_PUBLIC_API(JSObject *)
 JS_New(JSContext *cx, JSObject *ctor, unsigned argc, jsval *argv);
 
 extern JS_PUBLIC_API(JSObject *)
 JS_DefineObject(JSContext *cx, JSObject *obj, const char *name, const JSClass *clasp,
                 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,
                   JSPropertyOp getter, JSStrictPropertyOp setter, unsigned attrs);
 
 extern JS_PUBLIC_API(bool)
 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,
                          bool *foundp);
 
 extern JS_PUBLIC_API(bool)
 JS_AlreadyHasOwnPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id,
                              bool *foundp);
@@ -2955,34 +2956,34 @@ class MutableHandleBase<JSPropertyDescri
     JSPropertyDescriptor *extractMutable() {
         return static_cast<JS::MutableHandle<JSPropertyDescriptor>*>(this)->address();
     }
 };
 
 } /* 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);
 
 /*
  * Like JS_GetOwnPropertyDescriptorById but will return a property on
  * an object on the prototype chain (returned in desc->obj). If desc->obj is null,
  * 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)
 JS_GetProperty(JSContext *cx, JS::HandleObject obj, const char *name, JS::MutableHandleValue vp);
 
 extern JS_PUBLIC_API(bool)
 JS_GetPropertyById(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp);
 
@@ -3742,24 +3743,24 @@ JS_DecompileFunctionBody(JSContext *cx, 
  * points with signatures matching the following six, and that doesn't seem
  * worth the code bloat cost.  Such new entry points would probably have less
  * obvious names, too, so would not tend to be used.  The JS_SetOption call,
  * OTOH, can be more easily hacked into existing code that does not depend on
  * the bug; such code can continue to use the familiar JS_EvaluateScript,
  * 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);
 
 extern JS_PUBLIC_API(bool)
 JS_EvaluateUCScript(JSContext *cx, JS::Handle<JSObject*> obj,
                     const jschar *chars, unsigned length,
                     const char *filename, unsigned lineno,
--- a/js/src/shell/js.cpp
+++ b/js/src/shell/js.cpp
@@ -2882,18 +2882,18 @@ WorkerMain(void *arg)
             break;
 
         JSAutoCompartment ac(cx, global);
 
         JS::CompileOptions options(cx);
         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);
         JS_ExecuteScript(cx, global, script, result.address());
     } while (0);
 
     DestroyContext(cx, false);
     JS_DestroyRuntime(rt);
--- a/js/xpconnect/src/XPCShellImpl.cpp
+++ b/js/xpconnect/src/XPCShellImpl.cpp
@@ -334,18 +334,17 @@ Load(JSContext *cx, unsigned argc, jsval
         if (!file) {
             JS_ReportError(cx, "cannot open file '%s' for reading",
                            filename.ptr());
             return false;
         }
         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;
 
         JS::Rooted<JS::Value> result(cx);
         if (!compileOnly && !JS_ExecuteScript(cx, obj, script, result.address()))
             return false;
     }
@@ -887,18 +886,18 @@ my_GetErrorMessage(void *userRef, const 
 
     return &jsShell_ErrorFormatString[errorNumber];
 }
 
 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];
     JSString *str;
 
     if (forceTTY) {
         file = stdin;
     } else if (!isatty(fileno(file)))
--- a/netwerk/base/src/ProxyAutoConfig.cpp
+++ b/netwerk/base/src/ProxyAutoConfig.cpp
@@ -608,33 +608,32 @@ ProxyAutoConfig::SetupJS()
 
   if (mPACScript.IsEmpty())
     return NS_ERROR_FAILURE;
 
   mJSRuntime = JSRuntimeWrapper::Create();
   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
   // use nsIRUI scheme methods
   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");
     }
     else {
       alertMessage += NS_ConvertUTF8toUTF16(mPACURI);
     }
     PACLogToConsole(alertMessage);
--- a/security/manager/ssl/src/nsCrypto.cpp
+++ b/security/manager/ssl/src/nsCrypto.cpp
@@ -162,21 +162,21 @@ typedef struct nsKeyPairInfoStr {
                    /* extracted public key from ecPopCert */
 } nsKeyPairInfo;
 
 
 //This class is just used to pass arguments
 //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
 };
 
 //This class is used to run the callback code
 //passed to crypto.generateCRMFRequest
 //We have to do that for backwards compatibility
@@ -2049,19 +2049,18 @@ nsCrypto::GenerateCRMFRequest(JSContext*
     aRv.Throw(nrv);
     return nullptr;
   }
   if (MOZ_UNLIKELY(!principals)) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return nullptr;
   }
 
-  nsCryptoRunArgs *args = new nsCryptoRunArgs();
-
-  args->m_cx         = aContext;
+  nsCryptoRunArgs *args = new nsCryptoRunArgs(aContext);
+
   args->m_kungFuDeathGrip = GetISupportsFromContext(aContext);
   args->m_scope      = JS_GetParent(script_obj);
   if (!aJsCallback.IsVoid()) {
     args->m_jsCallback = aJsCallback;
   }
   args->m_principals = principals;
 
   nsCryptoRunnable *cryptoRunnable = new nsCryptoRunnable(args);
@@ -2164,55 +2163,47 @@ nsP12Runnable::Run()
 
   nsPKCS12Blob p12Cxt;
   
   p12Cxt.SetToken(mToken);
   p12Cxt.ExportToFile(localFile, mCertArr, mNumCerts);
   return NS_OK;
 }
 
-nsCryptoRunArgs::nsCryptoRunArgs() 
-{
-}
+nsCryptoRunArgs::nsCryptoRunArgs(JSContext *cx) : m_cx(cx), m_scope(cx) {}
+
 nsCryptoRunArgs::~nsCryptoRunArgs() {}
 
-
 nsCryptoRunnable::nsCryptoRunnable(nsCryptoRunArgs *args)
 {
   nsNSSShutDownPreventionLock locker;
   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);
 }
 
 //Implementation that runs the callback passed to 
 //crypto.generateCRMFRequest as an event.
 NS_IMETHODIMP
 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;
 }
 
 //Quick helper function to check if a newly issued cert
 //already exists in the user's database.
 static bool
 nsCertAlreadyExists(SECItem *derCert)