Skip to content

Commit be92eab

Browse files
committed
repl: hide top-level await feature behind a flag
PR-URL: #19604 Refs: #17807 Refs: #15566 (comment) Reviewed-By: Gus Caplan <[email protected]>
1 parent d864f63 commit be92eab

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

doc/api/repl.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,25 @@ Error: foo
179179
'foo'
180180
```
181181

182+
#### `await` keyword
183+
184+
With the `--experimental-repl-await` command line option specified,
185+
experimental support for the `await` keyword is enabled.
186+
187+
<!-- eslint-skip -->
188+
```js
189+
> await Promise.resolve(123)
190+
123
191+
> await Promise.reject(new Error('REPL await'))
192+
Error: REPL await
193+
at repl:1:45
194+
> const timeout = util.promisify(setTimeout);
195+
undefined
196+
> const old = Date.now(); await timeout(1000); console.log(Date.now() - old);
197+
1002
198+
undefined
199+
```
200+
182201
### Custom Evaluation Functions
183202

184203
When a new `repl.REPLServer` is created, a custom evaluation function may be

lib/repl.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const {
4747
makeRequireFunction,
4848
addBuiltinLibsToObject
4949
} = require('internal/modules/cjs/helpers');
50-
const { processTopLevelAwait } = require('internal/repl/await');
5150
const internalUtil = require('internal/util');
5251
const { isTypedArray } = require('internal/util/types');
5352
const util = require('util');
@@ -69,6 +68,10 @@ const {
6968
ERR_SCRIPT_EXECUTION_INTERRUPTED
7069
} = require('internal/errors').codes;
7170
const { sendInspectorCommand } = require('internal/util/inspector');
71+
const { experimentalREPLAwait } = process.binding('config');
72+
73+
// Lazy-loaded.
74+
let processTopLevelAwait;
7275

7376
const parentModule = module;
7477
const replMap = new WeakMap();
@@ -226,7 +229,11 @@ function REPLServer(prompt,
226229
wrappedCmd = true;
227230
}
228231

229-
if (code.includes('await')) {
232+
if (experimentalREPLAwait && code.includes('await')) {
233+
if (processTopLevelAwait === undefined) {
234+
({ processTopLevelAwait } = require('internal/repl/await'));
235+
}
236+
230237
const potentialWrappedCode = processTopLevelAwait(code);
231238
if (potentialWrappedCode !== null) {
232239
code = potentialWrappedCode;

src/node.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ bool config_experimental_modules = false;
243243
// that is used by lib/vm.js
244244
bool config_experimental_vm_modules = false;
245245

246+
// Set in node.cc by ParseArgs when --experimental-repl-await is used.
247+
// Used in node_config.cc to set a constant on process.binding('config')
248+
// that is used by lib/repl.js.
249+
bool config_experimental_repl_await = false;
250+
246251
// Set in node.cc by ParseArgs when --loader is used.
247252
// Used in node_config.cc to set a constant on process.binding('config')
248253
// that is used by lib/internal/bootstrap/node.js
@@ -3463,6 +3468,10 @@ static void PrintHelp() {
34633468
#if defined(NODE_HAVE_I18N_SUPPORT)
34643469
" --experimental-modules experimental ES Module support\n"
34653470
" and caching modules\n"
3471+
#endif // defined(NODE_HAVE_I18N_SUPPORT)
3472+
" --experimental-repl-await experimental await keyword support\n"
3473+
" in REPL\n"
3474+
#if defined(NODE_HAVE_I18N_SUPPORT)
34663475
" --experimental-vm-modules experimental ES Module support\n"
34673476
" in vm module\n"
34683477
#endif // defined(NODE_HAVE_I18N_SUPPORT)
@@ -3622,6 +3631,7 @@ static void CheckIfAllowedInEnv(const char* exe, bool is_env,
36223631
// Node options, sorted in `node --help` order for ease of comparison.
36233632
"--enable-fips",
36243633
"--experimental-modules",
3634+
"--experimental-repl-await",
36253635
"--experimental-vm-modules",
36263636
"--expose-http2", // keep as a non-op through v9.x
36273637
"--force-fips",
@@ -3819,6 +3829,8 @@ static void ParseArgs(int* argc,
38193829
new_v8_argc += 1;
38203830
} else if (strcmp(arg, "--experimental-vm-modules") == 0) {
38213831
config_experimental_vm_modules = true;
3832+
} else if (strcmp(arg, "--experimental-repl-await") == 0) {
3833+
config_experimental_repl_await = true;
38223834
} else if (strcmp(arg, "--loader") == 0) {
38233835
const char* module = argv[index + 1];
38243836
if (!config_experimental_modules) {

src/node_config.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ static void Initialize(Local<Object> target,
8989
if (config_experimental_vm_modules)
9090
READONLY_BOOLEAN_PROPERTY("experimentalVMModules");
9191

92+
if (config_experimental_repl_await)
93+
READONLY_BOOLEAN_PROPERTY("experimentalREPLAwait");
94+
9295
if (config_pending_deprecation)
9396
READONLY_BOOLEAN_PROPERTY("pendingDeprecation");
9497

src/node_internals.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ extern bool config_experimental_modules;
181181
// that is used by lib/vm.js
182182
extern bool config_experimental_vm_modules;
183183

184+
// Set in node.cc by ParseArgs when --experimental-repl-await is used.
185+
// Used in node_config.cc to set a constant on process.binding('config')
186+
// that is used by lib/repl.js.
187+
extern bool config_experimental_repl_await;
188+
184189
// Set in node.cc by ParseArgs when --loader is used.
185190
// Used in node_config.cc to set a constant on process.binding('config')
186191
// that is used by lib/internal/bootstrap/node.js

test/parallel/test-repl-top-level-await.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const repl = require('repl');
77

88
common.crashOnUnhandledRejection();
99

10-
// Flags: --expose-internals
10+
// Flags: --expose-internals --experimental-repl-await
1111

1212
const PROMPT = 'await repl > ';
1313

0 commit comments

Comments
 (0)