@@ -594,6 +594,20 @@ void Environment::AssignToContext(Local<v8::Context> context,
594
594
TrackContext (context);
595
595
}
596
596
597
+ void Environment::UnassignFromContext (Local<v8::Context> context) {
598
+ if (!context.IsEmpty ()) {
599
+ context->SetAlignedPointerInEmbedderData (ContextEmbedderIndex::kEnvironment ,
600
+ nullptr );
601
+ context->SetAlignedPointerInEmbedderData (ContextEmbedderIndex::kRealm ,
602
+ nullptr );
603
+ context->SetAlignedPointerInEmbedderData (
604
+ ContextEmbedderIndex::kBindingDataStoreIndex , nullptr );
605
+ context->SetAlignedPointerInEmbedderData (
606
+ ContextEmbedderIndex::kContextifyContext , nullptr );
607
+ }
608
+ UntrackContext (context);
609
+ }
610
+
597
611
void Environment::TryLoadAddon (
598
612
const char * filename,
599
613
int flags,
@@ -822,7 +836,6 @@ void Environment::InitializeMainContext(Local<Context> context,
822
836
const EnvSerializeInfo* env_info) {
823
837
principal_realm_ = std::make_unique<PrincipalRealm>(
824
838
this , context, MAYBE_FIELD_PTR (env_info, principal_realm));
825
- AssignToContext (context, principal_realm_.get (), ContextInfo (" " ));
826
839
if (env_info != nullptr ) {
827
840
DeserializeProperties (env_info);
828
841
}
@@ -892,9 +905,9 @@ Environment::~Environment() {
892
905
inspector_agent_.reset ();
893
906
#endif
894
907
895
- ctx-> SetAlignedPointerInEmbedderData (ContextEmbedderIndex:: kEnvironment ,
896
- nullptr );
897
- ctx-> SetAlignedPointerInEmbedderData (ContextEmbedderIndex:: kRealm , nullptr );
908
+ // Sub-realms should have been cleared with Environment's cleanup.
909
+ DCHECK_EQ (shadow_realms_. size (), 0 );
910
+ principal_realm_. reset ( );
898
911
899
912
if (trace_state_observer_) {
900
913
tracing::AgentWriterHandle* writer = GetTracingAgentWriter ();
@@ -917,10 +930,6 @@ Environment::~Environment() {
917
930
addon.Close ();
918
931
}
919
932
}
920
-
921
- for (auto realm : shadow_realms_) {
922
- realm->OnEnvironmentDestruct ();
923
- }
924
933
}
925
934
926
935
void Environment::InitializeLibuv () {
@@ -1716,6 +1725,9 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
1716
1725
std::cerr << *info << " \n " ;
1717
1726
}
1718
1727
1728
+ // Deserialize the realm's properties before running the deserialize
1729
+ // requests as the requests may need to access the realm's properties.
1730
+ principal_realm_->DeserializeProperties (&info->principal_realm );
1719
1731
RunDeserializeRequests ();
1720
1732
1721
1733
async_hooks_.Deserialize (ctx);
@@ -1726,8 +1738,6 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
1726
1738
exit_info_.Deserialize (ctx);
1727
1739
stream_base_state_.Deserialize (ctx);
1728
1740
should_abort_on_uncaught_toggle_.Deserialize (ctx);
1729
-
1730
- principal_realm_->DeserializeProperties (&info->principal_realm );
1731
1741
}
1732
1742
1733
1743
uint64_t GuessMemoryAvailableToTheProcess () {
0 commit comments