Skip to content

Commit

Permalink
src: add initial shadow realm support
Browse files Browse the repository at this point in the history
Add initial shadow realm support behind an off-by-default flag
`--experimental-shadow-realm`.
  • Loading branch information
legendecas committed Apr 25, 2022
1 parent 2691222 commit 8004257
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@
'src/node_report_module.cc',
'src/node_report_utils.cc',
'src/node_serdes.cc',
'src/node_shadow_realm.cc',
'src/node_snapshotable.cc',
'src/node_sockaddr.cc',
'src/node_stat_watcher.cc',
Expand Down
7 changes: 7 additions & 0 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "node_native_module_env.h"
#include "node_options-inl.h"
#include "node_platform.h"
#include "node_shadow_realm.h"
#include "node_v8_platform-inl.h"
#include "node_wasm_web_api.h"
#include "uv.h"
Expand Down Expand Up @@ -261,6 +262,12 @@ void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
isolate->SetWasmStreamingCallback(wasm_web_api::StartStreamingCompilation);
}

if (per_process::cli_options->get_per_isolate_options()
->experimental_shadow_realm) {
isolate->SetHostCreateShadowRealmContextCallback(
shadow_realm::HostCreateShadowRealmContextCallback);
}

if ((s.flags & SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK) == 0) {
auto* promise_reject_cb = s.promise_reject_callback ?
s.promise_reject_callback : PromiseRejectCallback;
Expand Down
9 changes: 9 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,15 @@ PerIsolateOptionsParser::PerIsolateOptionsParser(
Implies("--harmony-top-level-await", "--experimental-top-level-await");
ImpliesNot("--no-harmony-top-level-await", "--experimental-top-level-await");

AddOption("--experimental-shadow-realm",
"",
&PerIsolateOptions::experimental_shadow_realm,
kAllowedInEnvironment);
AddOption("--harmony-shadow-realm", "", V8Option{});
Implies("--experimental-shadow-realm", "--harmony-shadow-realm");
Implies("--harmony-shadow-realm", "--experimental-shadow-realm");
ImpliesNot("--no-harmony-shadow-realm", "--experimental-shadow-realm");

Insert(eop, &PerIsolateOptions::get_per_env_options);
}

Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class PerIsolateOptions : public Options {
bool report_uncaught_exception = false;
bool report_on_signal = false;
bool experimental_top_level_await = true;
bool experimental_shadow_realm = false;
std::string report_signal = "SIGUSR2";
inline EnvironmentOptions* get_per_env_options();
void CheckOptions(std::vector<std::string>* errors) override;
Expand Down
16 changes: 16 additions & 0 deletions src/node_shadow_realm.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "node_shadow_realm.h"

namespace node {
namespace shadow_realm {
using v8::Context;
using v8::Local;
using v8::MaybeLocal;

// static
MaybeLocal<Context> HostCreateShadowRealmContextCallback(
Local<Context> initiator_context) {
return Context::New(initiator_context->GetIsolate());
}

} // namespace shadow_realm
} // namespace node
19 changes: 19 additions & 0 deletions src/node_shadow_realm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef SRC_NODE_SHADOW_REALM_H_
#define SRC_NODE_SHADOW_REALM_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "v8.h"

namespace node {
namespace shadow_realm {

v8::MaybeLocal<v8::Context> HostCreateShadowRealmContextCallback(
v8::Local<v8::Context> initiator_context);

} // namespace shadow_realm
} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#endif // SRC_NODE_SHADOW_REALM_H_
12 changes: 12 additions & 0 deletions test/parallel/test-shadow-realm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Flags: --harmony-shadow-realm
'use strict';

require('../common');
const assert = require('assert');

// Validates we can construct ShadowRealm successfully.
const shadowRealm = new ShadowRealm();

const getter = shadowRealm.evaluate('globalThis.realmValue = "inner"; () => globalThis.realmValue;');
assert.strictEqual(getter(), 'inner');
assert.strictEqual('realmValue' in globalThis, false);

0 comments on commit 8004257

Please sign in to comment.