@@ -95,6 +95,20 @@ v8::Local<v8::Object> AsyncHooks::native_execution_async_resource(size_t i) {
9595 return PersistentToLocal::Strong (native_execution_async_resources_[i]);
9696}
9797
98+ inline void AsyncHooks::SetJSPromiseHooks (v8::Local<v8::Function> init,
99+ v8::Local<v8::Function> before,
100+ v8::Local<v8::Function> after,
101+ v8::Local<v8::Function> resolve) {
102+ js_promise_hooks_[0 ].Reset (env ()->isolate (), init);
103+ js_promise_hooks_[1 ].Reset (env ()->isolate (), before);
104+ js_promise_hooks_[2 ].Reset (env ()->isolate (), after);
105+ js_promise_hooks_[3 ].Reset (env ()->isolate (), resolve);
106+ for (auto it = contexts_.begin (); it != contexts_.end (); it++) {
107+ PersistentToLocal::Strong (*it)
108+ ->SetPromiseHooks (init, before, after, resolve);
109+ }
110+ }
111+
98112inline v8::Local<v8::String> AsyncHooks::provider_string (int idx) {
99113 return env ()->isolate_data ()->async_wrap_provider (idx);
100114}
@@ -217,6 +231,37 @@ void AsyncHooks::clear_async_id_stack() {
217231 fields_[kStackLength ] = 0 ;
218232}
219233
234+ inline void AsyncHooks::AddContext (v8::Local<v8::Context> ctx) {
235+ ctx->SetPromiseHooks (
236+ js_promise_hooks_[0 ].IsEmpty () ?
237+ v8::Local<v8::Function>() :
238+ PersistentToLocal::Strong (js_promise_hooks_[0 ]),
239+ js_promise_hooks_[1 ].IsEmpty () ?
240+ v8::Local<v8::Function>() :
241+ PersistentToLocal::Strong (js_promise_hooks_[1 ]),
242+ js_promise_hooks_[2 ].IsEmpty () ?
243+ v8::Local<v8::Function>() :
244+ PersistentToLocal::Strong (js_promise_hooks_[2 ]),
245+ js_promise_hooks_[3 ].IsEmpty () ?
246+ v8::Local<v8::Function>() :
247+ PersistentToLocal::Strong (js_promise_hooks_[3 ]));
248+
249+ size_t id = contexts_.size ();
250+ contexts_.resize (id + 1 );
251+ contexts_[id].Reset (env ()->isolate (), ctx);
252+ }
253+
254+ inline void AsyncHooks::RemoveContext (v8::Local<v8::Context> ctx) {
255+ for (auto it = contexts_.begin (); it != contexts_.end (); it++) {
256+ v8::Local<v8::Context> saved_context = PersistentToLocal::Strong (*it);
257+ if (saved_context == ctx) {
258+ it->Reset ();
259+ contexts_.erase (it);
260+ break ;
261+ }
262+ }
263+ }
264+
220265// The DefaultTriggerAsyncIdScope(AsyncWrap*) constructor is defined in
221266// async_wrap-inl.h to avoid a circular dependency.
222267
@@ -304,6 +349,8 @@ inline void Environment::AssignToContext(v8::Local<v8::Context> context,
304349#if HAVE_INSPECTOR
305350 inspector_agent ()->ContextCreated (context, info);
306351#endif // HAVE_INSPECTOR
352+
353+ this ->async_hooks ()->AddContext (context);
307354}
308355
309356inline Environment* Environment::GetCurrent (v8::Isolate* isolate) {
0 commit comments