Bug 1519322 - Wrap unhandled/handled promise properly in JS shell promise rejection tracker. r=jorendorff
authorTooru Fujisawa <arai_a@mac.com>
Fri, 11 Jan 2019 21:11:50 +0000
changeset 453544 0279e3d8b2f1
parent 453543 4af076789478
child 453545 466e822d2986
push id35360
push usernbeleuzu@mozilla.com
push dateSat, 12 Jan 2019 09:39:47 +0000
treeherdermozilla-central@cb35977ae7a4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorendorff
bugs1519322
milestone66.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 1519322 - Wrap unhandled/handled promise properly in JS shell promise rejection tracker. r=jorendorff Differential Revision: https://phabricator.services.mozilla.com/D16303
js/src/jit-test/tests/promise/unhandled-rejections-different-realm.js
js/src/shell/js.cpp
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/tests/promise/unhandled-rejections-different-realm.js
@@ -0,0 +1,19 @@
+// |jit-test| error:Unhandled rejection: "some reason"
+
+// Test JS shell's unhandled rejection tracking.
+
+var z = newGlobal();
+
+Promise.prototype.then = z.Promise.prototype.then;
+
+// Add unhandled rejection from other realm.
+evalcx("var p = (async function() { throw 'some reason' })()", z);
+
+// Add unhandled rejection from this realm.
+var p = (async function f() { throw 'other reason'; })();
+
+// Remove unhandled rejection from this realm.
+p.then();
+
+// Remove unhandled rejection from other realm.
+evalcx("p.then()", z);
--- a/js/src/shell/js.cpp
+++ b/js/src/shell/js.cpp
@@ -1072,22 +1072,19 @@ static bool TrackUnhandledRejections(JSC
     sc->unhandledRejectedPromises = SetObject::create(cx);
     if (!sc->unhandledRejectedPromises) {
       return false;
     }
   }
 
   RootedValue promiseVal(cx, ObjectValue(*promise));
 
-  Maybe<AutoRealm> ar;
-  if (cx->realm() != sc->unhandledRejectedPromises->realm()) {
-    ar.emplace(cx, sc->unhandledRejectedPromises);
-    if (!cx->compartment()->wrap(cx, &promiseVal)) {
-      return false;
-    }
+  AutoRealm ar(cx, sc->unhandledRejectedPromises);
+  if (!cx->compartment()->wrap(cx, &promiseVal)) {
+    return false;
   }
 
   switch (state) {
     case JS::PromiseRejectionHandlingState::Unhandled:
       if (!SetObject::add(cx, sc->unhandledRejectedPromises, promiseVal)) {
         return false;
       }
       break;
@@ -1125,17 +1122,17 @@ static void ForwardingPromiseRejectionTr
   args[0].setObject(*promise);
   args[1].setInt32(static_cast<int32_t>(state));
 
   if (!JS_WrapValue(cx, args[0])) {
     return;
   }
 
   RootedValue rval(cx);
-  (void) Call(cx, callback, UndefinedHandleValue, args, &rval);
+  (void)Call(cx, callback, UndefinedHandleValue, args, &rval);
 }
 
 static bool SetPromiseRejectionTrackerCallback(JSContext* cx, unsigned argc,
                                                Value* vp) {
   CallArgs args = CallArgsFromVp(argc, vp);
 
   if (!IsFunctionObject(args.get(0))) {
     JS_ReportErrorASCII(