enhance awaits (#5637)
This commit is contained in:
@@ -3914,7 +3914,7 @@ Compressor.prototype.compress = function(node) {
|
|||||||
var node = stat.body;
|
var node = stat.body;
|
||||||
if (!(node instanceof AST_Await)) break;
|
if (!(node instanceof AST_Await)) break;
|
||||||
var exp = node.expression;
|
var exp = node.expression;
|
||||||
if (!is_primitive(compressor, exp)) break;
|
if (!needs_enqueuing(compressor, exp)) break;
|
||||||
changed = true;
|
changed = true;
|
||||||
exp = exp.drop_side_effect_free(compressor, true);
|
exp = exp.drop_side_effect_free(compressor, true);
|
||||||
if (exp) {
|
if (exp) {
|
||||||
@@ -8569,7 +8569,7 @@ Compressor.prototype.compress = function(node) {
|
|||||||
if (arrow && compressor.option("arrows")) {
|
if (arrow && compressor.option("arrows")) {
|
||||||
if (!exp.value) {
|
if (!exp.value) {
|
||||||
drop_body = true;
|
drop_body = true;
|
||||||
} else if (!async || is_primitive(compressor, exp.value)) {
|
} else if (!async || needs_enqueuing(compressor, exp.value)) {
|
||||||
var dropped = exp.value.drop_side_effect_free(compressor);
|
var dropped = exp.value.drop_side_effect_free(compressor);
|
||||||
if (dropped !== exp.value) {
|
if (dropped !== exp.value) {
|
||||||
changed = true;
|
changed = true;
|
||||||
@@ -8588,7 +8588,7 @@ Compressor.prototype.compress = function(node) {
|
|||||||
exp.process_expression(false, function(node) {
|
exp.process_expression(false, function(node) {
|
||||||
var value = node.value;
|
var value = node.value;
|
||||||
if (value) {
|
if (value) {
|
||||||
if (async && !is_primitive(compressor, value)) return node;
|
if (async && !needs_enqueuing(compressor, value)) return node;
|
||||||
value = value.drop_side_effect_free(compressor, true);
|
value = value.drop_side_effect_free(compressor, true);
|
||||||
}
|
}
|
||||||
changed = true;
|
changed = true;
|
||||||
@@ -8598,11 +8598,11 @@ Compressor.prototype.compress = function(node) {
|
|||||||
scan_local_returns(exp, function(node) {
|
scan_local_returns(exp, function(node) {
|
||||||
var value = node.value;
|
var value = node.value;
|
||||||
if (value) {
|
if (value) {
|
||||||
if (async && !is_primitive(compressor, value)) return;
|
if (async && !needs_enqueuing(compressor, value)) return;
|
||||||
var dropped = value.drop_side_effect_free(compressor);
|
var dropped = value.drop_side_effect_free(compressor);
|
||||||
if (dropped !== value) {
|
if (dropped !== value) {
|
||||||
changed = true;
|
changed = true;
|
||||||
if (dropped && async && !is_primitive(compressor, dropped)) {
|
if (dropped && async && !needs_enqueuing(compressor, dropped)) {
|
||||||
dropped = dropped.negate(compressor);
|
dropped = dropped.negate(compressor);
|
||||||
}
|
}
|
||||||
node.value = dropped;
|
node.value = dropped;
|
||||||
@@ -8614,7 +8614,7 @@ Compressor.prototype.compress = function(node) {
|
|||||||
if (drop_body) exp.process_expression("awaits", function(node) {
|
if (drop_body) exp.process_expression("awaits", function(node) {
|
||||||
var body = node.body;
|
var body = node.body;
|
||||||
if (body instanceof AST_Await) {
|
if (body instanceof AST_Await) {
|
||||||
if (is_primitive(compressor, body.expression)) {
|
if (needs_enqueuing(compressor, body.expression)) {
|
||||||
changed = true;
|
changed = true;
|
||||||
body = body.expression.drop_side_effect_free(compressor, true);
|
body = body.expression.drop_side_effect_free(compressor, true);
|
||||||
if (!body) return make_node(AST_EmptyStatement, node);
|
if (!body) return make_node(AST_EmptyStatement, node);
|
||||||
@@ -8626,7 +8626,7 @@ Compressor.prototype.compress = function(node) {
|
|||||||
var tail = exprs[i];
|
var tail = exprs[i];
|
||||||
if (!(tail instanceof AST_Await)) break;
|
if (!(tail instanceof AST_Await)) break;
|
||||||
var value = tail.expression;
|
var value = tail.expression;
|
||||||
if (!is_primitive(compressor, value)) break;
|
if (!needs_enqueuing(compressor, value)) break;
|
||||||
changed = true;
|
changed = true;
|
||||||
if (exprs[i] = value.drop_side_effect_free(compressor)) break;
|
if (exprs[i] = value.drop_side_effect_free(compressor)) break;
|
||||||
}
|
}
|
||||||
@@ -8643,14 +8643,14 @@ Compressor.prototype.compress = function(node) {
|
|||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
});
|
});
|
||||||
var abort = !drop_body && exp.name || arrow && exp.value && !is_primitive(compressor, exp.value);
|
var abort = !drop_body && exp.name || arrow && exp.value && !needs_enqueuing(compressor, exp.value);
|
||||||
var tw = new TreeWalker(function(node) {
|
var tw = new TreeWalker(function(node) {
|
||||||
if (abort) return true;
|
if (abort) return true;
|
||||||
if (tw.parent() === exp && node.may_throw(compressor)) return abort = true;
|
if (tw.parent() === exp && node.may_throw(compressor)) return abort = true;
|
||||||
if (node instanceof AST_Await) return abort = true;
|
if (node instanceof AST_Await) return abort = true;
|
||||||
if (node instanceof AST_ForAwaitOf) return abort = true;
|
if (node instanceof AST_ForAwaitOf) return abort = true;
|
||||||
if (node instanceof AST_Return) {
|
if (node instanceof AST_Return) {
|
||||||
if (node.value && !is_primitive(compressor, node.value)) return abort = true;
|
if (node.value && !needs_enqueuing(compressor, node.value)) return abort = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Scope && node !== exp) return true;
|
if (node instanceof AST_Scope && node !== exp) return true;
|
||||||
@@ -8735,13 +8735,13 @@ Compressor.prototype.compress = function(node) {
|
|||||||
def(AST_Await, function(compressor) {
|
def(AST_Await, function(compressor) {
|
||||||
if (!compressor.option("awaits")) return this;
|
if (!compressor.option("awaits")) return this;
|
||||||
var exp = this.expression;
|
var exp = this.expression;
|
||||||
if (!is_primitive(compressor, exp)) return this;
|
if (!needs_enqueuing(compressor, exp)) return this;
|
||||||
if (exp instanceof AST_UnaryPrefix && exp.operator == "!") exp = exp.expression;
|
if (exp instanceof AST_UnaryPrefix && exp.operator == "!") exp = exp.expression;
|
||||||
var dropped = exp.drop_side_effect_free(compressor);
|
var dropped = exp.drop_side_effect_free(compressor);
|
||||||
if (dropped === exp) return this;
|
if (dropped === exp) return this;
|
||||||
if (!dropped) {
|
if (!dropped) {
|
||||||
dropped = make_node(AST_Number, exp, { value: 0 });
|
dropped = make_node(AST_Number, exp, { value: 0 });
|
||||||
} else if (!is_primitive(compressor, dropped)) {
|
} else if (!needs_enqueuing(compressor, dropped)) {
|
||||||
dropped = dropped.negate(compressor);
|
dropped = dropped.negate(compressor);
|
||||||
}
|
}
|
||||||
var node = this.clone();
|
var node = this.clone();
|
||||||
@@ -8998,7 +8998,7 @@ Compressor.prototype.compress = function(node) {
|
|||||||
expressions = expressions.slice(0, -1);
|
expressions = expressions.slice(0, -1);
|
||||||
end--;
|
end--;
|
||||||
var expr = expressions[end];
|
var expr = expressions[end];
|
||||||
last.expression = is_primitive(compressor, expr) ? expr : expr.negate(compressor);
|
last.expression = needs_enqueuing(compressor, expr) ? expr : expr.negate(compressor);
|
||||||
expressions[end] = last;
|
expressions[end] = last;
|
||||||
}
|
}
|
||||||
var assign, cond, lhs;
|
var assign, cond, lhs;
|
||||||
@@ -11489,20 +11489,21 @@ Compressor.prototype.compress = function(node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_primitive(compressor, node) {
|
function needs_enqueuing(compressor, node) {
|
||||||
if (node.is_constant()) return true;
|
if (node.is_constant()) return true;
|
||||||
if (node instanceof AST_Assign) return node.operator != "=" || is_primitive(compressor, node.right);
|
if (node instanceof AST_Assign) return node.operator != "=" || needs_enqueuing(compressor, node.right);
|
||||||
if (node instanceof AST_Binary) {
|
if (node instanceof AST_Binary) {
|
||||||
return !lazy_op[node.operator]
|
return !lazy_op[node.operator]
|
||||||
|| is_primitive(compressor, node.left) && is_primitive(compressor, node.right);
|
|| needs_enqueuing(compressor, node.left) && needs_enqueuing(compressor, node.right);
|
||||||
}
|
}
|
||||||
|
if (node instanceof AST_Call) return is_async(node.expression);
|
||||||
if (node instanceof AST_Conditional) {
|
if (node instanceof AST_Conditional) {
|
||||||
return is_primitive(compressor, node.consequent) && is_primitive(compressor, node.alternative);
|
return needs_enqueuing(compressor, node.consequent) && needs_enqueuing(compressor, node.alternative);
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Sequence) return is_primitive(compressor, node.tail_node());
|
if (node instanceof AST_Sequence) return needs_enqueuing(compressor, node.tail_node());
|
||||||
if (node instanceof AST_SymbolRef) {
|
if (node instanceof AST_SymbolRef) {
|
||||||
var fixed = node.fixed_value();
|
var fixed = node.fixed_value();
|
||||||
return fixed && is_primitive(compressor, fixed);
|
return fixed && needs_enqueuing(compressor, fixed);
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Template) return !node.tag || is_raw_tag(compressor, node.tag);
|
if (node instanceof AST_Template) return !node.tag || is_raw_tag(compressor, node.tag);
|
||||||
if (node instanceof AST_Unary) return true;
|
if (node instanceof AST_Unary) return true;
|
||||||
@@ -13630,6 +13631,7 @@ Compressor.prototype.compress = function(node) {
|
|||||||
def(AST_Node, noop);
|
def(AST_Node, noop);
|
||||||
def(AST_Assign, noop);
|
def(AST_Assign, noop);
|
||||||
def(AST_Await, function(compressor, scope, no_return, in_loop) {
|
def(AST_Await, function(compressor, scope, no_return, in_loop) {
|
||||||
|
if (!compressor.option("awaits")) return;
|
||||||
var self = this;
|
var self = this;
|
||||||
var inlined = self.expression.try_inline(compressor, scope, no_return, in_loop, true);
|
var inlined = self.expression.try_inline(compressor, scope, no_return, in_loop, true);
|
||||||
if (!inlined) return;
|
if (!inlined) return;
|
||||||
|
|||||||
@@ -529,6 +529,7 @@ inline_block_await: {
|
|||||||
|
|
||||||
inline_block_await_async: {
|
inline_block_await_async: {
|
||||||
options = {
|
options = {
|
||||||
|
awaits: true,
|
||||||
inline: true,
|
inline: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
@@ -2747,6 +2748,7 @@ issue_5177: {
|
|||||||
|
|
||||||
issue_5250: {
|
issue_5250: {
|
||||||
options = {
|
options = {
|
||||||
|
awaits: true,
|
||||||
inline: true,
|
inline: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
@@ -2855,6 +2857,7 @@ issue_5298: {
|
|||||||
|
|
||||||
issue_5305_1: {
|
issue_5305_1: {
|
||||||
options = {
|
options = {
|
||||||
|
awaits: true,
|
||||||
inline: true,
|
inline: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
@@ -2888,6 +2891,7 @@ issue_5305_1: {
|
|||||||
|
|
||||||
issue_5305_2: {
|
issue_5305_2: {
|
||||||
options = {
|
options = {
|
||||||
|
awaits: true,
|
||||||
inline: true,
|
inline: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
@@ -3231,6 +3235,57 @@ issue_5528_4: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
issue_5634_1: {
|
issue_5634_1: {
|
||||||
|
options = {
|
||||||
|
awaits: true,
|
||||||
|
inline: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = "foo";
|
||||||
|
(async function() {
|
||||||
|
(async function() {
|
||||||
|
try {
|
||||||
|
return {
|
||||||
|
then(resolve) {
|
||||||
|
console.log("bar");
|
||||||
|
resolve();
|
||||||
|
console.log("baz");
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
a = "moo";
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
})();
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = "foo";
|
||||||
|
(async function() {
|
||||||
|
(async function() {
|
||||||
|
try {
|
||||||
|
return {
|
||||||
|
then(resolve) {
|
||||||
|
console.log("bar");
|
||||||
|
resolve();
|
||||||
|
console.log("baz");
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
a = "moo";
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
})();
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"moo",
|
||||||
|
"bar",
|
||||||
|
"baz",
|
||||||
|
]
|
||||||
|
node_version: ">=8"
|
||||||
|
}
|
||||||
|
|
||||||
|
issue_5634_1_side_effects: {
|
||||||
options = {
|
options = {
|
||||||
awaits: true,
|
awaits: true,
|
||||||
inline: true,
|
inline: true,
|
||||||
@@ -3282,6 +3337,7 @@ issue_5634_1: {
|
|||||||
|
|
||||||
issue_5634_2: {
|
issue_5634_2: {
|
||||||
options = {
|
options = {
|
||||||
|
awaits: true,
|
||||||
inline: true,
|
inline: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
@@ -3330,6 +3386,56 @@ issue_5634_2: {
|
|||||||
node_version: ">=8"
|
node_version: ">=8"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
issue_5634_2_side_effects: {
|
||||||
|
options = {
|
||||||
|
awaits: true,
|
||||||
|
inline: true,
|
||||||
|
side_effects: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = "foo";
|
||||||
|
(async function() {
|
||||||
|
await async function() {
|
||||||
|
try {
|
||||||
|
return {
|
||||||
|
then(resolve) {
|
||||||
|
console.log("bar");
|
||||||
|
resolve();
|
||||||
|
console.log("baz");
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
a = "moo";
|
||||||
|
}
|
||||||
|
}();
|
||||||
|
})();
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = "foo";
|
||||||
|
(async function() {
|
||||||
|
try {
|
||||||
|
return {
|
||||||
|
then(resolve) {
|
||||||
|
console.log("bar");
|
||||||
|
resolve();
|
||||||
|
console.log("baz");
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
a = "moo";
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"moo",
|
||||||
|
"bar",
|
||||||
|
"baz",
|
||||||
|
]
|
||||||
|
node_version: ">=8"
|
||||||
|
}
|
||||||
|
|
||||||
issue_5634_3: {
|
issue_5634_3: {
|
||||||
options = {
|
options = {
|
||||||
awaits: true,
|
awaits: true,
|
||||||
@@ -3380,3 +3486,53 @@ issue_5634_3: {
|
|||||||
]
|
]
|
||||||
node_version: ">=8"
|
node_version: ">=8"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
issue_5634_3_side_effects: {
|
||||||
|
options = {
|
||||||
|
awaits: true,
|
||||||
|
inline: true,
|
||||||
|
side_effects: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = "foo";
|
||||||
|
(async function() {
|
||||||
|
return async function() {
|
||||||
|
try {
|
||||||
|
return {
|
||||||
|
then(resolve) {
|
||||||
|
console.log("bar");
|
||||||
|
resolve();
|
||||||
|
console.log("baz");
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
a = "moo";
|
||||||
|
}
|
||||||
|
}();
|
||||||
|
})();
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = "foo";
|
||||||
|
(async function() {
|
||||||
|
try {
|
||||||
|
return {
|
||||||
|
then(resolve) {
|
||||||
|
console.log("bar");
|
||||||
|
resolve();
|
||||||
|
console.log("baz");
|
||||||
|
},
|
||||||
|
};
|
||||||
|
} finally {
|
||||||
|
a = "moo";
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"moo",
|
||||||
|
"bar",
|
||||||
|
"baz",
|
||||||
|
]
|
||||||
|
node_version: ">=8"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user