From a616d1d13ccc464a909bed1cf6782d2646f53c48 Mon Sep 17 00:00:00 2001
From: Jon Coppeard <jcoppeard@mozilla.com>
Date: Tue, 2 Aug 2022 08:36:19 +0000
Subject: [PATCH] Bug 1782496 - Handle uncatchable exceptions during module
 evaluation r=yulia, a=dsmith

This makes sure we still set the module state to evaluated when there's an
uncatchable exception. The module's error will be set to undefined, which is
not great but it will probably never be used in this case.

Differential Revision: https://phabricator.services.mozilla.com/D153372
---
 js/src/jit-test/tests/modules/bug-1782496.js |  9 +++++++++
 js/src/vm/Modules.cpp                        | 13 +++++--------
 2 files changed, 14 insertions(+), 8 deletions(-)
 create mode 100644 js/src/jit-test/tests/modules/bug-1782496.js

diff --git a/js/src/jit-test/tests/modules/bug-1782496.js b/js/src/jit-test/tests/modules/bug-1782496.js
new file mode 100644
index 0000000000000..33ea725a7be19
--- /dev/null
+++ b/js/src/jit-test/tests/modules/bug-1782496.js
@@ -0,0 +1,9 @@
+// |jit-test| exitstatus: 6; allow-overrecursed
+
+setInterruptCallback(function() {
+  import("javascript:null");
+  interruptIf(true);
+});
+
+interruptIf(true);
+for (;;) {}  // Wait for interrupt.
diff --git a/js/src/vm/Modules.cpp b/js/src/vm/Modules.cpp
index fec310ac1c19e..03caf7795a4ab 100644
--- a/js/src/vm/Modules.cpp
+++ b/js/src/vm/Modules.cpp
@@ -1281,17 +1281,14 @@ bool js::ModuleEvaluate(JSContext* cx, Handle<ModuleObject*> moduleArg,
 
   // Step 9. f result is an abrupt completion, then:
   if (!ok) {
-    if (!cx->isExceptionPending()) {
-      return false;  // Uncatchable exception.
-    }
-
+    // Attempt to take any pending exception, but make sure we still handle
+    // uncatchable exceptions.
     Rooted<Value> error(cx);
-    if (!cx->getPendingException(&error)) {
-      return false;
+    if (cx->isExceptionPending()) {
+      std::ignore = cx->getPendingException(&error);
+      cx->clearPendingException();
     }
 
-    cx->clearPendingException();
-
     // Step 9.a. For each Cyclic Module Record m of stack, do
     for (ModuleObject* m : stack) {
       // Step 9.a.i. Assert: m.[[Status]] is evaluating.
-- 
GitLab