@@ -63,20 +63,28 @@ int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64;
63
63
void * const ContextEmbedderTag::kNodeContextTagPtr = const_cast <void *>(
64
64
static_cast <const void *>(&ContextEmbedderTag::kNodeContextTag ));
65
65
66
- void AsyncHooks::SetJSPromiseHooks (Local<Function> init,
66
+ void AsyncHooks::ResetPromiseHooks (Local<Function> init,
67
67
Local<Function> before,
68
68
Local<Function> after,
69
69
Local<Function> resolve) {
70
70
js_promise_hooks_[0 ].Reset (env ()->isolate (), init);
71
71
js_promise_hooks_[1 ].Reset (env ()->isolate (), before);
72
72
js_promise_hooks_[2 ].Reset (env ()->isolate (), after);
73
73
js_promise_hooks_[3 ].Reset (env ()->isolate (), resolve);
74
+ }
75
+
76
+ void Environment::ResetPromiseHooks (Local<Function> init,
77
+ Local<Function> before,
78
+ Local<Function> after,
79
+ Local<Function> resolve) {
80
+ async_hooks ()->ResetPromiseHooks (init, before, after, resolve);
81
+
74
82
for (auto it = contexts_.begin (); it != contexts_.end (); it++) {
75
83
if (it->IsEmpty ()) {
76
84
contexts_.erase (it--);
77
85
continue ;
78
86
}
79
- PersistentToLocal::Weak (env ()-> isolate () , *it)
87
+ PersistentToLocal::Weak (isolate_ , *it)
80
88
->SetPromiseHooks (init, before, after, resolve);
81
89
}
82
90
}
@@ -179,7 +187,7 @@ void AsyncHooks::clear_async_id_stack() {
179
187
fields_[kStackLength ] = 0 ;
180
188
}
181
189
182
- void AsyncHooks::AddContext (Local<Context> ctx) {
190
+ void AsyncHooks::InstallPromiseHooks (Local<Context> ctx) {
183
191
ctx->SetPromiseHooks (js_promise_hooks_[0 ].IsEmpty ()
184
192
? Local<Function>()
185
193
: PersistentToLocal::Strong (js_promise_hooks_[0 ]),
@@ -192,23 +200,24 @@ void AsyncHooks::AddContext(Local<Context> ctx) {
192
200
js_promise_hooks_[3 ].IsEmpty ()
193
201
? Local<Function>()
194
202
: PersistentToLocal::Strong (js_promise_hooks_[3 ]));
203
+ }
195
204
205
+ void Environment::TrackContext (Local<Context> context) {
196
206
size_t id = contexts_.size ();
197
207
contexts_.resize (id + 1 );
198
- contexts_[id].Reset (env ()-> isolate (), ctx );
208
+ contexts_[id].Reset (isolate_, context );
199
209
contexts_[id].SetWeak ();
200
210
}
201
211
202
- void AsyncHooks::RemoveContext (Local<Context> ctx) {
203
- Isolate* isolate = env ()->isolate ();
204
- HandleScope handle_scope (isolate);
212
+ void Environment::UntrackContext (Local<Context> context) {
213
+ HandleScope handle_scope (isolate_);
205
214
contexts_.erase (std::remove_if (contexts_.begin (),
206
215
contexts_.end (),
207
216
[&](auto && el) { return el.IsEmpty (); }),
208
217
contexts_.end ());
209
218
for (auto it = contexts_.begin (); it != contexts_.end (); it++) {
210
- Local<Context> saved_context = PersistentToLocal::Weak (isolate , *it);
211
- if (saved_context == ctx ) {
219
+ Local<Context> saved_context = PersistentToLocal::Weak (isolate_ , *it);
220
+ if (saved_context == context ) {
212
221
it->Reset ();
213
222
contexts_.erase (it);
214
223
break ;
@@ -543,7 +552,8 @@ void Environment::AssignToContext(Local<v8::Context> context,
543
552
inspector_agent ()->ContextCreated (context, info);
544
553
#endif // HAVE_INSPECTOR
545
554
546
- this ->async_hooks ()->AddContext (context);
555
+ this ->async_hooks ()->InstallPromiseHooks (context);
556
+ TrackContext (context);
547
557
}
548
558
549
559
void Environment::TryLoadAddon (
@@ -1466,8 +1476,9 @@ AsyncHooks::SerializeInfo AsyncHooks::Serialize(Local<Context> context,
1466
1476
context,
1467
1477
native_execution_async_resources_[i]);
1468
1478
}
1469
- CHECK_EQ (contexts_.size (), 1 );
1470
- CHECK_EQ (contexts_[0 ], env ()->context ());
1479
+
1480
+ // At the moment, promise hooks are not supported in the startup snapshot.
1481
+ // TODO(joyeecheung): support promise hooks in the startup snapshot.
1471
1482
CHECK (js_promise_hooks_[0 ].IsEmpty ());
1472
1483
CHECK (js_promise_hooks_[1 ].IsEmpty ());
1473
1484
CHECK (js_promise_hooks_[2 ].IsEmpty ());
@@ -1602,6 +1613,10 @@ EnvSerializeInfo Environment::Serialize(SnapshotCreator* creator) {
1602
1613
should_abort_on_uncaught_toggle_.Serialize (ctx, creator);
1603
1614
1604
1615
info.principal_realm = principal_realm_->Serialize (creator);
1616
+ // For now we only support serialization of the main context.
1617
+ // TODO(joyeecheung): support de/serialization of vm contexts.
1618
+ CHECK_EQ (contexts_.size (), 1 );
1619
+ CHECK_EQ (contexts_[0 ], context ());
1605
1620
return info;
1606
1621
}
1607
1622
0 commit comments