@@ -448,6 +448,12 @@ void IsolateData::CreateProperties() {
448
448
#undef V
449
449
450
450
// TODO(legendecas): eagerly create per isolate templates.
451
+ Local<FunctionTemplate> templ = FunctionTemplate::New (isolate ());
452
+ templ->InstanceTemplate ()->SetInternalFieldCount (
453
+ BaseObject::kInternalFieldCount );
454
+ templ->Inherit (BaseObject::GetConstructorTemplate (this ));
455
+ set_binding_data_ctor_template (templ);
456
+
451
457
set_contextify_global_template (
452
458
contextify::ContextifyContext::CreateGlobalTemplate (isolate_));
453
459
}
@@ -502,6 +508,10 @@ void TrackingTraceStateObserver::UpdateTraceCategoryState() {
502
508
return ;
503
509
}
504
510
511
+ if (env_->principal_realm () == nullptr ) {
512
+ return ;
513
+ }
514
+
505
515
bool async_hooks_enabled = (*(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED (
506
516
TRACING_CATEGORY_NODE1 (async_hooks)))) != 0 ;
507
517
@@ -517,9 +527,11 @@ void TrackingTraceStateObserver::UpdateTraceCategoryState() {
517
527
}
518
528
519
529
void Environment::AssignToContext (Local<v8::Context> context,
530
+ Realm* realm,
520
531
const ContextInfo& info) {
521
532
context->SetAlignedPointerInEmbedderData (ContextEmbedderIndex::kEnvironment ,
522
533
this );
534
+ context->SetAlignedPointerInEmbedderData (ContextEmbedderIndex::kRealm , realm);
523
535
// Used to retrieve bindings
524
536
context->SetAlignedPointerInEmbedderData (
525
537
ContextEmbedderIndex::kBindingListIndex , &(this ->bindings_ ));
@@ -594,56 +606,6 @@ std::unique_ptr<v8::BackingStore> Environment::release_managed_buffer(
594
606
return bs;
595
607
}
596
608
597
- void Environment::CreateProperties () {
598
- HandleScope handle_scope (isolate_);
599
- Local<Context> ctx = context ();
600
-
601
- {
602
- Context::Scope context_scope (ctx);
603
- Local<FunctionTemplate> templ = FunctionTemplate::New (isolate ());
604
- templ->InstanceTemplate ()->SetInternalFieldCount (
605
- BaseObject::kInternalFieldCount );
606
- templ->Inherit (BaseObject::GetConstructorTemplate (this ));
607
-
608
- set_binding_data_ctor_template (templ);
609
- }
610
-
611
- // Store primordials setup by the per-context script in the environment.
612
- Local<Object> per_context_bindings =
613
- GetPerContextExports (ctx).ToLocalChecked ();
614
- Local<Value> primordials =
615
- per_context_bindings->Get (ctx, primordials_string ()).ToLocalChecked ();
616
- CHECK (primordials->IsObject ());
617
- set_primordials (primordials.As <Object>());
618
-
619
- Local<String> prototype_string =
620
- FIXED_ONE_BYTE_STRING (isolate (), " prototype" );
621
-
622
- #define V (EnvPropertyName, PrimordialsPropertyName ) \
623
- { \
624
- Local<Value> ctor = \
625
- primordials.As <Object>() \
626
- ->Get (ctx, \
627
- FIXED_ONE_BYTE_STRING (isolate (), PrimordialsPropertyName)) \
628
- .ToLocalChecked (); \
629
- CHECK (ctor->IsObject ()); \
630
- Local<Value> prototype = \
631
- ctor.As <Object>()->Get (ctx, prototype_string).ToLocalChecked (); \
632
- CHECK (prototype->IsObject ()); \
633
- set_##EnvPropertyName (prototype.As <Object>()); \
634
- }
635
-
636
- V (primordials_safe_map_prototype_object, " SafeMap" );
637
- V (primordials_safe_set_prototype_object, " SafeSet" );
638
- V (primordials_safe_weak_map_prototype_object, " SafeWeakMap" );
639
- V (primordials_safe_weak_set_prototype_object, " SafeWeakSet" );
640
- #undef V
641
-
642
- Local<Object> process_object =
643
- node::CreateProcessObject (this ).FromMaybe (Local<Object>());
644
- set_process_object (process_object);
645
- }
646
-
647
609
std::string GetExecPath (const std::vector<std::string>& argv) {
648
610
char exec_path_buf[2 * PATH_MAX];
649
611
size_t exec_path_len = sizeof (exec_path_buf);
@@ -781,12 +743,11 @@ Environment::Environment(IsolateData* isolate_data,
781
743
782
744
void Environment::InitializeMainContext (Local<Context> context,
783
745
const EnvSerializeInfo* env_info) {
784
- context_.Reset (context->GetIsolate (), context);
785
- AssignToContext (context, ContextInfo (" " ));
746
+ principal_realm_ = std::make_unique<Realm>(
747
+ this , context, MAYBE_FIELD_PTR (env_info, principal_realm));
748
+ AssignToContext (context, principal_realm_.get (), ContextInfo (" " ));
786
749
if (env_info != nullptr ) {
787
750
DeserializeProperties (env_info);
788
- } else {
789
- CreateProperties ();
790
751
}
791
752
792
753
if (!options_->force_async_hooks_checks ) {
@@ -811,16 +772,18 @@ void Environment::InitializeMainContext(Local<Context> context,
811
772
}
812
773
813
774
Environment::~Environment () {
775
+ HandleScope handle_scope (isolate ());
776
+ Local<Context> ctx = context ();
777
+
814
778
if (Environment** interrupt_data = interrupt_data_.load ()) {
815
779
// There are pending RequestInterrupt() callbacks. Tell them not to run,
816
780
// then force V8 to run interrupts by compiling and running an empty script
817
781
// so as not to leak memory.
818
782
*interrupt_data = nullptr ;
819
783
820
784
Isolate::AllowJavascriptExecutionScope allow_js_here (isolate ());
821
- HandleScope handle_scope (isolate ());
822
785
TryCatch try_catch (isolate ());
823
- Context::Scope context_scope (context () );
786
+ Context::Scope context_scope (ctx );
824
787
825
788
#ifdef DEBUG
826
789
bool consistency_check = false ;
@@ -830,8 +793,8 @@ Environment::~Environment() {
830
793
#endif
831
794
832
795
Local<Script> script;
833
- if (Script::Compile (context () , String::Empty (isolate ())).ToLocal (&script))
834
- USE (script->Run (context () ));
796
+ if (Script::Compile (ctx , String::Empty (isolate ())).ToLocal (&script))
797
+ USE (script->Run (ctx ));
835
798
836
799
DCHECK (consistency_check);
837
800
}
@@ -846,16 +809,15 @@ Environment::~Environment() {
846
809
isolate ()->GetHeapProfiler ()->RemoveBuildEmbedderGraphCallback (
847
810
BuildEmbedderGraph, this );
848
811
849
- HandleScope handle_scope (isolate ());
850
-
851
812
#if HAVE_INSPECTOR
852
813
// Destroy inspector agent before erasing the context. The inspector
853
814
// destructor depends on the context still being accessible.
854
815
inspector_agent_.reset ();
855
816
#endif
856
817
857
- context ()->SetAlignedPointerInEmbedderData (ContextEmbedderIndex::kEnvironment ,
858
- nullptr );
818
+ ctx->SetAlignedPointerInEmbedderData (ContextEmbedderIndex::kEnvironment ,
819
+ nullptr );
820
+ ctx->SetAlignedPointerInEmbedderData (ContextEmbedderIndex::kRealm , nullptr );
859
821
860
822
if (trace_state_observer_) {
861
823
tracing::AgentWriterHandle* writer = GetTracingAgentWriter ();
@@ -1677,81 +1639,14 @@ EnvSerializeInfo Environment::Serialize(SnapshotCreator* creator) {
1677
1639
info.should_abort_on_uncaught_toggle =
1678
1640
should_abort_on_uncaught_toggle_.Serialize (ctx, creator);
1679
1641
1680
- uint32_t id = 0 ;
1681
- #define V (PropertyName, TypeName ) \
1682
- do { \
1683
- Local<TypeName> field = PropertyName (); \
1684
- if (!field.IsEmpty ()) { \
1685
- size_t index = creator->AddData (ctx, field); \
1686
- info.persistent_values .push_back ({#PropertyName, id, index }); \
1687
- } \
1688
- id++; \
1689
- } while (0 );
1690
- ENVIRONMENT_STRONG_PERSISTENT_VALUES (V)
1691
- #undef V
1692
-
1693
1642
// Do this after other creator->AddData() calls so that Snapshotable objects
1694
1643
// can use 0 to indicate that a SnapshotIndex is invalid.
1695
1644
SerializeSnapshotableObjects (this , creator, &info);
1696
1645
1697
- info.context = creator-> AddData (ctx, context () );
1646
+ info.principal_realm = principal_realm_-> Serialize (creator );
1698
1647
return info;
1699
1648
}
1700
1649
1701
- std::ostream& operator <<(std::ostream& output,
1702
- const std::vector<PropInfo>& vec) {
1703
- output << " {\n " ;
1704
- for (const auto & info : vec) {
1705
- output << " " << info << " ,\n " ;
1706
- }
1707
- output << " }" ;
1708
- return output;
1709
- }
1710
-
1711
- std::ostream& operator <<(std::ostream& output, const PropInfo& info) {
1712
- output << " { \" " << info.name << " \" , " << std::to_string (info.id ) << " , "
1713
- << std::to_string (info.index ) << " }" ;
1714
- return output;
1715
- }
1716
-
1717
- std::ostream& operator <<(std::ostream& output,
1718
- const std::vector<std::string>& vec) {
1719
- output << " {\n " ;
1720
- for (const auto & info : vec) {
1721
- output << " \" " << info << " \" ,\n " ;
1722
- }
1723
- output << " }" ;
1724
- return output;
1725
- }
1726
-
1727
- std::ostream& operator <<(std::ostream& output, const EnvSerializeInfo& i) {
1728
- output << " {\n "
1729
- << " // -- native_objects begins --\n "
1730
- << i.native_objects << " ,\n "
1731
- << " // -- native_objects ends --\n "
1732
- << " // -- builtins begins --\n "
1733
- << i.builtins << " ,\n "
1734
- << " // -- builtins ends --\n "
1735
- << " // -- async_hooks begins --\n "
1736
- << i.async_hooks << " ,\n "
1737
- << " // -- async_hooks ends --\n "
1738
- << i.tick_info << " , // tick_info\n "
1739
- << i.immediate_info << " , // immediate_info\n "
1740
- << " // -- performance_state begins --\n "
1741
- << i.performance_state << " ,\n "
1742
- << " // -- performance_state ends --\n "
1743
- << i.exiting << " , // exiting\n "
1744
- << i.stream_base_state << " , // stream_base_state\n "
1745
- << i.should_abort_on_uncaught_toggle
1746
- << " , // should_abort_on_uncaught_toggle\n "
1747
- << " // -- persistent_values begins --\n "
1748
- << i.persistent_values << " ,\n "
1749
- << " // -- persistent_values ends --\n "
1750
- << i.context << " , // context\n "
1751
- << " }" ;
1752
- return output;
1753
- }
1754
-
1755
1650
void Environment::EnqueueDeserializeRequest (DeserializeRequestCallback cb,
1756
1651
Local<Object> holder,
1757
1652
int index,
@@ -1789,44 +1684,12 @@ void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
1789
1684
stream_base_state_.Deserialize (ctx);
1790
1685
should_abort_on_uncaught_toggle_.Deserialize (ctx);
1791
1686
1687
+ principal_realm_->DeserializeProperties (&info->principal_realm );
1688
+
1792
1689
if (enabled_debug_list_.enabled (DebugCategory::MKSNAPSHOT)) {
1793
1690
fprintf (stderr, " deserializing...\n " );
1794
1691
std::cerr << *info << " \n " ;
1795
1692
}
1796
-
1797
- const std::vector<PropInfo>& values = info->persistent_values ;
1798
- size_t i = 0 ; // index to the array
1799
- uint32_t id = 0 ;
1800
- #define V (PropertyName, TypeName ) \
1801
- do { \
1802
- if (values.size () > i && id == values[i].id ) { \
1803
- const PropInfo& d = values[i]; \
1804
- DCHECK_EQ (d.name , #PropertyName); \
1805
- MaybeLocal<TypeName> maybe_field = \
1806
- ctx->GetDataFromSnapshotOnce <TypeName>(d.index ); \
1807
- Local<TypeName> field; \
1808
- if (!maybe_field.ToLocal (&field)) { \
1809
- fprintf (stderr, \
1810
- " Failed to deserialize environment value " #PropertyName \
1811
- " \n " ); \
1812
- } \
1813
- set_##PropertyName (field); \
1814
- i++; \
1815
- } \
1816
- id++; \
1817
- } while (0 );
1818
-
1819
- ENVIRONMENT_STRONG_PERSISTENT_VALUES (V);
1820
- #undef V
1821
-
1822
- MaybeLocal<Context> maybe_ctx_from_snapshot =
1823
- ctx->GetDataFromSnapshotOnce <Context>(info->context );
1824
- Local<Context> ctx_from_snapshot;
1825
- if (!maybe_ctx_from_snapshot.ToLocal (&ctx_from_snapshot)) {
1826
- fprintf (stderr,
1827
- " Failed to deserialize context back reference from the snapshot\n " );
1828
- }
1829
- CHECK_EQ (ctx_from_snapshot, ctx);
1830
1693
}
1831
1694
1832
1695
uint64_t GuessMemoryAvailableToTheProcess () {
@@ -2011,11 +1874,7 @@ void Environment::MemoryInfo(MemoryTracker* tracker) const {
2011
1874
tracker->TrackField (" async_hooks" , async_hooks_);
2012
1875
tracker->TrackField (" immediate_info" , immediate_info_);
2013
1876
tracker->TrackField (" tick_info" , tick_info_);
2014
-
2015
- #define V (PropertyName, TypeName ) \
2016
- tracker->TrackField (#PropertyName, PropertyName ());
2017
- ENVIRONMENT_STRONG_PERSISTENT_VALUES (V)
2018
- #undef V
1877
+ tracker->TrackField (" principal_realm" , principal_realm_);
2019
1878
2020
1879
// FIXME(joyeecheung): track other fields in Environment.
2021
1880
// Currently MemoryTracker is unable to track these
@@ -2164,12 +2023,14 @@ bool BaseObject::IsRootNode() const {
2164
2023
return !persistent_handle_.IsWeak ();
2165
2024
}
2166
2025
2167
- Local<FunctionTemplate> BaseObject::GetConstructorTemplate (Environment* env) {
2168
- Local<FunctionTemplate> tmpl = env->base_object_ctor_template ();
2026
+ Local<FunctionTemplate> BaseObject::GetConstructorTemplate (
2027
+ IsolateData* isolate_data) {
2028
+ Local<FunctionTemplate> tmpl = isolate_data->base_object_ctor_template ();
2169
2029
if (tmpl.IsEmpty ()) {
2170
- tmpl = NewFunctionTemplate (env->isolate (), nullptr );
2171
- tmpl->SetClassName (FIXED_ONE_BYTE_STRING (env->isolate (), " BaseObject" ));
2172
- env->set_base_object_ctor_template (tmpl);
2030
+ tmpl = NewFunctionTemplate (isolate_data->isolate (), nullptr );
2031
+ tmpl->SetClassName (
2032
+ FIXED_ONE_BYTE_STRING (isolate_data->isolate (), " BaseObject" ));
2033
+ isolate_data->set_base_object_ctor_template (tmpl);
2173
2034
}
2174
2035
return tmpl;
2175
2036
}
0 commit comments