Skip to content

Commit

Permalink
src: introduce node::Realm
Browse files Browse the repository at this point in the history
To distinguish per-context values from the node::Environment, split
those values to a new node::Realm structure and consolidate
bootstrapping methods with it.
  • Loading branch information
legendecas committed Aug 9, 2022
1 parent 472edc7 commit 4516d05
Show file tree
Hide file tree
Showing 18 changed files with 1,132 additions and 812 deletions.
4 changes: 4 additions & 0 deletions node.gyp
Expand Up @@ -512,6 +512,7 @@
'src/node_process_events.cc',
'src/node_process_methods.cc',
'src/node_process_object.cc',
'src/node_realm.cc',
'src/node_report.cc',
'src/node_report_module.cc',
'src/node_report_utils.cc',
Expand Down Expand Up @@ -570,6 +571,7 @@
'src/connection_wrap.h',
'src/debug_utils.h',
'src/debug_utils-inl.h',
'src/env_properties.h',
'src/env.h',
'src/env-inl.h',
'src/handle_wrap.h',
Expand Down Expand Up @@ -617,6 +619,8 @@
'src/node_platform.h',
'src/node_process.h',
'src/node_process-inl.h',
'src/node_realm.h',
'src/node_realm-inl.h',
'src/node_report.h',
'src/node_revert.h',
'src/node_root_certs.h',
Expand Down
11 changes: 7 additions & 4 deletions src/api/environment.cc
Expand Up @@ -5,6 +5,7 @@
#include "node_internals.h"
#include "node_options-inl.h"
#include "node_platform.h"
#include "node_realm-inl.h"
#include "node_shadow_realm.h"
#include "node_v8_platform-inl.h"
#include "node_wasm_web_api.h"
Expand Down Expand Up @@ -377,7 +378,7 @@ Environment* CreateEnvironment(
}
#endif

if (env->RunBootstrapping().IsEmpty()) {
if (env->principal_realm()->RunBootstrapping().IsEmpty()) {
FreeEnvironment(env);
return nullptr;
}
Expand Down Expand Up @@ -452,11 +453,13 @@ MaybeLocal<Value> LoadEnvironment(
builtins::BuiltinLoader::Add(
name.c_str(), UnionBytes(**main_utf16, main_utf16->length()));
env->set_main_utf16(std::move(main_utf16));
Realm* realm = env->principal_realm();

// Arguments must match the parameters specified in
// BuiltinLoader::LookupAndCompile().
std::vector<Local<Value>> args = {env->process_object(),
env->builtin_module_require()};
return ExecuteBootstrapper(env, name.c_str(), &args);
std::vector<Local<Value>> args = {realm->process_object(),
realm->builtin_module_require()};
return realm->ExecuteBootstrapper(name.c_str(), &args);
});
}

Expand Down
32 changes: 16 additions & 16 deletions src/env-inl.h
Expand Up @@ -31,6 +31,7 @@
#include "node_context_data.h"
#include "node_internals.h"
#include "node_perf_common.h"
#include "node_realm-inl.h"
#include "util-inl.h"
#include "uv.h"
#include "v8.h"
Expand Down Expand Up @@ -177,16 +178,7 @@ inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
}

inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
if (UNLIKELY(context.IsEmpty())) {
return nullptr;
}
if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
ContextEmbedderIndex::kContextTag)) {
return nullptr;
}
if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
ContextEmbedderIndex::kContextTag) !=
Environment::kNodeContextTagPtr)) {
if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
return nullptr;
}
return static_cast<Environment*>(
Expand Down Expand Up @@ -623,11 +615,13 @@ inline void Environment::set_can_call_into_js(bool can_call_into_js) {
}

inline bool Environment::has_run_bootstrapping_code() const {
return has_run_bootstrapping_code_;
return principal_realm_->has_run_bootstrapping_code();
}

inline void Environment::DoneBootstrapping() {
has_run_bootstrapping_code_ = true;
CHECK(has_run_bootstrapping_code());
// TODO(legendecas): distinguish base objects with realms.

// This adjusts the return value of base_object_created_after_bootstrap() so
// that tests that check the count do not have to account for internally
// created BaseObjects.
Expand Down Expand Up @@ -922,16 +916,22 @@ void Environment::set_process_exit_handler(

#define V(PropertyName, TypeName) \
inline v8::Local<TypeName> Environment::PropertyName() const { \
return PersistentToLocal::Strong(PropertyName##_); \
DCHECK_NOT_NULL(principal_realm_); \
return principal_realm_->PropertyName(); \
} \
inline void Environment::set_##PropertyName(v8::Local<TypeName> value) { \
PropertyName##_.Reset(isolate(), value); \
DCHECK_NOT_NULL(principal_realm_); \
principal_realm_->set_##PropertyName(value); \
}
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V)
PER_REALM_STRONG_PERSISTENT_VALUES(V)
#undef V

v8::Local<v8::Context> Environment::context() const {
return PersistentToLocal::Strong(context_);
return principal_realm()->context();
}

Realm* Environment::principal_realm() const {
return principal_realm_.get();
}

} // namespace node
Expand Down

0 comments on commit 4516d05

Please sign in to comment.