summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-07-14 22:39:20 +0200
committerGitHub <noreply@github.com>2022-07-14 22:39:20 +0200
commit1a7259b04b7229f6350a7a7c21b50497b5c80c17 (patch)
tree2bcea50749221b080d0402f5cf9bd55052437ae0 /core
parent48a7312f3871e3b76c51eb0143ca1c48da43b20c (diff)
feat: add "unhandledrejection" event support (#12994) (#15080)
Relanding #12994 This commit adds support for "unhandledrejection" event. This event will trigger event listeners registered using: "globalThis.addEventListener("unhandledrejection") "globalThis.onunhandledrejection" This is done by registering a default handler using "Deno.core.setPromiseRejectCallback" that allows to handle rejected promises in JavaScript instead of Rust. This commit will make it possible to polyfill "process.on("unhandledRejection")" in the Node compat layer. Co-authored-by: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'core')
-rw-r--r--core/01_core.js4
-rw-r--r--core/bindings.rs10
-rw-r--r--core/ops_builtin_v8.rs48
3 files changed, 53 insertions, 9 deletions
diff --git a/core/01_core.js b/core/01_core.js
index 156300327..21376b695 100644
--- a/core/01_core.js
+++ b/core/01_core.js
@@ -268,6 +268,10 @@
terminate: opSync.bind(null, "op_terminate"),
opNames: opSync.bind(null, "op_op_names"),
eventLoopHasMoreWork: opSync.bind(null, "op_event_loop_has_more_work"),
+ setPromiseRejectCallback: opSync.bind(
+ null,
+ "op_set_promise_reject_callback",
+ ),
});
ObjectAssign(globalThis.__bootstrap, { core });
diff --git a/core/bindings.rs b/core/bindings.rs
index 49dc62c0e..c9e27f087 100644
--- a/core/bindings.rs
+++ b/core/bindings.rs
@@ -282,14 +282,6 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
let state_rc = JsRuntime::state(scope);
let mut state = state_rc.borrow_mut();
- // Node compat: perform synchronous process.emit("unhandledRejection").
- //
- // Note the callback follows the (type, promise, reason) signature of Node's
- // internal promiseRejectHandler from lib/internal/process/promises.js, not
- // the (promise, reason) signature of the "unhandledRejection" event listener.
- //
- // Short-circuits Deno's regular unhandled rejection logic because that's
- // a) asynchronous, and b) always terminates.
if let Some(js_promise_reject_cb) = state.js_promise_reject_cb.clone() {
let js_uncaught_exception_cb = state.js_uncaught_exception_cb.clone();
@@ -331,6 +323,7 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
}
if tc_scope.has_caught() {
+ // TODO(bartlomieju): ensure that TODO provided below is still valid.
// If we get here, an exception was thrown by the unhandledRejection
// handler and there is ether no uncaughtException handler or the
// handler threw an exception of its own.
@@ -348,7 +341,6 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) {
} else {
let promise = message.get_promise();
let promise_global = v8::Global::new(scope, promise);
-
match message.get_event() {
PromiseRejectWithNoHandler => {
let error = message.get_value().unwrap();
diff --git a/core/ops_builtin_v8.rs b/core/ops_builtin_v8.rs
index 39469c0ad..a17e273f3 100644
--- a/core/ops_builtin_v8.rs
+++ b/core/ops_builtin_v8.rs
@@ -48,6 +48,9 @@ pub(crate) fn init_builtins_v8() -> Vec<OpDecl> {
op_apply_source_map::decl(),
op_set_format_exception_callback::decl(),
op_event_loop_has_more_work::decl(),
+ op_store_pending_promise_exception::decl(),
+ op_remove_pending_promise_exception::decl(),
+ op_has_pending_promise_exception::decl(),
]
}
@@ -792,3 +795,48 @@ fn op_set_format_exception_callback<'a>(
fn op_event_loop_has_more_work(scope: &mut v8::HandleScope) -> bool {
JsRuntime::event_loop_pending_state(scope).is_pending()
}
+
+#[op(v8)]
+fn op_store_pending_promise_exception<'a>(
+ scope: &mut v8::HandleScope<'a>,
+ promise: serde_v8::Value<'a>,
+ reason: serde_v8::Value<'a>,
+) {
+ let state_rc = JsRuntime::state(scope);
+ let mut state = state_rc.borrow_mut();
+ let promise_value =
+ v8::Local::<v8::Promise>::try_from(promise.v8_value).unwrap();
+ let promise_global = v8::Global::new(scope, promise_value);
+ let error_global = v8::Global::new(scope, reason.v8_value);
+ state
+ .pending_promise_exceptions
+ .insert(promise_global, error_global);
+}
+
+#[op(v8)]
+fn op_remove_pending_promise_exception<'a>(
+ scope: &mut v8::HandleScope<'a>,
+ promise: serde_v8::Value<'a>,
+) {
+ let state_rc = JsRuntime::state(scope);
+ let mut state = state_rc.borrow_mut();
+ let promise_value =
+ v8::Local::<v8::Promise>::try_from(promise.v8_value).unwrap();
+ let promise_global = v8::Global::new(scope, promise_value);
+ state.pending_promise_exceptions.remove(&promise_global);
+}
+
+#[op(v8)]
+fn op_has_pending_promise_exception<'a>(
+ scope: &mut v8::HandleScope<'a>,
+ promise: serde_v8::Value<'a>,
+) -> bool {
+ let state_rc = JsRuntime::state(scope);
+ let state = state_rc.borrow();
+ let promise_value =
+ v8::Local::<v8::Promise>::try_from(promise.v8_value).unwrap();
+ let promise_global = v8::Global::new(scope, promise_value);
+ state
+ .pending_promise_exceptions
+ .contains_key(&promise_global)
+}