Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: introduce node::Realm #44179

Merged
merged 1 commit into from Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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
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
Expand Up @@ -31,6 +31,7 @@
namespace node {

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

Expand Down Expand Up @@ -111,8 +112,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
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 @@ -830,14 +823,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 @@ -902,16 +899,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