Skip to content

Commit

Permalink
cache internal promise sym
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit committed Dec 9, 2024
1 parent 5460b65 commit 5b10136
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions core/runtime/jsrealm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub struct ContextState {
pub(crate) get_error_class_fn: GetErrorClassFn,
pub(crate) external_ops_tracker: ExternalOpsTracker,
pub(crate) promises: Promises,
pub(crate) internal_promise_sym: RefCell<Option<Rc<v8::Global<v8::Symbol>>>>,
}

type PromiseId = i32;
Expand Down Expand Up @@ -130,6 +131,7 @@ impl ContextState {
unrefed_ops: Default::default(),
external_ops_tracker,
promises: Default::default(),
internal_promise_sym: Default::default(),
}
}
}
Expand Down Expand Up @@ -219,6 +221,7 @@ impl JsRealmInner {
state.exception_state.prepare_to_destroy();
std::mem::take(&mut *state.js_event_loop_tick_cb.borrow_mut());
std::mem::take(&mut *state.js_wasm_streaming_cb.borrow_mut());
std::mem::take(&mut *state.internal_promise_sym.borrow_mut());

{
let ctx = self.context().open(isolate);
Expand Down
15 changes: 14 additions & 1 deletion core/runtime/jsruntime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,12 @@ impl JsRuntime {
/// Grab and store JavaScript bindings to callbacks necessary for the
/// JsRuntime to operate properly.
fn store_js_callbacks(&mut self, realm: &JsRealm, will_snapshot: bool) {
let (event_loop_tick_cb, build_custom_error_cb, wasm_instance_fn) = {
let (
event_loop_tick_cb,
build_custom_error_cb,
wasm_instance_fn,
internal_promise_sym,
) = {
let scope = &mut realm.handle_scope(self.v8_isolate());
let context = realm.context();
let context_local = v8::Local::new(scope, context);
Expand Down Expand Up @@ -1464,15 +1469,23 @@ impl JsRuntime {
}
}

let s =
FastString::from_static("Deno.core.internalPromiseId").v8_string(scope);
let sym = v8::Symbol::for_key(scope, s);
(
v8::Global::new(scope, event_loop_tick_cb),
v8::Global::new(scope, build_custom_error_cb),
wasm_instance_fn.map(|f| v8::Global::new(scope, f)),
v8::Global::new(scope, sym),
)
};

// Put global handles in the realm's ContextState
let state_rc = realm.0.state();
state_rc
.internal_promise_sym
.borrow_mut()
.replace(Rc::new(internal_promise_sym));
state_rc
.js_event_loop_tick_cb
.borrow_mut()
Expand Down
7 changes: 4 additions & 3 deletions core/runtime/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ pub fn make_promise(
let context = JsRealm::state_from_scope(scope);
let prom = v8::PromiseResolver::new(scope).unwrap();
let p = prom.get_promise(scope);
let s =
FastString::from_static("Deno.core.internalPromiseId").v8_string(scope);
let sym = v8::Symbol::for_key(scope, s);
let sym = v8::Local::new(
scope,
&*context.internal_promise_sym.borrow().clone().unwrap(),
);
let prom = v8::Global::new(scope, prom);
let id = context.promises.register_new(prom.clone());
let id_v = v8::Integer::new(scope, id);
Expand Down

0 comments on commit 5b10136

Please sign in to comment.