diff options
author | Andreu Botella <andreu@andreubotella.com> | 2023-01-14 19:39:00 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-15 09:09:00 +0530 |
commit | 05ef925eb095ae603f694fe8172ddcbd5b19b9b2 (patch) | |
tree | f4cd7fe88a62f5f427c2dba77f9902b13abaf501 /core/bindings.rs | |
parent | df8bfa26be69af46b8f166e255330b5ba8d48893 (diff) |
refactor(core): Move optional callbacks from `JsRuntimeState` to `ContextState` (#17422)
The `JsRuntimeState` struct stores a number of JS callbacks that are
used either in the event loop or when interacting with V8. Some of these
callback fields are vectors of callbacks, and therefore could plausibly
store at least one callback per realm. However, some of those fields are
`Option<v8::Global<v8::Function>>`, which would make the callbacks set
by a realm override the one that might have been set by a different
realm.
As it turns out, all of the current such optional callbacks
(`js_promise_reject_cb`, `js_format_exception_cb` and
`js_wasm_streaming_cb`) are only used from inside a realm, and therefore
this change makes it so such callbacks can only be set from inside a
realm, and will only affect that realm.
This is a reland of #15599.
Towards #13239.
Diffstat (limited to 'core/bindings.rs')
-rw-r--r-- | core/bindings.rs | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/core/bindings.rs b/core/bindings.rs index 6707f115c..a60db977a 100644 --- a/core/bindings.rs +++ b/core/bindings.rs @@ -16,6 +16,7 @@ use crate::modules::ModuleMap; use crate::modules::ResolutionKind; use crate::ops::OpCtx; use crate::runtime::SnapshotOptions; +use crate::JsRealm; use crate::JsRuntime; pub fn external_references( @@ -453,15 +454,15 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) { // SAFETY: `CallbackScope` can be safely constructed from `&PromiseRejectMessage` let scope = &mut unsafe { v8::CallbackScope::new(&message) }; - let state_rc = JsRuntime::state(scope); - let mut state = state_rc.borrow_mut(); + let context_state_rc = JsRealm::state_from_scope(scope); - if let Some(js_promise_reject_cb) = state.js_promise_reject_cb.clone() { + let promise_reject_cb = + context_state_rc.borrow().js_promise_reject_cb.clone(); + if let Some(js_promise_reject_cb) = promise_reject_cb { let tc_scope = &mut v8::TryCatch::new(scope); let undefined: v8::Local<v8::Value> = v8::undefined(tc_scope).into(); let type_ = v8::Integer::new(tc_scope, message.get_event() as i32); let promise = message.get_promise(); - drop(state); // Drop borrow, callbacks can call back into runtime. let reason = match message.get_event() { PromiseRejectWithNoHandler @@ -484,6 +485,7 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) { }; if has_unhandled_rejection_handler { + let state_rc = JsRuntime::state(tc_scope); let mut state = state_rc.borrow_mut(); if let Some(pending_mod_evaluate) = state.pending_mod_evaluate.as_mut() { if !pending_mod_evaluate.has_evaluated { @@ -494,6 +496,8 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) { } } } else { + let state_rc = JsRuntime::state(scope); + let mut state = state_rc.borrow_mut(); let promise = message.get_promise(); let promise_global = v8::Global::new(scope, promise); match message.get_event() { |