repl: hide top-level await feature behind a flag
PR-URL: https://github.com/nodejs/node/pull/19604 Refs: https://github.com/nodejs/node/pull/17807 Refs: https://github.com/nodejs/node/pull/15566#issuecomment-353428430 Reviewed-By: Gus Caplan <me@gus.host>
This commit is contained in:
parent
d864f63611
commit
be92eab92a
@ -179,6 +179,25 @@ Error: foo
|
|||||||
'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
|
||||||
|
> const timeout = util.promisify(setTimeout);
|
||||||
|
undefined
|
||||||
|
> const old = Date.now(); await timeout(1000); console.log(Date.now() - old);
|
||||||
|
1002
|
||||||
|
undefined
|
||||||
|
```
|
||||||
|
|
||||||
### Custom Evaluation Functions
|
### Custom Evaluation Functions
|
||||||
|
|
||||||
When a new `repl.REPLServer` is created, a custom evaluation function may be
|
When a new `repl.REPLServer` is created, a custom evaluation function may be
|
||||||
|
11
lib/repl.js
11
lib/repl.js
@ -47,7 +47,6 @@ const {
|
|||||||
makeRequireFunction,
|
makeRequireFunction,
|
||||||
addBuiltinLibsToObject
|
addBuiltinLibsToObject
|
||||||
} = require('internal/modules/cjs/helpers');
|
} = require('internal/modules/cjs/helpers');
|
||||||
const { processTopLevelAwait } = require('internal/repl/await');
|
|
||||||
const internalUtil = require('internal/util');
|
const internalUtil = require('internal/util');
|
||||||
const { isTypedArray } = require('internal/util/types');
|
const { isTypedArray } = require('internal/util/types');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
@ -69,6 +68,10 @@ const {
|
|||||||
ERR_SCRIPT_EXECUTION_INTERRUPTED
|
ERR_SCRIPT_EXECUTION_INTERRUPTED
|
||||||
} = require('internal/errors').codes;
|
} = require('internal/errors').codes;
|
||||||
const { sendInspectorCommand } = require('internal/util/inspector');
|
const { sendInspectorCommand } = require('internal/util/inspector');
|
||||||
|
const { experimentalREPLAwait } = process.binding('config');
|
||||||
|
|
||||||
|
// Lazy-loaded.
|
||||||
|
let processTopLevelAwait;
|
||||||
|
|
||||||
const parentModule = module;
|
const parentModule = module;
|
||||||
const replMap = new WeakMap();
|
const replMap = new WeakMap();
|
||||||
@ -226,7 +229,11 @@ function REPLServer(prompt,
|
|||||||
wrappedCmd = true;
|
wrappedCmd = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (code.includes('await')) {
|
if (experimentalREPLAwait && code.includes('await')) {
|
||||||
|
if (processTopLevelAwait === undefined) {
|
||||||
|
({ processTopLevelAwait } = require('internal/repl/await'));
|
||||||
|
}
|
||||||
|
|
||||||
const potentialWrappedCode = processTopLevelAwait(code);
|
const potentialWrappedCode = processTopLevelAwait(code);
|
||||||
if (potentialWrappedCode !== null) {
|
if (potentialWrappedCode !== null) {
|
||||||
code = potentialWrappedCode;
|
code = potentialWrappedCode;
|
||||||
|
12
src/node.cc
12
src/node.cc
@ -243,6 +243,11 @@ bool config_experimental_modules = false;
|
|||||||
// that is used by lib/vm.js
|
// that is used by lib/vm.js
|
||||||
bool config_experimental_vm_modules = false;
|
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.
|
// Set in node.cc by ParseArgs when --loader is used.
|
||||||
// Used in node_config.cc to set a constant on process.binding('config')
|
// Used in node_config.cc to set a constant on process.binding('config')
|
||||||
// that is used by lib/internal/bootstrap/node.js
|
// that is used by lib/internal/bootstrap/node.js
|
||||||
@ -3463,6 +3468,10 @@ static void PrintHelp() {
|
|||||||
#if defined(NODE_HAVE_I18N_SUPPORT)
|
#if defined(NODE_HAVE_I18N_SUPPORT)
|
||||||
" --experimental-modules experimental ES Module support\n"
|
" --experimental-modules experimental ES Module support\n"
|
||||||
" and caching modules\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"
|
" --experimental-vm-modules experimental ES Module support\n"
|
||||||
" in vm module\n"
|
" in vm module\n"
|
||||||
#endif // defined(NODE_HAVE_I18N_SUPPORT)
|
#endif // defined(NODE_HAVE_I18N_SUPPORT)
|
||||||
@ -3622,6 +3631,7 @@ static void CheckIfAllowedInEnv(const char* exe, bool is_env,
|
|||||||
// Node options, sorted in `node --help` order for ease of comparison.
|
// Node options, sorted in `node --help` order for ease of comparison.
|
||||||
"--enable-fips",
|
"--enable-fips",
|
||||||
"--experimental-modules",
|
"--experimental-modules",
|
||||||
|
"--experimental-repl-await",
|
||||||
"--experimental-vm-modules",
|
"--experimental-vm-modules",
|
||||||
"--expose-http2", // keep as a non-op through v9.x
|
"--expose-http2", // keep as a non-op through v9.x
|
||||||
"--force-fips",
|
"--force-fips",
|
||||||
@ -3819,6 +3829,8 @@ static void ParseArgs(int* argc,
|
|||||||
new_v8_argc += 1;
|
new_v8_argc += 1;
|
||||||
} else if (strcmp(arg, "--experimental-vm-modules") == 0) {
|
} else if (strcmp(arg, "--experimental-vm-modules") == 0) {
|
||||||
config_experimental_vm_modules = true;
|
config_experimental_vm_modules = true;
|
||||||
|
} else if (strcmp(arg, "--experimental-repl-await") == 0) {
|
||||||
|
config_experimental_repl_await = true;
|
||||||
} else if (strcmp(arg, "--loader") == 0) {
|
} else if (strcmp(arg, "--loader") == 0) {
|
||||||
const char* module = argv[index + 1];
|
const char* module = argv[index + 1];
|
||||||
if (!config_experimental_modules) {
|
if (!config_experimental_modules) {
|
||||||
|
@ -89,6 +89,9 @@ static void Initialize(Local<Object> target,
|
|||||||
if (config_experimental_vm_modules)
|
if (config_experimental_vm_modules)
|
||||||
READONLY_BOOLEAN_PROPERTY("experimentalVMModules");
|
READONLY_BOOLEAN_PROPERTY("experimentalVMModules");
|
||||||
|
|
||||||
|
if (config_experimental_repl_await)
|
||||||
|
READONLY_BOOLEAN_PROPERTY("experimentalREPLAwait");
|
||||||
|
|
||||||
if (config_pending_deprecation)
|
if (config_pending_deprecation)
|
||||||
READONLY_BOOLEAN_PROPERTY("pendingDeprecation");
|
READONLY_BOOLEAN_PROPERTY("pendingDeprecation");
|
||||||
|
|
||||||
|
@ -181,6 +181,11 @@ extern bool config_experimental_modules;
|
|||||||
// that is used by lib/vm.js
|
// that is used by lib/vm.js
|
||||||
extern bool config_experimental_vm_modules;
|
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.
|
// Set in node.cc by ParseArgs when --loader is used.
|
||||||
// Used in node_config.cc to set a constant on process.binding('config')
|
// Used in node_config.cc to set a constant on process.binding('config')
|
||||||
// that is used by lib/internal/bootstrap/node.js
|
// that is used by lib/internal/bootstrap/node.js
|
||||||
|
@ -7,7 +7,7 @@ const repl = require('repl');
|
|||||||
|
|
||||||
common.crashOnUnhandledRejection();
|
common.crashOnUnhandledRejection();
|
||||||
|
|
||||||
// Flags: --expose-internals
|
// Flags: --expose-internals --experimental-repl-await
|
||||||
|
|
||||||
const PROMPT = 'await repl > ';
|
const PROMPT = 'await repl > ';
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user