This repository has been archived by the owner on Dec 14, 2024. It is now read-only.
forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode_shadow_realm.cc
178 lines (153 loc) · 6.4 KB
/
node_shadow_realm.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "node_shadow_realm.h"
#include "env-inl.h"
#include "node_errors.h"
#include "node_process.h"
namespace node {
namespace shadow_realm {
using v8::Context;
using v8::EscapableHandleScope;
using v8::HandleScope;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::String;
using v8::Value;
using TryCatchScope = node::errors::TryCatchScope;
// static
ShadowRealm* ShadowRealm::New(Environment* env) {
ShadowRealm* realm = new ShadowRealm(env);
// TODO(legendecas): required by node::PromiseRejectCallback.
// Remove this once promise rejection doesn't need to be handled across
// realms.
realm->context()->SetSecurityToken(
env->principal_realm()->context()->GetSecurityToken());
// We do not expect the realm bootstrapping to throw any
// exceptions. If it does, exit the current Node.js instance.
TryCatchScope try_catch(env, TryCatchScope::CatchMode::kFatal);
if (realm->RunBootstrapping().IsEmpty()) {
delete realm;
return nullptr;
}
return realm;
}
// static
MaybeLocal<Context> HostCreateShadowRealmContextCallback(
Local<Context> initiator_context) {
Environment* env = Environment::GetCurrent(initiator_context);
EscapableHandleScope scope(env->isolate());
// We do not expect the realm bootstrapping to throw any
// exceptions. If it does, exit the current Node.js instance.
TryCatchScope try_catch(env, TryCatchScope::CatchMode::kFatal);
ShadowRealm* realm = ShadowRealm::New(env);
if (realm != nullptr) {
return scope.Escape(realm->context());
}
return MaybeLocal<Context>();
}
// static
void ShadowRealm::WeakCallback(const v8::WeakCallbackInfo<ShadowRealm>& data) {
ShadowRealm* realm = data.GetParameter();
realm->context_.Reset();
// Yield to pending weak callbacks before deleting the realm.
// This is necessary to avoid cleaning up base objects before their scheduled
// weak callbacks are invoked, which can lead to accessing to v8 apis during
// the first pass of the weak callback.
realm->env()->SetImmediate([realm](Environment* env) { delete realm; });
// Remove the cleanup hook to avoid deleting the realm again.
realm->env()->RemoveCleanupHook(DeleteMe, realm);
}
// static
void ShadowRealm::DeleteMe(void* data) {
ShadowRealm* realm = static_cast<ShadowRealm*>(data);
// Clear the context handle to avoid invoking the weak callback again.
// Also, the context internal slots are cleared and the context is no longer
// reference to the realm.
delete realm;
}
ShadowRealm::ShadowRealm(Environment* env)
: Realm(env, NewContext(env->isolate()), kShadowRealm) {
context_.SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter);
CreateProperties();
env->TrackShadowRealm(this);
env->AddCleanupHook(DeleteMe, this);
}
ShadowRealm::~ShadowRealm() {
while (HasCleanupHooks()) {
RunCleanup();
}
env_->UntrackShadowRealm(this);
if (context_.IsEmpty()) {
// This most likely happened because the weak callback cleared it.
return;
}
{
HandleScope handle_scope(isolate());
env_->UnassignFromContext(context());
}
}
v8::Local<v8::Context> ShadowRealm::context() const {
Local<Context> ctx = PersistentToLocal::Default(isolate_, context_);
DCHECK(!ctx.IsEmpty());
return ctx;
}
// V8 can not infer reference cycles between global persistent handles, e.g.
// the Realm's Context handle and the per-realm function handles.
// Attach the per-realm strong persistent values' lifetime to the context's
// global object to avoid the strong global references to the per-realm objects
// keep the context alive indefinitely.
#define V(PropertyName, TypeName) \
v8::Local<TypeName> ShadowRealm::PropertyName() const { \
return PersistentToLocal::Default(isolate_, PropertyName##_); \
} \
void ShadowRealm::set_##PropertyName(v8::Local<TypeName> value) { \
HandleScope scope(isolate_); \
PropertyName##_.Reset(isolate_, value); \
v8::Local<v8::Context> ctx = context(); \
if (value.IsEmpty()) { \
ctx->Global() \
->SetPrivate(ctx, \
isolate_data()->per_realm_##PropertyName(), \
v8::Undefined(isolate_)) \
.ToChecked(); \
} else { \
PropertyName##_.SetWeak(); \
ctx->Global() \
->SetPrivate(ctx, isolate_data()->per_realm_##PropertyName(), value) \
.ToChecked(); \
} \
}
PER_REALM_STRONG_PERSISTENT_VALUES(V)
#undef V
v8::MaybeLocal<v8::Value> ShadowRealm::BootstrapRealm() {
HandleScope scope(isolate_);
// Skip "internal/bootstrap/node" as it installs node globals and per-isolate
// callbacks.
if (!env_->no_browser_globals()) {
if (ExecuteBootstrapper("internal/bootstrap/web/exposed-wildcard")
.IsEmpty()) {
return MaybeLocal<Value>();
}
}
// The process object is not exposed globally in ShadowRealm yet.
// However, the process properties need to be setup for built-in modules.
// Specifically, process.cwd() is needed by the ESM loader.
if (ExecuteBootstrapper(
"internal/bootstrap/switches/does_not_own_process_state")
.IsEmpty()) {
return MaybeLocal<Value>();
}
// Setup process.env proxy.
Local<String> env_string = FIXED_ONE_BYTE_STRING(isolate_, "env");
Local<Object> env_proxy;
if (!isolate_data()->env_proxy_template()->NewInstance(context()).ToLocal(
&env_proxy) ||
process_object()->Set(context(), env_string, env_proxy).IsNothing()) {
return MaybeLocal<Value>();
}
if (ExecuteBootstrapper("internal/bootstrap/shadow_realm").IsEmpty()) {
return MaybeLocal<Value>();
}
return v8::True(isolate_);
}
} // namespace shadow_realm
} // namespace node