Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: hide top-level await feature behind a flag #19604

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
repl: hide top-level await feature behind a flag
  • Loading branch information
TimothyGu committed Apr 14, 2018
commit d8d5c2a9add5a16b9964371c017b22d941dd0696
19 changes: 19 additions & 0 deletions doc/api/repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ Error: foo
'foo'
```

#### `await` keyword

With the `--experimental-repl-await` command line option specified,
experimental support for the `await` keyword is enabled.

<!-- eslint-skip -->
```js
> await Promise.resolve(123)
123
> await Promise.reject(new Error('REPL await'))
Error: REPL await
at repl:1:45
> timeout = util.promisify(setTimeout)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe const timeout = and const old = to not emphasize sloppy mode?

[Function]
> old = Date.now(); await timeout(1000); console.log(Date.now() - old);
1002
undefined
```

### Custom Evaluation Functions

When a new `repl.REPLServer` is created, a custom evaluation function may be
Expand Down
10 changes: 8 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const {
makeRequireFunction,
addBuiltinLibsToObject
} = require('internal/modules/cjs/helpers');
const { processTopLevelAwait } = require('internal/repl/await');
const internalUtil = require('internal/util');
const { isTypedArray } = require('internal/util/types');
const util = require('util');
Expand All @@ -69,6 +68,10 @@ const {
ERR_SCRIPT_EXECUTION_INTERRUPTED
} = require('internal/errors').codes;
const { sendInspectorCommand } = require('internal/util/inspector');
const { experimentalREPLAwait } = process.binding('config');

// Lazy-loaded.
let processTopLevelAwait;

const parentModule = module;
const replMap = new WeakMap();
Expand Down Expand Up @@ -226,7 +229,10 @@ function REPLServer(prompt,
wrappedCmd = true;
}

if (code.includes('await')) {
if (experimentalREPLAwait && code.includes('await')) {
if (processTopLevelAwait === undefined)
({ processTopLevelAwait } = require('internal/repl/await'));

const potentialWrappedCode = processTopLevelAwait(code);
if (potentialWrappedCode !== null) {
code = potentialWrappedCode;
Expand Down
12 changes: 12 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ bool config_experimental_modules = false;
// that is used by lib/vm.js
bool config_experimental_vm_modules = false;

// Set in node.cc by ParseArgs when --experimental-repl-await is used.
// Used in node_config.cc to set a constant on process.binding('config')
// that is used by lib/repl.js.
bool config_experimental_repl_await = false;

// Set in node.cc by ParseArgs when --loader is used.
// Used in node_config.cc to set a constant on process.binding('config')
// that is used by lib/internal/bootstrap/node.js
Expand Down Expand Up @@ -3468,6 +3473,10 @@ static void PrintHelp() {
#if defined(NODE_HAVE_I18N_SUPPORT)
" --experimental-modules experimental ES Module support\n"
" and caching modules\n"
#endif // defined(NODE_HAVE_I18N_SUPPORT)
" --experimental-repl-await experimental await keyword support\n"
" in REPL\n"
#if defined(NODE_HAVE_I18N_SUPPORT)
" --experimental-vm-modules experimental ES Module support\n"
" in vm module\n"
#endif // defined(NODE_HAVE_I18N_SUPPORT)
Expand Down Expand Up @@ -3627,6 +3636,7 @@ static void CheckIfAllowedInEnv(const char* exe, bool is_env,
// Node options, sorted in `node --help` order for ease of comparison.
"--enable-fips",
"--experimental-modules",
"--experimental-repl-await",
"--experimental-vm-modules",
"--expose-http2", // keep as a non-op through v9.x
"--force-fips",
Expand Down Expand Up @@ -3823,6 +3833,8 @@ static void ParseArgs(int* argc,
new_v8_argc += 1;
} else if (strcmp(arg, "--experimental-vm-modules") == 0) {
config_experimental_vm_modules = true;
} else if (strcmp(arg, "--experimental-repl-await") == 0) {
config_experimental_repl_await = true;
} else if (strcmp(arg, "--loader") == 0) {
const char* module = argv[index + 1];
if (!config_experimental_modules) {
Expand Down
3 changes: 3 additions & 0 deletions src/node_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ static void Initialize(Local<Object> target,
if (config_experimental_vm_modules)
READONLY_BOOLEAN_PROPERTY("experimentalVMModules");

if (config_experimental_repl_await)
READONLY_BOOLEAN_PROPERTY("experimentalREPLAwait");

if (config_pending_deprecation)
READONLY_BOOLEAN_PROPERTY("pendingDeprecation");

Expand Down
5 changes: 5 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ extern bool config_experimental_modules;
// that is used by lib/vm.js
extern bool config_experimental_vm_modules;

// Set in node.cc by ParseArgs when --experimental-repl-await is used.
// Used in node_config.cc to set a constant on process.binding('config')
// that is used by lib/repl.js.
extern bool config_experimental_repl_await;

// Set in node.cc by ParseArgs when --loader is used.
// Used in node_config.cc to set a constant on process.binding('config')
// that is used by lib/internal/bootstrap/node.js
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-top-level-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const repl = require('repl');

common.crashOnUnhandledRejection();

// Flags: --expose-internals
// Flags: --expose-internals --experimental-repl-await

const PROMPT = 'await repl > ';

Expand Down