Skip to content

Commit 62bb2e7

Browse files
MylesBorinsaddaleax
authored andcommitted
module: unflag Top-Level Await
This unflags Top-Level await so it can be used by default in the module goal. This is accomplished by manually setting the --harmony-top-level-await flag. We are allowing this as a one of approval based on circumstances. It is not a precedent that future harmony features will be manually enabled. Refs: #34551 PR-URL: #34558 Reviewed-By: Mary Marchini <[email protected]> Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Shelley Vohr <[email protected]>
1 parent b9fb0c6 commit 62bb2e7

14 files changed

+33
-28
lines changed

doc/api/cli.md

-10
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,6 @@ the ability to import a directory that has an index file.
239239

240240
Please see [customizing ESM specifier resolution][] for example usage.
241241

242-
### `--experimental-top-level-await`
243-
<!-- YAML
244-
added: v14.3.0
245-
-->
246-
247-
Enable experimental top-level `await` keyword support, available only in ES
248-
module scripts.
249-
250-
(See also `--experimental-repl-await`.)
251-
252242
### `--experimental-vm-modules`
253243
<!-- YAML
254244
added: v9.6.0

doc/api/esm.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -1146,9 +1146,8 @@ would provide the exports interface for the instantiation of `module.wasm`.
11461146

11471147
## Experimental top-level `await`
11481148

1149-
When the `--experimental-top-level-await` flag is provided, `await` may be used
1150-
in the top level (outside of async functions) within modules. This implements
1151-
the [ECMAScript Top-Level `await` proposal][].
1149+
The `await` keyword may be used in the top level (outside of async functions)
1150+
within modules as per the [ECMAScript Top-Level `await` proposal][].
11521151

11531152
Assuming an `a.mjs` with
11541153

@@ -1166,8 +1165,7 @@ console.log(five); // Logs `5`
11661165
```
11671166

11681167
```bash
1169-
node b.mjs # fails
1170-
node --experimental-top-level-await b.mjs # works
1168+
node b.mjs # works
11711169
```
11721170

11731171
## Experimental loaders

doc/node.1

-3
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ keyword support in REPL.
157157
.It Fl -experimental-specifier-resolution
158158
Select extension resolution algorithm for ES Modules; either 'explicit' (default) or 'node'
159159
.
160-
.It Fl -experimental-top-level-await
161-
Enable experimental top-level await support in ES modules.
162-
.
163160
.It Fl -experimental-vm-modules
164161
Enable experimental ES module support in VM module.
165162
.

src/node.cc

+7
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,13 @@ int ProcessGlobalArgs(std::vector<std::string>* args,
779779
return 12;
780780
}
781781

782+
// TODO(mylesborins): remove this when the harmony-top-level-await flag
783+
// is removed in V8
784+
if (std::find(v8_args.begin(), v8_args.end(),
785+
"--no-harmony-top-level-await") == v8_args.end()) {
786+
v8_args.push_back("--harmony-top-level-await");
787+
}
788+
782789
auto env_opts = per_process::cli_options->per_isolate->per_env;
783790
if (std::find(v8_args.begin(), v8_args.end(),
784791
"--abort-on-uncaught-exception") != v8_args.end() ||

src/node_options.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -613,12 +613,13 @@ PerIsolateOptionsParser::PerIsolateOptionsParser(
613613
Implies("--report-signal", "--report-on-signal");
614614

615615
AddOption("--experimental-top-level-await",
616-
"enable experimental support for ECMAScript Top-Level Await",
616+
"",
617617
&PerIsolateOptions::experimental_top_level_await,
618618
kAllowedInEnvironment);
619619
AddOption("--harmony-top-level-await", "", V8Option{});
620620
Implies("--experimental-top-level-await", "--harmony-top-level-await");
621621
Implies("--harmony-top-level-await", "--experimental-top-level-await");
622+
ImpliesNot("--no-harmony-top-level-await", "--experimental-top-level-await");
622623

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

src/node_options.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class PerIsolateOptions : public Options {
188188
bool no_node_snapshot = false;
189189
bool report_uncaught_exception = false;
190190
bool report_on_signal = false;
191-
bool experimental_top_level_await = false;
191+
bool experimental_top_level_await = true;
192192
std::string report_signal = "SIGUSR2";
193193
inline EnvironmentOptions* get_per_env_options();
194194
void CheckOptions(std::vector<std::string>* errors) override;

test/es-module/test-esm-tla.mjs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// Flags: --experimental-top-level-await
2-
31
import '../common/index.mjs';
42
import fixtures from '../common/fixtures.js';
53
import assert from 'assert';
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
// Flags: --no-harmony-top-level-await
2+
13
'use strict';
24
await async () => 0;

test/message/esm_display_syntax_error.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
file:///*/test/message/esm_display_syntax_error.mjs:2
1+
file:///*/test/message/esm_display_syntax_error.mjs:4
22
await async () => 0;
33
^^^^^
44

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
file:///*/test/fixtures/es-module-loaders/syntax-error.mjs:2
22
await async () => 0;
3-
^^^^^
3+
^^^^^^^^^^^^^
44

5-
SyntaxError: Unexpected reserved word
5+
SyntaxError: Malformed arrow function parameter list
66
at Loader.moduleStrategy (internal/modules/esm/translators.js:*:*)

test/message/esm_loader_syntax_error.out

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
(Use `node --trace-warnings ...` to show where the warning was created)
33
file://*/test/fixtures/es-module-loaders/syntax-error.mjs:2
44
await async () => 0;
5-
^^^^^
5+
^^^^^^^^^^^^^
66

7-
SyntaxError: Unexpected reserved word
7+
SyntaxError: Malformed arrow function parameter list
88
at Loader.moduleStrategy (internal/modules/esm/translators.js:*:*)
99
at async link (internal/modules/esm/module_job.js:*:*)

test/parallel/test-cli-node-options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ if (!['arm', 'arm64'].includes(process.arch))
7979
expect('--interpreted-frames-native-stack', 'B\n');
8080

8181
// Workers can't eval as ES Modules. https://github.com/nodejs/node/issues/30682
82-
expectNoWorker('--experimental-top-level-await --input-type=module',
82+
expectNoWorker('--input-type=module',
8383
'B\n', 'console.log(await "B")');
8484

8585
function expectNoWorker(opt, want, command, wantsError) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Flags: --no-harmony-top-level-await
2+
3+
import {
4+
mustCall,
5+
disableCrashOnUnhandledRejection
6+
} from '../common/index.mjs';
7+
8+
disableCrashOnUnhandledRejection();
9+
10+
process.on('unhandledRejection', mustCall());
11+
Promise.reject(new Error('should not be fatal error'));

tools/code_cache/mkcodecache.cc

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ int main(int argc, char* argv[]) {
2727
#endif // _WIN32
2828

2929
v8::V8::SetFlagsFromString("--random_seed=42");
30+
v8::V8::SetFlagsFromString("--harmony-top-level-await");
3031

3132
if (argc < 2) {
3233
std::cerr << "Usage: " << argv[0] << " <path/to/output.cc>\n";

0 commit comments

Comments
 (0)