Bug 1277135 - Add an rval argument to JS::CloneAndExecuteScript(); r=jorendorff
authorEhsan Akhgari <ehsan@mozilla.com>
Tue, 31 May 2016 23:34:40 -0400
changeset 345781 317aecd284b8a55b335a016a1a5bed3278893c9f
parent 345780 b1a5c29d781b6ba6222f4be26094e4c8ae8d8943
child 345782 d6523dfaab78d8409b2da9eac0693960c2d11904
push id6389
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:38:22 +0000
treeherdermozilla-beta@01d67bfe6c81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorendorff
bugs1277135
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1277135 - Add an rval argument to JS::CloneAndExecuteScript(); r=jorendorff
dom/base/nsFrameMessageManager.cpp
dom/xul/XULDocument.cpp
js/src/builtin/TestingFunctions.cpp
js/src/jsapi-tests/testScriptObject.cpp
js/src/jsapi.cpp
js/src/jsapi.h
--- a/dom/base/nsFrameMessageManager.cpp
+++ b/dom/base/nsFrameMessageManager.cpp
@@ -1730,17 +1730,18 @@ nsMessageManagerScriptExecutor::LoadScri
   }
 
   JS::Rooted<JSObject*> global(rt, mGlobal->GetJSObject());
   if (global) {
     AutoEntryScript aes(global, "message manager script load");
     JSContext* cx = aes.cx();
     if (script) {
       if (aRunInGlobalScope) {
-        JS::CloneAndExecuteScript(cx, script);
+        JS::RootedValue rval(cx);
+        JS::CloneAndExecuteScript(cx, script, &rval);
       } else {
         JS::Rooted<JSObject*> scope(cx);
         bool ok = js::ExecuteInGlobalAndReturnScope(cx, global, script, &scope);
         if (ok) {
           // Force the scope to stay alive.
           mAnonymousGlobalScopes.AppendElement(scope);
         }
       }
--- a/dom/xul/XULDocument.cpp
+++ b/dom/xul/XULDocument.cpp
@@ -3519,17 +3519,18 @@ XULDocument::ExecuteScript(nsXULPrototyp
 
     JS::ExposeObjectToActiveJS(global);
     xpc_UnmarkGrayScript(scriptObject);
     JSAutoCompartment ac(cx, global);
 
     // The script is in the compilation scope. Clone it into the target scope
     // and execute it. On failure, ~AutoScriptEntry will handle exceptions, so
     // there is no need to manually check the return value.
-    JS::CloneAndExecuteScript(cx, scriptObject);
+    JS::RootedValue rval(cx);
+    JS::CloneAndExecuteScript(cx, scriptObject, &rval);
 
     return NS_OK;
 }
 
 
 nsresult
 XULDocument::CreateElementFromPrototype(nsXULPrototypeElement* aPrototype,
                                         Element** aResult,
--- a/js/src/builtin/TestingFunctions.cpp
+++ b/js/src/builtin/TestingFunctions.cpp
@@ -3042,17 +3042,18 @@ ShellCloneAndExecuteScript(JSContext* cx
     }
     if (!global->is<GlobalObject>()) {
         JS_ReportError(cx, "Argument must be a global object");
         return false;
     }
 
     AutoCompartment ac(cx, global);
 
-    if (!JS::CloneAndExecuteScript(cx, script))
+    JS::RootedValue rval(cx);
+    if (!JS::CloneAndExecuteScript(cx, script, &rval))
         return false;
 
     args.rval().setUndefined();
     return true;
 }
 
 static bool
 IsSimdAvailable(JSContext* cx, unsigned argc, Value* vp)
--- a/js/src/jsapi-tests/testScriptObject.cpp
+++ b/js/src/jsapi-tests/testScriptObject.cpp
@@ -164,8 +164,37 @@ BEGIN_FIXTURE_TEST(ScriptObjectFixture, 
     CHECK(fseek(script_stream, 0, SEEK_SET) != EOF);
     JS::CompileOptions options(cx);
     options.setFileAndLine("temporary file", 1);
     JS::RootedScript script(cx);
     CHECK(JS::Compile(cx, options, script_stream, &script));
     return tryScript(script);
 }
 END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileFileHandleForPrincipals)
+
+BEGIN_FIXTURE_TEST(ScriptObjectFixture, CloneAndExecuteScript)
+{
+    JS::RootedValue fortyTwo(cx);
+    fortyTwo.setInt32(42);
+    CHECK(JS_SetProperty(cx, global, "val", fortyTwo));
+    JS::RootedScript script(cx);
+    JS::CompileOptions options(cx);
+    options.setFileAndLine(__FILE__, __LINE__);
+    CHECK(JS_CompileScript(cx, "val", 3, options, &script));
+    JS::RootedValue value(cx);
+    CHECK(JS_ExecuteScript(cx, script, &value));
+    CHECK(value.toInt32() == 42);
+    {
+        JS::RootedObject global2(cx, createGlobal());
+        CHECK(global2);
+        JSAutoCompartment ac(cx, global2);
+        CHECK(JS_WrapValue(cx, &fortyTwo));
+        CHECK(JS_SetProperty(cx, global, "val", fortyTwo));
+        JS::RootedValue value2(cx);
+        CHECK(JS::CloneAndExecuteScript(cx, script, &value2));
+        CHECK(value2.toInt32() == 42);
+    }
+    JS::RootedValue value3(cx);
+    CHECK(JS_ExecuteScript(cx, script, &value3));
+    CHECK(value3.toInt32() == 42);
+    return true;
+}
+END_FIXTURE_TEST(ScriptObjectFixture, CloneAndExecuteScript)
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -4386,30 +4386,31 @@ JS_ExecuteScript(JSContext* cx, AutoObje
 
 MOZ_NEVER_INLINE JS_PUBLIC_API(bool)
 JS_ExecuteScript(JSContext* cx, AutoObjectVector& scopeChain, HandleScript scriptArg)
 {
     return ExecuteScript(cx, scopeChain, scriptArg, nullptr);
 }
 
 JS_PUBLIC_API(bool)
-JS::CloneAndExecuteScript(JSContext* cx, HandleScript scriptArg)
+JS::CloneAndExecuteScript(JSContext* cx, HandleScript scriptArg,
+                          JS::MutableHandleValue rval)
 {
     CHECK_REQUEST(cx);
     RootedScript script(cx, scriptArg);
     Rooted<ClonedBlockObject*> globalLexical(cx, &cx->global()->lexicalScope());
     if (script->compartment() != cx->compartment()) {
         Rooted<StaticScope*> staticLexical(cx, &globalLexical->staticBlock());
         script = CloneGlobalScript(cx, staticLexical, script);
         if (!script)
             return false;
 
         js::Debugger::onNewScript(cx, script);
     }
-    return ExecuteScript(cx, globalLexical, script, nullptr);
+    return ExecuteScript(cx, globalLexical, script, rval.address());
 }
 
 static const unsigned LARGE_SCRIPT_LENGTH = 500*1024;
 
 static bool
 Evaluate(JSContext* cx, HandleObject scope, Handle<StaticScope*> staticScope,
          const ReadOnlyCompileOptions& optionsArg,
          SourceBufferHolder& srcBuf, MutableHandleValue rval)
--- a/js/src/jsapi.h
+++ b/js/src/jsapi.h
@@ -4169,17 +4169,18 @@ JS_ExecuteScript(JSContext* cx, JS::Auto
 
 namespace JS {
 
 /**
  * Like the above, but handles a cross-compartment script. If the script is
  * cross-compartment, it is cloned into the current compartment before executing.
  */
 extern JS_PUBLIC_API(bool)
-CloneAndExecuteScript(JSContext* cx, JS::Handle<JSScript*> script);
+CloneAndExecuteScript(JSContext* cx, JS::Handle<JSScript*> script,
+                      JS::MutableHandleValue rval);
 
 } /* namespace JS */
 
 namespace JS {
 
 /**
  * Evaluate the given source buffer in the scope of the current global of cx.
  */