Skip to content

Commit 730654c

Browse files
committed
fixup! src: introduce node::Realm
1 parent 3179ee1 commit 730654c

File tree

7 files changed

+54
-28
lines changed

7 files changed

+54
-28
lines changed

src/base_object-inl.h

Lines changed: 6 additions & 0 deletions
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

Lines changed: 4 additions & 1 deletion
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

@@ -109,8 +110,10 @@ class BaseObject : public MemoryRetainer {
109110
// a BaseObjectPtr to this object.
110111
inline void Detach();
111112

112-
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
113+
static inline v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
113114
Environment* env);
115+
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
116+
IsolateData* isolate_data);
114117

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

src/env-inl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,12 @@ inline bool Environment::has_run_bootstrapping_code() const {
620620

621621
inline void Environment::DoneBootstrapping() {
622622
CHECK(has_run_bootstrapping_code());
623-
// TODO(legendecas): distinguish base objects with realms.
624623

625624
// This adjusts the return value of base_object_created_after_bootstrap() so
626625
// that tests that check the count do not have to account for internally
627626
// created BaseObjects.
627+
628+
// TODO(legendecas): track base objects by realms instead of environments.
628629
base_object_created_by_bootstrap_ = base_object_count_;
629630
}
630631

src/env.cc

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,13 @@ void IsolateData::CreateProperties() {
428428
NODE_ASYNC_PROVIDER_TYPES(V)
429429
#undef V
430430

431-
// TODO(legendecas): eagerly create per isolate templates.
431+
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
432+
templ->InstanceTemplate()->SetInternalFieldCount(
433+
BaseObject::kInternalFieldCount);
434+
templ->Inherit(BaseObject::GetConstructorTemplate(this));
435+
set_binding_data_ctor_template(templ);
436+
437+
// TODO(legendecas): eagerly create more per-isolate templates.
432438
}
433439

434440
IsolateData::IsolateData(Isolate* isolate,
@@ -578,17 +584,6 @@ std::unique_ptr<v8::BackingStore> Environment::release_managed_buffer(
578584
return bs;
579585
}
580586

581-
void Environment::CreateProperties() {
582-
HandleScope handle_scope(isolate_);
583-
584-
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
585-
templ->InstanceTemplate()->SetInternalFieldCount(
586-
BaseObject::kInternalFieldCount);
587-
templ->Inherit(BaseObject::GetConstructorTemplate(this));
588-
589-
set_binding_data_ctor_template(templ);
590-
}
591-
592587
std::string GetExecPath(const std::vector<std::string>& argv) {
593588
char exec_path_buf[2 * PATH_MAX];
594589
size_t exec_path_len = sizeof(exec_path_buf);
@@ -725,15 +720,10 @@ Environment::Environment(IsolateData* isolate_data,
725720
void Environment::InitializeMainContext(Local<Context> context,
726721
const EnvSerializeInfo* env_info) {
727722
principal_realm_ = std::make_unique<Realm>(
728-
isolate_data_,
729-
this,
730-
context,
731-
env_info == nullptr ? nullptr : &env_info->principal_realm);
723+
isolate_data_, this, context, MAYBE_FIELD_PTR(env_info, principal_realm));
732724
AssignToContext(context, ContextInfo(""));
733725
if (env_info != nullptr) {
734726
DeserializeProperties(env_info);
735-
} else {
736-
CreateProperties();
737727
}
738728

739729
if (!options_->force_async_hooks_checks) {
@@ -2015,12 +2005,14 @@ bool BaseObject::IsRootNode() const {
20152005
return !persistent_handle_.IsWeak();
20162006
}
20172007

2018-
Local<FunctionTemplate> BaseObject::GetConstructorTemplate(Environment* env) {
2019-
Local<FunctionTemplate> tmpl = env->base_object_ctor_template();
2008+
Local<FunctionTemplate> BaseObject::GetConstructorTemplate(
2009+
IsolateData* isolate_data) {
2010+
Local<FunctionTemplate> tmpl = isolate_data->base_object_ctor_template();
20202011
if (tmpl.IsEmpty()) {
2021-
tmpl = NewFunctionTemplate(env->isolate(), nullptr);
2022-
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "BaseObject"));
2023-
env->set_base_object_ctor_template(tmpl);
2012+
tmpl = NewFunctionTemplate(isolate_data->isolate(), nullptr);
2013+
tmpl->SetClassName(
2014+
FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "BaseObject"));
2015+
isolate_data->set_base_object_ctor_template(tmpl);
20242016
}
20252017
return tmpl;
20262018
}

src/env.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,11 @@ struct SnapshotData {
590590
SnapshotData() = default;
591591
};
592592

593+
/**
594+
* Environment is a per-isolate data structure that represents an execution
595+
* environment. Each environment has a principal realm. An environment can
596+
* create multiple subsidiary synthetic realms.
597+
*/
593598
class Environment : public MemoryRetainer {
594599
public:
595600
Environment(const Environment&) = delete;
@@ -604,7 +609,6 @@ class Environment : public MemoryRetainer {
604609
void MemoryInfo(MemoryTracker* tracker) const override;
605610

606611
EnvSerializeInfo Serialize(v8::SnapshotCreator* creator);
607-
void CreateProperties();
608612
void DeserializeProperties(const EnvSerializeInfo* info);
609613

610614
void PrintInfoForSnapshotIfDebug();

src/node_realm.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ void Realm::MemoryInfo(MemoryTracker* tracker) const {
4141
tracker->TrackField(#PropertyName, PropertyName());
4242
PER_REALM_STRONG_PERSISTENT_VALUES(V)
4343
#undef V
44+
45+
tracker->TrackField("isolate_data", isolate_data_);
46+
tracker->TrackField("env", env_);
4447
}
4548

4649
void Realm::CreateProperties() {
@@ -280,12 +283,13 @@ MaybeLocal<Value> Realm::RunBootstrapping() {
280283
return MaybeLocal<Value>();
281284
}
282285

283-
// TODO(legendecas): distinguish req_wrap and handle_wrap with realms.
284-
285286
// Make sure that no request or handle is created during bootstrap -
286287
// if necessary those should be done in pre-execution.
287288
// Usually, doing so would trigger the checks present in the ReqWrap and
288289
// HandleWrap classes, so this is only a consistency check.
290+
291+
// TODO(legendecas): track req_wrap and handle_wrap by realms instead of
292+
// environments.
289293
CHECK(env_->req_wrap_queue()->IsEmpty());
290294
CHECK(env_->handle_wrap_queue()->IsEmpty());
291295

src/node_realm.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010

1111
namespace node {
1212

13+
/**
14+
* node::Realm is a container for a set of JavaScript objects and functions
15+
* that associated with a particular global environment.
16+
*
17+
* An ECMAScript realm (https://tc39.es/ecma262/#sec-code-realms) representing
18+
* a global environment in which script is run. Each ECMAScript realm comes
19+
* with a global object and a set of intrinsic objects. An ECMAScript realm has
20+
* a [[HostDefined]] field, which contains the node::Realm object.
21+
*
22+
* Realm can be a principal realm or a synthetic realm. A principal realm is
23+
* created with an Environment as its principal global environment to evaluate
24+
* scripts. A synthetic realm is created with JS APIs like ShadowRealm.
25+
*
26+
* Native bindings and builtin modules can be evaluated in either a principal
27+
* realm or a synthetic realm.
28+
*/
1329
class Realm : public MemoryRetainer {
1430
public:
1531
static inline Realm* GetCurrent(v8::Isolate* isolate);

0 commit comments

Comments
 (0)