Skip to content

Commit 81d9cdb

Browse files
authored
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. PR-URL: nodejs#44179 Refs: nodejs#42528 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ad3c7bc commit 81d9cdb

21 files changed

+1142
-830
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"
@@ -378,7 +379,7 @@ Environment* CreateEnvironment(
378379
}
379380
#endif
380381

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

src/base_object-inl.h

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232

3333
namespace node {
3434

35+
// static
36+
v8::Local<v8::FunctionTemplate> BaseObject::GetConstructorTemplate(
37+
Environment* env) {
38+
return BaseObject::GetConstructorTemplate(env->isolate_data());
39+
}
40+
3541
void BaseObject::Detach() {
3642
CHECK_GT(pointer_data()->strong_ptr_count, 0);
3743
pointer_data()->is_detached = true;

src/base_object.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
namespace node {
3232

3333
class Environment;
34+
class IsolateData;
3435
template <typename T, bool kIsWeak>
3536
class BaseObjectPtrImpl;
3637

@@ -111,8 +112,10 @@ class BaseObject : public MemoryRetainer {
111112
// a BaseObjectPtr to this object.
112113
inline void Detach();
113114

114-
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
115+
static inline v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
115116
Environment* env);
117+
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
118+
IsolateData* isolate_data);
116119

117120
// Interface for transferring BaseObject instances using the .postMessage()
118121
// method of MessagePorts (and, by extension, Workers).

src/env-inl.h

+20-17
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"
@@ -614,15 +615,7 @@ inline void Environment::set_can_call_into_js(bool can_call_into_js) {
614615
}
615616

616617
inline bool Environment::has_run_bootstrapping_code() const {
617-
return has_run_bootstrapping_code_;
618-
}
619-
620-
inline void Environment::DoneBootstrapping() {
621-
has_run_bootstrapping_code_ = true;
622-
// This adjusts the return value of base_object_created_after_bootstrap() so
623-
// that tests that check the count do not have to account for internally
624-
// created BaseObjects.
625-
base_object_created_by_bootstrap_ = base_object_count_;
618+
return principal_realm_->has_run_bootstrapping_code();
626619
}
627620

628621
inline bool Environment::has_serialized_options() const {
@@ -830,14 +823,18 @@ void Environment::modify_base_object_count(int64_t delta) {
830823
base_object_count_ += delta;
831824
}
832825

833-
int64_t Environment::base_object_created_after_bootstrap() const {
834-
return base_object_count_ - base_object_created_by_bootstrap_;
835-
}
836-
837826
int64_t Environment::base_object_count() const {
838827
return base_object_count_;
839828
}
840829

830+
inline void Environment::set_base_object_created_by_bootstrap(int64_t count) {
831+
base_object_created_by_bootstrap_ = base_object_count_;
832+
}
833+
834+
int64_t Environment::base_object_created_after_bootstrap() const {
835+
return base_object_count_ - base_object_created_by_bootstrap_;
836+
}
837+
841838
void Environment::set_main_utf16(std::unique_ptr<v8::String::Value> str) {
842839
CHECK(!main_utf16_);
843840
main_utf16_ = std::move(str);
@@ -902,16 +899,22 @@ void Environment::set_process_exit_handler(
902899

903900
#define V(PropertyName, TypeName) \
904901
inline v8::Local<TypeName> Environment::PropertyName() const { \
905-
return PersistentToLocal::Strong(PropertyName##_); \
902+
DCHECK_NOT_NULL(principal_realm_); \
903+
return principal_realm_->PropertyName(); \
906904
} \
907905
inline void Environment::set_##PropertyName(v8::Local<TypeName> value) { \
908-
PropertyName##_.Reset(isolate(), value); \
906+
DCHECK_NOT_NULL(principal_realm_); \
907+
principal_realm_->set_##PropertyName(value); \
909908
}
910-
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
909+
PER_REALM_STRONG_PERSISTENT_VALUES(V)
911910
#undef V
912911

913912
v8::Local<v8::Context> Environment::context() const {
914-
return PersistentToLocal::Strong(context_);
913+
return principal_realm()->context();
914+
}
915+
916+
Realm* Environment::principal_realm() const {
917+
return principal_realm_.get();
915918
}
916919

917920
} // namespace node

0 commit comments

Comments
 (0)