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.

PR-URL: #44179
Refs: #42528
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
legendecas authored and danielleadams committed Jan 3, 2023
1 parent b11616b commit a7f3bc0
Show file tree
Hide file tree
Showing 21 changed files with 1,142 additions and 830 deletions.
4 changes: 4 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,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 @@ -585,6 +586,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 @@ -632,6 +634,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
Original file line number Diff line number Diff line change
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 @@ -378,7 +379,7 @@ Environment* CreateEnvironment(
}
#endif

if (env->RunBootstrapping().IsEmpty()) {
if (env->principal_realm()->RunBootstrapping().IsEmpty()) {
FreeEnvironment(env);
return nullptr;
}
Expand Down Expand Up @@ -453,11 +454,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
6 changes: 6 additions & 0 deletions src/base_object-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@

namespace node {

// static
v8::Local<v8::FunctionTemplate> BaseObject::GetConstructorTemplate(
Environment* env) {
return BaseObject::GetConstructorTemplate(env->isolate_data());
}

void BaseObject::Detach() {
CHECK_GT(pointer_data()->strong_ptr_count, 0);
pointer_data()->is_detached = true;
Expand Down
5 changes: 4 additions & 1 deletion src/base_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
namespace node {

class Environment;
class IsolateData;
template <typename T, bool kIsWeak>
class BaseObjectPtrImpl;

Expand Down Expand Up @@ -113,8 +114,10 @@ class BaseObject : public MemoryRetainer {
// a BaseObjectPtr to this object.
inline void Detach();

static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
static inline v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
Environment* env);
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
IsolateData* isolate_data);

// Interface for transferring BaseObject instances using the .postMessage()
// method of MessagePorts (and, by extension, Workers).
Expand Down
37 changes: 20 additions & 17 deletions src/env-inl.h
Original file line number Diff line number Diff line change
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 @@ -614,15 +615,7 @@ 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_;
}

inline void Environment::DoneBootstrapping() {
has_run_bootstrapping_code_ = true;
// 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.
base_object_created_by_bootstrap_ = base_object_count_;
return principal_realm_->has_run_bootstrapping_code();
}

inline bool Environment::has_serialized_options() const {
Expand Down Expand Up @@ -805,14 +798,18 @@ void Environment::modify_base_object_count(int64_t delta) {
base_object_count_ += delta;
}

int64_t Environment::base_object_created_after_bootstrap() const {
return base_object_count_ - base_object_created_by_bootstrap_;
}

int64_t Environment::base_object_count() const {
return base_object_count_;
}

inline void Environment::set_base_object_created_by_bootstrap(int64_t count) {
base_object_created_by_bootstrap_ = base_object_count_;
}

int64_t Environment::base_object_created_after_bootstrap() const {
return base_object_count_ - base_object_created_by_bootstrap_;
}

void Environment::set_main_utf16(std::unique_ptr<v8::String::Value> str) {
CHECK(!main_utf16_);
main_utf16_ = std::move(str);
Expand Down Expand Up @@ -877,16 +874,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();
}

inline void Environment::set_heap_snapshot_near_heap_limit(uint32_t limit) {
Expand Down
Loading

0 comments on commit a7f3bc0

Please sign in to comment.