Skip to content

Commit

Permalink
support top-level await (#5487)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jun 6, 2022
1 parent 25441d4 commit d2bd0d1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,8 @@ if (result.error) throw result.error;
Pass an object to specify custom [mangle property options](#mangle-properties-options).

- `module` (default: `false`) — set to `true` if you wish to process input as
ES module, i.e. implicit `"use strict";` alongside with `toplevel` enabled.
ES module, i.e. implicit `"use strict";` and support for top-level `await`,
alongside with `toplevel` enabled.

- `nameCache` (default: `null`) — pass an empty object `{}` or a previously
used `nameCache` object if you wish to cache mangled variable and
Expand Down Expand Up @@ -632,7 +633,13 @@ to be `false` and all symbol names will be omitted.

- `bare_returns` (default: `false`) — support top level `return` statements

- `html5_comments` (default: `true`)
- `expression` (default: `false`) — parse as a single expression, e.g. JSON

- `html5_comments` (default: `true`) — process HTML comment as workaround for
browsers which do not recognise `<script>` tags

- `module` (default: `false`) — set to `true` if you wish to process input as
ES module, i.e. implicit `"use strict";` and support for top-level `await`.

- `shebang` (default: `true`) — support `#!command` as the first line

Expand Down Expand Up @@ -1415,9 +1422,10 @@ To allow for better optimizations, the compiler makes various assumptions:
function f() {
throw 42;
}
} catch (e) {}
console.log(typeof f);
// Expected: "function"
// Actual: "undefined"
} catch (e) {
console.log(typeof f, e);
}
// Expected: "function 42"
// Actual: "undefined 42"
```
UglifyJS may modify the input which in turn may suppress those errors.
5 changes: 4 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2541,7 +2541,10 @@ function parse($TEXT, options) {
return function() {
var start = S.token;
var body = [];
if (options.module) S.input.add_directive("use strict");
if (options.module) {
S.in_async = true;
S.input.add_directive("use strict");
}
S.input.push_directives_stack();
while (!is("eof"))
body.push(statement());
Expand Down
6 changes: 4 additions & 2 deletions test/ufuzz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,15 @@ function createTopLevelCode() {
unique_vars.length = 0;
classes.length = 0;
allow_this = true;
async = false;
async = SUPPORT.async && rng(200) == 0;
has_await = false;
export_default = false;
generator = false;
loops = 0;
funcs = 0;
clazz = 0;
imports = 0;
in_class = 0;
in_class = async;
called = Object.create(null);
var s = [
strictMode(),
Expand Down Expand Up @@ -2087,6 +2087,7 @@ if (require.main !== module) {
}

function run_code(code, toplevel, timeout) {
if (async && has_await) code = "(async function(){\n" + code + "\n})();";
return sandbox.run_code(sandbox.patch_module_statements(code), toplevel, timeout);
}

Expand Down Expand Up @@ -2502,6 +2503,7 @@ for (var round = 1; round <= num_iterations; round++) {
minify_options.forEach(function(options) {
var o = JSON.parse(options);
var toplevel = sandbox.has_toplevel(o);
if (async && has_await) o.parse = { module: true };
o.validate = true;
uglify_code = UglifyJS.minify(original_code, o);
original_result = orig_result[toplevel ? 1 : 0];
Expand Down

0 comments on commit d2bd0d1

Please sign in to comment.