migrate safe transformations out of unsafe_comps (#2962)

fixes #2959
This commit is contained in:
Alex Lam S.L
2018-02-28 22:02:24 +08:00
committed by GitHub
parent 73e98dcda4
commit 0daa199fa8
5 changed files with 29 additions and 55 deletions

View File

@@ -605,8 +605,8 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
side effects permitting. side effects permitting.
- `comparisons` (default: `true`) -- apply certain optimizations to binary nodes, - `comparisons` (default: `true`) -- apply certain optimizations to binary nodes,
e.g. `!(a <= b) → a > b` (only when `unsafe_comps`), attempts to negate binary e.g. `!(a <= b) → a > b`, attempts to negate binary nodes, e.g.
nodes, e.g. `a = !b && !c && !d && !e → a=!(b||c||d||e)` etc. `a = !b && !c && !d && !e → a=!(b||c||d||e)` etc.
- `conditionals` (default: `true`) -- apply optimizations for `if`-s and conditional - `conditionals` (default: `true`) -- apply optimizations for `if`-s and conditional
expressions expressions
@@ -730,12 +730,8 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
- `unsafe` (default: `false`) -- apply "unsafe" transformations (discussion below) - `unsafe` (default: `false`) -- apply "unsafe" transformations (discussion below)
- `unsafe_comps` (default: `false`) -- Reverse `<` and `<=` to `>` and `>=` to - `unsafe_comps` (default: `false`) -- compress expressions like `a <= b` assuming
allow improved compression. This might be unsafe when an at least one of two none of the operands can be (coerced to) `NaN`.
operands is an object with computed values due the use of methods like `get`,
or `valueOf`. This could cause change in execution order after operands in the
comparison are switching. Compression only works if both `comparisons` and
`unsafe_comps` are both set to true.
- `unsafe_Function` (default: `false`) -- compress and mangle `Function(args, code)` - `unsafe_Function` (default: `false`) -- compress and mangle `Function(args, code)`
when both `args` and `code` are string literals. when both `args` and `code` are string literals.

View File

@@ -5041,11 +5041,9 @@ merge(Compressor.prototype, {
}); });
self = best_of(compressor, self, negated); self = best_of(compressor, self, negated);
} }
if (compressor.option("unsafe_comps")) { switch (self.operator) {
switch (self.operator) { case ">": reverse("<"); break;
case "<": reverse(">"); break; case ">=": reverse("<="); break;
case "<=": reverse(">="); break;
}
} }
} }
if (self.operator == "+") { if (self.operator == "+") {

View File

@@ -913,15 +913,15 @@ collapse_vars_unary: {
return delete x; return delete x;
} }
function f1(n) { function f1(n) {
return n > +!!n return +!!n < n;
} }
function f2(n) { function f2(n) {
var k = 7; var k = 7;
return k-- return k--;
} }
function f3(n) { function f3(n) {
var k = 7; var k = 7;
return ++k return ++k;
} }
function f4(n) { function f4(n) {
var k = 8 - n; var k = 8 - n;

View File

@@ -1,62 +1,42 @@
keep_comparisons: { comparisons: {
options = { options = {
comparisons: true, comparisons: true,
unsafe_comps: false
} }
input: { input: {
var obj1 = { var obj1, obj2;
valueOf: function() {triggeredFirst();}
}
var obj2 = {
valueOf: function() {triggeredSecond();}
}
var result1 = obj1 <= obj2; var result1 = obj1 <= obj2;
var result2 = obj1 < obj2; var result2 = obj1 < obj2;
var result3 = obj1 >= obj2; var result3 = obj1 >= obj2;
var result4 = obj1 > obj2; var result4 = obj1 > obj2;
} }
expect: { expect: {
var obj1 = { var obj1, obj2;
valueOf: function() {triggeredFirst();}
}
var obj2 = {
valueOf: function() {triggeredSecond();}
}
var result1 = obj1 <= obj2; var result1 = obj1 <= obj2;
var result2 = obj1 < obj2; var result2 = obj1 < obj2;
var result3 = obj1 >= obj2; var result3 = obj2 <= obj1;
var result4 = obj1 > obj2; var result4 = obj2 < obj1;
} }
} }
keep_comparisons_with_unsafe_comps: { unsafe_comps: {
options = { options = {
comparisons: true, comparisons: true,
unsafe_comps: true conditionals: true,
unsafe_comps: true,
} }
input: { input: {
var obj1 = { var obj1, obj2;
valueOf: function() {triggeredFirst();} obj1 <= obj2 ? f1() : g1();
} obj1 < obj2 ? f2() : g2();
var obj2 = { obj1 >= obj2 ? f3() : g3();
valueOf: function() {triggeredSecond();} obj1 > obj2 ? f4() : g4();
}
var result1 = obj1 <= obj2;
var result2 = obj1 < obj2;
var result3 = obj1 >= obj2;
var result4 = obj1 > obj2;
} }
expect: { expect: {
var obj1 = { var obj1, obj2;
valueOf: function() {triggeredFirst();} obj2 < obj1 ? g1() : f1();
} obj1 < obj2 ? f2() : g2();
var obj2 = { obj1 < obj2 ? g3() : f3();
valueOf: function() {triggeredSecond();} obj2 < obj1 ? f4() : g4();
}
var result1 = obj2 >= obj1;
var result2 = obj2 > obj1;
var result3 = obj1 >= obj2;
var result4 = obj1 > obj2;
} }
} }

View File

@@ -243,7 +243,7 @@ issue_1089: {
expect: { expect: {
function x() { function x() {
var f = document.getElementById("fname"); var f = document.getElementById("fname");
if (f.files[0].size > 12345) if (12345 < f.files[0].size)
return alert("alert"), f.focus(), !1; return alert("alert"), f.focus(), !1;
} }
} }