Skip to content

Commit afb9113

Browse files
committed
src: introduce node::Realm
To distinguish per-context values from the node::Environment, split those values to a new node::Realm structure and consolidate bootstrapping methods with it.
1 parent 472edc7 commit afb9113

17 files changed

+1124
-811
lines changed

node.gyp

+4
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@
512512
'src/node_process_events.cc',
513513
'src/node_process_methods.cc',
514514
'src/node_process_object.cc',
515+
'src/node_realm.cc',
515516
'src/node_report.cc',
516517
'src/node_report_module.cc',
517518
'src/node_report_utils.cc',
@@ -570,6 +571,7 @@
570571
'src/connection_wrap.h',
571572
'src/debug_utils.h',
572573
'src/debug_utils-inl.h',
574+
'src/env_properties.h',
573575
'src/env.h',
574576
'src/env-inl.h',
575577
'src/handle_wrap.h',
@@ -617,6 +619,8 @@
617619
'src/node_platform.h',
618620
'src/node_process.h',
619621
'src/node_process-inl.h',
622+
'src/node_realm.h',
623+
'src/node_realm-inl.h',
620624
'src/node_report.h',
621625
'src/node_revert.h',
622626
'src/node_root_certs.h',

src/api/environment.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "node_internals.h"
66
#include "node_options-inl.h"
77
#include "node_platform.h"
8+
#include "node_realm-inl.h"
89
#include "node_shadow_realm.h"
910
#include "node_v8_platform-inl.h"
1011
#include "node_wasm_web_api.h"
@@ -377,7 +378,7 @@ Environment* CreateEnvironment(
377378
}
378379
#endif
379380

380-
if (env->RunBootstrapping().IsEmpty()) {
381+
if (env->principal_realm()->RunBootstrapping().IsEmpty()) {
381382
FreeEnvironment(env);
382383
return nullptr;
383384
}
@@ -452,11 +453,13 @@ MaybeLocal<Value> LoadEnvironment(
452453
builtins::BuiltinLoader::Add(
453454
name.c_str(), UnionBytes(**main_utf16, main_utf16->length()));
454455
env->set_main_utf16(std::move(main_utf16));
456+
Realm* realm = env->principal_realm();
457+
455458
// Arguments must match the parameters specified in
456459
// BuiltinLoader::LookupAndCompile().
457-
std::vector<Local<Value>> args = {env->process_object(),
458-
env->builtin_module_require()};
459-
return ExecuteBootstrapper(env, name.c_str(), &args);
460+
std::vector<Local<Value>> args = {realm->process_object(),
461+
realm->builtin_module_require()};
462+
return realm->ExecuteBootstrapper(name.c_str(), &args);
460463
});
461464
}
462465

src/env-inl.h

+16-16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "node_context_data.h"
3232
#include "node_internals.h"
3333
#include "node_perf_common.h"
34+
#include "node_realm-inl.h"
3435
#include "util-inl.h"
3536
#include "uv.h"
3637
#include "v8.h"
@@ -177,16 +178,7 @@ inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
177178
}
178179

179180
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180-
if (UNLIKELY(context.IsEmpty())) {
181-
return nullptr;
182-
}
183-
if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
184-
ContextEmbedderIndex::kContextTag)) {
185-
return nullptr;
186-
}
187-
if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
188-
ContextEmbedderIndex::kContextTag) !=
189-
Environment::kNodeContextTagPtr)) {
181+
if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
190182
return nullptr;
191183
}
192184
return static_cast<Environment*>(
@@ -623,11 +615,13 @@ inline void Environment::set_can_call_into_js(bool can_call_into_js) {
623615
}
624616

625617
inline bool Environment::has_run_bootstrapping_code() const {
626-
return has_run_bootstrapping_code_;
618+
return principal_realm_->has_run_bootstrapping_code();
627619
}
628620

629621
inline void Environment::DoneBootstrapping() {
630-
has_run_bootstrapping_code_ = true;
622+
CHECK(has_run_bootstrapping_code());
623+
// TODO(legendecas): distinguish base objects with realms.
624+
631625
// This adjusts the return value of base_object_created_after_bootstrap() so
632626
// that tests that check the count do not have to account for internally
633627
// created BaseObjects.
@@ -922,16 +916,22 @@ void Environment::set_process_exit_handler(
922916

923917
#define V(PropertyName, TypeName) \
924918
inline v8::Local<TypeName> Environment::PropertyName() const { \
925-
return PersistentToLocal::Strong(PropertyName##_); \
919+
DCHECK_NOT_NULL(principal_realm_); \
920+
return principal_realm_->PropertyName(); \
926921
} \
927922
inline void Environment::set_##PropertyName(v8::Local<TypeName> value) { \
928-
PropertyName##_.Reset(isolate(), value); \
923+
DCHECK_NOT_NULL(principal_realm_); \
924+
principal_realm_->set_##PropertyName(value); \
929925
}
930-
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
926+
PER_REALM_STRONG_PERSISTENT_VALUES(V)
931927
#undef V
932928

933929
v8::Local<v8::Context> Environment::context() const {
934-
return PersistentToLocal::Strong(context_);
930+
return principal_realm()->context();
931+
}
932+
933+
Realm* Environment::principal_realm() const {
934+
return principal_realm_.get();
935935
}
936936

937937
} // namespace node

0 commit comments

Comments
 (0)