summaryrefslogtreecommitdiff
path: root/runtime/js/99_main.js
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-07-23 00:40:42 +0200
committerGitHub <noreply@github.com>2022-07-23 00:40:42 +0200
commit504d2936ecf1a5520ca20f83792a94b219e84f53 (patch)
treef4d23c520355f340374529d67b31657821e73163 /runtime/js/99_main.js
parent72199303d899b8ddf2ff46ed11bd513ed9cc47e2 (diff)
fix: unhandledrejection handling for sync throw in top level (#15279)
Fixes an edge in "unhandledrejection" event that prevent synchronous errors being surfaced when throw from a top-level scope.
Diffstat (limited to 'runtime/js/99_main.js')
-rw-r--r--runtime/js/99_main.js16
1 files changed, 14 insertions, 2 deletions
diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js
index 3421528d2..115de4d4d 100644
--- a/runtime/js/99_main.js
+++ b/runtime/js/99_main.js
@@ -602,7 +602,7 @@ delete Intl.v8BreakIterator;
WeakMapPrototypeDelete(pendingRejectionsReasons, promise);
if (!hasPendingException) {
- return;
+ continue;
}
const event = new PromiseRejectionEvent("unhandledrejection", {
@@ -610,9 +610,21 @@ delete Intl.v8BreakIterator;
promise,
reason,
});
+
+ const errorEventCb = (event) => {
+ if (event.error === reason) {
+ core.opSync("op_remove_pending_promise_exception", promise);
+ }
+ };
+ // Add a callback for "error" event - it will be dispatched
+ // if error is thrown during dispatch of "unhandledrejection"
+ // event.
+ globalThis.addEventListener("error", errorEventCb);
globalThis.dispatchEvent(event);
+ globalThis.removeEventListener("error", errorEventCb);
- // If event was not prevented we will let Rust side handle it.
+ // If event was not prevented (or "unhandledrejection" listeners didn't
+ // throw) we will let Rust side handle it.
if (event.defaultPrevented) {
core.opSync("op_remove_pending_promise_exception", promise);
}