Skip to content

Commit 8004257

Browse files
committed
src: add initial shadow realm support
Add initial shadow realm support behind an off-by-default flag `--experimental-shadow-realm`.
1 parent 2691222 commit 8004257

File tree

7 files changed

+65
-0
lines changed

7 files changed

+65
-0
lines changed

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@
531531
'src/node_report_module.cc',
532532
'src/node_report_utils.cc',
533533
'src/node_serdes.cc',
534+
'src/node_shadow_realm.cc',
534535
'src/node_snapshotable.cc',
535536
'src/node_sockaddr.cc',
536537
'src/node_stat_watcher.cc',

src/api/environment.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "node_native_module_env.h"
66
#include "node_options-inl.h"
77
#include "node_platform.h"
8+
#include "node_shadow_realm.h"
89
#include "node_v8_platform-inl.h"
910
#include "node_wasm_web_api.h"
1011
#include "uv.h"
@@ -261,6 +262,12 @@ void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
261262
isolate->SetWasmStreamingCallback(wasm_web_api::StartStreamingCompilation);
262263
}
263264

265+
if (per_process::cli_options->get_per_isolate_options()
266+
->experimental_shadow_realm) {
267+
isolate->SetHostCreateShadowRealmContextCallback(
268+
shadow_realm::HostCreateShadowRealmContextCallback);
269+
}
270+
264271
if ((s.flags & SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK) == 0) {
265272
auto* promise_reject_cb = s.promise_reject_callback ?
266273
s.promise_reject_callback : PromiseRejectCallback;

src/node_options.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,15 @@ PerIsolateOptionsParser::PerIsolateOptionsParser(
713713
Implies("--harmony-top-level-await", "--experimental-top-level-await");
714714
ImpliesNot("--no-harmony-top-level-await", "--experimental-top-level-await");
715715

716+
AddOption("--experimental-shadow-realm",
717+
"",
718+
&PerIsolateOptions::experimental_shadow_realm,
719+
kAllowedInEnvironment);
720+
AddOption("--harmony-shadow-realm", "", V8Option{});
721+
Implies("--experimental-shadow-realm", "--harmony-shadow-realm");
722+
Implies("--harmony-shadow-realm", "--experimental-shadow-realm");
723+
ImpliesNot("--no-harmony-shadow-realm", "--experimental-shadow-realm");
724+
716725
Insert(eop, &PerIsolateOptions::get_per_env_options);
717726
}
718727

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ class PerIsolateOptions : public Options {
207207
bool report_uncaught_exception = false;
208208
bool report_on_signal = false;
209209
bool experimental_top_level_await = true;
210+
bool experimental_shadow_realm = false;
210211
std::string report_signal = "SIGUSR2";
211212
inline EnvironmentOptions* get_per_env_options();
212213
void CheckOptions(std::vector<std::string>* errors) override;

src/node_shadow_realm.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "node_shadow_realm.h"
2+
3+
namespace node {
4+
namespace shadow_realm {
5+
using v8::Context;
6+
using v8::Local;
7+
using v8::MaybeLocal;
8+
9+
// static
10+
MaybeLocal<Context> HostCreateShadowRealmContextCallback(
11+
Local<Context> initiator_context) {
12+
return Context::New(initiator_context->GetIsolate());
13+
}
14+
15+
} // namespace shadow_realm
16+
} // namespace node

src/node_shadow_realm.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef SRC_NODE_SHADOW_REALM_H_
2+
#define SRC_NODE_SHADOW_REALM_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include "v8.h"
7+
8+
namespace node {
9+
namespace shadow_realm {
10+
11+
v8::MaybeLocal<v8::Context> HostCreateShadowRealmContextCallback(
12+
v8::Local<v8::Context> initiator_context);
13+
14+
} // namespace shadow_realm
15+
} // namespace node
16+
17+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
18+
19+
#endif // SRC_NODE_SHADOW_REALM_H_

test/parallel/test-shadow-realm.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Flags: --harmony-shadow-realm
2+
'use strict';
3+
4+
require('../common');
5+
const assert = require('assert');
6+
7+
// Validates we can construct ShadowRealm successfully.
8+
const shadowRealm = new ShadowRealm();
9+
10+
const getter = shadowRealm.evaluate('globalThis.realmValue = "inner"; () => globalThis.realmValue;');
11+
assert.strictEqual(getter(), 'inner');
12+
assert.strictEqual('realmValue' in globalThis, false);

0 commit comments

Comments
 (0)